]> git.sur5r.net Git - openldap/blob - build/shtool
b17658831e77e01f126086d4033fb8cbd7c8ba63
[openldap] / build / shtool
1 #!/bin/sh
2 ##
3 ##  GNU shtool -- The GNU Portable Shell Tool
4 ##  Copyright (c) 1994-2004 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.1 (11-Aug-2004)
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.1 (11-Aug-2004)"
71     echo "Copyright (c) 1994-2004 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.1 (11-Aug-2004)"
140     exit 0
141 fi
142 if [ ".$1" = ".-r" ] || [ ".$1" = ".--recreate" ]; then
143     shtoolize -obuild/shtool 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     if [ ".$TMPDIR" != . ]; then
408         tmpdir="$TMPDIR"
409     elif [ ".$TEMPDIR" != . ]; then
410         tmpdir="$TEMPDIR"
411     else
412         tmpdir="/tmp"
413     fi
414     tmpfile="$tmpdir/.shtool.$$"
415     rm -f $tmpfile >/dev/null 2>&1
416     touch $tmpfile
417     chmod 600 $tmpfile
418 fi
419
420 #   utility function: map string to lower case
421 util_lower () {
422     echo "$1" | tr 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 'abcdefghijklmnopqrstuvwxyz'
423 }
424
425 #   utility function: map string to upper case
426 util_upper () {
427     echo "$1" | tr 'abcdefghijklmnopqrstuvwxyz' 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
428 }
429
430 #   cleanup procedure
431 shtool_exit () {
432     rc="$1"
433     if [ ".$gen_tmpfile" = .yes ]; then
434         rm -f $tmpfile >/dev/null 2>&1 || true
435     fi
436     exit $rc
437 }
438
439 ##
440 ##  DISPATCH INTO SCRIPT BODY
441 ##
442
443 case $tool in
444
445 echo )
446     ##
447     ##  echo -- Print string with optional construct expansion
448     ##  Copyright (c) 1998-2004 Ralf S. Engelschall <rse@engelschall.com>
449     ##
450
451     text="$*"
452
453     #   check for broken escape sequence expansion
454     seo=''
455     bytes=`echo '\1' | wc -c | awk '{ printf("%s", $1); }'`
456     if [ ".$bytes" != .3 ]; then
457         bytes=`echo -E '\1' | wc -c | awk '{ printf("%s", $1); }'`
458         if [ ".$bytes" = .3 ]; then
459             seo='-E'
460         fi
461     fi
462
463     #   check for existing -n option (to suppress newline)
464     minusn=''
465     bytes=`echo -n 123 2>/dev/null | wc -c | awk '{ printf("%s", $1); }'`
466     if [ ".$bytes" = .3 ]; then
467         minusn='-n'
468     fi
469
470     #   determine terminal bold sequence
471     term_bold=''
472     term_norm=''
473     if [ ".$opt_e" = .yes ] && [ ".`echo $text | grep '%[Bb]'`" != . ]; then
474         case $TERM in
475             #   for the most important terminal types we directly know the sequences
476             xterm|xterm*|vt220|vt220*)
477                 term_bold=`awk 'BEGIN { printf("%c%c%c%c", 27, 91, 49, 109); }' </dev/null 2>/dev/null`
478                 term_norm=`awk 'BEGIN { printf("%c%c%c", 27, 91, 109); }' </dev/null 2>/dev/null`
479                 ;;
480             vt100|vt100*|cygwin)
481                 term_bold=`awk 'BEGIN { printf("%c%c%c%c%c%c", 27, 91, 49, 109, 0, 0); }' </dev/null 2>/dev/null`
482                 term_norm=`awk 'BEGIN { printf("%c%c%c%c%c", 27, 91, 109, 0, 0); }' </dev/null 2>/dev/null`
483                 ;;
484             #   for all others, we try to use a possibly existing `tput' or `tcout' utility
485             * )
486                 paths=`echo $PATH | sed -e 's/:/ /g'`
487                 for tool in tput tcout; do
488                     for dir in $paths; do
489                         if [ -r "$dir/$tool" ]; then
490                             for seq in bold md smso; do # 'smso' is last
491                                 bold="`$dir/$tool $seq 2>/dev/null`"
492                                 if [ ".$bold" != . ]; then
493                                     term_bold="$bold"
494                                     break
495                                 fi
496                             done
497                             if [ ".$term_bold" != . ]; then
498                                 for seq in sgr0 me rmso init reset; do # 'reset' is last
499                                     norm="`$dir/$tool $seq 2>/dev/null`"
500                                     if [ ".$norm" != . ]; then
501                                         term_norm="$norm"
502                                         break
503                                     fi
504                                 done
505                             fi
506                             break
507                         fi
508                     done
509                     if [ ".$term_bold" != . ] && [ ".$term_norm" != . ]; then
510                         break;
511                     fi
512                 done
513                 ;;
514         esac
515         if [ ".$term_bold" = . ] || [ ".$term_norm" = . ]; then
516             echo "$msgprefix:Warning: unable to determine terminal sequence for bold mode" 1>&2
517             term_bold=''
518             term_norm=''
519         fi
520     fi
521
522     #   determine user name
523     username=''
524     if [ ".$opt_e" = .yes ] && [ ".`echo $text | grep '%[uUgG]'`" != . ]; then
525         username="`(id -un) 2>/dev/null`"
526         if [ ".$username" = . ]; then
527             str="`(id) 2>/dev/null`"
528             if [ ".`echo $str | grep '^uid[     ]*=[    ]*[0-9]*('`" != . ]; then
529                 username=`echo $str | sed -e 's/^uid[   ]*=[    ]*[0-9]*(//' -e 's/).*$//'`
530             fi
531             if [ ".$username" = . ]; then
532                 username="$LOGNAME"
533                 if [ ".$username" = . ]; then
534                     username="$USER"
535                     if [ ".$username" = . ]; then
536                         username="`(whoami) 2>/dev/null |\
537                                    awk '{ printf("%s", $1); }'`"
538                         if [ ".$username" = . ]; then
539                             username="`(who am i) 2>/dev/null |\
540                                        awk '{ printf("%s", $1); }'`"
541                             if [ ".$username" = . ]; then
542                                 username='unknown'
543                             fi
544                         fi
545                     fi
546                 fi
547             fi
548         fi
549     fi
550
551     #   determine user id
552     userid=''
553     if [ ".$opt_e" = .yes ] && [ ".`echo $text | grep '%U'`" != . ]; then
554         userid="`(id -u) 2>/dev/null`"
555         if [ ".$userid" = . ]; then
556             userid="`(id -u ${username}) 2>/dev/null`"
557             if [ ".$userid" = . ]; then
558                 str="`(id) 2>/dev/null`"
559                 if [ ".`echo $str | grep '^uid[         ]*=[    ]*[0-9]*('`" != . ]; then
560                     userid=`echo $str | sed -e 's/^uid[         ]*=[    ]*//' -e 's/(.*$//'`
561                 fi
562                 if [ ".$userid" = . ]; then
563                     userid=`(getent passwd ${username}) 2>/dev/null | \
564                             sed -e 's/[^:]*:[^:]*://' -e 's/:.*$//'`
565                     if [ ".$userid" = . ]; then
566                         userid=`grep "^${username}:" /etc/passwd 2>/dev/null | \
567                                 sed -e 's/[^:]*:[^:]*://' -e 's/:.*$//'`
568                         if [ ".$userid" = . ]; then
569                             userid=`(ypcat passwd) 2>/dev/null |
570                                     grep "^${username}:" | \
571                                     sed -e 's/[^:]*:[^:]*://' -e 's/:.*$//'`
572                             if [ ".$userid" = . ]; then
573                                 userid='?'
574                             fi
575                         fi
576                     fi
577                 fi
578             fi
579         fi
580     fi
581
582     #   determine (primary) group id
583     groupid=''
584     if [ ".$opt_e" = .yes ] && [ ".`echo $text | grep '%[gG]'`" != . ]; then
585         groupid="`(id -g ${username}) 2>/dev/null`"
586         if [ ".$groupid" = . ]; then
587             str="`(id) 2>/dev/null`"
588             if [ ".`echo $str | grep 'gid[      ]*=[    ]*[0-9]*('`" != . ]; then
589                 groupid=`echo $str | sed -e 's/^.*gid[  ]*=[    ]*//' -e 's/(.*$//'`
590             fi
591             if [ ".$groupid" = . ]; then
592                 groupid=`(getent passwd ${username}) 2>/dev/null | \
593                          sed -e 's/[^:]*:[^:]*:[^:]*://' -e 's/:.*$//'`
594                 if [ ".$groupid" = . ]; then
595                     groupid=`grep "^${username}:" /etc/passwd 2>/dev/null | \
596                              sed -e 's/[^:]*:[^:]*:[^:]*://' -e 's/:.*$//'`
597                     if [ ".$groupid" = . ]; then
598                         groupid=`(ypcat passwd) 2>/dev/null | grep "^${username}:" | \
599                                  sed -e 's/[^:]*:[^:]*:[^:]*://' -e 's/:.*$//'`
600                         if [ ".$groupid" = . ]; then
601                             groupid='?'
602                         fi
603                     fi
604                 fi
605             fi
606         fi
607     fi
608
609     #   determine (primary) group name
610     groupname=''
611     if [ ".$opt_e" = .yes ] && [ ".`echo $text | grep '%g'`" != . ]; then
612         groupname="`(id -gn ${username}) 2>/dev/null`"
613         if [ ".$groupname" = . ]; then
614             str="`(id) 2>/dev/null`"
615             if [ ".`echo $str | grep 'gid[      ]*=[    ]*[0-9]*('`" != . ]; then
616                 groupname=`echo $str | sed -e 's/^.*gid[        ]*=[    ]*[0-9]*(//' -e 's/).*$//'`
617             fi
618             if [ ".$groupname" = . ]; then
619                 groupname=`(getent group) 2>/dev/null | \
620                            grep "^[^:]*:[^:]*:${groupid}:" | \
621                            sed -e 's/:.*$//'`
622                 if [ ".$groupname" = . ]; then
623                     groupname=`grep "^[^:]*:[^:]*:${groupid}:" /etc/group 2>/dev/null | \
624                                sed -e 's/:.*$//'`
625                     if [ ".$groupname" = . ]; then
626                         groupname=`(ypcat group) 2>/dev/null | \
627                                    grep "^[^:]*:[^:]*:${groupid}:" | \
628                                    sed -e 's/:.*$//'`
629                         if [ ".$groupname" = . ]; then
630                             groupname='?'
631                         fi
632                     fi
633                 fi
634             fi
635         fi
636     fi
637
638     #   determine host and domain name
639     hostname=''
640     domainname=''
641     if [ ".$opt_e" = .yes ] && [ ".`echo $text | grep '%h'`" != . ]; then
642         hostname="`(uname -n) 2>/dev/null |\
643                    awk '{ printf("%s", $1); }'`"
644         if [ ".$hostname" = . ]; then
645             hostname="`(hostname) 2>/dev/null |\
646                        awk '{ printf("%s", $1); }'`"
647             if [ ".$hostname" = . ]; then
648                 hostname='unknown'
649             fi
650         fi
651         case $hostname in
652             *.* )
653                 domainname=".`echo $hostname | cut -d. -f2-`"
654                 hostname="`echo $hostname | cut -d. -f1`"
655                 ;;
656         esac
657     fi
658     if [ ".$opt_e" = .yes ] && [ ".`echo $text | grep '%d'`" != . ]; then
659         if [ ".$domainname" = . ]; then
660             if [ -f /etc/resolv.conf ]; then
661                 domainname="`grep '^[   ]*domain' /etc/resolv.conf | sed -e 'q' |\
662                              sed -e 's/.*domain//' \
663                                  -e 's/^[       ]*//' -e 's/^ *//' -e 's/^      *//' \
664                                  -e 's/^\.//' -e 's/^/./' |\
665                              awk '{ printf("%s", $1); }'`"
666                 if [ ".$domainname" = . ]; then
667                     domainname="`grep '^[       ]*search' /etc/resolv.conf | sed -e 'q' |\
668                                  sed -e 's/.*search//' \
669                                      -e 's/^[   ]*//' -e 's/^ *//' -e 's/^      *//' \
670                                      -e 's/ .*//' -e 's/        .*//' \
671                                      -e 's/^\.//' -e 's/^/./' |\
672                                  awk '{ printf("%s", $1); }'`"
673                 fi
674             fi
675         fi
676     fi
677
678     #   determine current time
679     time_day=''
680     time_month=''
681     time_year=''
682     time_monthname=''
683     if [ ".$opt_e" = .yes ] && [ ".`echo $text | grep '%[DMYm]'`" != . ]; then
684         time_day=`date '+%d'`
685         time_month=`date '+%m'`
686         time_year=`date '+%Y' 2>/dev/null`
687         if [ ".$time_year" = . ]; then
688             time_year=`date '+%y'`
689             case $time_year in
690                 [5-9][0-9]) time_year="19$time_year" ;;
691                 [0-4][0-9]) time_year="20$time_year" ;;
692             esac
693         fi
694         case $time_month in
695             1|01) time_monthname='Jan' ;;
696             2|02) time_monthname='Feb' ;;
697             3|03) time_monthname='Mar' ;;
698             4|04) time_monthname='Apr' ;;
699             5|05) time_monthname='May' ;;
700             6|06) time_monthname='Jun' ;;
701             7|07) time_monthname='Jul' ;;
702             8|08) time_monthname='Aug' ;;
703             9|09) time_monthname='Sep' ;;
704               10) time_monthname='Oct' ;;
705               11) time_monthname='Nov' ;;
706               12) time_monthname='Dec' ;;
707         esac
708     fi
709
710     #   expand special ``%x'' constructs
711     if [ ".$opt_e" = .yes ]; then
712         text=`echo $seo "$text" |\
713               sed -e "s/%B/${term_bold}/g" \
714                   -e "s/%b/${term_norm}/g" \
715                   -e "s/%u/${username}/g" \
716                   -e "s/%U/${userid}/g" \
717                   -e "s/%g/${groupname}/g" \
718                   -e "s/%G/${groupid}/g" \
719                   -e "s/%h/${hostname}/g" \
720                   -e "s/%d/${domainname}/g" \
721                   -e "s/%D/${time_day}/g" \
722                   -e "s/%M/${time_month}/g" \
723                   -e "s/%Y/${time_year}/g" \
724                   -e "s/%m/${time_monthname}/g" 2>/dev/null`
725     fi
726
727     #   create output
728     if [ .$opt_n = .no ]; then
729         echo $seo "$text"
730     else
731         #   the harder part: echo -n is best, because
732         #   awk may complain about some \xx sequences.
733         if [ ".$minusn" != . ]; then
734             echo $seo $minusn "$text"
735         else
736             echo dummy | awk '{ printf("%s", TEXT); }' TEXT="$text"
737         fi
738     fi
739
740     shtool_exit 0
741     ;;
742
743 move )
744     ##
745     ##  move -- Move files with simultaneous substitution
746     ##  Copyright (c) 1999-2004 Ralf S. Engelschall <rse@engelschall.com>
747     ##
748
749     src="$1"
750     dst="$2"
751
752     #   consistency checks
753     if [ ".$src" = . ] || [ ".$dst" = . ]; then
754         echo "$msgprefix:Error: Invalid arguments" 1>&2
755         shtool_exit 1
756     fi
757     if [ ".$src" = ".$dst" ]; then
758         echo "$msgprefix:Error: Source and destination files are the same" 1>&2
759         shtool_exit 1
760     fi
761     expsrc="$src"
762     if [ ".$opt_e" = .yes ]; then
763         expsrc="`echo $expsrc`"
764     fi
765     if [ ".$opt_e" = .yes ]; then
766         if [ ".`echo "$src" | sed -e 's;^.*\\*.*$;;'`" = ".$src" ]; then
767             echo "$msgprefix:Error: Source doesn't contain wildcard ('*'): $dst" 1>&2
768             shtool_exit 1
769         fi
770         if [ ".`echo "$dst" | sed -e 's;^.*%[1-9].*$;;'`" = ".$dst" ]; then
771             echo "$msgprefix:Error: Destination doesn't contain substitution ('%N'): $dst" 1>&2
772             shtool_exit 1
773         fi
774         if [ ".$expsrc" = ".$src" ]; then
775             echo "$msgprefix:Error: Sources not found or no asterisk : $src" 1>&2
776             shtool_exit 1
777         fi
778     else
779         if [ ! -r "$src" ]; then
780             echo "$msgprefix:Error: Source not found: $src" 1>&2
781             shtool_exit 1
782         fi
783     fi
784
785     #   determine substitution patterns
786     if [ ".$opt_e" = .yes ]; then
787         srcpat=`echo "$src" | sed -e 's/\\./\\\\./g' -e 's/;/\\;/g' -e 's;\\*;\\\\(.*\\\\);g'`
788         dstpat=`echo "$dst" | sed -e 's;%\([1-9]\);\\\\\1;g'`
789     fi
790
791     #   iterate over source(s)
792     for onesrc in $expsrc; do
793         if [ .$opt_e = .yes ]; then
794             onedst=`echo $onesrc | sed -e "s;$srcpat;$dstpat;"`
795         else
796             onedst="$dst"
797         fi
798         errorstatus=0
799         if [ ".$opt_v" = .yes ]; then
800             echo "$onesrc -> $onedst"
801         fi
802         if [ ".$opt_p" = .yes ]; then
803             if [ -r $onedst ]; then
804                 if cmp -s $onesrc $onedst; then
805                     if [ ".$opt_t" = .yes ]; then
806                         echo "rm -f $onesrc" 1>&2
807                     fi
808                     rm -f $onesrc || errorstatus=$?
809                 else
810                     if [ ".$opt_t" = .yes ]; then
811                         echo "mv -f $onesrc $onedst" 1>&2
812                     fi
813                     mv -f $onesrc $onedst || errorstatus=$?
814                 fi
815             else
816                 if [ ".$opt_t" = .yes ]; then
817                     echo "mv -f $onesrc $onedst" 1>&2
818                 fi
819                 mv -f $onesrc $onedst || errorstatus=$?
820             fi
821         else
822             if [ ".$opt_t" = .yes ]; then
823                 echo "mv -f $onesrc $onedst" 1>&2
824             fi
825             mv -f $onesrc $onedst || errorstatus=$?
826         fi
827         if [ $errorstatus -ne 0 ]; then
828             break;
829         fi
830     done
831
832     shtool_exit $errorstatus
833     ;;
834
835 install )
836     ##
837     ##  install -- Install a program, script or datafile
838     ##  Copyright (c) 1997-2004 Ralf S. Engelschall <rse@engelschall.com>
839     ##
840
841     #   special case: "shtool install -d <dir> [...]" internally
842     #   maps to "shtool mkdir -f -p -m 755 <dir> [...]"
843     if [ "$opt_d" = yes ]; then
844         cmd="$0 mkdir -f -p -m 755"
845         if [ ".$opt_o" != . ]; then
846             cmd="$cmd -o '$opt_o'"
847         fi
848         if [ ".$opt_g" != . ]; then
849             cmd="$cmd -g '$opt_g'"
850         fi
851         if [ ".$opt_v" = .yes ]; then
852             cmd="$cmd -v"
853         fi
854         if [ ".$opt_t" = .yes ]; then
855             cmd="$cmd -t"
856         fi
857         for dir in "$@"; do
858             eval "$cmd $dir" || shtool_exit $?
859         done
860         shtool_exit 0
861     fi
862
863     #   determine source(s) and destination
864     argc=$#
865     srcs=""
866     while [ $# -gt 1 ]; do
867         srcs="$srcs $1"
868         shift
869     done
870     dstpath="$1"
871
872     #   type check for destination
873     dstisdir=0
874     if [ -d $dstpath ]; then
875         dstpath=`echo "$dstpath" | sed -e 's:/$::'`
876         dstisdir=1
877     fi
878
879     #   consistency check for destination
880     if [ $argc -gt 2 ] && [ $dstisdir = 0 ]; then
881         echo "$msgprefix:Error: multiple sources require destination to be directory" 1>&2
882         shtool_exit 1
883     fi
884
885     #   iterate over all source(s)
886     for src in $srcs; do
887         dst=$dstpath
888
889         #   if destination is a directory, append the input filename
890         if [ $dstisdir = 1 ]; then
891             dstfile=`echo "$src" | sed -e 's;.*/\([^/]*\)$;\1;'`
892             dst="$dst/$dstfile"
893         fi
894
895         #   check for correct arguments
896         if [ ".$src" = ".$dst" ]; then
897             echo "$msgprefix:Warning: source and destination are the same - skipped" 1>&2
898             continue
899         fi
900         if [ -d "$src" ]; then
901             echo "$msgprefix:Warning: source \`$src' is a directory - skipped" 1>&2
902             continue
903         fi
904
905         #   make a temp file name in the destination directory
906         dsttmp=`echo $dst |\
907                 sed -e 's;[^/]*$;;' -e 's;\(.\)/$;\1;' -e 's;^$;.;' \
908                     -e "s;\$;/#INST@$$#;"`
909
910         #   verbosity
911         if [ ".$opt_v" = .yes ]; then
912             echo "$src -> $dst" 1>&2
913         fi
914
915         #   copy or move the file name to the temp name
916         #   (because we might be not allowed to change the source)
917         if [ ".$opt_C" = .yes ]; then
918             opt_c=yes
919         fi
920         if [ ".$opt_c" = .yes ]; then
921             if [ ".$opt_t" = .yes ]; then
922                 echo "cp $src $dsttmp" 1>&2
923             fi
924             cp $src $dsttmp || shtool_exit $?
925         else
926             if [ ".$opt_t" = .yes ]; then
927                 echo "mv $src $dsttmp" 1>&2
928             fi
929             mv $src $dsttmp || shtool_exit $?
930         fi
931
932         #   adjust the target file
933         if [ ".$opt_e" != . ]; then
934             sed='sed'
935             OIFS="$IFS"; IFS="$ASC_NL"; set -- $opt_e; IFS="$OIFS"
936             for e
937             do
938                 sed="$sed -e '$e'"
939             done
940             cp $dsttmp $dsttmp.old
941             chmod u+w $dsttmp
942             eval "$sed <$dsttmp.old >$dsttmp" || shtool_exit $?
943             rm -f $dsttmp.old
944         fi
945         if [ ".$opt_s" = .yes ]; then
946             if [ ".$opt_t" = .yes ]; then
947                 echo "strip $dsttmp" 1>&2
948             fi
949             strip $dsttmp || shtool_exit $?
950         fi
951         if [ ".$opt_o" != . ]; then
952             if [ ".$opt_t" = .yes ]; then
953                 echo "chown $opt_o $dsttmp" 1>&2
954             fi
955             chown $opt_o $dsttmp || shtool_exit $?
956         fi
957         if [ ".$opt_g" != . ]; then
958             if [ ".$opt_t" = .yes ]; then
959                 echo "chgrp $opt_g $dsttmp" 1>&2
960             fi
961             chgrp $opt_g $dsttmp || shtool_exit $?
962         fi
963         if [ ".$opt_m" != ".-" ]; then
964             if [ ".$opt_t" = .yes ]; then
965                 echo "chmod $opt_m $dsttmp" 1>&2
966             fi
967             chmod $opt_m $dsttmp || shtool_exit $?
968         fi
969
970         #   determine whether to do a quick install
971         #   (has to be done _after_ the strip was already done)
972         quick=no
973         if [ ".$opt_C" = .yes ]; then
974             if [ -r $dst ]; then
975                 if cmp -s $src $dst; then
976                     quick=yes
977                 fi
978             fi
979         fi
980
981         #   finally, install the file to the real destination
982         if [ $quick = yes ]; then
983             if [ ".$opt_t" = .yes ]; then
984                 echo "rm -f $dsttmp" 1>&2
985             fi
986             rm -f $dsttmp
987         else
988             if [ ".$opt_t" = .yes ]; then
989                 echo "rm -f $dst && mv $dsttmp $dst" 1>&2
990             fi
991             rm -f $dst && mv $dsttmp $dst
992         fi
993     done
994
995     shtool_exit 0
996     ;;
997
998 mkdir )
999     ##
1000     ##  mkdir -- Make one or more directories
1001     ##  Copyright (c) 1996-2004 Ralf S. Engelschall <rse@engelschall.com>
1002     ##
1003
1004     errstatus=0
1005     for p in ${1+"$@"}; do
1006         #   if the directory already exists...
1007         if [ -d "$p" ]; then
1008             if [ ".$opt_f" = .no ] && [ ".$opt_p" = .no ]; then
1009                 echo "$msgprefix:Error: directory already exists: $p" 1>&2
1010                 errstatus=1
1011                 break
1012             else
1013                 continue
1014             fi
1015         fi
1016         #   if the directory has to be created...
1017         if [ ".$opt_p" = .no ]; then
1018             if [ ".$opt_t" = .yes ]; then
1019                 echo "mkdir $p" 1>&2
1020             fi
1021             mkdir $p || errstatus=$?
1022             if [ ".$opt_o" != . ]; then
1023                 if [ ".$opt_t" = .yes ]; then
1024                     echo "chown $opt_o $p" 1>&2
1025                 fi
1026                 chown $opt_o $p || errstatus=$?
1027             fi
1028             if [ ".$opt_g" != . ]; then
1029                 if [ ".$opt_t" = .yes ]; then
1030                     echo "chgrp $opt_g $p" 1>&2
1031                 fi
1032                 chgrp $opt_g $p || errstatus=$?
1033             fi
1034             if [ ".$opt_m" != . ]; then
1035                 if [ ".$opt_t" = .yes ]; then
1036                     echo "chmod $opt_m $p" 1>&2
1037                 fi
1038                 chmod $opt_m $p || errstatus=$?
1039             fi
1040         else
1041             #   the smart situation
1042             set fnord `echo ":$p" |\
1043                        sed -e 's/^:\//%/' \
1044                            -e 's/^://' \
1045                            -e 's/\// /g' \
1046                            -e 's/^%/\//'`
1047             shift
1048             pathcomp=''
1049             for d in ${1+"$@"}; do
1050                 pathcomp="$pathcomp$d"
1051                 case "$pathcomp" in
1052                     -* ) pathcomp="./$pathcomp" ;;
1053                 esac
1054                 if [ ! -d "$pathcomp" ]; then
1055                     if [ ".$opt_t" = .yes ]; then
1056                         echo "mkdir $pathcomp" 1>&2
1057                     fi
1058                     mkdir $pathcomp || errstatus=$?
1059                     if [ ".$opt_o" != . ]; then
1060                         if [ ".$opt_t" = .yes ]; then
1061                             echo "chown $opt_o $pathcomp" 1>&2
1062                         fi
1063                         chown $opt_o $pathcomp || errstatus=$?
1064                     fi
1065                     if [ ".$opt_g" != . ]; then
1066                         if [ ".$opt_t" = .yes ]; then
1067                             echo "chgrp $opt_g $pathcomp" 1>&2
1068                         fi
1069                         chgrp $opt_g $pathcomp || errstatus=$?
1070                     fi
1071                     if [ ".$opt_m" != . ]; then
1072                         if [ ".$opt_t" = .yes ]; then
1073                             echo "chmod $opt_m $pathcomp" 1>&2
1074                         fi
1075                         chmod $opt_m $pathcomp || errstatus=$?
1076                     fi
1077                 fi
1078                 pathcomp="$pathcomp/"
1079             done
1080         fi
1081     done
1082
1083     shtool_exit $errstatus
1084     ;;
1085
1086 mkln )
1087     ##
1088     ##  mkln -- Make link with calculation of relative paths
1089     ##  Copyright (c) 1998-2004 Ralf S. Engelschall <rse@engelschall.com>
1090     ##
1091
1092     #   determine source(s) and destination
1093     args=$?
1094     srcs=""
1095     while [ $# -gt 1 ]; do
1096         srcs="$srcs $1"
1097         shift
1098     done
1099     dst="$1"
1100     if [ ! -d $dst ]; then
1101         if [ $args -gt 2 ]; then
1102             echo "$msgprefix:Error: multiple sources not allowed when target isn't a directory" 1>&2
1103             shtool_exit 1
1104         fi
1105     fi
1106
1107     #   determine link options
1108     lnopt=""
1109     if [ ".$opt_f" = .yes ]; then
1110         lnopt="$lnopt -f"
1111     fi
1112     if [ ".$opt_s" = .yes ]; then
1113         lnopt="$lnopt -s"
1114     fi
1115
1116     #   iterate over sources
1117     for src in $srcs; do
1118         #   determine if one of the paths is an absolute path,
1119         #   because then we _have_ to use an absolute symlink
1120         oneisabs=0
1121         srcisabs=0
1122         dstisabs=0
1123         case $src in
1124             /* ) oneisabs=1; srcisabs=1 ;;
1125         esac
1126         case $dst in
1127             /* ) oneisabs=1; dstisabs=1 ;;
1128         esac
1129
1130         #   split source and destination into dir and base name
1131         if [ -d $src ]; then
1132             srcdir=`echo $src | sed -e 's;/*$;;'`
1133             srcbase=""
1134         else
1135             srcdir=`echo  $src | sed -e 's;^[^/]*$;;' -e 's;^\(.*/\)[^/]*$;\1;' -e 's;\(.\)/$;\1;'`
1136             srcbase=`echo $src | sed -e 's;.*/\([^/]*\)$;\1;'`
1137         fi
1138         if [ -d $dst ]; then
1139             dstdir=`echo $dst | sed -e 's;/*$;;'`
1140             dstbase=""
1141         else
1142             dstdir=`echo  $dst | sed -e 's;^[^/]*$;;' -e 's;^\(.*/\)[^/]*$;\1;' -e 's;\(.\)/$;\1;'`
1143             dstbase=`echo $dst | sed -e 's;.*/\([^/]*\)$;\1;'`
1144         fi
1145
1146         #   consistency check
1147         if [ ".$dstdir" != . ]; then
1148             if [ ! -d $dstdir ]; then
1149                 echo "$msgprefix:Error: destination directory not found: $dstdir" 1>&2
1150                 shtool_exit 1
1151             fi
1152         fi
1153
1154         #   make sure the source is reachable from the destination
1155         if [ $dstisabs = 1 ]; then
1156             if [ $srcisabs = 0 ]; then
1157                 if [ ".$srcdir" = . ]; then
1158                     srcdir="`pwd | sed -e 's;/*$;;'`"
1159                     srcisabs=1
1160                     oneisabs=1
1161                 elif [ -d $srcdir ]; then
1162                     srcdir="`cd $srcdir; pwd | sed -e 's;/*$;;'`"
1163                     srcisabs=1
1164                     oneisabs=1
1165                 fi
1166             fi
1167         fi
1168
1169         #   split away a common prefix
1170         prefix=""
1171         if [ ".$srcdir" = ".$dstdir" ] && [ ".$srcdir" != . ]; then
1172             prefix="$srcdir/"
1173             srcdir=""
1174             dstdir=""
1175         else
1176             while [ ".$srcdir" != . ] && [ ".$dstdir" != . ]; do
1177                 presrc=`echo $srcdir | sed -e 's;^\([^/]*\)/.*;\1;'`
1178                 predst=`echo $dstdir | sed -e 's;^\([^/]*\)/.*;\1;'`
1179                 if [ ".$presrc" != ".$predst" ]; then
1180                     break
1181                 fi
1182                 prefix="$prefix$presrc/"
1183                 srcdir=`echo $srcdir | sed -e 's;^[^/]*/*;;'`
1184                 dstdir=`echo $dstdir | sed -e 's;^[^/]*/*;;'`
1185             done
1186         fi
1187
1188         #   destination prefix is just the common prefix
1189         dstpre="$prefix"
1190
1191         #   determine source prefix which is the reverse directory
1192         #   step-up corresponding to the destination directory
1193         srcpre=""
1194
1195         isroot=0
1196         if [ ".$prefix" = . ] || [ ".$prefix" = ./ ]; then
1197             isroot=1
1198         fi
1199         if [ $oneisabs = 0 ] || [ $isroot = 0 ]; then
1200             pl="$dstdir/"
1201             OIFS="$IFS"; IFS='/'
1202             for pe in $pl; do
1203                 [ ".$pe" = .  ] && continue
1204                 [ ".$pe" = .. ] && continue
1205                 srcpre="../$srcpre"
1206             done
1207             IFS="$OIFS"
1208         else
1209             if [ $srcisabs = 1 ]; then
1210                 srcpre="$prefix"
1211             fi
1212         fi
1213
1214         #   determine destination symlink name
1215         if [ ".$dstbase" = . ]; then
1216             if [ ".$srcbase" != . ]; then
1217                 dstbase="$srcbase"
1218             else
1219                 dstbase=`echo "$prefix$srcdir" | sed -e 's;/*$;;' -e 's;.*/\([^/]*\)$;\1;'`
1220             fi
1221         fi
1222
1223         #   now finalize source and destination directory paths
1224         srcdir=`echo $srcdir | sed -e 's;\([^/]\)$;\1/;'`
1225         dstdir=`echo $dstdir | sed -e 's;\([^/]\)$;\1/;'`
1226
1227         #   run the final link command
1228         if [ ".$opt_t" = .yes ]; then
1229             echo "ln$lnopt $srcpre$srcdir$srcbase $dstpre$dstdir$dstbase"
1230         fi
1231         eval ln$lnopt $srcpre$srcdir$srcbase $dstpre$dstdir$dstbase
1232     done
1233
1234     shtool_exit 0
1235     ;;
1236
1237 subst )
1238     ##
1239     ##  subst -- Apply sed(1) substitution operations
1240     ##  Copyright (c) 2001-2004 Ralf S. Engelschall <rse@engelschall.com>
1241     ##
1242
1243     #   remember optional list of file(s)
1244     files="$*"
1245     files_num="$#"
1246
1247     #   parameter consistency check
1248     if [ $# -eq 0 ] && [ ".$opt_b" != . ]; then
1249         echo "$msgprefix:Error: option -b cannot be applied to stdin" 1>&2
1250         shtool_exit 1
1251     fi
1252     if [ $# -eq 0 ] && [ ".$opt_s" = .yes ]; then
1253         echo "$msgprefix:Error: option -s cannot be applied to stdin" 1>&2
1254         shtool_exit 1
1255     fi
1256
1257     #   build underlying sed(1) command
1258     sedcmd='sed'
1259     if [ ".$opt_e" != . ]; then
1260         OIFS="$IFS"; IFS="$ASC_NL"; set -- $opt_e; IFS="$OIFS"
1261         for e
1262         do
1263             sedcmd="$sedcmd -e '$e'"
1264         done
1265     elif [ ".$opt_f" != . ]; then
1266         if [ ! -f $opt_f ]; then
1267             echo "$msgprefix:Error: command file \`$opt_f' not found or not a regular file" 1>&2
1268             shtool_exit 1
1269         fi
1270         sedcmd="$sedcmd -f '$opt_f'"
1271     else
1272         echo "$msgprefix:Error: either -e option(s) or -f option required" 1>&2
1273         shtool_exit 1
1274     fi
1275
1276     #   determine extension for original file
1277     orig=".orig"
1278     if [ ".$opt_b" != . ]; then
1279         orig="$opt_b"
1280     fi
1281
1282     #   apply sed(1) operation(s)
1283     if [ ".$files" != . ]; then
1284         #   apply operation(s) to files
1285         substdone=no
1286         for file in $files; do
1287             test ".$file" = . && continue
1288             if [ ! -f $file ]; then
1289                 echo "$msgprefix:Warning: file \`$file' not found or not a regular file" 1>&2
1290                 continue
1291             fi
1292
1293             #   handle interactive mode
1294             if [ ".$opt_i" = .yes ]; then
1295                 eval "$sedcmd <$file >$file.new"
1296                 skip=no
1297                 if cmp $file $file.new >/dev/null 2>&1; then
1298                     rm -f $file.new
1299                     skip=yes
1300                 else
1301                     (diff -U1 $file $file.new >$tmpfile) 2>/dev/null
1302                     if [ ".`cat $tmpfile`" = . ]; then
1303                         (diff -C1 $file $file.new >$tmpfile) 2>/dev/null
1304                         if [ ".`cat $tmpfile`" = . ]; then
1305                             echo "$msgprefix:Warning: unable to show difference for file \`$file'" 1>&2
1306                             cp /dev/null $tmpfile
1307                         fi
1308                     fi
1309                     rm -f $file.new
1310                     cat $tmpfile
1311                     echo dummy | awk '{ printf("%s", TEXT); }' TEXT=">>> Apply [Y/n]: "
1312                     read input
1313                     if [ ".$input" != .Y ] &&\
1314                        [ ".$input" != .y ] &&\
1315                        [ ".$input" != . ]; then
1316                        skip=yes
1317                     fi
1318                 fi
1319                 if [ ".$skip" = .yes ]; then
1320                     if [ ".$opt_v" = .yes ]; then
1321                         echo "file \`$file' -- skipped" 1>&2
1322                     fi
1323                     continue
1324                 fi
1325             fi
1326
1327             #   apply sed(1) operation(s)
1328             if [ ".$opt_v" = .yes ]; then
1329                 echo "patching \`$file'" 1>&2
1330             fi
1331             if [ ".$opt_t" = .yes ]; then
1332                 echo "\$ cp -p $file $file$orig"
1333                 echo "\$ chmod u+w $file"
1334                 echo "\$ $sedcmd <$file$orig >$file"
1335             fi
1336             if [ ".$opt_n" = .no ]; then
1337                 cp -p $file $file$orig
1338                 chmod u+w $file >/dev/null 2>&1 || true
1339                 eval "$sedcmd <$file$orig >$file"
1340             fi
1341
1342             #   optionally fix timestamp
1343             if [ ".$opt_s" = .yes ]; then
1344                 if [ ".$opt_t" = .yes ]; then
1345                     echo "\$ touch -r $file$orig $file"
1346                 fi
1347                 if [ ".$opt_n" = .no ]; then
1348                     touch -r $file$orig $file
1349                 fi
1350             fi
1351
1352             #   optionally check whether any content change actually occurred 
1353             if [ ".$opt_q" = .no ]; then
1354                 if cmp $file$orig $file >/dev/null 2>&1; then
1355                     if [ ".$opt_w" = .yes ]; then
1356                         echo "$msgprefix:Warning: substitution resulted in no content change on file \"$file\"" 1>&2
1357                     fi
1358                 else
1359                     substdone=yes
1360                 fi
1361             fi
1362
1363             #   optionally remove preserved original file
1364             if [ ".$opt_b" = . ]; then
1365                 if [ ".$opt_t" = .yes ]; then
1366                     echo "\$ rm -f $file$orig"
1367                 fi
1368                 if [ ".$opt_n" = .no ]; then
1369                     rm -f $file$orig
1370                 fi
1371             fi
1372         done
1373         if [ ".$opt_q" = .no ] && [ ".$opt_w" = .no ]; then
1374             if [ ".$substdone" = .no ]; then
1375                 if [ ".$files_num" = .1 ]; then
1376                     echo "$msgprefix:Warning: substitution resulted in no content change on file \"$file\"" 1>&2
1377                 else
1378                     echo "$msgprefix:Warning: substitution resulted in no content change on any file" 1>&2
1379                 fi
1380             fi
1381         fi
1382     else
1383         #   apply operation(s) to stdin/stdout
1384         if [ ".$opt_v" = .yes ]; then
1385             echo "patching <stdin>" 1>&2
1386         fi
1387         if [ ".$opt_t" = .yes ]; then
1388             echo "\$ $sedcmd"
1389         fi
1390         if [ ".$opt_n" = .no ]; then
1391             eval "$sedcmd"
1392         fi
1393     fi
1394
1395     shtool_exit 0
1396     ;;
1397
1398 esac
1399
1400 shtool_exit 0
1401