]> git.sur5r.net Git - openldap/blob - build/shtool
ITS#3914 bug was very close to that leak: rwm_dnattr_rewrite() already freed the...
[openldap] / build / shtool
1 #!/bin/sh
2 ##
3 ##  GNU shtool -- The GNU Portable Shell Tool
4 ##  Copyright (c) 1994-2005 Ralf S. Engelschall <rse@engelschall.com>
5 ##
6 ##  See http://www.gnu.org/software/shtool/ for more information.
7 ##  See ftp://ftp.gnu.org/gnu/shtool/ for latest version.
8 ##
9 ##  Version:  2.0.2 (15-Jun-2005)
10 ##  Contents: 6/19 available modules
11 ##
12
13 ##
14 ##  This program is free software; you can redistribute it and/or modify
15 ##  it under the terms of the GNU General Public License as published by
16 ##  the Free Software Foundation; either version 2 of the License, or
17 ##  (at your option) any later version.
18 ##
19 ##  This program is distributed in the hope that it will be useful,
20 ##  but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ##  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 ##  General Public License for more details.
23 ##
24 ##  You should have received a copy of the GNU General Public License
25 ##  along with this program; if not, write to the Free Software
26 ##  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
27 ##  USA, or contact Ralf S. Engelschall <rse@engelschall.com>.
28 ##
29 ##  NOTICE: Given that you include this file verbatim into your own
30 ##  source tree, you are justified in saying that it remains separate
31 ##  from your package, and that this way you are simply just using GNU
32 ##  shtool. So, in this situation, there is no requirement that your
33 ##  package itself is licensed under the GNU General Public License in
34 ##  order to take advantage of GNU shtool.
35 ##
36
37 ##
38 ##  Usage: shtool [<options>] [<cmd-name> [<cmd-options>] [<cmd-args>]]
39 ##
40 ##  Available commands:
41 ##    echo       Print string with optional construct expansion
42 ##    move       Move files with simultaneous substitution
43 ##    install    Install a program, script or datafile
44 ##    mkdir      Make one or more directories
45 ##    mkln       Make link with calculation of relative paths
46 ##    subst      Apply sed(1) substitution operations
47 ##
48 ##  Not available commands (because module was not built-in):
49 ##    mdate      Pretty-print modification time of a file or dir
50 ##    table      Pretty-print a field-separated list as a table
51 ##    prop       Display progress with a running propeller
52 ##    mkshadow   Make a shadow tree through symbolic links
53 ##    fixperm    Fix file permissions inside a source tree
54 ##    rotate     Logfile rotation
55 ##    tarball    Roll distribution tarballs
56 ##    platform   Platform Identification Utility
57 ##    arx        Extended archive command
58 ##    slo        Separate linker options by library class
59 ##    scpp       Sharing C Pre-Processor
60 ##    version    Maintain a version information file
61 ##    path       Deal with program paths
62 ##
63
64 if [ $# -eq 0 ]; then
65     echo "$0:Error: invalid command line" 1>&2
66     echo "$0:Hint:  run \`$0 -h' for usage" 1>&2
67     exit 1
68 fi
69 if [ ".$1" = ".-h" ] || [ ".$1" = ".--help" ]; then
70     echo "This is GNU shtool, version 2.0.2 (15-Jun-2005)"
71     echo "Copyright (c) 1994-2005 Ralf S. Engelschall <rse@engelschall.com>"
72     echo "Report bugs to <bug-shtool@gnu.org>"
73     echo ''
74     echo "Usage: shtool [<options>] [<cmd-name> [<cmd-options>] [<cmd-args>]]"
75     echo ''
76     echo 'Available global <options>:'
77     echo '  -v, --version   display shtool version information'
78     echo '  -h, --help      display shtool usage help page (this one)'
79     echo '  -d, --debug     display shell trace information'
80     echo '  -r, --recreate  recreate this shtool script via shtoolize'
81     echo ''
82     echo 'Available <cmd-name> [<cmd-options>] [<cmd-args>]:'
83     echo '  echo     [-n|--newline] [-e|--expand] [<string> ...]'
84     echo '  move     [-v|--verbose] [-t|--trace] [-e|--expand] [-p|--preserve]'
85     echo '           <src-file> <dst-file>'
86     echo '  install  [-v|--verbose] [-t|--trace] [-d|--mkdir] [-c|--copy]'
87     echo '           [-C|--compare-copy] [-s|--strip] [-m|--mode <mode>]'
88     echo '           [-o|--owner <owner>] [-g|--group <group>] [-e|--exec'
89     echo '           <sed-cmd>] <file> [<file> ...] <path>'
90     echo '  mkdir    [-t|--trace] [-f|--force] [-p|--parents] [-m|--mode'
91     echo '           <mode>] [-o|--owner <owner>] [-g|--group <group>] <dir>'
92     echo '           [<dir> ...]'
93     echo '  mkln     [-t|--trace] [-f|--force] [-s|--symbolic] <src-path>'
94     echo '           [<src-path> ...] <dst-path>'
95     echo '  subst    [-v|--verbose] [-t|--trace] [-n|--nop] [-w|--warning]'
96     echo '           [-q|--quiet] [-s|--stealth] [-i|--interactive] [-b|--backup'
97     echo '           <ext>] [-e|--exec <cmd>] [-f|--file <cmd-file>] [<file>]'
98     echo '           [...]'
99     echo ''
100     echo 'Not available <cmd-name> (because module was not built-in):'
101     echo '  mdate    [-n|--newline] [-z|--zero] [-s|--shorten] [-d|--digits]'
102     echo '           [-f|--field-sep <str>] [-o|--order <spec>] <path>'
103     echo '  table    [-F|--field-sep <sep>] [-w|--width <width>] [-c|--columns'
104     echo '           <cols>] [-s|--strip <strip>] <str><sep><str>...'
105     echo '  prop     [-p|--prefix <str>]'
106     echo '  mkshadow [-v|--verbose] [-t|--trace] [-a|--all] <src-dir> <dst-dir>'
107     echo '  fixperm  [-v|--verbose] [-t|--trace] <path> [<path> ...]'
108     echo '  rotate   [-v|--verbose] [-t|--trace] [-f|--force] [-n|--num-files'
109     echo '           <count>] [-s|--size <size>] [-c|--copy] [-r|--remove]'
110     echo '           [-a|--archive-dir <dir>] [-z|--compress [<tool>:]<level>]'
111     echo '           [-b|--background] [-d|--delay] [-p|--pad <len>] [-m|--mode'
112     echo '           <mode>] [-o|--owner <owner>] [-g|--group <group>] [-M|--migrate'
113     echo '           <cmd>] [-P|--prolog <cmd>] [-E|--epilog <cmd>] <file> [...]'
114     echo '  tarball  [-t|--trace] [-v|--verbose] [-o|--output <tarball>]'
115     echo '           [-c|--compress <prog>] [-d|--directory <dir>] [-u|--user'
116     echo '           <user>] [-g|--group <group>] [-e|--exclude <pattern>]'
117     echo '           <path> [<path> ...]'
118     echo '  platform [-F|--format <format>] [-S|--sep <string>] [-C|--conc'
119     echo '           <string>] [-L|--lower] [-U|--upper] [-v|--verbose]'
120     echo '           [-c|--concise] [-n|--no-newline] [-t|--type <type>]'
121     echo '           [-V|--version] [-h|--help]'
122     echo '  arx      [-t|--trace] [-C|--command <cmd>] <op> <archive> [<file>'
123     echo '           ...]'
124     echo '  slo      [-p|--prefix <str>] -- -L<dir> -l<lib> [-L<dir> -l<lib>'
125     echo '           ...]'
126     echo '  scpp     [-v|--verbose] [-p|--preserve] [-f|--filter <filter>]'
127     echo '           [-o|--output <ofile>] [-t|--template <tfile>] [-M|--mark'
128     echo '           <mark>] [-D|--define <dname>] [-C|--class <cname>]'
129     echo '           <file> [<file> ...]'
130     echo '  version  [-l|--language <lang>] [-n|--name <name>] [-p|--prefix'
131     echo '           <prefix>] [-s|--set <version>] [-e|--edit] [-i|--increase'
132     echo '           <knob>] [-d|--display <type>] <file>'
133     echo '  path     [-s|--suppress] [-r|--reverse] [-d|--dirname] [-b|--basename]'
134     echo '           [-m|--magic] [-p|--path <path>] <str> [<str> ...]'
135     echo ''
136     exit 0
137 fi
138 if [ ".$1" = ".-v" ] || [ ".$1" = ".--version" ]; then
139     echo "GNU shtool 2.0.2 (15-Jun-2005)"
140     exit 0
141 fi
142 if [ ".$1" = ".-r" ] || [ ".$1" = ".--recreate" ]; then
143     shtoolize -oshtool echo move install mkdir mkln subst
144     exit 0
145 fi
146 if [ ".$1" = ".-d" ] || [ ".$1" = ".--debug" ]; then
147     shift
148     set -x
149 fi
150 name=`echo "$0" | sed -e 's;.*/\([^/]*\)$;\1;' -e 's;-sh$;;' -e 's;\.sh$;;'`
151 case "$name" in
152     echo|move|install|mkdir|mkln|subst )
153         #   implicit tool command selection
154         tool="$name"
155         ;;
156     * )
157         #   explicit tool command selection
158         tool="$1"
159         shift
160         ;;
161 esac
162 arg_spec=""
163 opt_spec=""
164 gen_tmpfile=no
165
166 ##
167 ##  DISPATCH INTO SCRIPT PROLOG
168 ##
169
170 case $tool in
171     echo )
172         str_tool="echo"
173         str_usage="[-n|--newline] [-e|--expand] [<string> ...]"
174         arg_spec="0+"
175         opt_spec="n.e."
176         opt_alias="n:newline,e:expand"
177         opt_n=no
178         opt_e=no
179         ;;
180     move )
181         str_tool="move"
182         str_usage="[-v|--verbose] [-t|--trace] [-e|--expand] [-p|--preserve] <src-file> <dst-file>"
183         arg_spec="2="
184         opt_spec="v.t.e.p."
185         opt_alias="v:verbose,t:trace,e:expand,p:preserve"
186         opt_v=no
187         opt_t=no
188         opt_e=no
189         opt_p=no
190         ;;
191     install )
192         str_tool="install"
193         str_usage="[-v|--verbose] [-t|--trace] [-d|--mkdir] [-c|--copy] [-C|--compare-copy] [-s|--strip] [-m|--mode <mode>] [-o|--owner <owner>] [-g|--group <group>] [-e|--exec <sed-cmd>] <file> [<file> ...] <path>"
194         arg_spec="1+"
195         opt_spec="v.t.d.c.C.s.m:o:g:e+"
196         opt_alias="v:verbose,t:trace,d:mkdir,c:copy,C:compare-copy,s:strip,m:mode,o:owner,g:group,e:exec"
197         opt_v=no
198         opt_t=no
199         opt_d=no
200         opt_c=no
201         opt_C=no
202         opt_s=no
203         opt_m="0755"
204         opt_o=""
205         opt_g=""
206         opt_e=""
207         ;;
208     mkdir )
209         str_tool="mkdir"
210         str_usage="[-t|--trace] [-f|--force] [-p|--parents] [-m|--mode <mode>] [-o|--owner <owner>] [-g|--group <group>] <dir> [<dir> ...]"
211         arg_spec="1+"
212         opt_spec="t.f.p.m:o:g:"
213         opt_alias="t:trace,f:force,p:parents,m:mode,o:owner,g:group"
214         opt_t=no
215         opt_f=no
216         opt_p=no
217         opt_m=""
218         opt_o=""
219         opt_g=""
220         ;;
221     mkln )
222         str_tool="mkln"
223         str_usage="[-t|--trace] [-f|--force] [-s|--symbolic] <src-path> [<src-path> ...] <dst-path>"
224         arg_spec="2+"
225         opt_spec="t.f.s."
226         opt_alias="t:trace,f:force,s:symbolic"
227         opt_t=no
228         opt_f=no
229         opt_s=no
230         ;;
231     subst )
232         str_tool="subst"
233         str_usage="[-v|--verbose] [-t|--trace] [-n|--nop] [-w|--warning] [-q|--quiet] [-s|--stealth] [-i|--interactive] [-b|--backup <ext>] [-e|--exec <cmd>] [-f|--file <cmd-file>] [<file>] [...]"
234         gen_tmpfile=yes
235         arg_spec="0+"
236         opt_spec="v.t.n.w.q.s.i.b:e+f:"
237         opt_alias="v:verbose,t:trace,n:nop,w:warning,q:quiet,s:stealth,i:interactive,b:backup,e:exec,f:file"
238         opt_v=no
239         opt_t=no
240         opt_n=no
241         opt_w=no
242         opt_q=no
243         opt_s=no
244         opt_i=no
245         opt_b=""
246         opt_e=""
247         opt_f=""
248         ;;
249     -* )
250         echo "$0:Error: unknown option \`$tool'" 2>&1
251         echo "$0:Hint:  run \`$0 -h' for usage" 2>&1
252         exit 1
253         ;;
254     * )
255         echo "$0:Error: unknown command \`$tool'" 2>&1
256         echo "$0:Hint:  run \`$0 -h' for usage" 2>&1
257         exit 1
258         ;;
259 esac
260
261 ##
262 ##  COMMON UTILITY CODE
263 ##
264
265 #   commonly used ASCII values
266 ASC_TAB="       "
267 ASC_NL="
268 "
269
270 #   determine name of tool
271 if [ ".$tool" != . ]; then
272     #   used inside shtool script
273     toolcmd="$0 $tool"
274     toolcmdhelp="shtool $tool"
275     msgprefix="shtool:$tool"
276 else
277     #   used as standalone script
278     toolcmd="$0"
279     toolcmdhelp="sh $0"
280     msgprefix="$str_tool"
281 fi
282
283 #   parse argument specification string
284 eval `echo $arg_spec |\
285       sed -e 's/^\([0-9]*\)\([+=]\)/arg_NUMS=\1; arg_MODE=\2/'`
286
287 #   parse option specification string
288 eval `echo h.$opt_spec |\
289       sed -e 's/\([a-zA-Z0-9]\)\([.:+]\)/opt_MODE_\1=\2;/g'`
290
291 #   parse option alias string
292 eval `echo h:help,$opt_alias |\
293       sed -e 's/-/_/g' -e 's/\([a-zA-Z0-9]\):\([^,]*\),*/opt_ALIAS_\2=\1;/g'`
294
295 #   interate over argument line
296 opt_PREV=''
297 while [ $# -gt 0 ]; do
298     #   special option stops processing
299     if [ ".$1" = ".--" ]; then
300         shift
301         break
302     fi
303
304     #   determine option and argument
305     opt_ARG_OK=no
306     if [ ".$opt_PREV" != . ]; then
307         #   merge previous seen option with argument
308         opt_OPT="$opt_PREV"
309         opt_ARG="$1"
310         opt_ARG_OK=yes
311         opt_PREV=''
312     else
313         #   split argument into option and argument
314         case "$1" in
315             --[a-zA-Z0-9]*=*)
316                 eval `echo "x$1" |\
317                       sed -e 's/^x--\([a-zA-Z0-9-]*\)=\(.*\)$/opt_OPT="\1";opt_ARG="\2"/'`
318                 opt_STR=`echo $opt_OPT | sed -e 's/-/_/g'`
319                 eval "opt_OPT=\${opt_ALIAS_${opt_STR}-${opt_OPT}}"
320                 ;;
321             --[a-zA-Z0-9]*)
322                 opt_OPT=`echo "x$1" | cut -c4-`
323                 opt_STR=`echo $opt_OPT | sed -e 's/-/_/g'`
324                 eval "opt_OPT=\${opt_ALIAS_${opt_STR}-${opt_OPT}}"
325                 opt_ARG=''
326                 ;;
327             -[a-zA-Z0-9]*)
328                 eval `echo "x$1" |\
329                       sed -e 's/^x-\([a-zA-Z0-9]\)/opt_OPT="\1";/' \
330                           -e 's/";\(.*\)$/"; opt_ARG="\1"/'`
331                 ;;
332             -[a-zA-Z0-9])
333                 opt_OPT=`echo "x$1" | cut -c3-`
334                 opt_ARG=''
335                 ;;
336             *)
337                 break
338                 ;;
339         esac
340     fi
341
342     #   eat up option
343     shift
344
345     #   determine whether option needs an argument
346     eval "opt_MODE=\$opt_MODE_${opt_OPT}"
347     if [ ".$opt_ARG" = . ] && [ ".$opt_ARG_OK" != .yes ]; then
348         if [ ".$opt_MODE" = ".:" ] || [ ".$opt_MODE" = ".+" ]; then
349             opt_PREV="$opt_OPT"
350             continue
351         fi
352     fi
353
354     #   process option
355     case $opt_MODE in
356         '.' )
357             #   boolean option
358             eval "opt_${opt_OPT}=yes"
359             ;;
360         ':' )
361             #   option with argument (multiple occurances override)
362             eval "opt_${opt_OPT}=\"\$opt_ARG\""
363             ;;
364         '+' )
365             #   option with argument (multiple occurances append)
366             eval "opt_${opt_OPT}=\"\$opt_${opt_OPT}\${ASC_NL}\$opt_ARG\""
367             ;;
368         * )
369             echo "$msgprefix:Error: unknown option: \`$opt_OPT'" 1>&2
370             echo "$msgprefix:Hint:  run \`$toolcmdhelp -h' or \`man shtool' for details" 1>&2
371             exit 1
372             ;;
373     esac
374 done
375 if [ ".$opt_PREV" != . ]; then
376     echo "$msgprefix:Error: missing argument to option \`$opt_PREV'" 1>&2
377     echo "$msgprefix:Hint:  run \`$toolcmdhelp -h' or \`man shtool' for details" 1>&2
378     exit 1
379 fi
380
381 #   process help option
382 if [ ".$opt_h" = .yes ]; then
383     echo "Usage: $toolcmdhelp $str_usage"
384     exit 0
385 fi
386
387 #   complain about incorrect number of arguments
388 case $arg_MODE in
389     '=' )
390         if [ $# -ne $arg_NUMS ]; then
391             echo "$msgprefix:Error: invalid number of arguments (exactly $arg_NUMS expected)" 1>&2
392             echo "$msgprefix:Hint:  run \`$toolcmd -h' or \`man shtool' for details" 1>&2
393             exit 1
394         fi
395         ;;
396     '+' )
397         if [ $# -lt $arg_NUMS ]; then
398             echo "$msgprefix:Error: invalid number of arguments (at least $arg_NUMS expected)" 1>&2
399             echo "$msgprefix:Hint:  run \`$toolcmd -h' or \`man shtool' for details" 1>&2
400             exit 1
401         fi
402         ;;
403 esac
404
405 #   establish a temporary file on request
406 if [ ".$gen_tmpfile" = .yes ]; then
407     #   create (explicitly) secure temporary directory
408     if [ ".$TMPDIR" != . ]; then
409         tmpdir="$TMPDIR"
410     elif [ ".$TEMPDIR" != . ]; then
411         tmpdir="$TEMPDIR"
412     else
413         tmpdir="/tmp"
414     fi
415     tmpdir="$tmpdir/.shtool.$$"
416     ( umask 077
417       rm -rf "$tmpdir" >/dev/null 2>&1 || true
418       mkdir  "$tmpdir" >/dev/null 2>&1
419       if [ $? -ne 0 ]; then
420           echo "$msgprefix:Error: failed to create temporary directory \`$tmpdir'" 1>&2
421           exit 1
422       fi
423     )
424
425     #   create (implicitly) secure temporary file
426     tmpfile="$tmpdir/shtool.tmp"
427     touch "$tmpfile"
428 fi
429
430 #   utility function: map string to lower case
431 util_lower () {
432     echo "$1" | tr 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 'abcdefghijklmnopqrstuvwxyz'
433 }
434
435 #   utility function: map string to upper case
436 util_upper () {
437     echo "$1" | tr 'abcdefghijklmnopqrstuvwxyz' 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
438 }
439
440 #   cleanup procedure
441 shtool_exit () {
442     rc="$1"
443     if [ ".$gen_tmpfile" = .yes ]; then
444         rm -rf "$tmpdir" >/dev/null 2>&1 || true
445     fi
446     exit $rc
447 }
448
449 ##
450 ##  DISPATCH INTO SCRIPT BODY
451 ##
452
453 case $tool in
454
455 echo )
456     ##
457     ##  echo -- Print string with optional construct expansion
458     ##  Copyright (c) 1998-2005 Ralf S. Engelschall <rse@engelschall.com>
459     ##
460
461     text="$*"
462
463     #   check for broken escape sequence expansion
464     seo=''
465     bytes=`echo '\1' | wc -c | awk '{ printf("%s", $1); }'`
466     if [ ".$bytes" != .3 ]; then
467         bytes=`echo -E '\1' | wc -c | awk '{ printf("%s", $1); }'`
468         if [ ".$bytes" = .3 ]; then
469             seo='-E'
470         fi
471     fi
472
473     #   check for existing -n option (to suppress newline)
474     minusn=''
475     bytes=`echo -n 123 2>/dev/null | wc -c | awk '{ printf("%s", $1); }'`
476     if [ ".$bytes" = .3 ]; then
477         minusn='-n'
478     fi
479
480     #   determine terminal bold sequence
481     term_bold=''
482     term_norm=''
483     if [ ".$opt_e" = .yes ] && [ ".`echo $text | grep '%[Bb]'`" != . ]; then
484         case $TERM in
485             #   for the most important terminal types we directly know the sequences
486             xterm|xterm*|vt220|vt220*)
487                 term_bold=`awk 'BEGIN { printf("%c%c%c%c", 27, 91, 49, 109); }' </dev/null 2>/dev/null`
488                 term_norm=`awk 'BEGIN { printf("%c%c%c", 27, 91, 109); }' </dev/null 2>/dev/null`
489                 ;;
490             vt100|vt100*|cygwin)
491                 term_bold=`awk 'BEGIN { printf("%c%c%c%c%c%c", 27, 91, 49, 109, 0, 0); }' </dev/null 2>/dev/null`
492                 term_norm=`awk 'BEGIN { printf("%c%c%c%c%c", 27, 91, 109, 0, 0); }' </dev/null 2>/dev/null`
493                 ;;
494             #   for all others, we try to use a possibly existing `tput' or `tcout' utility
495             * )
496                 paths=`echo $PATH | sed -e 's/:/ /g'`
497                 for tool in tput tcout; do
498                     for dir in $paths; do
499                         if [ -r "$dir/$tool" ]; then
500                             for seq in bold md smso; do # 'smso' is last
501                                 bold="`$dir/$tool $seq 2>/dev/null`"
502                                 if [ ".$bold" != . ]; then
503                                     term_bold="$bold"
504                                     break
505                                 fi
506                             done
507                             if [ ".$term_bold" != . ]; then
508                                 for seq in sgr0 me rmso init reset; do # 'reset' is last
509                                     norm="`$dir/$tool $seq 2>/dev/null`"
510                                     if [ ".$norm" != . ]; then
511                                         term_norm="$norm"
512                                         break
513                                     fi
514                                 done
515                             fi
516                             break
517                         fi
518                     done
519                     if [ ".$term_bold" != . ] && [ ".$term_norm" != . ]; then
520                         break;
521                     fi
522                 done
523                 ;;
524         esac
525         if [ ".$term_bold" = . ] || [ ".$term_norm" = . ]; then
526             echo "$msgprefix:Warning: unable to determine terminal sequence for bold mode" 1>&2
527             term_bold=''
528             term_norm=''
529         fi
530     fi
531
532     #   determine user name
533     username=''
534     if [ ".$opt_e" = .yes ] && [ ".`echo $text | grep '%[uUgG]'`" != . ]; then
535         username="`(id -un) 2>/dev/null`"
536         if [ ".$username" = . ]; then
537             str="`(id) 2>/dev/null`"
538             if [ ".`echo $str | grep '^uid[     ]*=[    ]*[0-9]*('`" != . ]; then
539                 username=`echo $str | sed -e 's/^uid[   ]*=[    ]*[0-9]*(//' -e 's/).*$//'`
540             fi
541             if [ ".$username" = . ]; then
542                 username="$LOGNAME"
543                 if [ ".$username" = . ]; then
544                     username="$USER"
545                     if [ ".$username" = . ]; then
546                         username="`(whoami) 2>/dev/null |\
547                                    awk '{ printf("%s", $1); }'`"
548                         if [ ".$username" = . ]; then
549                             username="`(who am i) 2>/dev/null |\
550                                        awk '{ printf("%s", $1); }'`"
551                             if [ ".$username" = . ]; then
552                                 username='unknown'
553                             fi
554                         fi
555                     fi
556                 fi
557             fi
558         fi
559     fi
560
561     #   determine user id
562     userid=''
563     if [ ".$opt_e" = .yes ] && [ ".`echo $text | grep '%U'`" != . ]; then
564         userid="`(id -u) 2>/dev/null`"
565         if [ ".$userid" = . ]; then
566             userid="`(id -u ${username}) 2>/dev/null`"
567             if [ ".$userid" = . ]; then
568                 str="`(id) 2>/dev/null`"
569                 if [ ".`echo $str | grep '^uid[         ]*=[    ]*[0-9]*('`" != . ]; then
570                     userid=`echo $str | sed -e 's/^uid[         ]*=[    ]*//' -e 's/(.*$//'`
571                 fi
572                 if [ ".$userid" = . ]; then
573                     userid=`(getent passwd ${username}) 2>/dev/null | \
574                             sed -e 's/[^:]*:[^:]*://' -e 's/:.*$//'`
575                     if [ ".$userid" = . ]; then
576                         userid=`grep "^${username}:" /etc/passwd 2>/dev/null | \
577                                 sed -e 's/[^:]*:[^:]*://' -e 's/:.*$//'`
578                         if [ ".$userid" = . ]; then
579                             userid=`(ypcat passwd) 2>/dev/null |
580                                     grep "^${username}:" | \
581                                     sed -e 's/[^:]*:[^:]*://' -e 's/:.*$//'`
582                             if [ ".$userid" = . ]; then
583                                 userid='?'
584                             fi
585                         fi
586                     fi
587                 fi
588             fi
589         fi
590     fi
591
592     #   determine (primary) group id
593     groupid=''
594     if [ ".$opt_e" = .yes ] && [ ".`echo $text | grep '%[gG]'`" != . ]; then
595         groupid="`(id -g ${username}) 2>/dev/null`"
596         if [ ".$groupid" = . ]; then
597             str="`(id) 2>/dev/null`"
598             if [ ".`echo $str | grep 'gid[      ]*=[    ]*[0-9]*('`" != . ]; then
599                 groupid=`echo $str | sed -e 's/^.*gid[  ]*=[    ]*//' -e 's/(.*$//'`
600             fi
601             if [ ".$groupid" = . ]; then
602                 groupid=`(getent passwd ${username}) 2>/dev/null | \
603                          sed -e 's/[^:]*:[^:]*:[^:]*://' -e 's/:.*$//'`
604                 if [ ".$groupid" = . ]; then
605                     groupid=`grep "^${username}:" /etc/passwd 2>/dev/null | \
606                              sed -e 's/[^:]*:[^:]*:[^:]*://' -e 's/:.*$//'`
607                     if [ ".$groupid" = . ]; then
608                         groupid=`(ypcat passwd) 2>/dev/null | grep "^${username}:" | \
609                                  sed -e 's/[^:]*:[^:]*:[^:]*://' -e 's/:.*$//'`
610                         if [ ".$groupid" = . ]; then
611                             groupid='?'
612                         fi
613                     fi
614                 fi
615             fi
616         fi
617     fi
618
619     #   determine (primary) group name
620     groupname=''
621     if [ ".$opt_e" = .yes ] && [ ".`echo $text | grep '%g'`" != . ]; then
622         groupname="`(id -gn ${username}) 2>/dev/null`"
623         if [ ".$groupname" = . ]; then
624             str="`(id) 2>/dev/null`"
625             if [ ".`echo $str | grep 'gid[      ]*=[    ]*[0-9]*('`" != . ]; then
626                 groupname=`echo $str | sed -e 's/^.*gid[        ]*=[    ]*[0-9]*(//' -e 's/).*$//'`
627             fi
628             if [ ".$groupname" = . ]; then
629                 groupname=`(getent group) 2>/dev/null | \
630                            grep "^[^:]*:[^:]*:${groupid}:" | \
631                            sed -e 's/:.*$//'`
632                 if [ ".$groupname" = . ]; then
633                     groupname=`grep "^[^:]*:[^:]*:${groupid}:" /etc/group 2>/dev/null | \
634                                sed -e 's/:.*$//'`
635                     if [ ".$groupname" = . ]; then
636                         groupname=`(ypcat group) 2>/dev/null | \
637                                    grep "^[^:]*:[^:]*:${groupid}:" | \
638                                    sed -e 's/:.*$//'`
639                         if [ ".$groupname" = . ]; then
640                             groupname='?'
641                         fi
642                     fi
643                 fi
644             fi
645         fi
646     fi
647
648     #   determine host and domain name
649     hostname=''
650     domainname=''
651     if [ ".$opt_e" = .yes ] && [ ".`echo $text | grep '%h'`" != . ]; then
652         hostname="`(uname -n) 2>/dev/null |\
653                    awk '{ printf("%s", $1); }'`"
654         if [ ".$hostname" = . ]; then
655             hostname="`(hostname) 2>/dev/null |\
656                        awk '{ printf("%s", $1); }'`"
657             if [ ".$hostname" = . ]; then
658                 hostname='unknown'
659             fi
660         fi
661         case $hostname in
662             *.* )
663                 domainname=".`echo $hostname | cut -d. -f2-`"
664                 hostname="`echo $hostname | cut -d. -f1`"
665                 ;;
666         esac
667     fi
668     if [ ".$opt_e" = .yes ] && [ ".`echo $text | grep '%d'`" != . ]; then
669         if [ ".$domainname" = . ]; then
670             if [ -f /etc/resolv.conf ]; then
671                 domainname="`grep '^[   ]*domain' /etc/resolv.conf | sed -e 'q' |\
672                              sed -e 's/.*domain//' \
673                                  -e 's/^[       ]*//' -e 's/^ *//' -e 's/^      *//' \
674                                  -e 's/^\.//' -e 's/^/./' |\
675                              awk '{ printf("%s", $1); }'`"
676                 if [ ".$domainname" = . ]; then
677                     domainname="`grep '^[       ]*search' /etc/resolv.conf | sed -e 'q' |\
678                                  sed -e 's/.*search//' \
679                                      -e 's/^[   ]*//' -e 's/^ *//' -e 's/^      *//' \
680                                      -e 's/ .*//' -e 's/        .*//' \
681                                      -e 's/^\.//' -e 's/^/./' |\
682                                  awk '{ printf("%s", $1); }'`"
683                 fi
684             fi
685         fi
686     fi
687
688     #   determine current time
689     time_day=''
690     time_month=''
691     time_year=''
692     time_monthname=''
693     if [ ".$opt_e" = .yes ] && [ ".`echo $text | grep '%[DMYm]'`" != . ]; then
694         time_day=`date '+%d'`
695         time_month=`date '+%m'`
696         time_year=`date '+%Y' 2>/dev/null`
697         if [ ".$time_year" = . ]; then
698             time_year=`date '+%y'`
699             case $time_year in
700                 [5-9][0-9]) time_year="19$time_year" ;;
701                 [0-4][0-9]) time_year="20$time_year" ;;
702             esac
703         fi
704         case $time_month in
705             1|01) time_monthname='Jan' ;;
706             2|02) time_monthname='Feb' ;;
707             3|03) time_monthname='Mar' ;;
708             4|04) time_monthname='Apr' ;;
709             5|05) time_monthname='May' ;;
710             6|06) time_monthname='Jun' ;;
711             7|07) time_monthname='Jul' ;;
712             8|08) time_monthname='Aug' ;;
713             9|09) time_monthname='Sep' ;;
714               10) time_monthname='Oct' ;;
715               11) time_monthname='Nov' ;;
716               12) time_monthname='Dec' ;;
717         esac
718     fi
719
720     #   expand special ``%x'' constructs
721     if [ ".$opt_e" = .yes ]; then
722         text=`echo $seo "$text" |\
723               sed -e "s/%B/${term_bold}/g" \
724                   -e "s/%b/${term_norm}/g" \
725                   -e "s/%u/${username}/g" \
726                   -e "s/%U/${userid}/g" \
727                   -e "s/%g/${groupname}/g" \
728                   -e "s/%G/${groupid}/g" \
729                   -e "s/%h/${hostname}/g" \
730                   -e "s/%d/${domainname}/g" \
731                   -e "s/%D/${time_day}/g" \
732                   -e "s/%M/${time_month}/g" \
733                   -e "s/%Y/${time_year}/g" \
734                   -e "s/%m/${time_monthname}/g" 2>/dev/null`
735     fi
736
737     #   create output
738     if [ .$opt_n = .no ]; then
739         echo $seo "$text"
740     else
741         #   the harder part: echo -n is best, because
742         #   awk may complain about some \xx sequences.
743         if [ ".$minusn" != . ]; then
744             echo $seo $minusn "$text"
745         else
746             echo dummy | awk '{ printf("%s", TEXT); }' TEXT="$text"
747         fi
748     fi
749
750     shtool_exit 0
751     ;;
752
753 move )
754     ##
755     ##  move -- Move files with simultaneous substitution
756     ##  Copyright (c) 1999-2005 Ralf S. Engelschall <rse@engelschall.com>
757     ##
758
759     src="$1"
760     dst="$2"
761
762     #   consistency checks
763     if [ ".$src" = . ] || [ ".$dst" = . ]; then
764         echo "$msgprefix:Error: Invalid arguments" 1>&2
765         shtool_exit 1
766     fi
767     if [ ".$src" = ".$dst" ]; then
768         echo "$msgprefix:Error: Source and destination files are the same" 1>&2
769         shtool_exit 1
770     fi
771     expsrc="$src"
772     if [ ".$opt_e" = .yes ]; then
773         expsrc="`echo $expsrc`"
774     fi
775     if [ ".$opt_e" = .yes ]; then
776         if [ ".`echo "$src" | sed -e 's;^.*\\*.*$;;'`" = ".$src" ]; then
777             echo "$msgprefix:Error: Source doesn't contain wildcard ('*'): $dst" 1>&2
778             shtool_exit 1
779         fi
780         if [ ".`echo "$dst" | sed -e 's;^.*%[1-9].*$;;'`" = ".$dst" ]; then
781             echo "$msgprefix:Error: Destination doesn't contain substitution ('%N'): $dst" 1>&2
782             shtool_exit 1
783         fi
784         if [ ".$expsrc" = ".$src" ]; then
785             echo "$msgprefix:Error: Sources not found or no asterisk : $src" 1>&2
786             shtool_exit 1
787         fi
788     else
789         if [ ! -r "$src" ]; then
790             echo "$msgprefix:Error: Source not found: $src" 1>&2
791             shtool_exit 1
792         fi
793     fi
794
795     #   determine substitution patterns
796     if [ ".$opt_e" = .yes ]; then
797         srcpat=`echo "$src" | sed -e 's/\\./\\\\./g' -e 's/;/\\;/g' -e 's;\\*;\\\\(.*\\\\);g'`
798         dstpat=`echo "$dst" | sed -e 's;%\([1-9]\);\\\\\1;g'`
799     fi
800
801     #   iterate over source(s)
802     for onesrc in $expsrc; do
803         if [ .$opt_e = .yes ]; then
804             onedst=`echo $onesrc | sed -e "s;$srcpat;$dstpat;"`
805         else
806             onedst="$dst"
807         fi
808         errorstatus=0
809         if [ ".$opt_v" = .yes ]; then
810             echo "$onesrc -> $onedst"
811         fi
812         if [ ".$opt_p" = .yes ]; then
813             if [ -r $onedst ]; then
814                 if cmp -s $onesrc $onedst; then
815                     if [ ".$opt_t" = .yes ]; then
816                         echo "rm -f $onesrc" 1>&2
817                     fi
818                     rm -f $onesrc || errorstatus=$?
819                 else
820                     if [ ".$opt_t" = .yes ]; then
821                         echo "mv -f $onesrc $onedst" 1>&2
822                     fi
823                     mv -f $onesrc $onedst || errorstatus=$?
824                 fi
825             else
826                 if [ ".$opt_t" = .yes ]; then
827                     echo "mv -f $onesrc $onedst" 1>&2
828                 fi
829                 mv -f $onesrc $onedst || errorstatus=$?
830             fi
831         else
832             if [ ".$opt_t" = .yes ]; then
833                 echo "mv -f $onesrc $onedst" 1>&2
834             fi
835             mv -f $onesrc $onedst || errorstatus=$?
836         fi
837         if [ $errorstatus -ne 0 ]; then
838             break;
839         fi
840     done
841
842     shtool_exit $errorstatus
843     ;;
844
845 install )
846     ##
847     ##  install -- Install a program, script or datafile
848     ##  Copyright (c) 1997-2005 Ralf S. Engelschall <rse@engelschall.com>
849     ##
850
851     #   special case: "shtool install -d <dir> [...]" internally
852     #   maps to "shtool mkdir -f -p -m 755 <dir> [...]"
853     if [ "$opt_d" = yes ]; then
854         cmd="$0 mkdir -f -p -m 755"
855         if [ ".$opt_o" != . ]; then
856             cmd="$cmd -o '$opt_o'"
857         fi
858         if [ ".$opt_g" != . ]; then
859             cmd="$cmd -g '$opt_g'"
860         fi
861         if [ ".$opt_v" = .yes ]; then
862             cmd="$cmd -v"
863         fi
864         if [ ".$opt_t" = .yes ]; then
865             cmd="$cmd -t"
866         fi
867         for dir in "$@"; do
868             eval "$cmd $dir" || shtool_exit $?
869         done
870         shtool_exit 0
871     fi
872
873     #   determine source(s) and destination
874     argc=$#
875     srcs=""
876     while [ $# -gt 1 ]; do
877         srcs="$srcs $1"
878         shift
879     done
880     dstpath="$1"
881
882     #   type check for destination
883     dstisdir=0
884     if [ -d $dstpath ]; then
885         dstpath=`echo "$dstpath" | sed -e 's:/$::'`
886         dstisdir=1
887     fi
888
889     #   consistency check for destination
890     if [ $argc -gt 2 ] && [ $dstisdir = 0 ]; then
891         echo "$msgprefix:Error: multiple sources require destination to be directory" 1>&2
892         shtool_exit 1
893     fi
894
895     #   iterate over all source(s)
896     for src in $srcs; do
897         dst=$dstpath
898
899         #   if destination is a directory, append the input filename
900         if [ $dstisdir = 1 ]; then
901             dstfile=`echo "$src" | sed -e 's;.*/\([^/]*\)$;\1;'`
902             dst="$dst/$dstfile"
903         fi
904
905         #   check for correct arguments
906         if [ ".$src" = ".$dst" ]; then
907             echo "$msgprefix:Warning: source and destination are the same - skipped" 1>&2
908             continue
909         fi
910         if [ -d "$src" ]; then
911             echo "$msgprefix:Warning: source \`$src' is a directory - skipped" 1>&2
912             continue
913         fi
914
915         #   make a temp file name in the destination directory
916         dsttmp=`echo $dst |\
917                 sed -e 's;[^/]*$;;' -e 's;\(.\)/$;\1;' -e 's;^$;.;' \
918                     -e "s;\$;/#INST@$$#;"`
919
920         #   verbosity
921         if [ ".$opt_v" = .yes ]; then
922             echo "$src -> $dst" 1>&2
923         fi
924
925         #   copy or move the file name to the temp name
926         #   (because we might be not allowed to change the source)
927         if [ ".$opt_C" = .yes ]; then
928             opt_c=yes
929         fi
930         if [ ".$opt_c" = .yes ]; then
931             if [ ".$opt_t" = .yes ]; then
932                 echo "cp $src $dsttmp" 1>&2
933             fi
934             cp $src $dsttmp || shtool_exit $?
935         else
936             if [ ".$opt_t" = .yes ]; then
937                 echo "mv $src $dsttmp" 1>&2
938             fi
939             mv $src $dsttmp || shtool_exit $?
940         fi
941
942         #   adjust the target file
943         if [ ".$opt_e" != . ]; then
944             sed='sed'
945             OIFS="$IFS"; IFS="$ASC_NL"; set -- $opt_e; IFS="$OIFS"
946             for e
947             do
948                 sed="$sed -e '$e'"
949             done
950             cp $dsttmp $dsttmp.old
951             chmod u+w $dsttmp
952             eval "$sed <$dsttmp.old >$dsttmp" || shtool_exit $?
953             rm -f $dsttmp.old
954         fi
955         if [ ".$opt_s" = .yes ]; then
956             if [ ".$opt_t" = .yes ]; then
957                 echo "strip $dsttmp" 1>&2
958             fi
959             strip $dsttmp || shtool_exit $?
960         fi
961         if [ ".$opt_o" != . ]; then
962             if [ ".$opt_t" = .yes ]; then
963                 echo "chown $opt_o $dsttmp" 1>&2
964             fi
965             chown $opt_o $dsttmp || shtool_exit $?
966         fi
967         if [ ".$opt_g" != . ]; then
968             if [ ".$opt_t" = .yes ]; then
969                 echo "chgrp $opt_g $dsttmp" 1>&2
970             fi
971             chgrp $opt_g $dsttmp || shtool_exit $?
972         fi
973         if [ ".$opt_m" != ".-" ]; then
974             if [ ".$opt_t" = .yes ]; then
975                 echo "chmod $opt_m $dsttmp" 1>&2
976             fi
977             chmod $opt_m $dsttmp || shtool_exit $?
978         fi
979
980         #   determine whether to do a quick install
981         #   (has to be done _after_ the strip was already done)
982         quick=no
983         if [ ".$opt_C" = .yes ]; then
984             if [ -r $dst ]; then
985                 if cmp -s $src $dst; then
986                     quick=yes
987                 fi
988             fi
989         fi
990
991         #   finally, install the file to the real destination
992         if [ $quick = yes ]; then
993             if [ ".$opt_t" = .yes ]; then
994                 echo "rm -f $dsttmp" 1>&2
995             fi
996             rm -f $dsttmp
997         else
998             if [ ".$opt_t" = .yes ]; then
999                 echo "rm -f $dst && mv $dsttmp $dst" 1>&2
1000             fi
1001             rm -f $dst && mv $dsttmp $dst
1002         fi
1003     done
1004
1005     shtool_exit 0
1006     ;;
1007
1008 mkdir )
1009     ##
1010     ##  mkdir -- Make one or more directories
1011     ##  Copyright (c) 1996-2005 Ralf S. Engelschall <rse@engelschall.com>
1012     ##
1013
1014     errstatus=0
1015     for p in ${1+"$@"}; do
1016         #   if the directory already exists...
1017         if [ -d "$p" ]; then
1018             if [ ".$opt_f" = .no ] && [ ".$opt_p" = .no ]; then
1019                 echo "$msgprefix:Error: directory already exists: $p" 1>&2
1020                 errstatus=1
1021                 break
1022             else
1023                 continue
1024             fi
1025         fi
1026         #   if the directory has to be created...
1027         if [ ".$opt_p" = .no ]; then
1028             if [ ".$opt_t" = .yes ]; then
1029                 echo "mkdir $p" 1>&2
1030             fi
1031             mkdir $p || errstatus=$?
1032             if [ ".$opt_o" != . ]; then
1033                 if [ ".$opt_t" = .yes ]; then
1034                     echo "chown $opt_o $p" 1>&2
1035                 fi
1036                 chown $opt_o $p || errstatus=$?
1037             fi
1038             if [ ".$opt_g" != . ]; then
1039                 if [ ".$opt_t" = .yes ]; then
1040                     echo "chgrp $opt_g $p" 1>&2
1041                 fi
1042                 chgrp $opt_g $p || errstatus=$?
1043             fi
1044             if [ ".$opt_m" != . ]; then
1045                 if [ ".$opt_t" = .yes ]; then
1046                     echo "chmod $opt_m $p" 1>&2
1047                 fi
1048                 chmod $opt_m $p || errstatus=$?
1049             fi
1050         else
1051             #   the smart situation
1052             set fnord `echo ":$p" |\
1053                        sed -e 's/^:\//%/' \
1054                            -e 's/^://' \
1055                            -e 's/\// /g' \
1056                            -e 's/^%/\//'`
1057             shift
1058             pathcomp=''
1059             for d in ${1+"$@"}; do
1060                 pathcomp="$pathcomp$d"
1061                 case "$pathcomp" in
1062                     -* ) pathcomp="./$pathcomp" ;;
1063                 esac
1064                 if [ ! -d "$pathcomp" ]; then
1065                     if [ ".$opt_t" = .yes ]; then
1066                         echo "mkdir $pathcomp" 1>&2
1067                     fi
1068                     mkdir $pathcomp || errstatus=$?
1069                     if [ ".$opt_o" != . ]; then
1070                         if [ ".$opt_t" = .yes ]; then
1071                             echo "chown $opt_o $pathcomp" 1>&2
1072                         fi
1073                         chown $opt_o $pathcomp || errstatus=$?
1074                     fi
1075                     if [ ".$opt_g" != . ]; then
1076                         if [ ".$opt_t" = .yes ]; then
1077                             echo "chgrp $opt_g $pathcomp" 1>&2
1078                         fi
1079                         chgrp $opt_g $pathcomp || errstatus=$?
1080                     fi
1081                     if [ ".$opt_m" != . ]; then
1082                         if [ ".$opt_t" = .yes ]; then
1083                             echo "chmod $opt_m $pathcomp" 1>&2
1084                         fi
1085                         chmod $opt_m $pathcomp || errstatus=$?
1086                     fi
1087                 fi
1088                 pathcomp="$pathcomp/"
1089             done
1090         fi
1091     done
1092
1093     shtool_exit $errstatus
1094     ;;
1095
1096 mkln )
1097     ##
1098     ##  mkln -- Make link with calculation of relative paths
1099     ##  Copyright (c) 1998-2005 Ralf S. Engelschall <rse@engelschall.com>
1100     ##
1101
1102     #   determine source(s) and destination
1103     args=$?
1104     srcs=""
1105     while [ $# -gt 1 ]; do
1106         srcs="$srcs $1"
1107         shift
1108     done
1109     dst="$1"
1110     if [ ! -d $dst ]; then
1111         if [ $args -gt 2 ]; then
1112             echo "$msgprefix:Error: multiple sources not allowed when target isn't a directory" 1>&2
1113             shtool_exit 1
1114         fi
1115     fi
1116
1117     #   determine link options
1118     lnopt=""
1119     if [ ".$opt_f" = .yes ]; then
1120         lnopt="$lnopt -f"
1121     fi
1122     if [ ".$opt_s" = .yes ]; then
1123         lnopt="$lnopt -s"
1124     fi
1125
1126     #   iterate over sources
1127     for src in $srcs; do
1128         #   determine if one of the paths is an absolute path,
1129         #   because then we _have_ to use an absolute symlink
1130         oneisabs=0
1131         srcisabs=0
1132         dstisabs=0
1133         case $src in
1134             /* ) oneisabs=1; srcisabs=1 ;;
1135         esac
1136         case $dst in
1137             /* ) oneisabs=1; dstisabs=1 ;;
1138         esac
1139
1140         #   split source and destination into dir and base name
1141         if [ -d $src ]; then
1142             srcdir=`echo $src | sed -e 's;/*$;;'`
1143             srcbase=""
1144         else
1145             srcdir=`echo  $src | sed -e 's;^[^/]*$;;' -e 's;^\(.*/\)[^/]*$;\1;' -e 's;\(.\)/$;\1;'`
1146             srcbase=`echo $src | sed -e 's;.*/\([^/]*\)$;\1;'`
1147         fi
1148         if [ -d $dst ]; then
1149             dstdir=`echo $dst | sed -e 's;/*$;;'`
1150             dstbase=""
1151         else
1152             dstdir=`echo  $dst | sed -e 's;^[^/]*$;;' -e 's;^\(.*/\)[^/]*$;\1;' -e 's;\(.\)/$;\1;'`
1153             dstbase=`echo $dst | sed -e 's;.*/\([^/]*\)$;\1;'`
1154         fi
1155
1156         #   consistency check
1157         if [ ".$dstdir" != . ]; then
1158             if [ ! -d $dstdir ]; then
1159                 echo "$msgprefix:Error: destination directory not found: $dstdir" 1>&2
1160                 shtool_exit 1
1161             fi
1162         fi
1163
1164         #   make sure the source is reachable from the destination
1165         if [ $dstisabs = 1 ]; then
1166             if [ $srcisabs = 0 ]; then
1167                 if [ ".$srcdir" = . ]; then
1168                     srcdir="`pwd | sed -e 's;/*$;;'`"
1169                     srcisabs=1
1170                     oneisabs=1
1171                 elif [ -d $srcdir ]; then
1172                     srcdir="`cd $srcdir; pwd | sed -e 's;/*$;;'`"
1173                     srcisabs=1
1174                     oneisabs=1
1175                 fi
1176             fi
1177         fi
1178
1179         #   split away a common prefix
1180         prefix=""
1181         if [ ".$srcdir" = ".$dstdir" ] && [ ".$srcdir" != . ]; then
1182             prefix="$srcdir/"
1183             srcdir=""
1184             dstdir=""
1185         else
1186             while [ ".$srcdir" != . ] && [ ".$dstdir" != . ]; do
1187                 presrc=`echo $srcdir | sed -e 's;^\([^/]*\)/.*;\1;'`
1188                 predst=`echo $dstdir | sed -e 's;^\([^/]*\)/.*;\1;'`
1189                 if [ ".$presrc" != ".$predst" ]; then
1190                     break
1191                 fi
1192                 prefix="$prefix$presrc/"
1193                 srcdir=`echo $srcdir | sed -e 's;^[^/]*/*;;'`
1194                 dstdir=`echo $dstdir | sed -e 's;^[^/]*/*;;'`
1195             done
1196         fi
1197
1198         #   destination prefix is just the common prefix
1199         dstpre="$prefix"
1200
1201         #   determine source prefix which is the reverse directory
1202         #   step-up corresponding to the destination directory
1203         srcpre=""
1204
1205         isroot=0
1206         if [ ".$prefix" = . ] || [ ".$prefix" = ./ ]; then
1207             isroot=1
1208         fi
1209         if [ $oneisabs = 0 ] || [ $isroot = 0 ]; then
1210             pl="$dstdir/"
1211             OIFS="$IFS"; IFS='/'
1212             for pe in $pl; do
1213                 [ ".$pe" = .  ] && continue
1214                 [ ".$pe" = .. ] && continue
1215                 srcpre="../$srcpre"
1216             done
1217             IFS="$OIFS"
1218         else
1219             if [ $srcisabs = 1 ]; then
1220                 srcpre="$prefix"
1221             fi
1222         fi
1223
1224         #   determine destination symlink name
1225         if [ ".$dstbase" = . ]; then
1226             if [ ".$srcbase" != . ]; then
1227                 dstbase="$srcbase"
1228             else
1229                 dstbase=`echo "$prefix$srcdir" | sed -e 's;/*$;;' -e 's;.*/\([^/]*\)$;\1;'`
1230             fi
1231         fi
1232
1233         #   now finalize source and destination directory paths
1234         srcdir=`echo $srcdir | sed -e 's;\([^/]\)$;\1/;'`
1235         dstdir=`echo $dstdir | sed -e 's;\([^/]\)$;\1/;'`
1236
1237         #   run the final link command
1238         if [ ".$opt_t" = .yes ]; then
1239             echo "ln$lnopt $srcpre$srcdir$srcbase $dstpre$dstdir$dstbase"
1240         fi
1241         eval ln$lnopt $srcpre$srcdir$srcbase $dstpre$dstdir$dstbase
1242     done
1243
1244     shtool_exit 0
1245     ;;
1246
1247 subst )
1248     ##
1249     ##  subst -- Apply sed(1) substitution operations
1250     ##  Copyright (c) 2001-2005 Ralf S. Engelschall <rse@engelschall.com>
1251     ##
1252
1253     #   remember optional list of file(s)
1254     files="$*"
1255     files_num="$#"
1256
1257     #   parameter consistency check
1258     if [ $# -eq 0 ] && [ ".$opt_b" != . ]; then
1259         echo "$msgprefix:Error: option -b cannot be applied to stdin" 1>&2
1260         shtool_exit 1
1261     fi
1262     if [ $# -eq 0 ] && [ ".$opt_s" = .yes ]; then
1263         echo "$msgprefix:Error: option -s cannot be applied to stdin" 1>&2
1264         shtool_exit 1
1265     fi
1266
1267     #   build underlying sed(1) command
1268     sedcmd='sed'
1269     if [ ".$opt_e" != . ]; then
1270         OIFS="$IFS"; IFS="$ASC_NL"; set -- $opt_e; IFS="$OIFS"
1271         for e
1272         do
1273             sedcmd="$sedcmd -e '$e'"
1274         done
1275     elif [ ".$opt_f" != . ]; then
1276         if [ ! -f $opt_f ]; then
1277             echo "$msgprefix:Error: command file \`$opt_f' not found or not a regular file" 1>&2
1278             shtool_exit 1
1279         fi
1280         sedcmd="$sedcmd -f '$opt_f'"
1281     else
1282         echo "$msgprefix:Error: either -e option(s) or -f option required" 1>&2
1283         shtool_exit 1
1284     fi
1285
1286     #   determine extension for original file
1287     orig=".orig"
1288     if [ ".$opt_b" != . ]; then
1289         orig="$opt_b"
1290     fi
1291
1292     #   apply sed(1) operation(s)
1293     if [ ".$files" != . ]; then
1294         #   apply operation(s) to files
1295         substdone=no
1296         for file in $files; do
1297             test ".$file" = . && continue
1298             if [ ! -f $file ]; then
1299                 echo "$msgprefix:Warning: file \`$file' not found or not a regular file" 1>&2
1300                 continue
1301             fi
1302
1303             #   handle interactive mode
1304             if [ ".$opt_i" = .yes ]; then
1305                 eval "$sedcmd <$file >$file.new"
1306                 skip=no
1307                 if cmp $file $file.new >/dev/null 2>&1; then
1308                     rm -f $file.new
1309                     skip=yes
1310                 else
1311                     (diff -U1 $file $file.new >$tmpfile) 2>/dev/null
1312                     if [ ".`cat $tmpfile`" = . ]; then
1313                         (diff -C1 $file $file.new >$tmpfile) 2>/dev/null
1314                         if [ ".`cat $tmpfile`" = . ]; then
1315                             echo "$msgprefix:Warning: unable to show difference for file \`$file'" 1>&2
1316                             cp /dev/null $tmpfile
1317                         fi
1318                     fi
1319                     rm -f $file.new
1320                     cat $tmpfile
1321                     echo dummy | awk '{ printf("%s", TEXT); }' TEXT=">>> Apply [Y/n]: "
1322                     read input
1323                     if [ ".$input" != .Y ] &&\
1324                        [ ".$input" != .y ] &&\
1325                        [ ".$input" != . ]; then
1326                        skip=yes
1327                     fi
1328                 fi
1329                 if [ ".$skip" = .yes ]; then
1330                     if [ ".$opt_v" = .yes ]; then
1331                         echo "file \`$file' -- skipped" 1>&2
1332                     fi
1333                     continue
1334                 fi
1335             fi
1336
1337             #   apply sed(1) operation(s)
1338             if [ ".$opt_v" = .yes ]; then
1339                 echo "patching \`$file'" 1>&2
1340             fi
1341             if [ ".$opt_t" = .yes ]; then
1342                 echo "\$ cp -p $file $file$orig"
1343                 echo "\$ chmod u+w $file"
1344                 echo "\$ $sedcmd <$file$orig >$file"
1345             fi
1346             if [ ".$opt_n" = .no ]; then
1347                 cp -p $file $file$orig
1348                 chmod u+w $file >/dev/null 2>&1 || true
1349                 eval "$sedcmd <$file$orig >$file"
1350             fi
1351
1352             #   optionally fix timestamp
1353             if [ ".$opt_s" = .yes ]; then
1354                 if [ ".$opt_t" = .yes ]; then
1355                     echo "\$ touch -r $file$orig $file"
1356                 fi
1357                 if [ ".$opt_n" = .no ]; then
1358                     touch -r $file$orig $file
1359                 fi
1360             fi
1361
1362             #   optionally check whether any content change actually occurred 
1363             if [ ".$opt_q" = .no ]; then
1364                 if cmp $file$orig $file >/dev/null 2>&1; then
1365                     if [ ".$opt_w" = .yes ]; then
1366                         echo "$msgprefix:Warning: substitution resulted in no content change on file \"$file\"" 1>&2
1367                     fi
1368                 else
1369                     substdone=yes
1370                 fi
1371             fi
1372
1373             #   optionally remove preserved original file
1374             if [ ".$opt_b" = . ]; then
1375                 if [ ".$opt_t" = .yes ]; then
1376                     echo "\$ rm -f $file$orig"
1377                 fi
1378                 if [ ".$opt_n" = .no ]; then
1379                     rm -f $file$orig
1380                 fi
1381             fi
1382         done
1383         if [ ".$opt_q" = .no ] && [ ".$opt_w" = .no ]; then
1384             if [ ".$substdone" = .no ]; then
1385                 if [ ".$files_num" = .1 ]; then
1386                     echo "$msgprefix:Warning: substitution resulted in no content change on file \"$file\"" 1>&2
1387                 else
1388                     echo "$msgprefix:Warning: substitution resulted in no content change on any file" 1>&2
1389                 fi
1390             fi
1391         fi
1392     else
1393         #   apply operation(s) to stdin/stdout
1394         if [ ".$opt_v" = .yes ]; then
1395             echo "patching <stdin>" 1>&2
1396         fi
1397         if [ ".$opt_t" = .yes ]; then
1398             echo "\$ $sedcmd"
1399         fi
1400         if [ ".$opt_n" = .no ]; then
1401             eval "$sedcmd"
1402         fi
1403     fi
1404
1405     shtool_exit 0
1406     ;;
1407
1408 esac
1409
1410 shtool_exit 0
1411