Subversion Repositories LCARS

Rev

Rev 15 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
15 PointedEar 1
#!/bin/bash
2
 
3
APPNAME="EazyTranslator"
4
APPVER="0.97a"
5
APPFILENAME=`basename $0`
6
APPFILEDIR=`echo $0 | sed 's/'$APPFILENAME$'//'`
7
 
8
# dictionary file
9
DICT="$TRANSLATE_DIR"
10
if [ -z "$DICT" ]; then DICT=$APPFILEDIR ; fi
11
 
12
# delimiter name (for help page only) and character (string) for separating original expression
13
# and translation in dictionary file
14
DELIMITER_NAME="colon"
15
DELIMITER=":"
16
 
17
# all help on a single page
18
SINGLE_PAGE=0
19
 
20
# bell character
21
CH_SOUND=""
22
CH_SOUND_ON=$'\a'
23
 
24
RESULT=""
25
 
26
function title {
27
  if [ $SINGLE_PAGE -eq 0 ]; then clear ; fi
28
  echo
29
  echo $APPNAME" "$APPVER" - Stream editor to use and to manage dictionary files"
30
  echo "Requires 'basename', 'grep' and 'sed', optionally 'sort' and 'mktemp' in PATH"
31
  echo
32
}
33
 
34
function copyright {
35
  echo "Copyright (C) 2001  Thomas Lahn (webmaster@PointedEars.de)"
36
  echo "Be sure to have 'easyTrans' or similar in mail subject line for fast response."
37
  echo
38
}
39
 
40
function pause {
41
  if [ "$1" == "c" ]; then
42
    echo "Hit RETURN to continue..."
43
  else
44
    echo "Hit RETURN for the next page"
45
  fi
46
  read
47
}
48
 
49
function copying {
50
  title
51
  copyright
52
  echo "This program is free software; you can redistribute it and/or modify"
53
  echo "it under the terms of the GNU General Public License as published by"
54
  echo "the Free Software Foundation; either version 2 of the License, or"
55
  echo "(at your option) any later version."
56
  echo
57
  echo "This program is distributed in the hope that it will be useful,"
58
  echo "but WITHOUT ANY WARRANTY; without even the implied warranty of"
59
  echo "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the"
60
  echo "GNU General Public License for more details."
61
  echo
62
  echo "You should have received a copy of the GNU General Public License"
63
  echo "along with this program (COPYING file); if not, write to the"
64
  echo "Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA."
65
  echo
66
}
67
 
