Subversion Repositories LCARS

Rev

Rev 16 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 16 Rev 288
Line 1... Line 1...
1
#!/bin/bash
1
#!/bin/bash
2
2
3
APPNAME="EazyTranslator"
3
appname="EazyTranslator"
4
APPVER="0.97a"
4
appver="0.97a"
5
APPFILENAME=`basename $0`
5
appfilename=$(readlink $(basename "$0"))
6
APPFILEDIR=`echo $0 | sed 's/'$APPFILENAME$'//'`
6
[ -n "$appfilename" ] && appfiledir=$(echo "$0" | sed "s/$appfilename\$//")
7
7
8
# dictionary file
8
# dictionary file
9
DICT="$TRANSLATE_DIR"
9
dict="$TRANSLATE_DIR"
10
if [ -z "$DICT" ]; then DICT=$APPFILEDIR ; fi
10
[ -z "$dict" ] && dict=$appfiledir
11
11
12
# delimiter name (for help page only) and character (string) for separating original expression
12
# delimiter name (for help page only) and character (string) for separating original expression
13
# and translation in dictionary file
13
# and translation in dictionary file
14
DELIMITER_NAME="colon"
14
delimiter_name="colon"
15
DELIMITER=":"
15
delimiter=":"
16
16
17
# all help on a single page
17
# all help on a single page
18
SINGLE_PAGE=0
18
single_page=0
19
19
20
# bell character
20
# bell character
21
CH_SOUND=""
21
ch_sound=""
22
CH_SOUND_ON=$'\a'
22
ch_sound_on=$'\a'
23
23
24
RESULT=""
24
result=""
25
25
26
function title {
26
title() {
27
  if [ $SINGLE_PAGE -eq 0 ]; then clear ; fi
27
  #[ $single_page -eq 0 ] && clear
28
  echo
28
  echo "
29
  echo $APPNAME" "$APPVER" - Stream editor to use and to manage dictionary files"
29
$appname $appver - Stream editor to use and to manage dictionary files
30
  echo "Requires 'basename', 'grep' and 'sed', optionally 'sort' and 'mktemp' in PATH"
30
Requires 'basename', 'grep' and 'sed', optionally 'sort' and 'mktemp' in PATH
31
  echo
31
"
32
}
32
}
33
33
34
function copyright {
34
copyright() {
35
  echo "Copyright (C) 2001  Thomas Lahn (webmaster@PointedEars.de)"
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."
36
Be sure to have 'easyTrans' or similar in mail subject line for fast response.
37
  echo
37
"
38
}
38
}
39
39
40
function pause {
40
pause() {
41
  if [ "$1" == "c" ]; then
41
  if [ "$1" == "c" ]; then
42
    echo "Hit RETURN to continue..."
42
    echo "Hit RETURN to continue..."
43
  else
43
  else
44
    echo "Hit RETURN for the next page"
44
    echo "Hit RETURN for the next page"
45
  fi
45
  fi
46
  read
46
  read
47
}
47
}
48
48
49
function copying {
49
copying() {
50
  title
50
  title
51
  copyright
51
  copyright
52
  echo "This program is free software; you can redistribute it and/or modify"
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"
53
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"
54
the Free Software Foundation; either version 2 of the License, or
55
  echo "(at your option) any later version."
55
(at your option) any later version.
56
  echo
56

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

62
  echo "You should have received a copy of the GNU General Public License"
62
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"
63
along with this program (COPYING file); if not, write to the
64
  echo "Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA."
64
Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
65
  echo
65
"
66
}
66
}
67
67
68
function cmdHelp {
68
cmdHelp() {
69
  title
69
  title
70
  echo $APPFILENAME" EXPRESSION DICTIONARY [OPTIONS]"
70
  echo "$appfilename EXPRESSION DICTIONARY [OPTIONS]
71
  echo
71

72
  echo "Tries to translate EXPRESSION looking up DICTIONARY and writes the result"
72
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
73

75
  echo "Environment:"
74
Environment:
76
  echo
75

77
  echo "TRANSLATE_DIR     Dictionary folder root (absolute path '/')"
76
TRANSLATE_DIR     Dictionary folder root (absolute path '/')"
78
if [ -n "$TRANSLATE_DIR" ]; then
77
  if [ -n "$TRANSLATE_DIR" ]; then
79
  local INVALID_PATH=" -- INVALID PATH!"
78
    local INVALID_PATH=" -- INVALID PATH!"
80
  local INVALID_FORMAT=" -- INVALID FORMAT!"
79
    local INVALID_FORMAT=" -- INVALID FORMAT!"
81
  # : AutoCorrect performed!
80
    # : AutoCorrect performed!
82
  local INVALID_MSG=""
81
    local INVALID_MSG=""
83
  if [ ! -e $TRANSLATE_DIR ]; then
82
    if [ ! -e $TRANSLATE_DIR ]; then
84
    INVALID_MSG=$INVALID_PATH
83
      INVALID_MSG=$INVALID_PATH
85
  else
84
    else
86
    local GREPRES=`echo $TRANSLATE_DIR | grep -e "\/$"`
85
      local GREPRES=$(echo $TRANSLATE_DIR | grep -e "\/$")
87
    if [ -z "$GREPRES" ]; then INVALID_MSG=$INVALID_FORMAT ; fi
86
      [ -z "$GREPRES" ] && INVALID_MSG=$INVALID_FORMAT
88
  fi
87
    fi
89
  echo "                  (currently '"$TRANSLATE_DIR"'"$INVALID_MSG")"
88
    echo "                  (currently '"$TRANSLATE_DIR"'"$INVALID_MSG")"
90
  #if [ "$INVALID_MSG" == "$INVALID_FORMAT" ]; then
89
    #if [ "$INVALID_MSG" == "$INVALID_FORMAT" ]; then
91
    #export TRANSLATE_DIR=$TRANSLATE_DIR"/"
90
      #export TRANSLATE_DIR=$TRANSLATE_DIR"/"
92
    #set -a
91
      #set -a
93
  #fi
92
    #fi
94
fi
93
  fi
-
 
94
  echo "\
95
  echo "                  If undefined, this is the program directory"
95
                    If undefined, this is the program directory
96
  echo "                  (currently '"$APPFILEDIR"')."
96
                    (currently '$appfiledir').
97
  echo "TRANSLATE_OPTIONS Default options to overwrite command-line options"
97
TRANSLATE_OPTIONS Default options to overwrite command-line options"
98
if [ -n "$TRANSLATE_OPTIONS" ]; then
98
if [ -n "$TRANSLATE_OPTIONS" ]; then
99
  echo "                  (currently '"$TRANSLATE_OPTIONS"')"
99
  echo "                  (currently '"$TRANSLATE_OPTIONS"')"
100
fi
100
fi
101
  echo
101
  echo "
102
  echo "Arguments:"
102
Arguments:
103
  echo
103

104
  echo "EXPRESSION        Word or (double-quoted) phrase to be translated"
104
EXPRESSION        Word or (double-quoted) phrase to be translated
105
  echo "DICTIONARY        Path of dictionary file relative to TRANSLATE_DIR"
105
DICTIONARY        Path of dictionary file relative to TRANSLATE_DIR
106
  echo
106
"
107
  if [ $SINGLE_PAGE -eq 0 ]; then pause "c" ; fi
107
  [ $single_page -eq 0 ] && pause "c"
-
 
108
  echo "\
108
  echo "Translation OPTIONS:"
109
Translation OPTIONS:
109
  echo "  -b, --brackets  If not in DICTIONARY, writes given WORD or EXPRESSION"
110
  -b, --brackets  If not in DICTIONARY, writes given WORD or EXPRESSION
110
  echo "                  as [WORD] or [EXPRESSION]."
111
                    as [WORD] or [EXPRESSION].
111
  echo "  -m, --messages  Return error messages instead of null-strings."
112
  -m, --messages  Return error messages instead of null-strings.
112
  echo "  -p, --phrase    Translate EXPRESSION as entire phrase. If not given,"
113
  -p, --phrase    Translate EXPRESSION as entire phrase. If not given,
113
  echo "                  each WORD of EXPRESSION is translated seperately."
114
                    each WORD of EXPRESSION is translated seperately.
114
  echo "  -r, --reverse   Perform reverse translation. Recommended only if"
115
  -r, --reverse   Perform reverse translation. Recommended only if
115
  echo "                  no appropriate dictionary file for vice-versa translation is"
116
                    no appropriate dictionary file for vice-versa translation is
116
  echo "                  available and -p is also used."
117
                    available and -p is also used.
117
  echo "  -s, --sound     Beep on fatal errors."
118
  -s, --sound     Beep on fatal errors.
118
  echo "  -v, --verbose   Display flow of operation. Includes -m behavior."
119
  -v, --verbose   Display flow of operation. Includes -m behavior.
119
  echo "  -z, --zero      Return not translatable tokens as null-strings."
120
  -z, --zero      Return not translatable tokens as null-strings.
120
  echo "                  Overwrites -b."
121
                    Overwrites -b.
121
  echo
122
"
122
  if [ $SINGLE_PAGE -eq 0 ]; then pause "c" ; fi
123
  [ $single_page -eq 0 ] && pause "c"
-
 
124
  echo "\
123
  echo $APPFILENAME" EXPRESSION DICTIONARY COMMAND TRANSLATION [INFO] [OPTIONS]"
125
$appfilename EXPRESSION DICTIONARY COMMAND TRANSLATION [INFO] [OPTIONS]
124
  echo $APPFILENAME" COMMAND DICTIONARY [INFO] [OPTIONS]"
126
$appfilename COMMAND DICTIONARY [INFO] [OPTIONS]
125
  echo
127

126
  echo "Dictionary file COMMANDs:"
128
Dictionary file COMMANDs:
127
  echo "  -a,  --add      If not in DICTIONARY, add EXPRESSION with TRANSLATION"
129
  -a,  --add      If not in DICTIONARY, add EXPRESSION with TRANSLATION
128
  echo "                  to DICTIONARY and write TRANSLATION."
130
                    to DICTIONARY and write TRANSLATION.
129
  echo "                  If DICTIONARY not exists, create the file with INFO"
131
                    If DICTIONARY not exists, create the file with INFO
130
  echo "                  and add the entry; if INFO is a null-string,"
132
                    and add the entry; if INFO is a null-string,
131
  echo "                  default INFO is added, containing program version,"
133
                    default INFO is added, containing program version,
132
  echo "                  user name and timestamp. Requires 'sort'."
134
                    user name and timestamp. Requires 'sort'.
133
  echo "  -ai, --addinfo  Add information data INFO to DICTIONARY."
135
  -ai, --addinfo  Add information data INFO to DICTIONARY.
134
  echo "                  Must be used as first argument."
136
                    Must be used as first argument.
135
  echo "  -c,  --create   Create new DICTIONARY with INFO (see -a)."
137
  -c,  --create   Create new DICTIONARY with INFO (see -a).
136
  echo "                  Existing files are replaced. Must be used as first argument."
138
                    Existing files are replaced. Must be used as first argument.
-
 
139

137
  echo "  -d,  --delete   If used with EXPRESSION and DICTIONARY, remove EXPRESSION"
140
  -d,  --delete   If used with EXPRESSION and DICTIONARY, remove EXPRESSION
138
  echo "                  from DICTIONARY instead of translating."
141
                    from DICTIONARY instead of translating.
139
  echo "                  If used as first argument, delete DICTIONARY."
142
                    If used as first argument, delete DICTIONARY.
140
  echo "  -i,  --info     Display information about DICTIONARY."
143
  -i,  --info     Display information about DICTIONARY.
141
  echo "                  Must be used as first argument."
144
                    Must be used as first argument.
142
  echo
145
"
143
  if [ $SINGLE_PAGE -eq 0 ]; then pause "c" ; fi
146
  [ $single_page -eq 0 ] && pause "c"
-
 
147
  echo "\
144
  echo "  -o, --overwrite Like -a but overwrite a contained translation of"
148
  -o, --overwrite Like -a but overwrite a contained translation of
145
  echo "                  EXPRESSION with TRANSLATION without question."
149
                    EXPRESSION with TRANSLATION without question.
146
  echo "                  Additionally requires 'mktemp'."
150
                    Additionally requires 'mktemp'.
147
  echo "  -R, --repair    Repair DICTIONARY instead of translating. Requires 'mktemp'."
151
  -R, --repair    Repair DICTIONARY instead of translating. Requires 'mktemp'.
148
  echo "                  Info data is be kept but invalid entries are removed."
152
                    Info data is be kept but invalid entries are removed.
149
  echo "                  USE WITH CAUTION!"
153
                    USE WITH CAUTION!
150
  echo "  -s, --sort      Sort DICTIONARY instead of translating. Requires 'sort'."
154
  -s, --sort      Sort DICTIONARY instead of translating. Requires 'sort'.
151
  echo "                  Includes --sound when used with -v."
155
                    Includes --sound when used with -v.
152
  echo "                  Must be used as first argument."
156
                    Must be used as first argument.
153
  echo
157
"
154
  if [ $SINGLE_PAGE -eq 0 ]; then pause "c" ; fi
158
  [ $single_page -eq 0 ] && pause "c"
-
 
159
  echo "\
155
  echo $APPFILENAME" OPTION [OPTION]"
160
$appfilename OPTION [OPTION]
156
  echo
161

157
  echo "Help page OPTIONs:"
162
Help page OPTIONs:
158
  echo "  --1             Display help on one page (without 'clear' and user input)."
163
  --1             Display help on one page (without 'clear' and user input).
159
  echo "                  Useful with redirection "
164
                    Useful with redirection
160
  echo "                  (try '"$APPFILENAME" --1 --? > translate.doc.txt')."
165
                    (try '"$appfilename" --1 --? > translate.doc.txt').
161
  echo "                  Must be given before all other help page options."
166
                    Must be given before all other help page options.
162
  echo "  --a, --about    Display information about the program."
167
  --a, --about    Display information about the program.
163
  echo "  --c, --cmd      Display this help page."
168
  --c, --cmd      Display this help page.
164
  echo "  --d, --dict     Display help about dictionary files."
169
  --d, --dict     Display help about dictionary files.
165
  echo "  --dev, --emp    Display special information for developers and employers."
170
  --dev, --emp    Display special information for developers and employers.
166
  echo "  --x, --example  Display example."
171
  --x, --example  Display example.
167
  echo "  --?, --help     Display all help pages."
172
  --?, --help     Display all help pages.
168
  echo
173
"
169
}
174
}
170
175
171
function example {
176
example() {
172
  title
177
  title
173
  echo "EXAMPLE: If you would like to translate the English words 'a few' into German,"
178
  echo "EXAMPLE: If you would like to translate the English words 'a few' into German,
174
  echo
179

175
  echo "  "$APPFILENAME" ''a few'' en-de"
180
  $appfilename 'a few' en-de
176
  echo
181

177
  echo "should write the German words"
182
should write the German words
178
  echo
183

179
  echo "  ein(e) wenig(e)\n"
184
  ein(e) wenig(e)\n
180
  echo
185

181
  echo "(without indent) to stdout if the echo dictionary file 'en-de' contains"
186
(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"
187
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):"
188
entire phrase (which seems to make more sense here):
184
  echo
189

185
  echo "  "$APPFILENAME" ''a few'' en-de -p"
190
  $appfilename 'a few' en-de -p
186
  echo
191

187
  echo "should instead write the German word 'einige\n'"
192
should instead write the German word 'einige\n'
188
  echo "(replace '' in input with the double-quote character)."
193
(replace '' in input with the double-quote character).
-
 
194

189
  echo
195
"
190
}
196
}
191
197
192
function dictHelp {
198
dictHelp() {
193
  title
199
  title
-
 
200
  echo "\
194
  echo "DICTIONARY FILES:"
201
DICTIONARY FILES:
195
  echo "You may create/improve dictionary files to be used with "$APPNAME
202
You may create/improve dictionary files to be used with $appname
196
  echo "of your own. Translation data must match the following expression:"
203
of your own. Translation data must match the following expression:
197
  echo
204

198
  echo "#"$DELIMITER"File description displayed when option -i is used\n[#"$DELIMITER"File description\n]"
205
#${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]]"
206
[Expression"$delimiter"translation\\n[Next expression"$delimiter"next translation\\n]]
200
  echo "Last expression"$DELIMITER"last translation\z"
207
Last expression"$delimiter"last translation\z
201
  echo
208

202
  echo "Parts enclosed in rectangle brackets are optional. The "$DELIMITER_NAME" ("$DELIMITER") is to be used"
209
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."
210
as delimiter character between original and translated expression only.
204
  echo "Dictionary file names should contain common language identifiers separated"
211
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)."
212
by a dash (such as 'en-de.dic' for an English-German dictionary file).
206
  echo
213

207
  echo "Program updates and dictionaries can be obtained from"
214
Program updates and dictionaries can be obtained from
208
  echo "'http://pointedears.de/dev/unix/translate/'."
215
'http://pointedears.de/tools/eazytrans/'.
209
  echo "Thank you for using a program by PointedEars."
216
Thank you for using a program by PointedEars.
210
  echo
217
"
211
}
218
}
212
219
213
function devInfo {
220
devInfo() {
214
  title
221
  title
215
  copyright
222
  copyright
216
  echo "INFORMATION FOR DEVELOPERS (KNOWN ISSUES)..."
-
 
217
  echo
223
  echo "\
218
  echo "- Sorting the dictionary unfortunately also sorts its info data by now."
-
 
219
  echo
224
KNOWN ISSUES
220
  echo "...AND FOR EMPLOYERS:"
-
 
221
  echo
225

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,"
226
- Sorting the dictionary unfortunately also sorts its info data by now.
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
"
227
}
228
}
228
229
229
function help {
230
help() {
230
  copying
231
  copying
231
  if [ $SINGLE_PAGE -eq 0 ]; then pause ; fi
232
  [ $single_page -eq 0 ] && pause
232
  cmdHelp
233
  cmdHelp
233
  if [ $SINGLE_PAGE -eq 0 ]; then pause ; fi
234
  [ $single_page -eq 0 ] && pause
234
  example
235
  example
235
  if [ $SINGLE_PAGE -eq 0 ]; then pause ; fi
236
  [ $single_page -eq 0 ] && pause
236
  dictHelp
237
  dictHelp
237
  if [ $SINGLE_PAGE -eq 0 ]; then pause ; fi
238
  [ $single_page -eq 0 ] && pause
238
  devInfo
239
  devInfo
239
}
240
}
240
241
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 {
242
check_dictionary() {
259
  local EXPR="$1"
-
 
260
  if [ $REVERSE -eq 1 ]; then translateReverse "$1" ; return ; fi
243
  # check if dictionary file (contains at least '#$delimiter')
261
  if [ $VERBOSE -eq 1 ]; then echo "Executing: grep -i -e '^"$EXPR$DELIMITER"' '"$DICT"'" ; fi
244
  [ $verbose -eq 1 ] && echo "Executing: grep -e '."$delimiter".' '"$dict"'"
262
  RESULT=`grep -i -e "^$EXPR$DELIMITER" "$DICT"`
245
  result=$(grep -e "#$delimiter." "$dict")
263
  if [ $VERBOSE -eq 1 ]; then echo "grep returned: '"$RESULT"'" ; fi
-
 
264
  if [ -n "$RESULT" ]; then
246
  if [ -z "$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
247
    [ $verbose -eq 1 ] && echo "grep returned: ''"
271
  fi
-
 
272
-
 
273
  # Should return only characters after the separation character until EOL
248
    echo $dict": Not a (valid) dictionary file (type '"$appfilename" --d' for details)."
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
249
    [ $verbose -eq 1 ] && echo
351
      if [ -z "$TRANSLATE_DIR" ]; then
250
    exit $EDICT_WRONG_FORMAT
352
        echo "Dictionary root (program directory): '"$APPFILEDIR"'"
-
 
353
      else
251
  else
-
 
252
    if [ $verbose -eq 1 ]; then
354
        echo "Dictionary root (TRANSLATE_DIR): '"$TRANSLATE_DIR"'"
253
      echo "grep returned not a null-string: '$dict' seems to be a dictionary file"
355
      fi
254
    fi
356
      echo "Dictionary file: '"$2"'"
-
 
357
      echo "Dictionary file path: '"$DICT"'"
-
 
358
    fi
255
  fi
-
 
256
  result=""
-
 
257
}
359
258
360
    DEFAULT_INFO=$APPNAME" "$APPVER" dictionary file created by '"`whoami`"' on "`date`
-
 
361
-
 
362
    if [ $CREATE -eq 1 ]; then
259
create() {
363
      if [ $VERBOSE -eq 1 ]; then
-
 
364
        echo $DICT": Creating dictionary"
260
  [ $verbose -eq 1 ] && echo $dict": Creating dictionary"
365
      fi
-
 
366
      if [ -z "$3" ]; then
261
  if [ -z "$3" ]; then
367
        if [ $VERBOSE -eq 1 ]; then echo $DICT": Adding information: '"$DEFAULT_INFO ; fi
262
    [ $verbose -eq 1 ] && echo $dict": Adding information: '"$DEFAULT_INFO
368
        echo "#"$DELIMITER$EFAULT_INFO &>$DICT
263
    echo "#"$delimiter$DEFAULT_INFO &>$dict
369
      else
264
  else
370
        if [ $VERBOSE -eq 1 ]; then echo $DICT": Adding information: '"$3"'" ; fi
265
    [ $verbose -eq 1 ] && echo $dict": Adding information: '"$3"'"
371
        echo "#"$DELIMITER$3 &>$DICT
266
    echo "#"$delimiter$3 &>$dict
372
      fi
267
  fi
373
      if [ $VERBOSE -eq 1 ]; then echo ; fi
268
  [ $verbose -eq 1 ] && echo
374
      exit $?
269
  exit $?
375
    fi
-
 
376
270
}
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
271
393
      if [ $ADDINFO -eq 1 ]; then
272
add_info() {
394
        if [ -z "$3" ]; then
273
  if [ -z "$3" ]; then
395
          if [ $VERBOSE -eq 1 ]; then echo $DICT": Adding information: '"$DEFAULT_INFO"'" ; fi
274
    [ $verbose -eq 1 ] && echo $dict": Adding information: '"$DEFAULT_INFO"'"
396
          echo "#"$DELIMITER$DEFAULT_INFO >>$DICT
275
    echo "#"$delimiter$DEFAULT_INFO >>$dict
397
        else
276
  else
398
          if [ $VERBOSE -eq 1 ]; then echo $DICT": Adding information: '"$3"'" ; fi
277
    [ $verbose -eq 1 ] && echo $dict": Adding information: '"$3"'"
399
          echo "#"$DELIMITER$3 >>$DICT
278
    echo "#"$delimiter$3 >>$dict
400
        fi
279
  fi
401
        if [ $VERBOSE -eq 1 ]; then echo ; fi
280
  [ $verbose -eq 1 ] && echo
402
        exit $?
281
  exit $?
403
      fi
282
}
404
283
405
      if [ $INFO -eq 1 ]; then
284
info() {
406
        if [ $VERBOSE -eq 1 ]; then
285
  if [ $verbose -eq 1 ]; then
407
          echo "Obtaining information..."
286
    echo "Obtaining information..."
408
          echo
287
    echo
409
        fi
288
  fi
410
        grep "^#$DELIMITER" "$DICT" | sed 's/^#'$DELIMITER'//'
289
  grep "^#$delimiter" "$dict" | sed 's/^#'$delimiter'//'
411
        RESULT=`grep -e "^#$DELIMITER" "$DICT"`
290
  result=$(grep -e "^#$delimiter" "$dict")
412
        if [ -z "$RESULT" ]; then echo $CH_SOUND$DICT": No information available" ; fi
291
  [ -z "$result" ] && echo $ch_sound$dict": No information available"
413
        if [ $VERBOSE -eq 1 ]; then echo ; fi
292
  [ $verbose -eq 1 ] && echo
414
        exit $ESUCCESS
293
  exit $ESUCCESS
415
      fi
294
}
416
295
417
      if [ $ADD -eq 1 ]; then
296
add_translation() {
418
        if [ $VERBOSE -eq 1 ]; then echo ; fi
297
  [ $verbose -eq 1 ] && echo
419
        RESULT=`grep -i -e "^$1$DELIMITER" "$DICT"`
298
  result=$(grep -i -e "^$1$delimiter" "$dict")
420
        if [ -z "$RESULT" ]; then
299
  if [ -z "$result" ]; then
421
          echo $DICT": Adding expression: '"$1"'"
300
    echo $dict": Adding expression: '"$1"'"
422
          echo $DICT": Adding translation: '"$4"'"
301
    echo $dict": Adding translation: '"$4"'"
423
          if [ -n "$4" ]; then
302
    if [ -n "$4" ]; then
424
            echo $1$DELIMITER$4 >>$DICT
303
      echo $1$delimiter$4 >>$dict
425
          else
304
    else
426
            if [ $MESSAGES -eq 1 ] || [ $VERBOSE -eq 1 ]; then echo $CH_SOUND$APPFILENAME": No translation given for '"$1"'" ; fi
305
      [ $messages -eq 1 ] || [ $verbose -eq 1 ] && echo $ch_sound$appfilename": No translation given for '"$1"'"
427
          fi
306
    fi
428
          SORT=1
307
    sort=1
429
        else
308
  else
430
          translate "$1"
309
    translate "$1"
431
          echo $CH_SOUND$DICT": Expression already contained: '"$1"':"$RESULT
310
    echo $ch_sound$dict": Expression already contained: '"$1"':"$result
432
          if [ $VERBOSE -eq 1 ]; then echo ; fi
311
    [ $verbose -eq 1 ] && echo
433
          exit $EDICT_EXPR_CONTAINED
312
    exit $EDICT_EXPR_CONTAINED
434
        fi
313
  fi
435
      fi
314
}
436
315
437
      if [ $OVERWRITE -eq 1 ]; then
316
replace_translation() {
438
        if [ $VERBOSE -eq 1 ]; then echo ; fi
317
  [ $verbose -eq 1 ] && echo
439
        echo $DICT": Replacing expression: '"$1"'"
318
  echo $dict": Replacing expression: '"$1"'"
440
        echo $DICT": Replacing translation: '"$4"'"
319
  echo $dict": Replacing translation: '"$4"'"
441
        TMPFILE=`mktemp -q /tmp/$APPFILENAME.XXXXXX`
320
  TMPFILE=$(mktemp -q /tmp/$appfilename.XXXXXX)
442
        if [ $? -ne 0 ]; then
321
  if [ $? -ne 0 ]; then
443
          if [ $VERBOSE -eq 1 ]; then echo $APPFILENAME": Can't create temp file, exiting..." ; fi
322
    [ $verbose -eq 1 ] && echo $appfilename": Can't create temp file, exiting..."
444
          exit EDICT_TEMP_ERROR
323
    exit EDICT_TEMP_ERROR
445
        else
324
  else
446
          if [ -n "$4" ]; then
325
    if [ -n "$4" ]; then
447
            grep -iv -e "^$1$DELIMITER" "$DICT" &>$TMPFILE
326
      grep -iv -e "^$1$delimiter" "$dict" &>$TMPFILE
448
            if [ $? -eq 0 ]; then echo $1$DELIMITER$4 >>$TMPFILE ; fi
327
      [ $? -eq 0 ] && echo $1$delimiter$4 >>$TMPFILE
449
            if [ $? -eq 0 ]; then mv $TMPFILE $DICT &>/dev/null ; fi
328
      [ $? -eq 0 ] && mv $TMPFILE $dict &>/dev/null
450
            if [ $? -ne 0 ]; then
329
      if [ $? -ne 0 ]; then
451
              if [ $VERBOSE -eq 1 ]; then echo $APPFILENAME": Unable to replace dictionary file: '"$DICT"'" ; fi
330
        [ $verbose -eq 1 ] && echo $appfilename": Unable to replace dictionary file: '"$dict"'"
452
              exit EDICT_REPLACE_ERROR
331
        exit EDICT_REPLACE_ERROR
453
            else
332
      else
454
              SORT=1
333
        sort=1
455
            fi
334
      fi
456
          else
335
    else
457
            if [ $MESSAGES -eq 1 ] || [ $VERBOSE -eq 1 ]; then echo $CH_SOUND$APPFILENAME": No translation given for '"$1"'" ; fi
336
      [ $messages -eq 1 ] || [ $verbose -eq 1 ] && echo $ch_sound$appfilename": No translation given for '"$1"'"
458
          fi
-
 
459
        fi
337
    fi
460
      fi
338
  fi
-
 
339
}
461
340
462
      if [ $DELETE -eq 1 ]; then
341
delete() {
463
        if [ $DICTIONARY -eq 0 ]; then
342
  if [ $dictionary -eq 0 ]; then
464
          # delete entry
343
    # delete entry
465
          if [ $VERBOSE -eq 1 ]; then echo $DICT": Removing expression: "$1 ; fi
344
    [ $verbose -eq 1 ] && echo $dict": Removing expression: "$1
466
          RESULT=`grep -i -e "^$1$DELIMITER" "$DICT"`
345
    result=$(grep -i -e "^$1$delimiter" "$dict")
467
          if [ -z "$RESULT" ]; then
346
    if [ -z "$result" ]; then
468
              if [ $MESSAGES -eq 1 ] || [ $VERBOSE -eq 1 ]; then echo $DICT": Expression not contained in dictionary: '"$1"'" ; fi
347
        [ $messages -eq 1 ] || [ $verbose -eq 1 ] && echo $dict": Expression not contained in dictionary: '"$1"'"
469
              exit $EDICT_DELETE_EXPR_NOT_FOUND
348
        exit $EDICT_DELETE_EXPR_NOT_FOUND
470
          fi
349
    fi
471
          TMPFILE=`mktemp -q /tmp/$APPFILENAME.XXXXXX`
350
    TMPFILE=$(mktemp -q /tmp/$appfilename.XXXXXX)
472
          if [ $? -ne 0 ]; then
351
    if [ $? -ne 0 ]; then
473
            if [ $VERBOSE -eq 1 ]; then echo $APPFILENAME": Can't create temp file, exiting..." ; fi
352
      [ $verbose -eq 1 ] && echo $appfilename": Can't create temp file, exiting..."
474
            exit EDICT_TEMP_ERROR
353
      exit EDICT_TEMP_ERROR
475
          else
354
    else
476
            grep -iv -e "$1$DELIMITER" "$DICT" &>$TMPFILE
355
      grep -iv -e "$1$delimiter" "$dict" &>$TMPFILE
477
            if [ $? -eq 0 ]; then mv $TMPFILE $DICT &>/dev/null ; fi
356
      [ $? -eq 0 ] && mv $TMPFILE $dict &>/dev/null
478
            if [ $? -ne 0 ]; then
357
      if [ $? -ne 0 ]; then
479
              if [ $VERBOSE -eq 1 ]; then echo $DICT": Unable to modify dictionary (cannot replace file)" ; fi
358
        [ $verbose -eq 1 ] && echo $dict": Unable to modify dictionary (cannot replace file)"
480
              exit EDICT_REPLACE_ERROR
359
        exit EDICT_REPLACE_ERROR
481
            else
360
      else
482
              if [ $VERBOSE -eq 1 ]; then echo $DICT": Modification successful" ; fi
361
        [ $verbose -eq 1 ] && echo $dict": Modification successful"
483
              exit $ESUCCESS
362
        exit $ESUCCESS
484
            fi
363
      fi
485
          fi
364
    fi
486
        else
365
  else
487
          # delete dictionary
366
    # delete dictionary
488
          if [ $VERBOSE -eq 1 ]; then
367
    if [ $verbose -eq 1 ]; then
489
            echo $APPFILENAME": Deleting dictionary: '"$DICT"'"
368
      echo $appfilename": Deleting dictionary: '"$dict"'"
490
            echo $APPFILENAME": Executing: rm '"$DICT"' &>/dev/null"
369
      echo $appfilename": Executing: rm '"$dict"' &>/dev/null"
491
          fi
370
    fi
492
          rm $DICT &>/dev/null
371
    rm $dict &>/dev/null
493
          if [ $? -ne 0 ]; then
372
    if [ $? -ne 0 ]; then
494
            if [ $MESSAGES -eq 1 ] || [ $VERBOSE -eq 1 ]; then echo $DICT": Unable to remove dictionary" ; fi
373
      [ $messages -eq 1 ] || [ $verbose -eq 1 ] && echo $dict": Unable to remove dictionary"
495
            exit $EDICT_REMOVE
374
      exit $EDICT_REMOVE
496
          else
375
    else
497
            if [ $VERBOSE -eq 1 ]; then
376
      if [ $verbose -eq 1 ]; then
498
              echo $DICT": Dictionary successfully removed"
377
        echo $dict": Dictionary successfully removed"
499
              echo
378
        echo
500
            fi
379
      fi
501
            exit $ESUCCESS
380
      exit $ESUCCESS
502
          fi
381
    fi
503
        fi
382
  fi
504
      fi
383
}
505
384
506
      if [ $SORT -eq 1 ]; then
385
sort_dictionary() {
507
        if [ $VERBOSE -eq 1 ]; then
386
  if [ $verbose -eq 1 ]; then
508
          echo "Sorting vocabulary: '"$DICT"'"
387
    echo "Sorting dictionary: '"$dict"'"
509
          echo "Executing: sort -d -f -o "$DICT" -t "$DELIMITER" "$DICT
388
    echo "Executing: sort -d -f -o "$dict" -t "$delimiter" "$dict
510
        fi
389
  fi
511
        RESULT=`sort -d -f -o "$DICT" -t $DELIMITER "$DICT"`
390
  result=$(sort -d -f -o "$dict" -t $delimiter "$dict")
512
        SORT_EXIT=$?
391
  SORT_EXIT=$?
513
        if [ $VERBOSE -eq 1 ]; then
392
  if [ $verbose -eq 1 ]; then
514
          echo "sort returned exit code "$SORT_EXIT
393
    echo "sort returned exit code "$SORT_EXIT
515
          if [ $SORT_EXIT -eq 0 ]; then
394
    if [ $SORT_EXIT -eq 0 ]; then
516
            echo "Sorting successful: '"$DICT"'"
395
      echo "Sorting successful: '"$dict"'"
517
          else
396
    else
518
            echo "Sorting failed: '"$DICT"'"
397
      echo "Sorting failed: '"$dict"'"
519
          fi
398
    fi
520
          echo
399
    echo
521
        fi
400
  fi
522
        exit $SORT_EXIT
401
  exit $SORT_EXIT
523
      fi
402
}
524
403
525
      if [ $REPAIR -eq 1 ]; then
404
repair() {
526
        if [ $VERBOSE -eq 1 ]; then echo $DICT": Repairing dictionary" ; fi
405
  [ $verbose -eq 1 ] && echo $dict": Repairing dictionary"
527
        TMPFILE=`mktemp -q /tmp/$APPFILENAME.XXXXXX`
406
  TMPFILE=$(mktemp -q /tmp/$appfilename.XXXXXX)
528
        if [ $? -ne 0 ]; then
407
  if [ $? -ne 0 ]; then
529
          if [ $VERBOSE -eq 1 ]; then echo $APPFILENAME": Can't create temp file, exiting..." ; fi
408
    [ $verbose -eq 1 ] && echo $appfilename": Can't create temp file, exiting..."
530
          exit EDICT_TEMP_ERROR
409
    exit EDICT_TEMP_ERROR
531
        else
410
  else
532
          grep -e "^#$DELIMITER" "$DICT" &>$TMPFILE
411
    grep -e "^#$delimiter" "$dict" &>$TMPFILE
533
          grep -e ".$DELIMITER." "$DICT" >>$TMPFILE
412
    grep -e ".$delimiter." "$dict" >>$TMPFILE
534
          if [ $? -eq 0 ]; then mv $TMPFILE $DICT &>/dev/null ; fi
413
    [ $? -eq 0 ] && mv $TMPFILE $dict &>/dev/null
535
          if [ $? -ne 0 ]; then
414
    if [ $? -ne 0 ]; then
536
            if [ $VERBOSE -eq 1 ]; then echo $DICT": Unable to repair dictionary (cannot replace file)" ; fi
415
      [ $verbose -eq 1 ] && echo $dict": Unable to repair dictionary (cannot replace file)"
537
            exit EDICT_REPLACE_ERROR
416
      exit EDICT_REPLACE_ERROR
538
          else
417
    else
539
            $DICT": Repair successful"
418
      $dict": Repair successful"
-
 
419
    fi
-
 
420
  fi
-
 
421
}
-
 
422
-
 
423
translateReverse() {
-
 
424
  local EXPR="$1"
-
 
425
  [ $verbose -eq 1 ] && echo >&2 "Executing: grep -i -e '"$delimiter$EXPR"$' '"$dict"'"
-
 
426
  result=$(egrep -i -e "$delimiter(\([^\)]+\))?.*\<$EXPR\>.*(\([^\)]+\))?$" "$dict")
-
 
427
  [ $verbose -eq 1 ] && echo >&2 "grep returned: '"$result"'"
-
 
428
  if [ -n "$result" ]; then
-
 
429
    if [ $verbose -eq 1 ]; then
-
 
430
      echo >&2 "Reading expression from recordset (with sed requires only 1 step! :o)
-
 
431
Executing: echo $result | sed 's/$delimiter[ ]*[^$delimiter]*//'"
-
 
432
    fi
-
 
433
  # result=$(echo $result | sed 's/'$delimiter'[ ]*[^'$delimiter']*//')
-
 
434
  result=$(echo $result | sed 's/'$delimiter'.*$//')
-
 
435
    [ $verbose -eq 1 ] && echo >&2 "sed returned: '"$result"'"
540
          fi
436
  fi
-
 
437
-
 
438
  # Should return only characters before the delimiter until BOL (expression)
-
 
439
}
-
 
440
-
 
441
translate() {
-
 
442
  local EXPR="$1"
-
 
443
  if [ $reverse -eq 1 ]; then
-
 
444
    translateReverse "$1"
-
 
445
    return
-
 
446
  fi
-
 
447
  [ $verbose -eq 1 ] && echo >&2 "Executing: grep -i -e '^"$EXPR$delimiter"' '"$dict"'"
-
 
448
  result=$(grep -i -e "^$EXPR$delimiter" "$dict")
-
 
449
  [ $verbose -eq 1 ] && echo >&2 "grep returned: '"$result"'"
-
 
450
  if [ -n "$result" ]; then
-
 
451
    if [ $verbose -eq 1 ]; then
-
 
452
      echo >&2 "Reading translation from recordset (with sed requires only 1 step! :o)
-
 
453
Executing: echo $result | sed 's/[^$delimiter]*$delimiter[ ]*//'"
541
        fi
454
    fi
-
 
455
    result=$(echo $result | sed 's/[^'$delimiter']*'$delimiter'[ ]*//')
-
 
456
    [ $verbose -eq 1 ] && echo >&2 "sed returned: '"$result"'"
542
      fi
457
  fi
543
458
-
 
459
  # Should return only characters after the separation character until EOL
-
 
460
  # (translation)
-
 
461
}
-
 
462
-
 
463
translate_all() {
-
 
464
  local second_pass=0
-
 
465
  [ "$1" = "-2" ] && {
-
 
466
    shift
-
 
467
    second_pass=1
-
 
468
  }
544
      TRANSL=""
469
  TRANSL=""
545
      REVERSE_TRANSL=""
470
  REVERSE_TRANSL=""
546
      REVERSE_TRANSL_CAPT="T"
471
  REVERSE_TRANSL_CAPT="T"
547
      REVERSE_TRANSL_ON=" (reverse)"
472
  REVERSE_TRANSL_on=" (reverse)"
548
      REVERSE_TRANSL_CAPT_ON="Reverse t"
473
  REVERSE_TRANSL_CAPT_on="Reverse t"
549
      if [ $REVERSE -eq 1 ]; then
474
  if [ $reverse -eq 1 ]; then
550
        REVERSE_TRANSL=$REVERSE_TRANSL_ON
475
    REVERSE_TRANSL=$REVERSE_TRANSL_on
551
        REVERSE_TRANSL_CAPT=$REVERSE_TRANSL_CAPT_ON
476
    REVERSE_TRANSL_CAPT=$REVERSE_TRANSL_CAPT_on
552
      fi
477
  fi
553
      if [ $PHRASE -eq 1 ] ; then
478
  if [ $phrase -eq 1 ] ; then
554
        if [ $VERBOSE -eq 1 ]; then echo "Looking up phrase"$REVERSE_TRANSL": '"$1"'" ; fi
479
    [ $verbose -eq 1 ] && echo "Looking up phrase"$REVERSE_TRANSL": '"$1"'"
555
        if [ $ZERO -eq 0 ]; then
480
    if [ $zero -eq 0 ]; then
-
 
481
      TRANSL="$1"
556
          TRANSL="$1" ; if [ $BRACKETS -eq 1 ]; then TRANSL="["$TRANSL"]" ; fi
482
      [ $brackets -eq 1 ] && TRANSL="["$TRANSL"]"
557
        fi
483
    fi
558
        translate "$1"
484
    translate "$1"
559
        if [ -n "$RESULT" ]; then
485
    if [ -n "$result" ]; then
560
          TRANSL=$RESULT
486
      TRANSL=$result
561
          if [ $VERBOSE -eq 1 ]; then echo $REVERSE_TRANSL_CAPT"ranslation: '"$TRANSL"'" ; fi
487
      [ $verbose -eq 1 ] && echo $REVERSE_TRANSL_CAPT"ranslation: '"$TRANSL"'"
562
        else
488
    else
563
          if [ $VERBOSE -eq 1 ]; then
489
      if [ $verbose -eq 1 ]; then
564
            if [ $ZERO -eq 1 ]; then
490
        if [ $zero -eq 1 ]; then
565
              echo $REVERSE_TRANSL_CAPT"ranslation failed (return not translatable tokens as null-string): '"$1"'"
491
          echo $REVERSE_TRANSL_CAPT"ranslation failed (return not translatable tokens as null-string): '"$1"'"
566
            else
492
        else
567
              if [ $BRACKETS -eq 1 ]; then
493
          if [ $brackets -eq 1 ]; then
568
                echo $CH_SOUND$REVERSE_TRANSL_CAPT"ranslation failed (enclosing phrase in brackets): '"$1"'"
494
            echo $ch_sound$REVERSE_TRANSL_CAPT"ranslation failed (enclosing phrase in brackets): '"$1"'"
569
              else
495
          else
570
                echo $CH_SOUND$REVERSE_TRANSL_CAPT"ranslation failed (leave 'as is'): '"$1"'"
496
            echo $ch_sound$REVERSE_TRANSL_CAPT"ranslation failed (leave 'as is'): '"$1"'"
571
              fi
497
          fi
572
            fi
498
        fi
573
          fi
499
      fi
574
        fi
500
    fi
575
      else
501
  else
576
        for word in $1; do
502
    for word in $@; do
577
          if [ $ZERO -eq 0 ]; then  TRANSLWORD="$word" ; else TRANSLWORD="" ; fi
503
      if [ $zero -eq 0 ]; then
-
 
504
        TRANSLWORD="$word"
-
 
505
      else
-
 
506
        TRANSLWORD=""
-
 
507
      fi
578
          if [ $VERBOSE -eq 1 ]; then echo "Looking up word"$REVERSE_TRANSL": "$word ; fi
508
      [ $verbose -eq 1 ] && echo "Looking up word"$REVERSE_TRANSL": "$word
579
          translate "$word"
509
      translate "$word"
-
 
510
      if [ -n "$result" ]; then
-
 
511
        TRANSLWORD=$result
-
 
512
      elif [ -n "$wsep" ] && [ $second_pass -eq 0 ]; then
-
 
513
        word=${word//$wsep/ }
-
 
514
        translate_all -2 $word
580
          if [ -n "$RESULT" ]; then
515
        if [ -n "$result" ]; then
581
            TRANSLWORD=$RESULT
516
          TRANSLWORD=$result
-
 
517
        fi
-
 
518
      fi
582
            if [ -n "$RESULT" ] && [ $VERBOSE -eq 1 ]; then echo $REVERSE_TRANSL_CAPT"ranslation: '"$TRANSLWORD"'" ; fi
519
      [ -n "$result" ] && [ $verbose -eq 1 ] && echo $REVERSE_TRANSL_CAPT"ranslation: '"$TRANSLWORD"'"
-
 
520
583
          else
521
      if [ -z "$result" ]; then
584
            if [ $ZERO -eq 0 ] && [ $BRACKETS -eq 1 ]; then TRANSLWORD="["$TRANSLWORD"]" ; fi
522
        [ $zero -eq 0 ] && [ $brackets -eq 1 ] && TRANSLWORD="["$TRANSLWORD"]"
585
            if [ $VERBOSE -eq 1 ]; then
523
        if [ $verbose -eq 1 ]; then
586
              if [ $ZERO -eq 1 ]; then
524
          if [ $zero -eq 1 ]; then
587
                echo $REVERSE_TRANSL_CAPT"ranslation failed (return not translatable tokens as null-string): "$word
525
            echo $REVERSE_TRANSL_CAPT"ranslation failed (return not translatable tokens as null-string): "$word
588
              else
526
          else
589
                if [ $BRACKETS -eq 1 ]; then
527
            if [ $brackets -eq 1 ]; then
590
                  echo $REVERSE_TRANSL_CAPT"ranslation failed (enclosing word in brackets): "$word
528
              echo $REVERSE_TRANSL_CAPT"ranslation failed (enclosing word in brackets): "$word
591
                else
529
            else
592
                  echo $REVERSE_TRANSL_CAPT"ranslation failed (leave 'as is'): "$word
530
              echo $REVERSE_TRANSL_CAPT"ranslation failed (leave 'as is'): "$word
593
                fi
531
            fi
594
              fi
532
          fi
595
            fi
533
        fi
596
          fi
534
      fi
597
          TRANSL=$TRANSL" "$TRANSLWORD
535
      TRANSL=$TRANSL" "$TRANSLWORD
598
        done
536
    done
599
      fi
537
  fi
-
 
538
}
-
 
539
-
 
540
# exit codes
-
 
541
ESUCCESS=0
-
 
542
ENO_DICT=1
-
 
543
EDICT_DIR_NOT_FOUND=2
-
 
544
EDICT_NOT_FOUND=3
-
 
545
EDICT_WRONG_FORMAT=4
-
 
546
EDICT_EXPR_CONTAINED=5
-
 
547
EDICT_TEMP_ERROR=6
-
 
548
EDICT_CREATE_ERROR=7
-
 
549
EDICT_DELETE_EXPR_NOT_FOUND=8
-
 
550
EDICT_REMOVE=9
-
 
551
-
 
552
# argument flags
-
 
553
dictionary=0 # if 1, -d refers to the dictionary file instead of an entry
-
 
554
-
 
555
# option flags - may be replaced by declare -i FLAG
-
 
556
add=0
-
 
557
addinfo=0
-
 
558
brackets=0
-
 
559
create=0
-
 
560
delete=0
-
 
561
info=0
-
 
562
messages=0
-
 
563
overwrite=0
-
 
564
phrase=0
-
 
565
repair=0
-
 
566
reverse=0
-
 
567
show_version=0
-
 
568
sort=0
-
 
569
sound=0
-
 
570
verbose=0
-
 
571
zero=0
-
 
572
-
 
573
if $(getopt -T >/dev/null 2>&1); [ $? = 4 ]; then
-
 
574
  getopt_type=long
-
 
575
#  echo "getopt(1) type:     enhanced" >&2
-
 
576
  tmp=$(getopt -o hVbmpoRrSsvzA:a:c:D:d:E:e:i:t: \
-
 
577
               -l help,version\
-
 
578
,brackets,messages,phrase,overwrite,repair,reverse,sort,sound,verbose,zero\
-
 
579
,add:,addinfo:,dictionary:,delete:word-separator:\
-
 
580
,about,cmd,dictionary-help,dev,emp,1,x,example \
-
 
581
               -n "$appname" \
-
 
582
               -- "$@")
-
 
583
else
-
 
584
  getopt_type=short
-
 
585
#  echo "getopt(1) type:     old" >&2
-
 
586
  tmp=$(getopt bdhVmpoRrSsvzA:a:c:D:E:e:i: "$@")
-
 
587
fi
-
 
588
-
 
589
getopt_exit_code=$?
-
 
590
-
 
591
if [ $getopt_exit_code -eq 0 ]; then
-
 
592
##     getopt  returns  error  code 0 for successful parsing, 1 if
-
 
593
##     getopt(3) returns errors, 2 if it does not understand its
-
 
594
##     own parameters, 3 if an internal error occurs like out-of-
-
 
595
##     memory, and 4 if it is called with -T.
-
 
596
#
-
 
597
# Note the quotes around '$tmp': they are essential!
-
 
598
#  echo $tmp
-
 
599
# remove "--"
-
 
600
#  for i in $tmp; do if [ "$i" != "--" ]; then tmp2="${tmp2} $i"; fi; done
-
 
601
  eval set -- "$tmp"
-
 
602
  echo "${extd}New arguments:      $*$norm" >&2
-
 
603
-
 
604
  # First pass to make sure that only help is displayed if wanted
-
 
605
  for arg in $*
-
 
606
  do
-
 
607
    case "$arg" in
-
 
608
      -h | --help)      help; exit $ESUCCESS;;
-
 
609
#      -V | --version)  show_version=1;;
-
 
610
      --)               break;;
-
 
611
    esac
-
 
612
  done
-
 
613
-
 
614
  # Second pass to deal with the other options
-
 
615
  while true
-
 
616
  do
-
 
617
    case "$1" in
-
 
618
      # switches
-
 
619
      -b | --brackets)       brackets=1;;
