Subversion Repositories LCARS

Rev

Rev 16 | Blame | Compare with Previous | Last modification | View Log | RSS feed

1
#!/bin/bash

appname="EazyTranslator"
appver="0.97a"
appfilename=$(readlink $(basename "$0"))
[ -n "$appfilename" ] && appfiledir=$(echo "$0" | sed "s/$appfilename\$//")

# dictionary file
dict="$TRANSLATE_DIR"
[ -z "$dict" ] && dict=$appfiledir

# delimiter name (for help page only) and character (string) for separating original expression
# and translation in dictionary file
delimiter_name="colon"
delimiter=":"

# all help on a single page
single_page=0

# bell character
ch_sound=""
ch_sound_on=$'\a'

result=""

title() {
  #[ $single_page -eq 0 ] && clear
  echo "
$appname $appver - Stream editor to use and to manage dictionary files
Requires 'basename', 'grep' and 'sed', optionally 'sort' and 'mktemp' in PATH
"

}

copyright() {
  echo "Copyright (C) 2001  Thomas Lahn (webmaster@PointedEars.de)
Be sure to have 'easyTrans' or similar in mail subject line for fast response.
"

}

pause() {
  if [ "$1" == "c" ]; then
    echo "Hit RETURN to continue..."
  else
    echo "Hit RETURN for the next page"
  fi
  read
}

copying() {
  title
  copyright
  echo "This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program (COPYING file); if not, write to the
Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
"

}