68
function cmdHelp {
69
  title
70
  echo $APPFILENAME" EXPRESSION DICTIONARY [OPTIONS]"
71
  echo
72
  echo "Tries to translate EXPRESSION looking up DICTIONARY and writes the result"
73
  echo "to standard output (stdout) in a single line followed by a newline (\n)."
74
  echo
75
  echo "Environment:"
76
  echo
77
  echo "TRANSLATE_DIR     Dictionary folder root (absolute path '/')"
78
if [ -n "$TRANSLATE_DIR" ]; then
79
  local INVALID_PATH=" -- INVALID PATH!"
80
  local INVALID_FORMAT=" -- INVALID FORMAT!"
81
  # : AutoCorrect performed!
82
  local INVALID_MSG=""
83
  if [ ! -e $TRANSLATE_DIR ]; then
84
    INVALID_MSG=$INVALID_PATH
85
  else
86
    local GREPRES=`echo $TRANSLATE_DIR | grep -e "\/$"`
87
    if [ -z "$GREPRES" ]; then INVALID_MSG=$INVALID_FORMAT ; fi
88
  fi
89
  echo "                  (currently '"$TRANSLATE_DIR"'"$INVALID_MSG")"
90
  #if [ "$INVALID_MSG" == "$INVALID_FORMAT" ]; then
91
    #export TRANSLATE_DIR=$TRANSLATE_DIR"/"
92
    #set -a
93
  #fi
94
fi
95
  echo "                  If undefined, this is the program directory"
96
  echo "                  (currently '"$APPFILEDIR"')."
97
  echo "TRANSLATE_OPTIONS Default options to overwrite command-line options"
98
if [ -n "$TRANSLATE_OPTIONS" ]; then
99
  echo "                  (currently '"$TRANSLATE_OPTIONS"')"
100
fi
101
  echo
102
  echo "Arguments:"
103
  echo
104
  echo "EXPRESSION        Word or (double-quoted) phrase to be translated"
105
  echo "DICTIONARY        Path of dictionary file relative to TRANSLATE_DIR"
106
  echo
107
  if [ $SINGLE_PAGE -eq 0 ]; then pause "c" ; fi
108
  echo "Translation OPTIONS:"
109
  echo "  -b, --brackets  If not in DICTIONARY, writes given WORD or EXPRESSION"
110
  echo "                  as [WORD] or [EXPRESSION]."
111
  echo "  -m, --messages  Return error messages instead of null-strings."
112
  echo "  -p, --phrase    Translate EXPRESSION as entire phrase. If not given,"
113
  echo "                  each WORD of EXPRESSION is translated seperately."
114
  echo "  -r, --reverse   Perform reverse translation. Recommended only if"
115
  echo "                  no appropriate dictionary file for vice-versa translation is"
116
  echo "                  available and -p is also used."
117
  echo "  -s, --sound     Beep on fatal errors."
118
  echo "  -v, --verbose   Display flow of operation. Includes -m behavior."
119
  echo "  -z, --zero      Return not translatable tokens as null-strings."
120
  echo "                  Overwrites -b."
121
  echo
122
  if [ $SINGLE_PAGE -eq 0 ]; then pause "c" ; fi
123
  echo $APPFILENAME" EXPRESSION DICTIONARY COMMAND TRANSLATION [INFO] [OPTIONS]"
124
  echo $APPFILENAME" COMMAND DICTIONARY [INFO] [OPTIONS]"
125
  echo
126
  echo "Dictionary file COMMANDs:"
127
  echo "  -a,  --add      If not in DICTIONARY, add EXPRESSION with TRANSLATION"
128
  echo "                  to DICTIONARY and write TRANSLATION."
129
  echo "                  If DICTIONARY not exists, create the file with INFO"
130
  echo "                  and add the entry; if INFO is a null-string,"
131
  echo "                  default INFO is added, containing program version,"
132
  echo "                  user name and timestamp. Requires 'sort'."
133
  echo "  -ai, --addinfo  Add information data INFO to DICTIONARY."
134
  echo "                  Must be used as first argument."
135
  echo "  -c,  --create   Create new DICTIONARY with INFO (see -a)."
136
  echo "                  Existing files are replaced. Must be used as first argument."
137
  echo "  -d,  --delete   If used with EXPRESSION and DICTIONARY, remove EXPRESSION"
138
  echo "                  from DICTIONARY instead of translating."
139
  echo "                  If used as first argument, delete DICTIONARY."
140
  echo "  -i,  --info     Display information about DICTIONARY."
141
  echo "                  Must be used as first argument."
142
  echo
143
  if [ $SINGLE_PAGE -eq 0 ]; then pause "c" ; fi
144
  echo "  -o, --overwrite Like -a but overwrite a contained translation of"
145
  echo "                  EXPRESSION with TRANSLATION without question."
146
  echo "                  Additionally requires 'mktemp'."
147
  echo "  -R, --repair    Repair DICTIONARY instead of translating. Requires 'mktemp'."
148
  echo "                  Info data is be kept but invalid entries are removed."
149
  echo "                  USE WITH CAUTION!"
150
  echo "  -s, --sort      Sort DICTIONARY instead of translating. Requires 'sort'."
151
  echo "                  Includes --sound when used with -v."
152
  echo "                  Must be used as first argument."
153
  echo
154
  if [ $SINGLE_PAGE -eq 0 ]; then pause "c" ; fi
155
  echo $APPFILENAME" OPTION [OPTION]"
156
  echo
157
  echo "Help page OPTIONs:"
158
  echo "  --1             Display help on one page (without 'clear' and user input)."
159
  echo "                  Useful with redirection "
160
  echo "                  (try '"$APPFILENAME" --1 --? > translate.doc.txt')."
161
  echo "                  Must be given before all other help page options."
162
  echo "  --a, --about    Display information about the program."
163
  echo "  --c, --cmd      Display this help page."
164
  echo "  --d, --dict     Display help about dictionary files."
165
  echo "  --dev, --emp    Display special information for developers and employers."
166
  echo "  --x, --example  Display example."
167
  echo "  --?, --help     Display all help pages."
168
  echo
169
}
170
 