-
 
620
      -r | --reverse)        reverse=1;;
-
 
621
      -v | --verbose)        verbose=1; title; copyright;;
-
 
622
      # options
-
 
623
      -D | --dictionary)     shift; dict="$1";;
-
 
624
      -t | --word-separator) shift; wsep="$1";;
-
 
625
      --)                    shift; break;;
-
 
626
    esac
-
 
627
    shift
-
 
628
  done
-
 
629
  [ -n "$*" ] && args=$args" $*"
-
 
630
  [ -z "$args" -a $show_version -eq 0 -a -z "$dict" ] && echo help
-
 
631
  set -- $args
-
 
632
else
-
 
633
#  echo "getopt exited: $getopt_exit_code
-
 
634
#  " >&2
-
 
635
  if [ $getopt_exit_code -eq 1 ] || [ $getopt_exit_code -eq 2 ]; then
-
 
636
    echo
-
 
637
    echo help
-
 
638
  else
-
 
639
    exit $getopt_exit_code
-
 
640
  fi
-
 
641
fi
-
 
642
-
 
643
if [ -n "$1" ]; then
-
 
644
  # retrieve arguments and use default settings from environment variable
-
 
645
  arguments=$*
-
 
646
  [ -n "$TRANSLATE_OPTIONS" ] && arguments=$arguments" "$TRANSLATE_OPTIONS