cmdHelp() {
  title
  echo "$appfilename EXPRESSION DICTIONARY [OPTIONS]

Tries to translate EXPRESSION looking up DICTIONARY and writes the result

Environment:

TRANSLATE_DIR     Dictionary folder root (absolute path '/')"

  if [ -n "$TRANSLATE_DIR" ]; then
    local INVALID_PATH=" -- INVALID PATH!"
    local INVALID_FORMAT=" -- INVALID FORMAT!"
    # : AutoCorrect performed!
    local INVALID_MSG=""
    if [ ! -e $TRANSLATE_DIR ]; then
      INVALID_MSG=$INVALID_PATH
    else
      local GREPRES=$(echo $TRANSLATE_DIR | grep -e "\/$")
      [ -z "$GREPRES" ] && INVALID_MSG=$INVALID_FORMAT
    fi
    echo "                  (currently '"$TRANSLATE_DIR"'"$INVALID_MSG")"
    #if [ "$INVALID_MSG" == "$INVALID_FORMAT" ]; then
      #export TRANSLATE_DIR=$TRANSLATE_DIR"/"
      #set -a
    #fi
  fi
  echo "\
                   If undefined, this is the program directory
                    (currently '$appfiledir').
TRANSLATE_OPTIONS Default options to overwrite command-line options"

if [ -n "$TRANSLATE_OPTIONS" ]; then
  echo "                  (currently '"$TRANSLATE_OPTIONS"')"
fi
  echo "
Arguments:

EXPRESSION        Word or (double-quoted) phrase to be translated
DICTIONARY        Path of dictionary file relative to TRANSLATE_DIR
"

  [ $single_page -eq 0 ] && pause "c"
  echo "\
Translation OPTIONS:
  -b, --brackets  If not in DICTIONARY, writes given WORD or EXPRESSION
                    as [WORD] or [EXPRESSION].
  -m, --messages  Return error messages instead of null-strings.
  -p, --phrase    Translate EXPRESSION as entire phrase. If not given,
                    each WORD of EXPRESSION is translated seperately.
  -r, --reverse   Perform reverse translation. Recommended only if
                    no appropriate dictionary file for vice-versa translation is
                    available and -p is also used.
  -s, --sound     Beep on fatal errors.
  -v, --verbose   Display flow of operation. Includes -m behavior.
  -z, --zero      Return not translatable tokens as null-strings.
                    Overwrites -b.
"

  [ $single_page -eq 0 ] && pause "c"
  echo "\
$appfilename EXPRESSION DICTIONARY COMMAND TRANSLATION [INFO] [OPTIONS]
$appfilename COMMAND DICTIONARY [INFO] [OPTIONS]

Dictionary file COMMANDs:
  -a,  --add      If not in DICTIONARY, add EXPRESSION with TRANSLATION
                    to DICTIONARY and write TRANSLATION.
                    If DICTIONARY not exists, create the file with INFO
                    and add the entry; if INFO is a null-string,
                    default INFO is added, containing program version,
                    user name and timestamp. Requires 'sort'.
  -ai, --addinfo  Add information data INFO to DICTIONARY.
                    Must be used as first argument.
  -c,  --create   Create new DICTIONARY with INFO (see -a).
                    Existing files are replaced. Must be used as first argument.

  -d,  --delete   If used with EXPRESSION and DICTIONARY, remove EXPRESSION
                    from DICTIONARY instead of translating.
                    If used as first argument, delete DICTIONARY.
  -i,  --info     Display information about DICTIONARY.
                    Must be used as first argument.
"

  [ $single_page -eq 0 ] && pause "c"
  echo "\
 -o, --overwrite Like -a but overwrite a contained translation of
                    EXPRESSION with TRANSLATION without question.
                    Additionally requires 'mktemp'.
  -R, --repair    Repair DICTIONARY instead of translating. Requires 'mktemp'.
                    Info data is be kept but invalid entries are removed.
                    USE WITH CAUTION!
  -s, --sort      Sort DICTIONARY instead of translating. Requires 'sort'.
                    Includes --sound when used with -v.
                    Must be used as first argument.
"

  [ $single_page -eq 0 ] && pause "c"
  echo "\
$appfilename OPTION [OPTION]

Help page OPTIONs:
  --1             Display help on one page (without 'clear' and user input).
                    Useful with redirection
                    (try '"
$appfilename" --1 --? > translate.doc.txt').
                    Must be given before all other help page options.
  --a, --about    Display information about the program.
  --c, --cmd      Display this help page.
  --d, --dict     Display help about dictionary files.
  --dev, --emp    Display special information for developers and employers.
  --x, --example  Display example.
  --?, --help     Display all help pages.
"

}

example() {
  title
  echo "EXAMPLE: If you would like to translate the English words 'a few' into German,

  $appfilename 'a few' en-de

should write the German words

  ein(e) wenig(e)\n

(without indent) to stdout if the echo dictionary file 'en-de' contains
a correct entry for it (see next page). You may also translate it as
entire phrase (which seems to make more sense here):

  $appfilename 'a few' en-de -p

should instead write the German word 'einige\n'
(replace '' in input with the double-quote character).

"

}

dictHelp() {
  title
  echo "\
DICTIONARY FILES:
You may create/improve dictionary files to be used with $appname
of your own. Translation data must match the following expression:

#${delimiter}File description displayed when option -i is used\\n[#${delimiter}File description\\n]
[Expression"
$delimiter"translation\\n[Next expression"$delimiter"next translation\\n]]
Last expression"
$delimiter"last translation\z

Parts enclosed in rectangle brackets are optional. The $delimiter_name ($delimiter) is to be used
as delimiter character between original and translated expression only.
Dictionary file names should contain common language identifiers separated
by a dash (such as 'en-de.dic' for an English-German dictionary file).

Program updates and dictionaries can be obtained from
'http://pointedears.de/tools/eazytrans/'.
Thank you for using a program by PointedEars.
"

}

devInfo() {
  title
  copyright
  echo "\
KNOWN ISSUES

- Sorting the dictionary unfortunately also sorts its info data by now.
"

}

help() {
  copying
  [ $single_page -eq 0 ] && pause
  cmdHelp
  [ $single_page -eq 0 ] && pause
  example
  [ $single_page -eq 0 ] && pause
  dictHelp
  [ $single_page -eq 0 ] && pause
  devInfo
}

check_dictionary() {
  # check if dictionary file (contains at least '#$delimiter')
  [ $verbose -eq 1 ] && echo "Executing: grep -e '."$delimiter".' '"$dict"'"
  result=$(grep -e "#$delimiter." "$dict")
  if [ -z "$result" ]; then
    [ $verbose -eq 1 ] && echo "grep returned: ''"
    echo $dict": Not a (valid) dictionary file (type '"$appfilename" --d' for details)."
    [ $verbose -eq 1 ] && echo
    exit $EDICT_WRONG_FORMAT
  else
    if [ $verbose -eq 1 ]; then
      echo "grep returned not a null-string: '$dict' seems to be a dictionary file"
    fi
  fi
  result=""
}

create() {
  [ $verbose -eq 1 ] && echo $dict": Creating dictionary"
  if [ -z "$3" ]; then
    [ $verbose -eq 1 ] && echo $dict": Adding information: '"$DEFAULT_INFO
    echo "#"$delimiter$DEFAULT_INFO &>$dict
  else
    [ $verbose -eq 1 ] && echo $dict": Adding information: '"$3"'"
    echo "#"$delimiter$3 &>$dict
  fi
  [ $verbose -eq 1 ] && echo
  exit $?
}

add_info() {
  if [ -z "$3" ]; then
    [ $verbose -eq 1 ] && echo $dict": Adding information: '"$DEFAULT_INFO"'"
    echo "#"$delimiter$DEFAULT_INFO >>$dict
  else
    [ $verbose -eq 1 ] && echo $dict": Adding information: '"$3"'"
    echo "#"$delimiter$3 >>$dict
  fi
  [ $verbose -eq 1 ] && echo
  exit $?
}

info() {
  if [ $verbose -eq 1 ]; then
    echo "Obtaining information..."
    echo
  fi
  grep "^#$delimiter" "$dict" | sed 's/^#'$delimiter'//'
  result=$(grep -e "^#$delimiter" "$dict")
  [ -z "$result" ] && echo $ch_sound$dict": No information available"
  [ $verbose -eq 1 ] && echo
  exit $ESUCCESS
}

add_translation() {
  [ $verbose -eq 1 ] && echo
  result=$(grep -i -e "^$1$delimiter" "$dict")
  if [ -z "$result" ]; then
    echo $dict": Adding expression: '"$1"'"
    echo $dict": Adding translation: '"$4"'"
    if [ -n "$4" ]; then
      echo $1$delimiter$4 >>$dict
    else
      [ $messages -eq 1 ] || [ $verbose -eq 1 ] && echo $ch_sound$appfilename": No translation given for '"$1"'"
    fi
    sort=1
  else
    translate "$1"
    echo $ch_sound$dict": Expression already contained: '"$1"':"$result
    [ $verbose -eq 1 ] && echo
    exit $EDICT_EXPR_CONTAINED
  fi
}

replace_translation() {
  [ $verbose -eq 1 ] && echo
  echo $dict": Replacing expression: '"$1"'"
  echo $dict": Replacing translation: '"$4"'"
  TMPFILE=$(mktemp -q /tmp/$appfilename.XXXXXX)
  if [ $? -ne 0 ]; then
    [ $verbose -eq 1 ] && echo $appfilename": Can't create temp file, exiting..."
    exit EDICT_TEMP_ERROR
  else
    if [ -n "$4" ]; then
      grep -iv -e "^$1$delimiter" "$dict" &>$TMPFILE
      [ $? -eq 0 ] && echo $1$delimiter$4 >>$TMPFILE
      [ $? -eq 0 ] && mv $TMPFILE $dict &>/dev/null
      if [ $? -ne 0 ]; then
        [ $verbose -eq 1 ] && echo $appfilename": Unable to replace dictionary file: '"$dict"'"
        exit EDICT_REPLACE_ERROR
      else
        sort=1
      fi
    else
      [ $messages -eq 1 ] || [ $verbose -eq 1 ] && echo $ch_sound$appfilename": No translation given for '"$1"'"
    fi
  fi
}

delete() {
  if [ $dictionary -eq 0 ]; then
    # delete entry
    [ $verbose -eq 1 ] && echo $dict": Removing expression: "$1
    result=$(grep -i -e "^$1$delimiter" "$dict")
    if [ -z "$result" ]; then
        [ $messages -eq 1 ] || [ $verbose -eq 1 ] && echo $dict": Expression not contained in dictionary: '"$1"'"
        exit $EDICT_DELETE_EXPR_NOT_FOUND
    fi
    TMPFILE=$(mktemp -q /tmp/$appfilename.XXXXXX)
    if [ $? -ne 0 ]; then
      [ $verbose -eq 1 ] && echo $appfilename": Can't create temp file, exiting..."
      exit EDICT_TEMP_ERROR
    else
      grep -iv -e "$1$delimiter" "$dict" &>$TMPFILE
      [ $? -eq 0 ] && mv $TMPFILE $dict &>/dev/null
      if [ $? -ne 0 ]; then
        [ $verbose -eq 1 ] && echo $dict": Unable to modify dictionary (cannot replace file)"
        exit EDICT_REPLACE_ERROR
      else
        [ $verbose -eq 1 ] && echo $dict": Modification successful"
        exit $ESUCCESS
      fi
    fi
  else
    # delete dictionary
    if [ $verbose -eq 1 ]; then
      echo $appfilename": Deleting dictionary: '"$dict"'"
      echo $appfilename": Executing: rm '"$dict"' &>/dev/null"
    fi
    rm $dict &>/dev/null
    if [ $? -ne 0 ]; then
      [ $messages -eq 1 ] || [ $verbose -eq 1 ] && echo $dict": Unable to remove dictionary"
      exit $EDICT_REMOVE
    else
      if [ $verbose -eq 1 ]; then
        echo $dict": Dictionary successfully removed"
        echo
      fi
      exit $ESUCCESS
    fi
  fi
}

sort_dictionary() {
  if [ $verbose -eq 1 ]; then
    echo "Sorting dictionary: '"$dict"'"
    echo "Executing: sort -d -f -o "$dict" -t "$delimiter" "$dict
  fi
  result=$(sort -d -f -o "$dict" -t $delimiter "$dict")
  SORT_EXIT=$?
  if [ $verbose -eq 1 ]; then
    echo "sort returned exit code "$SORT_EXIT
    if [ $SORT_EXIT -eq 0 ]; then
      echo "Sorting successful: '"$dict"'"
    else
      echo "Sorting failed: '"$dict"'"
    fi
    echo
  fi
  exit $SORT_EXIT
}

repair() {
  [ $verbose -eq 1 ] && echo $dict": Repairing dictionary"
  TMPFILE=$(mktemp -q /tmp/$appfilename.XXXXXX)
  if [ $? -ne 0 ]; then
    [ $verbose -eq 1 ] && echo $appfilename": Can't create temp file, exiting..."
    exit EDICT_TEMP_ERROR
  else
    grep -e "^#$delimiter" "$dict" &>$TMPFILE
    grep -e ".$delimiter." "$dict" >>$TMPFILE
    [ $? -eq 0 ] && mv $TMPFILE $dict &>/dev/null
    if [ $? -ne 0 ]; then
      [ $verbose -eq 1 ] && echo $dict": Unable to repair dictionary (cannot replace file)"
      exit EDICT_REPLACE_ERROR
    else
      $dict": Repair successful"
    fi
  fi
}

translateReverse() {
  local EXPR="$1"
  [ $verbose -eq 1 ] && echo >&2 "Executing: grep -i -e '"$delimiter$EXPR"$' '"$dict"'"
  result=$(egrep -i -e "$delimiter(\([^\)]+\))?.*\<$EXPR\>.*(\([^\)]+\))?$" "$dict")
  [ $verbose -eq 1 ] && echo >&2 "grep returned: '"$result"'"
  if [ -n "$result" ]; then
    if [ $verbose -eq 1 ]; then
      echo >&2 "Reading expression from recordset (with sed requires only 1 step! :o)
Executing: echo $result | sed 's/$delimiter[ ]*[^$delimiter]*//'"

    fi
  # result=$(echo $result | sed 's/'$delimiter'[ ]*[^'$delimiter']*//')
  result=$(echo $result | sed 's/'$delimiter'.*$//')
    [ $verbose -eq 1 ] && echo >&2 "sed returned: '"$result"'"
  fi

  # Should return only characters before the delimiter until BOL (expression)
}

translate() {
  local EXPR="$1"
  if [ $reverse -eq 1 ]; then
    translateReverse "$1"
    return
  fi
  [ $verbose -eq 1 ] && echo >&2 "Executing: grep -i -e '^"$EXPR$delimiter"' '"$dict"'"
  result=$(grep -i -e "^$EXPR$delimiter" "$dict")
  [ $verbose -eq 1 ] && echo >&2 "grep returned: '"$result"'"
  if [ -n "$result" ]; then
    if [ $verbose -eq 1 ]; then
      echo >&2 "Reading translation from recordset (with sed requires only 1 step! :o)
Executing: echo $result | sed 's/[^$delimiter]*$delimiter[ ]*//'"

    fi
    result=$(echo $result | sed 's/[^'$delimiter']*'$delimiter'[ ]*//')
    [ $verbose -eq 1 ] && echo >&2 "sed returned: '"$result"'"
  fi

  # Should return only characters after the separation character until EOL
  # (translation)
}

translate_all() {
  local second_pass=0
  [ "$1" = "-2" ] && {
    shift
    second_pass=1
  }
  TRANSL=""
  REVERSE_TRANSL=""
  REVERSE_TRANSL_CAPT="T"
  REVERSE_TRANSL_on=" (reverse)"
  REVERSE_TRANSL_CAPT_on="Reverse t"
  if [ $reverse -eq 1 ]; then
    REVERSE_TRANSL=$REVERSE_TRANSL_on
    REVERSE_TRANSL_CAPT=$REVERSE_TRANSL_CAPT_on
  fi
  if [ $phrase -eq 1 ] ; then
    [ $verbose -eq 1 ] && echo "Looking up phrase"$REVERSE_TRANSL": '"$1"'"
    if [ $zero -eq 0 ]; then
      TRANSL="$1"
      [ $brackets -eq 1 ] && TRANSL="["$TRANSL"]"
    fi
    translate "$1"
    if [ -n "$result" ]; then
      TRANSL=$result
      [ $verbose -eq 1 ] && echo $REVERSE_TRANSL_CAPT"ranslation: '"$TRANSL"'"
    else
      if [ $verbose -eq 1 ]; then
        if [ $zero -eq 1 ]; then
          echo $REVERSE_TRANSL_CAPT"ranslation failed (return not translatable tokens as null-string): '"$1"'"
        else
          if [ $brackets -eq 1 ]; then
            echo $ch_sound$REVERSE_TRANSL_CAPT"ranslation failed (enclosing phrase in brackets): '"$1"'"
          else
            echo $ch_sound$REVERSE_TRANSL_CAPT"ranslation failed (leave 'as is'): '"$1"'"
          fi
        fi
      fi
    fi
  else
    for word in $@; do
      if [ $zero -eq 0 ]; then
        TRANSLWORD="$word"
      else
        TRANSLWORD=""
      fi
      [ $verbose -eq 1 ] && echo "Looking up word"$REVERSE_TRANSL": "$word
      translate "$word"
      if [ -n "$result" ]; then
        TRANSLWORD=$result
      elif [ -n "$wsep" ] && [ $second_pass -eq 0 ]; then
        word=${word//$wsep/ }
        translate_all -2 $word
        if [ -n "$result" ]; then
          TRANSLWORD=$result
        fi
      fi
      [ -n "$result" ] && [ $verbose -eq 1 ] && echo $REVERSE_TRANSL_CAPT"ranslation: '"$TRANSLWORD"'"

      if [ -z "$result" ]; then
        [ $zero -eq 0 ] && [ $brackets -eq 1 ] && TRANSLWORD="["$TRANSLWORD"]"
        if [ $verbose -eq 1 ]; then
          if [ $zero -eq 1 ]; then
            echo $REVERSE_TRANSL_CAPT"ranslation failed (return not translatable tokens as null-string): "$word
          else
            if [ $brackets -eq 1 ]; then
              echo $REVERSE_TRANSL_CAPT"ranslation failed (enclosing word in brackets): "$word
            else
              echo $REVERSE_TRANSL_CAPT"ranslation failed (leave 'as is'): "$word
            fi
          fi
        fi
      fi
      TRANSL=$TRANSL" "$TRANSLWORD
    done
  fi
}

# exit codes
ESUCCESS=0
ENO_DICT=1
EDICT_DIR_NOT_FOUND=2
EDICT_NOT_FOUND=3
EDICT_WRONG_FORMAT=4
EDICT_EXPR_CONTAINED=5
EDICT_TEMP_ERROR=6
EDICT_CREATE_ERROR=7
EDICT_DELETE_EXPR_NOT_FOUND=8
EDICT_REMOVE=9

# argument flags
dictionary=0 # if 1, -d refers to the dictionary file instead of an entry

# option flags - may be replaced by declare -i FLAG
add=0
addinfo=0
brackets=0
create=0
delete=0
info=0
messages=0
overwrite=0
phrase=0
repair=0
reverse=0
show_version=0
sort=0
sound=0
verbose=0
zero=0

if $(getopt -T >/dev/null 2>&1); [ $? = 4 ]; then
  getopt_type=long
#  echo "getopt(1) type:     enhanced" >&2
  tmp=$(getopt -o hVbmpoRrSsvzA:a:c:D:d:E:e:i:t: \
               -l help,version\
,brackets,messages,phrase,overwrite,repair,reverse,sort,sound,verbose,zero\
,add:,addinfo:,dictionary:,delete:word-separator:\
,about,cmd,dictionary-help,dev,emp,1,x,example \
               -n "$appname" \
               -- "$@")
else
  getopt_type=short
#  echo "getopt(1) type:     old" >&2
  tmp=$(getopt bdhVmpoRrSsvzA:a:c:D:E:e:i: "$@")
fi

getopt_exit_code=$?

if [ $getopt_exit_code -eq 0 ]; then
##     getopt  returns  error  code 0 for successful parsing, 1 if
##     getopt(3) returns errors, 2 if it does not understand its
##     own parameters, 3 if an internal error occurs like out-of-
##     memory, and 4 if it is called with -T.
#
# Note the quotes around '$tmp': they are essential!
#  echo $tmp
# remove "--"
#  for i in $tmp; do if [ "$i" != "--" ]; then tmp2="${tmp2} $i"; fi; done
  eval set -- "$tmp"
  echo "${extd}New arguments:      $*$norm" >&2

  # First pass to make sure that only help is displayed if wanted
  for arg in $*
  do
    case "$arg" in
      -h | --help)      help; exit $ESUCCESS;;
#      -V | --version)  show_version=1;;
      --)               break;;
    esac
  done

  # Second pass to deal with the other options
  while true
  do
    case "$1" in
      # switches
      -b | --brackets)       brackets=1;;
      -r | --reverse)        reverse=1;;
      -v | --verbose)        verbose=1; title; copyright;;
      # options
      -D | --dictionary)     shift; dict="$1";;
      -t | --word-separator) shift; wsep="$1";;
      --)                    shift; break;;
    esac
    shift
  done
  [ -n "$*" ] && args=$args" $*"
  [ -z "$args" -a $show_version -eq 0 -a -z "$dict" ] && echo help
  set -- $args