171
function example {
172
  title
173
  echo "EXAMPLE: If you would like to translate the English words 'a few' into German,"
174
  echo
175
  echo "  "$APPFILENAME" ''a few'' en-de"
176
  echo
177
  echo "should write the German words"
178
  echo
179
  echo "  ein(e) wenig(e)\n"
180
  echo
181
  echo "(without indent) to stdout if the echo dictionary file 'en-de' contains"
182
  echo "a correct entry for it (see next page). You may also translate it as"
183
  echo "entire phrase (which seems to make more sense here):"
184
  echo
185
  echo "  "$APPFILENAME" ''a few'' en-de -p"
186
  echo
187
  echo "should instead write the German word 'einige\n'"
188
  echo "(replace '' in input with the double-quote character)."
189
  echo
190
}
191
 
192
function dictHelp {
193
  title
194
  echo "DICTIONARY FILES:"
195
  echo "You may create/improve dictionary files to be used with "$APPNAME
196
  echo "of your own. Translation data must match the following expression:"
197
  echo
198
  echo "#"$DELIMITER"File description displayed when option -i is used\n[#"$DELIMITER"File description\n]"
199
  echo "[Expression"$DELIMITER"translation\n[Next expression"$DELIMITER"next translation\n]]"
200
  echo "Last expression"$DELIMITER"last translation\z"
201
  echo
202
  echo "Parts enclosed in rectangle brackets are optional. The "$DELIMITER_NAME" ("$DELIMITER") is to be used"
203
  echo "as delimiter character between original and translated expression only."
204
  echo "Dictionary file names should contain common language identifiers separated"
205
  echo "by a dash (such as 'en-de.dic' for an English-German dictionary file)."
206
  echo
207
  echo "Program updates and dictionaries can be obtained from"
208
  echo "'http://pointedears.de/dev/unix/translate/'."
209
  echo "Thank you for using a program by PointedEars."
210
  echo
211
}
212
 
213
function devInfo {
214
  title
215
  copyright
216
  echo "INFORMATION FOR DEVELOPERS (KNOWN ISSUES)..."
217
  echo
218
  echo "- Sorting the dictionary unfortunately also sorts its info data by now."
219
  echo
220
  echo "...AND FOR EMPLOYERS:"
221
  echo
222
  echo "BTW, if you have an idea for improving EasyTranslator or for another (field of)"
223
  echo "application you want me to develop for you (BASIC, DOS batch, Windows INF,"
224
  echo "Visual Basic, bash, Pascal, Delphi, C, HTML/JavaScript, and I am still learning"
225
  echo "other languages) feel free to mail me, too. THNX!    -- PointedEars, 2001-03-28"
226
  echo
227
}
228
 
229
function help {
230
  copying
231
  if [ $SINGLE_PAGE -eq 0 ]; then pause ; fi
232
  cmdHelp
233
  if [ $SINGLE_PAGE -eq 0 ]; then pause ; fi
234
  example
235
  if [ $SINGLE_PAGE -eq 0 ]; then pause ; fi
236
  dictHelp
237
  if [ $SINGLE_PAGE -eq 0 ]; then pause ; fi
238
  devInfo
239
}
240
 
241
function translateReverse {
242
  local EXPR="$1"
243
  if [ $VERBOSE -eq 1 ]; then echo "Executing: grep -i -e '"$DELIMITER$EXPR"$' '"$DICT"'" ; fi
244
  RESULT=`grep -i -e "$DELIMITER$EXPR$" "$DICT"`
245
  if [ $VERBOSE -eq 1 ]; then echo "grep returned: '"$RESULT"'" ; fi
246
  if [ -n "$RESULT" ]; then
247
    if [ $VERBOSE -eq 1 ]; then
248
      echo "Reading expression from recordset (with sed requires only 1 step! :o)" ;
249
      echo "Executing: echo $RESULT | sed 's/$DELIMITER[ ]*[^$DELIMITER]*//'"
250
    fi
251
    RESULT=`echo $RESULT | sed 's/'$DELIMITER'[ ]*[^'$DELIMITER']*//'`
252
    if [ $VERBOSE -eq 1 ]; then echo "sed returned: '"$RESULT"'" ; fi
253
  fi
254
 
255
  # Should return only characters before the delimiter until BOL (expression)
256
}
257
 