-
 
647
 
-
 
648
  # check options
-
 
649
#  for argument in $arguments
-
 
650
#  do
-
 
651
#    case "$argument" in
-
 
652
#      "-m" | "--messages") messages=1;;
-
 
653
#      "-p" | "--phrase")   phrase=1;;
-
 
654
#      "-r" | "--reverse")  reverse=1;;
-
 
655
#      "-s" | "--sound")    sound=1;;
-
 
656
#      "-z" | "--zero")     zero=1;;
-
 
657
#      "--a" | "--about")   copying; exit $ESUCCESS;;
-
 
658
#      "--c" | "--cmd")     cmdHelp; exit $ESUCCESS;;
-
 
659
#      "--d" | "--dictionary-help")    dictHelp; exit $ESUCCESS;;
-
 
660
#      "--dev" | "--emp")   devInfo; exit $ESUCCESS;;
-
 
661
#      "--1") single_page=1;;
-
 
662
#      "--x" | "--example") example; exit $ESUCCESS;;
-
 
663
#      "-h" | "--help")    help; exit $ESUCCESS;;
-
 
664
#    esac
-
 
665
#  done
-
 
666
 
-
 
667
  [ $sound -eq 1 ] && ch_sound=$ch_sound_on
-
 
668
 
-
 
669
  if [ -n "$dict" ]; then