else
#  echo "getopt exited: $getopt_exit_code
#  " >&2
  if [ $getopt_exit_code -eq 1 ] || [ $getopt_exit_code -eq 2 ]; then
    echo
    echo help
  else
    exit $getopt_exit_code
  fi
fi

if [ -n "$1" ]; then
  # retrieve arguments and use default settings from environment variable
  arguments=$*
  [ -n "$TRANSLATE_OPTIONS" ] && arguments=$arguments" "$TRANSLATE_OPTIONS
 
  # check options
#  for argument in $arguments
#  do
#    case "$argument" in
#      "-m" | "--messages") messages=1;;
#      "-p" | "--phrase")   phrase=1;;
#      "-r" | "--reverse")  reverse=1;;
#      "-s" | "--sound")    sound=1;;
#      "-z" | "--zero")     zero=1;;
#      "--a" | "--about")   copying; exit $ESUCCESS;;
#      "--c" | "--cmd")     cmdHelp; exit $ESUCCESS;;
#      "--d" | "--dictionary-help")    dictHelp; exit $ESUCCESS;;
#      "--dev" | "--emp")   devInfo; exit $ESUCCESS;;
#      "--1") single_page=1;;
#      "--x" | "--example") example; exit $ESUCCESS;;
#      "-h" | "--help")    help; exit $ESUCCESS;;
#    esac
#  done
 
  [ $sound -eq 1 ] && ch_sound=$ch_sound_on
 
  if [ -n "$dict" ]; then