258
function translate {
259
  local EXPR="$1"
260
  if [ $REVERSE -eq 1 ]; then translateReverse "$1" ; return ; fi
261
  if [ $VERBOSE -eq 1 ]; then echo "Executing: grep -i -e '^"$EXPR$DELIMITER"' '"$DICT"'" ; fi
262
  RESULT=`grep -i -e "^$EXPR$DELIMITER" "$DICT"`
263
  if [ $VERBOSE -eq 1 ]; then echo "grep returned: '"$RESULT"'" ; fi
264
  if [ -n "$RESULT" ]; then
265
    if [ $VERBOSE -eq 1 ]; then
266
      echo "Reading translation from recordset (with sed requires only 1 step! :o)" ;
267
      echo "Executing: echo $RESULT | sed 's/[^$DELIMITER]*$DELIMITER[ ]*//'"
268
    fi
269
    RESULT=`echo $RESULT | sed 's/[^'$DELIMITER']*'$DELIMITER'[ ]*//'`
270
    if [ $VERBOSE -eq 1 ]; then echo "sed returned: '"$RESULT"'" ; fi
271
  fi
272
 
273
  # Should return only characters after the separation character until EOL
274
  # (translation)
275
}
276
 
277
# exit codes
278
ESUCCESS=0
279
ENO_DICT=1
280
EDICT_DIR_NOT_FOUND=2
281
EDICT_NOT_FOUND=3
282
EDICT_WRONG_FORMAT=4
283
EDICT_EXPR_CONTAINED=5
284
EDICT_TEMP_ERROR=6
285
EDICT_CREATE_ERROR=7
286
EDICT_DELETE_EXPR_NOT_FOUND=8
287
EDICT_REMOVE=9
288
 
289
# argument flags
290
DICTIONARY=0 # if 1, -d refers to the dictionary file instead of an entry
291
 
292
# option flags - may be replaced by declare -i FLAG
293
ADD=0
294
ADDINFO=0
295
BRACKETS=0
296
CREATE=0
297
DELETE=0
298
INFO=0
299
MESSAGES=0
300
OVERWRITE=0
301
PHRASE=0
302
REPAIR=0
303
REVERSE=0
304
SORT=0
305
SOUND=0
306
VERBOSE=0
307
ZERO=0
308
 
309
if [ -n "$1" ]; then
310
  # retrieve arguments and use default settings from environment variable
311
  ARGUMENTS=$* ; if [ -n "$TRANSLATE_OPTIONS" ]; then ARGUMENTS=$ARGUMENTS" "$TRANSLATE_OPTIONS ; fi
312
  # check options
313
  for argument in $ARGUMENTS; do
314
    case "$argument" in
315
      "-b" | "--brackets") BRACKETS=1;;
316
      "-m" | "--messages") MESSAGES=1;;
317
      "-p" | "--phrase")   PHRASE=1;;
318
      "-r" | "--reverse")  REVERSE=1;;
319
      "-s" | "--sound")    SOUND=1;;
320
      "-v" | "--verbose")  VERBOSE=1 ; title ; copyright;;
321
      "-z" | "--zero")     ZERO=1;;
322
      "--a" | "--about")   copying ; exit $ESUCCESS;;
323
      "--c" | "--cmd")     cmdHelp ; exit $ESUCCESS;;
324
      "--d" | "--dict")    dictHelp ; exit $ESUCCESS;;
325
      "--dev" | "--emp")   devInfo ; exit $ESUCCESS;;
326
      "--1") SINGLE_PAGE=1;;
327
      "--x" | "--example") example ; exit $ESUCCESS;;
328
      "--?" | "--help")    help ; exit $ESUCCESS;;
329
    esac
330
  done
331
  if [ $SOUND -eq 1 ]; then CH_SOUND=$CH_SOUND_ON ; fi
332
  if [ -n "$2" ]; then
333
# concatenate dictionary root and given dictionary file
334
    DICT="$DICT$2"
335
# check for dictionary commands
336
    case "$1" in
337
      "-d" | "--delete") DELETE=1 ; DICTIONARY=1 ;;
338
      "-c" | "--create") CREATE=1;;
339
      "-i" | "--info")   INFO=1;;
340
      "-s" | "--sort")   SORT=1;;
341
      "-R" | "--repair") REPAIR=1;;
342
    esac
343
    case "$3" in
344
      "-a"  | "--add")       ADD=1;;
345
      "-ai" | "--addinfo")   ADDINFO=1;;
346
      "-d"  | "--delete")    DELETE=1;;
347
      "-o"  | "--overwrite") OVERWRITE=1;;
348
    esac
349
 