-
 
670
#    # concatenate dictionary root and given dictionary file
-
 
671
#    dict="$dict$2"
-
 
672
#    # check for dictionary commands
-
 
673
#    case "$1" in
-
 
674
#      "-d" | "--delete") delete=1 ; dictionary=1 ;;
-
 
675
#      "-c" | "--create") create1;;
-
 
676
#      "-i" | "--info")   info=1;;
-
 
677
#      "-S" | "--sort")   sort=1;;
-
 
678
#      "-R" | "--repair") repair=1;;
-
 
679
#    esac
-
 
680
#    
-
 
681
#    case "$3" in
-
 
682
#      "-a"  | "--add")       add=1;;
-
 
683
#      "-A" | "--addinfo")   addinfo=1;;
-
 
684
#      "-d"  | "--delete")    delete=1;;
-
 
685
#      "-o"  | "--overwrite") overwrite=1;;
-
 
686
#    esac
-
 
687
#
-
 
688
#    if [ $verbose -eq 1 ]; then
-
 
689
#      if [ -z "$TRANSLATE_DIR" ]; then
-
 
690
#        echo "Dictionary root (program directory): '"$appfiledir"'"
-
 
691
#      else
-
 
692
#        echo "Dictionary root (TRANSLATE_DIR): '"$TRANSLATE_DIR"'"
-
 