#    # concatenate dictionary root and given dictionary file
#    dict="$dict$2"
#    # check for dictionary commands
#    case "$1" in
#      "-d" | "--delete") delete=1 ; dictionary=1 ;;
#      "-c" | "--create") create1;;
#      "-i" | "--info")   info=1;;
#      "-S" | "--sort")   sort=1;;
#      "-R" | "--repair") repair=1;;
#    esac
#    
#    case "$3" in
#      "-a"  | "--add")       add=1;;
#      "-A" | "--addinfo")   addinfo=1;;
#      "-d"  | "--delete")    delete=1;;
#      "-o"  | "--overwrite") overwrite=1;;
#    esac
#
#    if [ $verbose -eq 1 ]; then
#      if [ -z "$TRANSLATE_DIR" ]; then
#        echo "Dictionary root (program directory): '"$appfiledir"'"
#      else
#        echo "Dictionary root (TRANSLATE_DIR): '"$TRANSLATE_DIR"'"
#      fi
#      echo "Dictionary file: '$2'"
#      echo "Dictionary file path: '$dict'"
#    fi
#
#    DEFAULT_INFO="$appname $appver dictionary file created by '$(whoami)' on $(date)"
#
#    [ $create -eq 1 ] && create
#
    if [ -e "$dict" ]; then # if dictionary file exists
      check_dictionary