350
    if [ $VERBOSE -eq 1 ]; then
351
      if [ -z "$TRANSLATE_DIR" ]; then
352
        echo "Dictionary root (program directory): '"$APPFILEDIR"'"
353
      else
354
        echo "Dictionary root (TRANSLATE_DIR): '"$TRANSLATE_DIR"'"
355
      fi
356
      echo "Dictionary file: '"$2"'"
357
      echo "Dictionary file path: '"$DICT"'"
358
    fi
359
 
360
    DEFAULT_INFO=$APPNAME" "$APPVER" dictionary file created by '"`whoami`"' on "`date`
361
 
362
    if [ $CREATE -eq 1 ]; then
363
      if [ $VERBOSE -eq 1 ]; then
364
        echo $DICT": Creating dictionary"
365
      fi
366
      if [ -z "$3" ]; then
367
        if [ $VERBOSE -eq 1 ]; then echo $DICT": Adding information: '"$DEFAULT_INFO ; fi
368
        echo "#"$DELIMITER$EFAULT_INFO &>$DICT
369
      else
370
        if [ $VERBOSE -eq 1 ]; then echo $DICT": Adding information: '"$3"'" ; fi
371
        echo "#"$DELIMITER$3 &>$DICT
372
      fi
373
      if [ $VERBOSE -eq 1 ]; then echo ; fi
374
      exit $?
375
    fi
376
 
377
    if [ -e "$DICT" ]; then # if dictionary file exists
378
      # check if dictionary file (contains at least '#$DELIMITER')
379
      if [ $VERBOSE -eq 1 ]; then echo "Executing: grep -e '."$DELIMITER".' '"$DICT"'" ; fi
380
      RESULT=`grep -e "#$DELIMITER." "$DICT"`
381
      if [ -z "$RESULT" ]; then
382
        if [ $VERBOSE -eq 1 ]; then echo "grep returned: ''" ; fi
383
        echo $DICT": Not a (valid) dictionary file (type '"$APPFILENAME" --d' for details)."
384
        if [ $VERBOSE -eq 1 ]; then echo ; fi
385
        exit $EDICT_WRONG_FORMAT
386
      else
387
        if [ $VERBOSE -eq 1 ]; then
388
          echo "grep returned not a null-string: '$DICT' seems to be a dictionary file"
389
        fi
390
      fi
391
      RESULT=""
392
 
393
      if [ $ADDINFO -eq 1 ]; then
394
        if [ -z "$3" ]; then
395
          if [ $VERBOSE -eq 1 ]; then echo $DICT": Adding information: '"$DEFAULT_INFO"'" ; fi
396
          echo "#"$DELIMITER$DEFAULT_INFO >>$DICT
397
        else
398
          if [ $VERBOSE -eq 1 ]; then echo $DICT": Adding information: '"$3"'" ; fi
399
          echo "#"$DELIMITER$3 >>$DICT
400
        fi
401
        if [ $VERBOSE -eq 1 ]; then echo ; fi
402
        exit $?
403
      fi
404
 
405
      if [ $INFO -eq 1 ]; then
406
        if [ $VERBOSE -eq 1 ]; then
407
          echo "Obtaining information..."
408
          echo
409
        fi
410
        grep "^#$DELIMITER" "$DICT" | sed 's/^#'$DELIMITER'//'
411
        RESULT=`grep -e "^#$DELIMITER" "$DICT"`
412
        if [ -z "$RESULT" ]; then echo $CH_SOUND$DICT": No information available" ; fi
413
        if [ $VERBOSE -eq 1 ]; then echo ; fi
414
        exit $ESUCCESS
415
      fi
416
 
417
      if [ $ADD -eq 1 ]; then
418
        if [ $VERBOSE -eq 1 ]; then echo ; fi
419
        RESULT=`grep -i -e "^$1$DELIMITER" "$DICT"`
420
        if [ -z "$RESULT" ]; then
421
          echo $DICT": Adding expression: '"$1"'"
422
          echo $DICT": Adding translation: '"$4"'"
423
          if [ -n "$4" ]; then
424
            echo $1$DELIMITER$4 >>$DICT
425
          else
426
            if [ $MESSAGES -eq 1 ] || [ $VERBOSE -eq 1 ]; then echo $CH_SOUND$APPFILENAME": No translation given for '"$1"'" ; fi
427
          fi
428
          SORT=1
429
        else
