3 # This file is distributed with OpenLDAP Software, but is an
4 # an independently licensed program.
7 ## GNU shtool -- The GNU Portable Shell Tool
8 ## Copyright (c) 1994-2002 Ralf S. Engelschall <rse@engelschall.com>
10 ## See http://www.gnu.org/software/shtool/ for more information.
11 ## See ftp://ftp.gnu.org/gnu/shtool/ for latest version.
13 ## Version: 1.6.0 (01-Feb-2002)
14 ## Contents: 6/19 available modules
18 ## This program is free software; you can redistribute it and/or modify
19 ## it under the terms of the GNU General Public License as published by
20 ## the Free Software Foundation; either version 2 of the License, or
21 ## (at your option) any later version.
23 ## This program is distributed in the hope that it will be useful,
24 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
25 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 ## General Public License for more details.
28 ## You should have received a copy of the GNU General Public License
29 ## along with this program; if not, write to the Free Software
30 ## Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
31 ## USA, or contact Ralf S. Engelschall <rse@engelschall.com>.
33 ## NOTICE: Given that you include this file verbatim into your own
34 ## source tree, you are justified in saying that it remains separate
35 ## from your package, and that this way you are simply just using GNU
36 ## shtool. So, in this situation, there is no requirement that your
37 ## package itself is licensed under the GNU General Public License in
38 ## order to take advantage of GNU shtool.
42 ## Usage: shtool [<options>] [<cmd-name> [<cmd-options>] [<cmd-args>]]
44 ## Available commands:
45 ## echo Print string with optional construct expansion
46 ## move Move files with simultaneous substitution
47 ## install Install a program, script or datafile
48 ## mkdir Make one or more directories
49 ## mkln Make link with calculation of relative paths
50 ## subst Apply sed(1) substitution operations
52 ## Not available commands (because module was not built-in):
53 ## mdate Pretty-print modification time of a file or dir
54 ## table Pretty-print a field-separated list as a table
55 ## prop Display progress with a running propeller
56 ## mkshadow Make a shadow tree through symbolic links
57 ## fixperm Fix file permissions inside a source tree
58 ## rotate Logfile rotation
59 ## tarball Roll distribution tarballs
60 ## guessos Simple operating system guesser
61 ## arx Extended archive command
62 ## slo Separate linker options by library class
63 ## scpp Sharing C Pre-Processor
64 ## version Maintain a version information file
65 ## path Deal with program paths
69 echo "$0:Error: invalid command line" 1>&2
70 echo "$0:Hint: run \`$0 -h' for usage" 1>&2
73 if [ ".$1" = ".-h" -o ".$1" = ".--help" ]; then
74 echo "This is GNU shtool, version 1.6.0 (01-Feb-2002)"
75 echo "Copyright (c) 1994-2002 Ralf S. Engelschall <rse@engelschall.com>"
76 echo "Report bugs to <bug-shtool@gnu.org>"
78 echo "Usage: shtool [<options>] [<cmd-name> [<cmd-options>] [<cmd-args>]]"
80 echo 'Available global <options>:'
81 echo ' -v, --version display shtool version information'
82 echo ' -h, --help display shtool usage help page (this one)'
83 echo ' -d, --debug display shell trace information'
84 echo ' -r, --recreate recreate this shtool script via shtoolize'
86 echo 'Available <cmd-name> [<cmd-options>] [<cmd-args>]:'
87 echo ' echo [-n|--newline] [-e|--expand] [<str> ...]'
88 echo ' move [-v|--verbose] [-t|--trace] [-e|--expand] [-p|--preserve]'
89 echo ' <src-file> <dst-file>'
90 echo ' install [-v|--verbose] [-t|--trace] [-c|--copy] [-C|--compare-copy]'
91 echo ' [-s|--strip] [-m|--mode <mode>] [-o|--owner <owner>]'
92 echo ' [-g|--group <group>] [-e|--exec <sed-cmd>] <file> [<file>'
94 echo ' mkdir [-t|--trace] [-f|--force] [-p|--parents] [-m|--mode'
95 echo ' <mode>] <dir> [<dir> ...]'
96 echo ' mkln [-t|--trace] [-f|--force] [-s|--symbolic] <src-path>'
97 echo ' [<src-path> ...] <dst-path>'
98 echo ' subst [-v|--verbose] [-t|--trace] [-n|--nop] [-s|--stealth]'
99 echo ' [-i|--interactive] [-b|--backup <ext>] [-e|--exec <cmd>]'
100 echo ' [-f|--file <cmd-file>] [<file>] [...]'
102 echo 'Not available <cmd-name> (because module was not built-in):'
103 echo ' mdate [-n|--newline] [-z|--zero] [-s|--shorten] [-d|--digits]'
104 echo ' [-f|--field-sep <str>] [-o|--order <spec>] <path>'
105 echo ' table [-F|--field-sep <sep>] [-w|--width <width>] [-c|--columns'
106 echo ' <cols>] [-s|--strip <strip>] <str><sep><str>...'
107 echo ' prop [-p|--prefix <str>]'
108 echo ' mkshadow [-v|--verbose] [-t|--trace] [-a|--all] <src-dir> <dst-dir>'
109 echo ' fixperm [-v|--verbose] [-t|--trace] <path> [<path> ...]'
110 echo ' rotate [-v|--verbose] [-t|--trace] [-f|--force] [-n|--num-files'
111 echo ' <count>] [-s|--min-size <size>] [-c|--copy] [-r|--remove]'
112 echo ' [-a|--archive-dir <dir>] [-z|--compress [<tool>:]<level>]'
113 echo ' [-b|--background] [-d|--delay] [-p|--pad <len>] [-o|--owner'
114 echo ' <owner>] [-g|--group <group>] [-m|--mode <mode>] [-M|--migrate'
115 echo ' <cmd>] [-P|--prolog <cmd>] [-E|--epilog <cmd>] <file> [...]'
116 echo ' tarball [-t|--trace] [-v|--verbose] [-o|--output <tarball>]'
117 echo ' [-c|--compress <prog>] [-d|--directory <dir>] [-u|--user'
118 echo ' <user>] [-g|--group <group>] [-e|--exclude <pattern>]'
119 echo ' <path> [<path> ...]'
121 echo ' arx [-t|--trace] [-C|--command <cmd>] <op> <archive> [<file>'
123 echo ' slo [-p|--prefix <str>] -- -L<dir> -l<lib> [-L<dir> -l<lib>'
125 echo ' scpp [-v|--verbose] [-p|--preserve] [-f|--filter <filter>]'
126 echo ' [-o|--output <ofile>] [-t|--template <tfile>] [-M|--mark'
127 echo ' <mark>] [-D|--define <dname>] [-C|--class <cname>]'
128 echo ' <file> [<file> ...]'
129 echo ' version [-l|--language <lang>] [-n|--name <name>] [-p|--prefix'
130 echo ' <prefix>] [-s|--set <version>] [-e|--edit] [-i|--increase'
131 echo ' <knob>] [-d|--display <type>] <file>'
132 echo ' path [-s|--suppress] [-r|--reverse] [-d|--dirname] [-b|--basename]'
133 echo ' [-m|--magic] [-p|--path <path>] <str> [<str> ...]'
137 if [ ".$1" = ".-v" -o ".$1" = ."--version" ]; then
138 echo "GNU shtool 1.6.0 (01-Feb-2002)"
141 if [ ".$1" = ".-r" -o ".$1" = ."--recreate" ]; then
142 shtoolize -obuild/shtool echo move install mkdir mkln subst
145 if [ ".$1" = ".-d" -o ".$1" = ."--debug" ]; then
149 name=`echo "$0" | sed -e 's;.*/\([^/]*\)$;\1;' -e 's;-sh$;;' -e 's;\.sh$;;'`
151 echo|move|install|mkdir|mkln|subst )
152 # implicit tool command selection
156 # explicit tool command selection
166 ## DISPATCH INTO SCRIPT PROLOG
172 str_usage="[-n|--newline] [-e|--expand] [<str> ...]"
175 opt_alias="n:newline,e:expand"
181 str_usage="[-v|--verbose] [-t|--trace] [-e|--expand] [-p|--preserve] <src-file> <dst-file>"
184 opt_alias="v:verbose,t:trace,e:expand,p:preserve"
192 str_usage="[-v|--verbose] [-t|--trace] [-c|--copy] [-C|--compare-copy] [-s|--strip] [-m|--mode <mode>] [-o|--owner <owner>] [-g|--group <group>] [-e|--exec <sed-cmd>] <file> [<file> ...] <path>"
194 opt_spec="v.t.c.C.s.m:o:g:e+"
195 opt_alias="v:verbose,t:trace,c:copy,C:compare-copy,s:strip,m:mode,o:owner,g:group,e:exec"
208 str_usage="[-t|--trace] [-f|--force] [-p|--parents] [-m|--mode <mode>] <dir> [<dir> ...]"
211 opt_alias="t:trace,f:force,p:parents,m:mode"
219 str_usage="[-t|--trace] [-f|--force] [-s|--symbolic] <src-path> [<src-path> ...] <dst-path>"
222 opt_alias="t:trace,f:force,s:symbolic"
229 str_usage="[-v|--verbose] [-t|--trace] [-n|--nop] [-s|--stealth] [-i|--interactive] [-b|--backup <ext>] [-e|--exec <cmd>] [-f|--file <cmd-file>] [<file>] [...]"
232 opt_spec="v.t.n.s.i.b:e+f:"
233 opt_alias="v:verbose,t:trace,n:nop,s:stealth,i:interactive,b:backup,e:exec,f:file"
244 echo "$0:Error: unknown option \`$tool'" 2>&1
245 echo "$0:Hint: run \`$0 -h' for usage" 2>&1
249 echo "$0:Error: unknown command \`$tool'" 2>&1
250 echo "$0:Hint: run \`$0 -h' for usage" 2>&1
256 ## COMMON UTILITY CODE
259 # commonly used ASCII values
264 # determine name of tool
265 if [ ".$tool" != . ]; then
266 # used inside shtool script
268 toolcmdhelp="shtool $tool"
269 msgprefix="shtool:$tool"
271 # used as standalone script
274 msgprefix="$str_tool"
277 # parse argument specification string
278 eval `echo $arg_spec |\
279 sed -e 's/^\([0-9]*\)\([+=]\)/arg_NUMS=\1; arg_MODE=\2/'`
281 # parse option specification string
282 eval `echo h.$opt_spec |\
283 sed -e 's/\([a-zA-Z0-9]\)\([.:+]\)/opt_MODE_\1=\2;/g'`
285 # parse option alias string
286 eval `echo h:help,$opt_alias |\
287 tr 'x-' 'x_' | sed -e 's/\([a-zA-Z0-9]\):\([^,]*\),*/opt_ALIAS_\2=\1;/g'`
289 # interate over argument line
291 while [ $# -gt 0 ]; do
292 # special option stops processing
293 if [ ".$1" = ".--" ]; then
298 # determine option and argument
300 if [ ".$opt_PREV" != . ]; then
301 # merge previous seen option with argument
307 # split argument into option and argument
311 sed -e 's/^x--\([a-zA-Z0-9-]*\)=\(.*\)$/opt_OPT="\1";opt_ARG="\2"/'`
312 opt_STR=`echo $opt_OPT | tr 'x-' 'x_'`
313 eval "opt_OPT=\${opt_ALIAS_${opt_STR}-${opt_OPT}}"
316 opt_OPT=`echo "x$1" | cut -c4-`
317 opt_STR=`echo $opt_OPT | tr 'x-' 'x_'`
318 eval "opt_OPT=\${opt_ALIAS_${opt_STR}-${opt_OPT}}"
323 sed -e 's/^x-\([a-zA-Z0-9]\)/opt_OPT="\1";/' \
324 -e 's/";\(.*\)$/"; opt_ARG="\1"/'`
327 opt_OPT=`echo "x$1" | cut -c3-`
339 # determine whether option needs an argument
340 eval "opt_MODE=\$opt_MODE_${opt_OPT}"
341 if [ ".$opt_ARG" = . -a ".$opt_ARG_OK" != .yes ]; then
342 if [ ".$opt_MODE" = ".:" -o ".$opt_MODE" = ".+" ]; then
352 eval "opt_${opt_OPT}=yes"
355 # option with argument (multiple occurances override)
356 eval "opt_${opt_OPT}=\"\$opt_ARG\""
359 # option with argument (multiple occurances append)
360 eval "opt_${opt_OPT}=\"\$opt_${opt_OPT}\${ASC_NL}\$opt_ARG\""
363 echo "$msgprefix:Error: unknown option: \`$opt_OPT'" 1>&2
364 echo "$msgprefix:Hint: run \`$toolcmdhelp -h' or \`man shtool' for details" 1>&2
369 if [ ".$opt_PREV" != . ]; then
370 echo "$msgprefix:Error: missing argument to option \`$opt_PREV'" 1>&2
371 echo "$msgprefix:Hint: run \`$toolcmdhelp -h' or \`man shtool' for details" 1>&2
375 # process help option
376 if [ ".$opt_h" = .yes ]; then
377 echo "Usage: $toolcmdhelp $str_usage"
381 # complain about incorrect number of arguments
384 if [ $# -ne $arg_NUMS ]; then
385 echo "$msgprefix:Error: invalid number of arguments (exactly $arg_NUMS expected)" 1>&2
386 echo "$msgprefix:Hint: run \`$toolcmd -h' or \`man shtool' for details" 1>&2
391 if [ $# -lt $arg_NUMS ]; then
392 echo "$msgprefix:Error: invalid number of arguments (at least $arg_NUMS expected)" 1>&2
393 echo "$msgprefix:Hint: run \`$toolcmd -h' or \`man shtool' for details" 1>&2
399 # establish a temporary file on request
400 if [ ".$gen_tmpfile" = .yes ]; then
401 if [ ".$TMPDIR" != . ]; then
403 elif [ ".$TEMPDIR" != . ]; then
408 tmpfile="$tmpdir/.shtool.$$"
409 rm -f $tmpfile >/dev/null 2>&1
415 ## DISPATCH INTO SCRIPT BODY
422 ## echo -- Print string with optional construct expansion
423 ## Copyright (c) 1998-2002 Ralf S. Engelschall <rse@engelschall.com>
424 ## Originally written for WML as buildinfo
429 # check for broken escape sequence expansion
431 bytes=`echo '\1' | wc -c | awk '{ printf("%s", $1); }'`
432 if [ ".$bytes" != .3 ]; then
433 bytes=`echo -E '\1' | wc -c | awk '{ printf("%s", $1); }'`
434 if [ ".$bytes" = .3 ]; then
439 # check for existing -n option (to suppress newline)
441 bytes=`echo -n 123 2>/dev/null | wc -c | awk '{ printf("%s", $1); }'`
442 if [ ".$bytes" = .3 ]; then
446 # determine terminal bold sequence
449 if [ ".$opt_e" = .yes -a ".`echo $text | egrep '%[Bb]'`" != . ]; then
451 # for the most important terminal types we directly know the sequences
452 xterm|xterm*|vt220|vt220*)
453 term_bold=`awk 'BEGIN { printf("%c%c%c%c", 27, 91, 49, 109); }' </dev/null 2>/dev/null`
454 term_norm=`awk 'BEGIN { printf("%c%c%c", 27, 91, 109); }' </dev/null 2>/dev/null`
457 term_bold=`awk 'BEGIN { printf("%c%c%c%c%c%c", 27, 91, 49, 109, 0, 0); }' </dev/null 2>/dev/null`
458 term_norm=`awk 'BEGIN { printf("%c%c%c%c%c", 27, 91, 109, 0, 0); }' </dev/null 2>/dev/null`
460 # for all others, we try to use a possibly existing `tput' or `tcout' utility
462 paths=`echo $PATH | sed -e 's/:/ /g'`
463 for tool in tput tcout; do
464 for dir in $paths; do
465 if [ -r "$dir/$tool" ]; then
466 for seq in bold md smso; do # 'smso' is last
467 bold="`$dir/$tool $seq 2>/dev/null`"
468 if [ ".$bold" != . ]; then
473 if [ ".$term_bold" != . ]; then
474 for seq in sgr0 me rmso reset; do # 'reset' is last
475 norm="`$dir/$tool $seq 2>/dev/null`"
476 if [ ".$norm" != . ]; then
485 if [ ".$term_bold" != . -a ".$term_norm" != . ]; then
491 if [ ".$term_bold" = . -o ".$term_norm" = . ]; then
492 echo "$msgprefix:Warning: unable to determine terminal sequence for bold mode" 1>&2
496 # determine user name
498 if [ ".$opt_e" = .yes -a ".`echo $text | egrep '%[uUgG]'`" != . ]; then
500 if [ ".$username" = . ]; then
502 if [ ".$username" = . ]; then
503 username="`(whoami) 2>/dev/null |\
504 awk '{ printf("%s", $1); }'`"
505 if [ ".$username" = . ]; then
506 username="`(who am i) 2>/dev/null |\
507 awk '{ printf("%s", $1); }'`"
508 if [ ".$username" = . ]; then
518 if [ ".$opt_e" = .yes -a ".`echo $text | egrep '%U'`" != . ]; then
519 userid="`(id -u) 2>/dev/null`"
520 if [ ".$userid" = . ]; then
521 str="`(id) 2>/dev/null`"
522 if [ ".`echo $str | grep '^uid[ ]*=[ ]*[0-9]*('`" != . ]; then
523 userid=`echo $str | sed -e 's/^uid[ ]*=[ ]*//' -e 's/(.*//'`
525 if [ ".$userid" = . ]; then
526 userid=`egrep "^${username}:" /etc/passwd 2>/dev/null | \
527 sed -e 's/[^:]*:[^:]*://' -e 's/:.*$//'`
528 if [ ".$userid" = . ]; then
529 userid=`(ypcat passwd) 2>/dev/null |
530 egrep "^${username}:" | \
531 sed -e 's/[^:]*:[^:]*://' -e 's/:.*$//'`
532 if [ ".$userid" = . ]; then
540 # determine (primary) group id
542 if [ ".$opt_e" = .yes -a ".`echo $text | egrep '%[gG]'`" != . ]; then
543 groupid=`egrep "^${username}:" /etc/passwd 2>/dev/null | \
544 sed -e 's/[^:]*:[^:]*:[^:]*://' -e 's/:.*$//'`
545 if [ ".$groupid" = . ]; then
546 groupid=`(ypcat passwd) 2>/dev/null | egrep "^${username}:" | \
547 sed -e 's/[^:]*:[^:]*:[^:]*://' -e 's/:.*$//'`
548 if [ ".$groupid" = . ]; then
554 # determine (primary) group name
556 if [ ".$opt_e" = .yes -a ".`echo $text | egrep '%g'`" != . ]; then
557 groupname=`egrep "^[^:]*:[^:]*:${groupid}:" /etc/group 2>/dev/null | \
559 if [ ".$groupname" = . ]; then
560 groupname=`(ypcat group) 2>/dev/null | \
561 egrep "^[^:]*:[^:]*:${groupid}:" | \
563 if [ ".$groupname" = . ]; then
569 # determine host and domain name
572 if [ ".$opt_e" = .yes -a ".`echo $text | egrep '%h'`" != . ]; then
573 hostname="`(uname -n) 2>/dev/null |\
574 awk '{ printf("%s", $1); }'`"
575 if [ ".$hostname" = . ]; then
576 hostname="`(hostname) 2>/dev/null |\
577 awk '{ printf("%s", $1); }'`"
578 if [ ".$hostname" = . ]; then
584 domainname=".`echo $hostname | cut -d. -f2-`"
585 hostname="`echo $hostname | cut -d. -f1`"
589 if [ ".$opt_e" = .yes -a ".`echo $text | egrep '%d'`" != . ]; then
590 if [ ".$domainname" = . ]; then
591 if [ -f /etc/resolv.conf ]; then
592 domainname="`egrep '^[ ]*domain' /etc/resolv.conf | head -1 |\
593 sed -e 's/.*domain//' \
594 -e 's/^[ ]*//' -e 's/^ *//' -e 's/^ *//' \
595 -e 's/^\.//' -e 's/^/./' |\
596 awk '{ printf("%s", $1); }'`"
597 if [ ".$domainname" = . ]; then
598 domainname="`egrep '^[ ]*search' /etc/resolv.conf | head -1 |\
599 sed -e 's/.*search//' \
600 -e 's/^[ ]*//' -e 's/^ *//' -e 's/^ *//' \
601 -e 's/ .*//' -e 's/ .*//' \
602 -e 's/^\.//' -e 's/^/./' |\
603 awk '{ printf("%s", $1); }'`"
609 # determine current time
614 if [ ".$opt_e" = .yes -a ".`echo $text | egrep '%[DMYm]'`" != . ]; then
615 time_day=`date '+%d'`
616 time_month=`date '+%m'`
617 time_year=`date '+%Y' 2>/dev/null`
618 if [ ".$time_year" = . ]; then
619 time_year=`date '+%y'`
621 [5-9][0-9]) time_year="19$time_year" ;;
622 [0-4][0-9]) time_year="20$time_year" ;;
626 1|01) time_monthname='Jan' ;;
627 2|02) time_monthname='Feb' ;;
628 3|03) time_monthname='Mar' ;;
629 4|04) time_monthname='Apr' ;;
630 5|05) time_monthname='May' ;;
631 6|06) time_monthname='Jun' ;;
632 7|07) time_monthname='Jul' ;;
633 8|08) time_monthname='Aug' ;;
634 9|09) time_monthname='Sep' ;;
635 10) time_monthname='Oct' ;;
636 11) time_monthname='Nov' ;;
637 12) time_monthname='Dec' ;;
641 # expand special ``%x'' constructs
642 if [ ".$opt_e" = .yes ]; then
643 text=`echo $seo "$text" |\
644 sed -e "s/%B/${term_bold}/g" \
645 -e "s/%b/${term_norm}/g" \
646 -e "s/%u/${username}/g" \
647 -e "s/%U/${userid}/g" \
648 -e "s/%g/${groupname}/g" \
649 -e "s/%G/${groupid}/g" \
650 -e "s/%h/${hostname}/g" \
651 -e "s/%d/${domainname}/g" \
652 -e "s/%D/${time_day}/g" \
653 -e "s/%M/${time_month}/g" \
654 -e "s/%Y/${time_year}/g" \
655 -e "s/%m/${time_monthname}/g" 2>/dev/null`
659 if [ .$opt_n = .no ]; then
662 # the harder part: echo -n is best, because
663 # awk may complain about some \xx sequences.
664 if [ ".$minusn" != . ]; then
665 echo $seo $minusn "$text"
667 echo dummy | awk '{ printf("%s", TEXT); }' TEXT="$text"
674 ## move -- Move files with simultaneous substitution
675 ## Copyright (c) 1999-2002 Ralf S. Engelschall <rse@engelschall.com>
676 ## Originally written for shtool
683 if [ ".$src" = . -o ".$dst" = . ]; then
684 echo "$msgprefix:Error: Invalid arguments" 1>&2
687 if [ ".$src" = ".$dst" ]; then
688 echo "$msgprefix:Error: Source and destination files are the same" 1>&2
692 if [ ".$opt_e" = .yes ]; then
693 expsrc="`echo $expsrc`"
695 if [ ".$opt_e" = .yes ]; then
696 if [ ".`echo "$src" | sed -e 's;^.*\\*.*$;;'`" = ".$src" ]; then
697 echo "$msgprefix:Error: Source doesn't contain wildcard ('*'): $dst" 1>&2
700 if [ ".`echo "$dst" | sed -e 's;^.*%[1-9].*$;;'`" = ".$dst" ]; then
701 echo "$msgprefix:Error: Destination doesn't contain substitution ('%N'): $dst" 1>&2
704 if [ ".$expsrc" = ".$src" ]; then
705 echo "$msgprefix:Error: Sources not found or no asterisk : $src" 1>&2
709 if [ ! -r "$src" ]; then
710 echo "$msgprefix:Error: Source not found: $src" 1>&2
715 # determine substitution patterns
716 if [ ".$opt_e" = .yes ]; then
717 srcpat=`echo "$src" | sed -e 's/\\./\\\\./g' -e 's/;/\\;/g' -e 's;\\*;\\\\(.*\\\\);g'`
718 dstpat=`echo "$dst" | sed -e 's;%\([1-9]\);\\\\\1;g'`
721 # iterate over source(s)
722 for onesrc in $expsrc; do
723 if [ .$opt_e = .yes ]; then
724 onedst=`echo $onesrc | sed -e "s;$srcpat;$dstpat;"`
729 if [ ".$opt_v" = .yes ]; then
730 echo "$onesrc -> $onedst"
732 if [ ".$opt_p" = .yes ]; then
733 if [ -r $onedst ]; then
734 if cmp -s $onesrc $onedst; then
735 if [ ".$opt_t" = .yes ]; then
736 echo "rm -f $onesrc" 1>&2
738 rm -f $onesrc || errorstatus=$?
740 if [ ".$opt_t" = .yes ]; then
741 echo "mv -f $onesrc $onedst" 1>&2
743 mv -f $onesrc $onedst || errorstatus=$?
746 if [ ".$opt_t" = .yes ]; then
747 echo "mv -f $onesrc $onedst" 1>&2
749 mv -f $onesrc $onedst || errorstatus=$?
752 if [ ".$opt_t" = .yes ]; then
753 echo "mv -f $onesrc $onedst" 1>&2
755 mv -f $onesrc $onedst || errorstatus=$?
757 if [ $errorstatus -ne 0 ]; then
766 ## install -- Install a program, script or datafile
767 ## Copyright (c) 1997-2002 Ralf S. Engelschall <rse@engelschall.com>
768 ## Originally written for shtool
771 # determine source(s) and destination
774 while [ $# -gt 1 ]; do
780 # type check for destination
782 if [ -d $dstpath ]; then
783 dstpath=`echo "$dstpath" | sed -e 's:/$::'`
787 # consistency check for destination
788 if [ $argc -gt 2 -a $dstisdir = 0 ]; then
789 echo "$msgprefix:Error: multiple sources require destination to be directory" 1>&2
793 # iterate over all source(s)
797 # if destination is a directory, append the input filename
798 if [ $dstisdir = 1 ]; then
799 dstfile=`echo "$src" | sed -e 's;.*/\([^/]*\)$;\1;'`
803 # check for correct arguments
804 if [ ".$src" = ".$dst" ]; then
805 echo "$msgprefix:Warning: source and destination are the same - skipped" 1>&2
808 if [ -d "$src" ]; then
809 echo "$msgprefix:Warning: source \`$src' is a directory - skipped" 1>&2
813 # make a temp file name in the destination directory
815 sed -e 's;[^/]*$;;' -e 's;\(.\)/$;\1;' -e 's;^$;.;' \
816 -e "s;\$;/#INST@$$#;"`
819 if [ ".$opt_v" = .yes ]; then
820 echo "$src -> $dst" 1>&2
823 # copy or move the file name to the temp name
824 # (because we might be not allowed to change the source)
825 if [ ".$opt_C" = .yes ]; then
828 if [ ".$opt_c" = .yes ]; then
829 if [ ".$opt_t" = .yes ]; then
830 echo "cp $src $dsttmp" 1>&2
832 cp $src $dsttmp || exit $?
834 if [ ".$opt_t" = .yes ]; then
835 echo "mv $src $dsttmp" 1>&2
837 mv $src $dsttmp || exit $?
840 # adjust the target file
841 if [ ".$opt_e" != . ]; then
843 OIFS="$IFS"; IFS="$ASC_NL"; set -- $opt_e; IFS="$OIFS"
848 cp $dsttmp $dsttmp.old
849 eval "$sed <$dsttmp.old >$dsttmp" || exit $?
852 if [ ".$opt_s" = .yes ]; then
853 if [ ".$opt_t" = .yes ]; then
854 echo "strip $dsttmp" 1>&2
856 strip $dsttmp || exit $?
858 if [ ".$opt_o" != . ]; then
859 if [ ".$opt_t" = .yes ]; then
860 echo "chown $opt_o $dsttmp" 1>&2
862 chown $opt_o $dsttmp || exit $?
864 if [ ".$opt_g" != . ]; then
865 if [ ".$opt_t" = .yes ]; then
866 echo "chgrp $opt_g $dsttmp" 1>&2
868 chgrp $opt_g $dsttmp || exit $?
870 if [ ".$opt_m" != ".-" ]; then
871 if [ ".$opt_t" = .yes ]; then
872 echo "chmod $opt_m $dsttmp" 1>&2
874 chmod $opt_m $dsttmp || exit $?
877 # determine whether to do a quick install
878 # (has to be done _after_ the strip was already done)
880 if [ ".$opt_C" = .yes ]; then
882 if cmp -s $src $dst; then
888 # finally, install the file to the real destination
889 if [ $quick = yes ]; then
890 if [ ".$opt_t" = .yes ]; then
891 echo "rm -f $dsttmp" 1>&2
895 if [ ".$opt_t" = .yes ]; then
896 echo "rm -f $dst && mv $dsttmp $dst" 1>&2
898 rm -f $dst && mv $dsttmp $dst
905 ## mkdir -- Make one or more directories
906 ## Copyright (c) 1996-2002 Ralf S. Engelschall <rse@engelschall.com>
907 ## Originally written for public domain by Noah Friedman <friedman@prep.ai.mit.edu>
908 ## Cleaned up and enhanced for shtool
912 for p in ${1+"$@"}; do
913 # if the directory already exists...
915 if [ ".$opt_f" = .no -a ".$opt_p" = .no ]; then
916 echo "$msgprefix:Error: directory already exists: $p" 1>&2
923 # if the directory has to be created...
924 if [ ".$opt_p" = .no ]; then
925 if [ ".$opt_t" = .yes ]; then
928 mkdir $p || errstatus=$?
929 if [ ".$opt_m" != . ]; then
930 if [ ".$opt_t" = .yes ]; then
931 echo "chmod $opt_m $p" 1>&2
933 chmod $opt_m $p || errstatus=$?
936 # the smart situation
937 set fnord `echo ":$p" |\
944 for d in ${1+"$@"}; do
945 pathcomp="$pathcomp$d"
947 -* ) pathcomp="./$pathcomp" ;;
949 if [ ! -d "$pathcomp" ]; then
950 if [ ".$opt_t" = .yes ]; then
951 echo "mkdir $pathcomp" 1>&2
953 mkdir $pathcomp || errstatus=$?
954 if [ ".$opt_m" != . ]; then
955 if [ ".$opt_t" = .yes ]; then
956 echo "chmod $opt_m $pathcomp" 1>&2
958 chmod $opt_m $pathcomp || errstatus=$?
961 pathcomp="$pathcomp/"
970 ## mkln -- Make link with calculation of relative paths
971 ## Copyright (c) 1999-2002 Ralf S. Engelschall <rse@engelschall.com>
972 ## Originally written for shtool
977 while [ $# -gt 1 ]; do
982 if [ ! -d $dst ]; then
983 if [ $args -gt 2 ]; then
984 echo "$msgprefix:Error: multiple sources not allowed when target isn't a directory" 1>&2
989 # determine link options
991 if [ ".$opt_f" = .yes ]; then
994 if [ ".$opt_s" = .yes ]; then
998 # iterate over sources
1000 # determine if one of the paths is an absolute path,
1001 # because then we _have_ to use an absolute symlink
1006 /* ) oneisabs=1; srcisabs=1 ;;
1009 /* ) oneisabs=1; dstisabs=1 ;;
1012 # split source and destination into dir and base name
1013 if [ -d $src ]; then
1014 srcdir=`echo $src | sed -e 's;/*$;;'`
1017 srcdir=`echo $src | sed -e 's;^[^/]*$;;' -e 's;^\(.*/\)[^/]*$;\1;' -e 's;\(.\)/$;\1;'`
1018 srcbase=`echo $src | sed -e 's;.*/\([^/]*\)$;\1;'`
1020 if [ -d $dst ]; then
1021 dstdir=`echo $dst | sed -e 's;/*$;;'`
1024 dstdir=`echo $dst | sed -e 's;^[^/]*$;;' -e 's;^\(.*/\)[^/]*$;\1;' -e 's;\(.\)/$;\1;'`
1025 dstbase=`echo $dst | sed -e 's;.*/\([^/]*\)$;\1;'`
1029 if [ ".$dstdir" != . ]; then
1030 if [ ! -d $dstdir ]; then
1031 echo "$msgprefix:Error: destination directory not found: $dstdir" 1>&2
1036 # make sure the source is reachable from the destination
1037 if [ $dstisabs = 1 ]; then
1038 if [ $srcisabs = 0 ]; then
1039 if [ -d $srcdir ]; then
1040 srcdir="`cd $srcdir; pwd | sed -e 's;/*$;;'`"
1047 # split away a common prefix
1049 if [ ".$srcdir" = ".$dstdir" -a ".$srcdir" != . ]; then
1054 while [ ".$srcdir" != . -a ".$dstdir" != . ]; do
1055 presrc=`echo $srcdir | sed -e 's;^\([^/]*\)/.*;\1;'`
1056 predst=`echo $dstdir | sed -e 's;^\([^/]*\)/.*;\1;'`
1057 if [ ".$presrc" != ".$predst" ]; then
1060 prefix="$prefix$presrc/"
1061 srcdir=`echo $srcdir | sed -e 's;^[^/]*/*;;'`
1062 dstdir=`echo $dstdir | sed -e 's;^[^/]*/*;;'`
1066 # destination prefix is just the common prefix
1069 # determine source prefix which is the reverse directory
1070 # step-up corresponding to the destination directory
1072 if [ $oneisabs = 0 ] || [ ".$prefix" != . -a ".$prefix" != ./ ]; then
1074 OIFS="$IFS"; IFS='/'
1076 [ ".$pe" = . ] && continue
1081 if [ $srcisabs = 1 ]; then
1086 # determine destination symlink name
1087 if [ ".$dstbase" = . ]; then
1088 if [ ".$srcbase" != . ]; then
1091 dstbase=`echo "$prefix$srcdir" | sed -e 's;/*$;;' -e 's;.*/\([^/]*\)$;\1;'`
1095 # now finalize source and destination directory paths
1096 srcdir=`echo $srcdir | sed -e 's;\([^/]\)$;\1/;'`
1097 dstdir=`echo $dstdir | sed -e 's;\([^/]\)$;\1/;'`
1099 # run the final link command
1100 if [ ".$opt_t" = .yes ]; then
1101 echo "ln$lnopt $srcpre$srcdir$srcbase $dstpre$dstdir$dstbase"
1103 eval ln$lnopt $srcpre$srcdir$srcbase $dstpre$dstdir$dstbase
1109 ## subst -- Apply sed(1) substitution operations
1110 ## Copyright (c) 2001-2002 Ralf S. Engelschall <rse@engelschall.com>
1111 ## Originally written for OpenPKG's rpmtool
1114 # remember optional list of file(s)
1117 # parameter consistency check
1118 if [ $# -eq 0 -a ".$opt_b" != . ]; then
1119 echo "$msgprefix:Error: option -b cannot be applied to stdin" 1>&2
1122 if [ $# -eq 0 -a ".$opt_s" = .yes ]; then
1123 echo "$msgprefix:Error: option -s cannot be applied to stdin" 1>&2
1127 # build underlying sed(1) command
1129 if [ ".$opt_e" != . ]; then
1130 OIFS="$IFS"; IFS="$ASC_NL"; set -- $opt_e; IFS="$OIFS"
1133 sedcmd="$sedcmd -e '$e'"
1135 elif [ ".$opt_f" != . ]; then
1136 if [ ! -f $opt_f ]; then
1137 echo "$msgprefix:Error: command file \`$opt_f' not found or not a regular file" 1>&2
1140 sedcmd="$sedcmd -f '$opt_f'"
1142 echo "$msgprefix:Error: either -e option(s) or -f option required" 1>&2
1146 # determine extension for original file
1148 if [ ".$opt_b" != . ]; then
1152 # apply sed(1) operation(s)
1153 if [ ".$files" != . ]; then
1154 # apply operation(s) to files
1155 for file in $files; do
1156 test ".$file" = . && continue
1157 if [ ! -f $file ]; then
1158 echo "$msgprefix:Warning: file \`$file' not found or not a regular file" 1>&2
1162 # handle interactive mode
1163 if [ ".$opt_i" = .yes ]; then
1164 eval "$sedcmd <$file >$file.new"
1166 if cmp $file $file.new >/dev/null 2>&1; then
1170 (diff -u1 $file $file.new >$tmpfile) 2>/dev/null
1171 if [ ".`cat $tmpfile`" = . ]; then
1172 (diff -C1 $file $file.new >$tmpfile) 2>/dev/null
1173 if [ ".`cat $tmpfile`" = . ]; then
1174 echo "$msgprefix:Warning: unable to show difference for file \`$file'" 1>&2
1175 cp /dev/null $tmpfile
1180 echo dummy | awk '{ printf("%s", TEXT); }' TEXT=">>> Apply [Y/n]: "
1182 if [ ".$input" != .Y ] &&\
1183 [ ".$input" != .y ] &&\
1184 [ ".$input" != . ]; then
1188 if [ ".$skip" = .yes ]; then
1189 if [ ".$opt_v" = .yes ]; then
1190 echo "file \`$file' -- skipped" 1>&2
1196 # apply sed(1) operation(s)
1197 if [ ".$opt_v" = .yes ]; then
1198 echo "patching \`$file'" 1>&2
1200 if [ ".$opt_t" = .yes ]; then
1201 echo "\$ cp -p $file $file$orig"
1202 echo "\$ $sedcmd <$file$orig >$file"
1204 if [ ".$opt_n" = .no ]; then
1205 cp -p $file $file$orig
1206 eval "$sedcmd <$file$orig >$file"
1209 # optionally fix timestamp
1210 if [ ".$opt_s" = .yes ]; then
1211 if [ ".$opt_t" = .yes ]; then
1212 echo "\$ touch -r $file$orig $file"
1214 if [ ".$opt_n" = .no ]; then
1215 touch -r $file$orig $file
1219 # optionally remove preserved original file
1220 if [ ".$opt_b" = . ]; then
1221 if [ ".$opt_t" = .yes ]; then
1222 echo "\$ rm -f $file$orig"
1224 if [ ".$opt_n" = .no ]; then
1230 # apply operation(s) to stdin/stdout
1231 if [ ".$opt_v" = .yes ]; then
1232 echo "patching <stdin>" 1>&2
1234 if [ ".$opt_t" = .yes ]; then
1237 if [ ".$opt_n" = .no ]; then