693
#      fi
-
 
694
#      echo "Dictionary file: '$2'"
-
 
695
#      echo "Dictionary file path: '$dict'"
-
 
696
#    fi
-
 
697
#
-
 
698
#    DEFAULT_INFO="$appname $appver dictionary file created by '$(whoami)' on $(date)"
-
 
699
#
-
 
700
#    [ $create -eq 1 ] && create
-
 
701
#
-
 
702
    if [ -e "$dict" ]; then # if dictionary file exists
-
 
703
      check_dictionary
-
 
704
-
 
705
#      [ $addinfo -eq 1 ] && add_info
-
 
706
#      [ $info -eq 1 ] && info
-
 
707
#      [ $add -eq 1 ] && add_translation
-
 
708
#      [ $overwrite -eq 1 ] && replace_translation
-
 
709
#      [ $delete -eq 1 ] && delete
-
 
710
#      [ $sort -eq 1 ] && sort_dictionary
-
 
711
#      [ $repair -eq 1 ] && repair
-
 
712
     
-
 
713
      translate_all "$@"
-
 
714
           
600
      if [ $VERBOSE -eq 1 ]; then echo "Overall translation:" ; fi
715
      [ $verbose -eq 1 ] && echo "Overall translation:"
601
      if [ -n "$TRANSL" ]; then echo $TRANSL ; fi