430
          translate "$1"
431
          echo $CH_SOUND$DICT": Expression already contained: '"$1"':"$RESULT
432
          if [ $VERBOSE -eq 1 ]; then echo ; fi
433
          exit $EDICT_EXPR_CONTAINED
434
        fi
435
      fi
436
 
437
      if [ $OVERWRITE -eq 1 ]; then
438
        if [ $VERBOSE -eq 1 ]; then echo ; fi
439
        echo $DICT": Replacing expression: '"$1"'"
440
        echo $DICT": Replacing translation: '"$4"'"
441
        TMPFILE=`mktemp -q /tmp/$APPFILENAME.XXXXXX`
442
        if [ $? -ne 0 ]; then
443
          if [ $VERBOSE -eq 1 ]; then echo $APPFILENAME": Can't create temp file, exiting..." ; fi
444
          exit EDICT_TEMP_ERROR
445
        else
446
          if [ -n "$4" ]; then
447
            grep -iv -e "^$1$DELIMITER" "$DICT" &>$TMPFILE
448
            if [ $? -eq 0 ]; then echo $1$DELIMITER$4 >>$TMPFILE ; fi
449
            if [ $? -eq 0 ]; then mv $TMPFILE $DICT &>/dev/null ; fi
450
            if [ $? -ne 0 ]; then
451
              if [ $VERBOSE -eq 1 ]; then echo $APPFILENAME": Unable to replace dictionary file: '"$DICT"'" ; fi
452
              exit EDICT_REPLACE_ERROR
453
            else
454
              SORT=1
455
            fi
456
          else
457
            if [ $MESSAGES -eq 1 ] || [ $VERBOSE -eq 1 ]; then echo $CH_SOUND$APPFILENAME": No translation given for '"$1"'" ; fi
458
          fi
459
        fi
460
      fi
461
 
462
      if [ $DELETE -eq 1 ]; then
463
        if [ $DICTIONARY -eq 0 ]; then
464
          # delete entry
465
          if [ $VERBOSE -eq 1 ]; then echo $DICT": Removing expression: "$1 ; fi
466
          RESULT=`grep -i -e "^$1$DELIMITER" "$DICT"`
467
          if [ -z "$RESULT" ]; then
468
              if [ $MESSAGES -eq 1 ] || [ $VERBOSE -eq 1 ]; then echo $DICT": Expression not contained in dictionary: '"$1"'" ; fi
469
              exit $EDICT_DELETE_EXPR_NOT_FOUND
470
          fi
471
          TMPFILE=`mktemp -q /tmp/$APPFILENAME.XXXXXX`
472
          if [ $? -ne 0 ]; then
473
            if [ $VERBOSE -eq 1 ]; then echo $APPFILENAME": Can't create temp file, exiting..." ; fi
474
            exit EDICT_TEMP_ERROR
475
          else
476
            grep -iv -e "$1$DELIMITER" "$DICT" &>$TMPFILE
477
            if [ $? -eq 0 ]; then mv $TMPFILE $DICT &>/dev/null ; fi
478
            if [ $? -ne 0 ]; then
479
              if [ $VERBOSE -eq 1 ]; then echo $DICT": Unable to modify dictionary (cannot replace file)" ; fi
480
              exit EDICT_REPLACE_ERROR
481
            else
482
              if [ $VERBOSE -eq 1 ]; then echo $DICT": Modification successful" ; fi
483
              exit $ESUCCESS
484
            fi
485
          fi
486
        else
487
          # delete dictionary
488
          if [ $VERBOSE -eq 1 ]; then
489
            echo $APPFILENAME": Deleting dictionary: '"$DICT"'"
490
            echo $APPFILENAME": Executing: rm '"$DICT"' &>/dev/null"
491
          fi
492
          rm $DICT &>/dev/null
493
          if [ $? -ne 0 ]; then
494
            if [ $MESSAGES -eq 1 ] || [ $VERBOSE -eq 1 ]; then echo $DICT": Unable to remove dictionary" ; fi
495
            exit $EDICT_REMOVE
496
          else
497
            if [ $VERBOSE -eq 1 ]; then
498
              echo $DICT": Dictionary successfully removed"
499
              echo
500
            fi
501
            exit $ESUCCESS
502
          fi
503
        fi
504
      fi
505
 
506
      if [ $SORT -eq 1 ]; then
507
        if [ $VERBOSE -eq 1 ]; then
