#!/bin/ksh

# SymbolToString.ksh
# Wed Mar 18 16:04:46 PDT 2009
# Chieh Cheng
# http://www.CynosureX.com/

# GNU General Public License (GPL) Version 2, June 1991

  scriptName=`basename "$0"`

  convert ()
  {
    ch="$1"
    text=""

    case ${ch} in
      ".")
        text="period"
        ;;

      ":")
        text="colon"
        ;;

      ";")
        text="semi-colon"
        ;;

      "[")
        text="open-bracket"
        ;;

      "]")
        text="close-bracket"
        ;;

      "{")
        text="open-brace"
        ;;

      "}")
        text="close-brace"
        ;;

      "(")
        text="open-parentheses"
        ;;

      ")")
        text="close-parentheses"
        ;;

      "&")
        text="ampersand"
        ;;

      "$")
        text="dollar"
        ;;

      "@")
        text="at"
        ;;

      "#")
        text="hash"
#        text="number"
#        text="pound"
        ;;

      "~")
        text="tilde"
#        text="squiggly-line"
        ;;

      "'")
        text="apostrophe"
        ;;

      '`')
        text="back-quote"
        ;;

      "<")
        text="less-than"
        ;;

      ">")
        text="greater-than"
        ;;

      "?")
        text="question-mark"
        ;;

      "/")
        text="slash"
        ;;

      "\\")
        text="backslash"
        ;;

      "|")
        text="pipe"
        ;;

      "!")
        text="exclamation"
        ;;

      "%")
        text="percent"
        ;;

      "^")
        text="caret"
        ;;

      "_")
        text="underscore"
        ;;

      "-")
        text="dash"
        ;;

      "+")
        text="addition"
        ;;

      "=")
        text="equal"
        ;;

      "*")
        text="asterisk"
        ;;

      *)
        text="${ch}"
        ;;
    esac

    echo "${text}"
  }

  usage ()
  {
    echo "  Usage:   ${scriptName} \"string\""
  }

  if [ $# -eq 1 ]
  then
    symbols="$1"
    length=${#symbols}
    result=""
    first="true"
    same="false"
    pos=0
    spaceDelimiter="_${scriptName}_"

    while [ ${pos} -lt ${length} ]
    do
      next=`expr ${pos} + 1`
      ch=`java SubString "${symbols}" ${pos} ${next}`
      pos=${next}

      word=`convert "${ch}"`

      if [ "${word}" = "${ch}" ]
      then
        if [ "${same}" = "true" ] || [ "${word}" = " " ]
        then
          result="${result}${word}"
        else
          result="${result}${spaceDelimiter}${word}"
        fi
        same="true"
      else
        result="${result}${spaceDelimiter}${word}"
        same="false"
      fi

      first="false"
    done

    result=`echo "${result}" | sed "s/^${spaceDelimiter}//g"`
    result=`echo "${result}" | sed "s/ ${spaceDelimiter}/ /g"`
    result=`echo "${result}" | sed "s/${spaceDelimiter}/ /g"`
    echo "${result}"
  else
    usage
  fi