716
      [ -n "$TRANSL" ] && echo $TRANSL
602
      if [ $VERBOSE -eq 1 ]; then echo ; fi
717
      [ $verbose -eq 1 ] && echo
603
    else
718
    else
604
      # send errmsg to stderr
719
      # send errmsg to stderr
605
      if [ -n "$TRANSLATE_DIR" ] && [ ! -e "$TRANSLATE_DIR" ]; then
720
      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
721
        [ $messages -eq 1 ] || [ $verbose -eq 1 ] && echo $ch_sound"$appfilename: Unable to locate dictionary root: '"$TRANSLATE_DIR"'" 1>&2
607
      else
722
      else
608
        if [ $INFO -eq 0 ] && [ $ADD -eq 1 ] || [ $OVERWRITE -eq 1 ]; then
723
        if [ $info -eq 0 ] && [ $add -eq 1 ] || [ $overwrite -eq 1 ]; then
609
          if [ $VERBOSE -eq 1 ]; then
724
          if [ $verbose -eq 1 ]; then
610
            echo
725
            echo
611
            echo $DICT": Creating dictionary"
726
            echo $dict": Creating dictionary"
612
          fi
727
          fi
613
          if [ -n "$4" ]; then
728
          if [ -n "$4" ]; then
614
            if [ -z "$5" ]; then