508
          echo "Sorting vocabulary: '"$DICT"'"
509
          echo "Executing: sort -d -f -o "$DICT" -t "$DELIMITER" "$DICT
510
        fi
511
        RESULT=`sort -d -f -o "$DICT" -t $DELIMITER "$DICT"`
512
        SORT_EXIT=$?
513
        if [ $VERBOSE -eq 1 ]; then
514
          echo "sort returned exit code "$SORT_EXIT
515
          if [ $SORT_EXIT -eq 0 ]; then
516
            echo "Sorting successful: '"$DICT"'"
517
          else
518
            echo "Sorting failed: '"$DICT"'"
519
          fi
520
          echo
521
        fi
522
        exit $SORT_EXIT
523
      fi
524
 
525
      if [ $REPAIR -eq 1 ]; then
526
        if [ $VERBOSE -eq 1 ]; then echo $DICT": Repairing dictionary" ; fi
527
        TMPFILE=`mktemp -q /tmp/$APPFILENAME.XXXXXX`
528
        if [ $? -ne 0 ]; then
529
          if [ $VERBOSE -eq 1 ]; then echo $APPFILENAME": Can't create temp file, exiting..." ; fi
530
          exit EDICT_TEMP_ERROR
531
        else
532
          grep -e "^#$DELIMITER" "$DICT" &>$TMPFILE
533
          grep -e ".$DELIMITER." "$DICT" >>$TMPFILE
534
          if [ $? -eq 0 ]; then mv $TMPFILE $DICT &>/dev/null ; fi
535
          if [ $? -ne 0 ]; then
536
            if [ $VERBOSE -eq 1 ]; then echo $DICT": Unable to repair dictionary (cannot replace file)" ; fi
537
            exit EDICT_REPLACE_ERROR
538
          else
539
            $DICT": Repair successful"
540
          fi
541
        fi
542
      fi
543
 
544
      TRANSL=""
545
      REVERSE_TRANSL=""
546
      REVERSE_TRANSL_CAPT="T"
547
      REVERSE_TRANSL_ON=" (reverse)"
548
      REVERSE_TRANSL_CAPT_ON="Reverse t"
549
      if [ $REVERSE -eq 1 ]; then
550
        REVERSE_TRANSL=$REVERSE_TRANSL_ON
551
        REVERSE_TRANSL_CAPT=$REVERSE_TRANSL_CAPT_ON
552
      fi
553
      if [ $PHRASE -eq 1 ] ; then
554
        if [ $VERBOSE -eq 1 ]; then echo "Looking up phrase"$REVERSE_TRANSL": '"$1"'" ; fi
555
        if [ $ZERO -eq 0 ]; then
556
          TRANSL="$1" ; if [ $BRACKETS -eq 1 ]; then TRANSL="["$TRANSL"]" ; fi
557
        fi
558
        translate "$1"
559
        if [ -n "$RESULT" ]; then
560
          TRANSL=$RESULT
561
          if [ $VERBOSE -eq 1 ]; then echo $REVERSE_TRANSL_CAPT"ranslation: '"$TRANSL"'" ; fi
562
        else
563
          if [ $VERBOSE -eq 1 ]; then
564
            if [ $ZERO -eq 1 ]; then
565
              echo $REVERSE_TRANSL_CAPT"ranslation failed (return not translatable tokens as null-string): '"$1"'"
566
            else
567
              if [ $BRACKETS -eq 1 ]; then
568
                echo $CH_SOUND$REVERSE_TRANSL_CAPT"ranslation failed (enclosing phrase in brackets): '"$1"'"
569
              else
570
                echo $CH_SOUND$REVERSE_TRANSL_CAPT"ranslation failed (leave 'as is'): '"$1"'"
571
              fi
572
            fi
573
          fi
574
        fi
575
      else
576
        for word in $1; do
577
          if [ $ZERO -eq 0 ]; then  TRANSLWORD="$word" ; else TRANSLWORD="" ; fi
578
          if [ $VERBOSE -eq 1 ]; then echo "Looking up word"$REVERSE_TRANSL": "$word ; fi
579
          translate "$word"
580
          if [ -n "$RESULT" ]; then
581
            TRANSLWORD=$RESULT
582
            if [ -n "$RESULT" ] && [ $VERBOSE -eq 1 ]; then echo $REVERSE_TRANSL_CAPT"ranslation: '"$TRANSLWORD"'" ; fi