#      [ $addinfo -eq 1 ] && add_info
#      [ $info -eq 1 ] && info
#      [ $add -eq 1 ] && add_translation
#      [ $overwrite -eq 1 ] && replace_translation
#      [ $delete -eq 1 ] && delete
#      [ $sort -eq 1 ] && sort_dictionary
#      [ $repair -eq 1 ] && repair
     
      translate_all "$@"
           
      [ $verbose -eq 1 ] && echo "Overall translation:"
      [ -n "$TRANSL" ] && echo $TRANSL
      [ $verbose -eq 1 ] && echo
    else
      # send errmsg to stderr
      if [ -n "$TRANSLATE_DIR" ] && [ ! -e "$TRANSLATE_DIR" ]; then
        [ $messages -eq 1 ] || [ $verbose -eq 1 ] && echo $ch_sound"$appfilename: Unable to locate dictionary root: '"$TRANSLATE_DIR"'" 1>&2
      else
        if [ $info -eq 0 ] && [ $add -eq 1 ] || [ $overwrite -eq 1 ]; then
          if [ $verbose -eq 1 ]; then
            echo
            echo $dict": Creating dictionary"
          fi
          if [ -n "$4" ]; then
            if [ -z "$5" ]; then
              [ $verbose -eq 1 ] && echo $dict": Adding information: '"$appname" "$appver" dictionary file created by '"$(whoami)"' on "$(date)"'"
              echo "#"$delimiter$appname" "$appver" dictionary file created by '"$(whoami)"' on "$(date) &>$dict
            else
              [ $verbose -eq 1 ] && echo $dict": Adding information: '"$5"'"
              echo "#"$delimiter$5 &>$dict
            fi
            if [ $? -eq 0 ]; then
              if [ $verbose -eq 1 ]; then
                echo $dict": Adding expression: '"$1"'"
                echo $dict": Adding translation: '"$4"'"
              fi
              echo $1$delimiter$4 >>$dict
            else
              [ $messages -eq 1 ] || [ $verbose -eq 1 ] && echo $ch_sound$appfilename": Unable to create dictionary file: '"$dict"'"
              exit EDICT_CREATE_ERROR
            fi
          else
            [ $messages -eq 1 ] || [ $verbose -eq 1 ] && echo $ch_sound$appfilename": No translation given for '"$1"'"
          fi
        else
          if [ $messages -eq 1 ] || [ $verbose -eq 1 ]; then
           echo >&2 "$ch_sound$appfilename: Unable to locate dictionary file: '$dict'"
          fi
        fi
      fi
      [ $verbose -eq 1 ] && echo
      if [ -n "$TRANSLATE_DIR" ] && [ ! -e "$TRANSLATE_DIR" ]; then
        exit $EDICT_DIR_NOT_FOUND
      else
        exit $EDICT_NOT_FOUND
      fi
    fi
  else
    if [ $messages -eq 1 ] || [ $verbose -eq 1 ]; then
      # send errmsg to stderr
      echo >&2 $ch_sound$appfilename": No dictionary file given"
    fi
    [ $verbose -eq 1 ] && echo
    exit $ENO_DICT
  fi
else
  help
fi