729
            if [ -z "$5" ]; then
615
              if [ $VERBOSE -eq 1 ]; then echo $DICT": Adding information: '"$APPNAME" "$APPVER" dictionary file created by '"`whoami`"' on "`date`"'" ; fi
730
              [ $verbose -eq 1 ] && echo $dict": Adding information: '"$appname" "$appver" dictionary file created by '"$(whoami)"' on "$(date)"'"
616
              echo "#"$DELIMITER$APPNAME" "$APPVER" dictionary file created by '"`whoami`"' on "`date` &>$DICT
731
              echo "#"$delimiter$appname" "$appver" dictionary file created by '"$(whoami)"' on "$(date) &>$dict
617
            else
732
            else
618
              if [ $VERBOSE -eq 1 ]; then echo $DICT": Adding information: '"$5"'" ; fi
733
              [ $verbose -eq 1 ] && echo $dict": Adding information: '"$5"'"
619
              echo "#"$DELIMITER$5 &>$DICT
734
              echo "#"$delimiter$5 &>$dict
620
            fi
735
            fi
621
            if [ $? -eq 0 ]; then
736
            if [ $? -eq 0 ]; then
622
              if [ $VERBOSE -eq 1 ]; then
737
              if [ $verbose -eq 1 ]; then
623
                echo $DICT": Adding expression: '"$1"'"