583
          else
584
            if [ $ZERO -eq 0 ] && [ $BRACKETS -eq 1 ]; then TRANSLWORD="["$TRANSLWORD"]" ; fi
585
            if [ $VERBOSE -eq 1 ]; then
586
              if [ $ZERO -eq 1 ]; then
587
                echo $REVERSE_TRANSL_CAPT"ranslation failed (return not translatable tokens as null-string): "$word
588
              else
589
                if [ $BRACKETS -eq 1 ]; then
590
                  echo $REVERSE_TRANSL_CAPT"ranslation failed (enclosing word in brackets): "$word
591
                else
592
                  echo $REVERSE_TRANSL_CAPT"ranslation failed (leave 'as is'): "$word
593
                fi
594
              fi
595
            fi
596
          fi
597
          TRANSL=$TRANSL" "$TRANSLWORD
598
        done
599
      fi
600
      if [ $VERBOSE -eq 1 ]; then echo "Overall translation:" ; fi
601
      if [ -n "$TRANSL" ]; then echo $TRANSL ; fi
602
      if [ $VERBOSE -eq 1 ]; then echo ; fi
603
    else
604
      # send errmsg to stderr
605
      if [ -n "$TRANSLATE_DIR" ] && [ ! -e "$TRANSLATE_DIR" ]; then
606
        if [ $MESSAGES -eq 1 ] || [ $VERBOSE -eq 1 ]; then echo $CH_SOUND"$APPFILENAME: Unable to locate dictionary root: '"$TRANSLATE_DIR"'" 1>&2 ; fi
607
      else
608
        if [ $INFO -eq 0 ] && [ $ADD -eq 1 ] || [ $OVERWRITE -eq 1 ]; then
609
          if [ $VERBOSE -eq 1 ]; then
610
            echo
611
            echo $DICT": Creating dictionary"
612
          fi
613
          if [ -n "$4" ]; then
614
            if [ -z "$5" ]; then
615
              if [ $VERBOSE -eq 1 ]; then echo $DICT": Adding information: '"$APPNAME" "$APPVER" dictionary file created by '"`whoami`"' on "`date`"'" ; fi
616
              echo "#"$DELIMITER$APPNAME" "$APPVER" dictionary file created by '"`whoami`"' on "`date` &>$DICT
617
            else
618
              if [ $VERBOSE -eq 1 ]; then echo $DICT": Adding information: '"$5"'" ; fi
619
              echo "#"$DELIMITER$5 &>$DICT
620
            fi
621
            if [ $? -eq 0 ]; then
622
              if [ $VERBOSE -eq 1 ]; then
623
                echo $DICT": Adding expression: '"$1"'"
624
                echo $DICT": Adding translation: '"$4"'"
625
              fi
626
              echo $1$DELIMITER$4 >>$DICT
627
            else
628
              if [ $MESSAGES -eq 1 ] || [ $VERBOSE -eq 1 ]; then echo $CH_SOUND$APPFILENAME": Unable to create dictionary file: '"$DICT"'" ; fi
629
              exit EDICT_CREATE_ERROR
630
            fi
631
          else
632
            if [ $MESSAGES -eq 1 ] || [ $VERBOSE -eq 1 ]; then echo $CH_SOUND$APPFILENAME": No translation given for '"$1"'" ; fi
633
          fi
634
        else
635
          if [ $MESSAGES -eq 1 ] || [ $VERBOSE -eq 1 ]; then echo $CH_SOUND$APPFILENAME": Unable to locate dictionary file: '"$DICT"' (please double-quote phrases)" 1>&2 ; fi
636
        fi
637
      fi
638
      if [ $VERBOSE -eq 1 ]; then echo ; fi
639
      if [ -n "$TRANSLATE_DIR" ] && [ ! -e "$TRANSLATE_DIR" ]; then
640
        exit $EDICT_DIR_NOT_FOUND
641
      else
642
        exit $EDICT_NOT_FOUND
643
      fi
644
    fi
645
  else
646
    if [ $MESSAGES -eq 1 ] || [ $VERBOSE -eq 1 ]; then
647
      # send errmsg to stderr
648
      echo $CH_SOUND$APPFILENAME": No dictionary file given" 1>&2
649
    fi
650
    if [ $VERBOSE -eq 1 ]; then echo ; fi
651
    exit $ENO_DICT
652
  fi
653
else
654
  help
655
fi