738
                echo $dict": Adding expression: '"$1"'"
624
                echo $DICT": Adding translation: '"$4"'"
739
                echo $dict": Adding translation: '"$4"'"
625
              fi
740
              fi
626
              echo $1$DELIMITER$4 >>$DICT
741
              echo $1$delimiter$4 >>$dict
627
            else
742
            else
628
              if [ $MESSAGES -eq 1 ] || [ $VERBOSE -eq 1 ]; then echo $CH_SOUND$APPFILENAME": Unable to create dictionary file: '"$DICT"'" ; fi
743
              [ $messages -eq 1 ] || [ $verbose -eq 1 ] && echo $ch_sound$appfilename": Unable to create dictionary file: '"$dict"'"
629
              exit EDICT_CREATE_ERROR
744
              exit EDICT_CREATE_ERROR
630
            fi
745
            fi
631
          else
746
          else
632
            if [ $MESSAGES -eq 1 ] || [ $VERBOSE -eq 1 ]; then echo $CH_SOUND$APPFILENAME": No translation given for '"$1"'" ; fi
747
            [ $messages -eq 1 ] || [ $verbose -eq 1 ] && echo $ch_sound$appfilename": No translation given for '"$1"'"
633
          fi
748
          fi
634
        else
749
        else
-
 
750
          if [ $messages -eq 1 ] || [ $verbose -eq 1 ]; then
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
751
           echo >&2 "$ch_sound$appfilename: Unable to locate dictionary file: '$dict'"
-
 
752
          fi
636
        fi
753
        fi
637
      fi
754
      fi
638
      if [ $VERBOSE -eq 1 ]; then echo ; fi
755
      [ $verbose -eq 1 ] && echo
639
      if [ -n "$TRANSLATE_DIR" ] && [ ! -e "$TRANSLATE_DIR" ]; then
756
      if [ -n "$TRANSLATE_DIR" ] && [ ! -e "$TRANSLATE_DIR" ]; then
640
        exit $EDICT_DIR_NOT_FOUND
757
        exit $EDICT_DIR_NOT_FOUND
641
      else
758
      else
642
        exit $EDICT_NOT_FOUND
759
        exit $EDICT_NOT_FOUND
643
      fi
760
      fi
644
    fi
761
    fi
645
  else
762
  else
646
    if [ $MESSAGES -eq 1 ] || [ $VERBOSE -eq 1 ]; then
763
    if [ $messages -eq 1 ] || [ $verbose -eq 1 ]; then
647
      # send errmsg to stderr
764
      # send errmsg to stderr
648
      echo $CH_SOUND$APPFILENAME": No dictionary file given" 1>&2
765
      echo >&2 $ch_sound$appfilename": No dictionary file given"
649
    fi
766
    fi
650
    if [ $VERBOSE -eq 1 ]; then echo ; fi
767
    [ $verbose -eq 1 ] && echo
651
    exit $ENO_DICT
768
    exit $ENO_DICT
652
  fi
769
  fi
653
else
770
else
654
  help
771
  help
655
fi
772
fi