3 ## GNU shtool -- The GNU Portable Shell Tool
4 ## Copyright (c) 1994-2004 Ralf S. Engelschall <rse@engelschall.com>
6 ## See http://www.gnu.org/software/shtool/ for more information.
7 ## See ftp://ftp.gnu.org/gnu/shtool/ for latest version.
9 ## Version: 2.0.1 (11-Aug-2004)
10 ## Contents: 6/19 available modules
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.
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.
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>.
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.
38 ## Usage: shtool [<options>] [<cmd-name> [<cmd-options>] [<cmd-args>]]
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
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
65 echo "$0:Error: invalid command line" 1>&2
66 echo "$0:Hint: run \`$0 -h' for usage" 1>&2
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>"
74 echo "Usage: shtool [<options>] [<cmd-name> [<cmd-options>] [<cmd-args>]]"
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'
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>'
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>]'
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>'
124 echo ' slo [-p|--prefix <str>] -- -L<dir> -l<lib> [-L<dir> -l<lib>'
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> ...]'
138 if [ ".$1" = ".-v" ] || [ ".$1" = ".--version" ]; then
139 echo "GNU shtool 2.0.1 (11-Aug-2004)"
142 if [ ".$1" = ".-r" ] || [ ".$1" = ".--recreate" ]; then
143 shtoolize -obuild/shtool echo move install mkdir mkln subst
146 if [ ".$1" = ".-d" ] || [ ".$1" = ".--debug" ]; then
150 name=`echo "$0" | sed -e 's;.*/\([^/]*\)$;\1;' -e 's;-sh$;;' -e 's;\.sh$;;'`
152 echo|move|install|mkdir|mkln|subst )
153 # implicit tool command selection
157 # explicit tool command selection
167 ## DISPATCH INTO SCRIPT PROLOG
173 str_usage="[-n|--newline] [-e|--expand] [<string> ...]"
176 opt_alias="n:newline,e:expand"
182 str_usage="[-v|--verbose] [-t|--trace] [-e|--expand] [-p|--preserve] <src-file> <dst-file>"
185 opt_alias="v:verbose,t:trace,e:expand,p:preserve"
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>"
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"
210 str_usage="[-t|--trace] [-f|--force] [-p|--parents] [-m|--mode <mode>] [-o|--owner <owner>] [-g|--group <group>] <dir> [<dir> ...]"
212 opt_spec="t.f.p.m:o:g:"
213 opt_alias="t:trace,f:force,p:parents,m:mode,o:owner,g:group"
223 str_usage="[-t|--trace] [-f|--force] [-s|--symbolic] <src-path> [<src-path> ...] <dst-path>"
226 opt_alias="t:trace,f:force,s:symbolic"
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>] [...]"
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"
250 echo "$0:Error: unknown option \`$tool'" 2>&1
251 echo "$0:Hint: run \`$0 -h' for usage" 2>&1
255 echo "$0:Error: unknown command \`$tool'" 2>&1
256 echo "$0:Hint: run \`$0 -h' for usage" 2>&1
262 ## COMMON UTILITY CODE
265 # commonly used ASCII values
270 # determine name of tool
271 if [ ".$tool" != . ]; then
272 # used inside shtool script
274 toolcmdhelp="shtool $tool"
275 msgprefix="shtool:$tool"
277 # used as standalone script
280 msgprefix="$str_tool"
283 # parse argument specification string
284 eval `echo $arg_spec |\
285 sed -e 's/^\([0-9]*\)\([+=]\)/arg_NUMS=\1; arg_MODE=\2/'`
287 # parse option specification string
288 eval `echo h.$opt_spec |\
289 sed -e 's/\([a-zA-Z0-9]\)\([.:+]\)/opt_MODE_\1=\2;/g'`
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'`
295 # interate over argument line
297 while [ $# -gt 0 ]; do
298 # special option stops processing
299 if [ ".$1" = ".--" ]; then
304 # determine option and argument
306 if [ ".$opt_PREV" != . ]; then
307 # merge previous seen option with argument
313 # split argument into option and argument
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}}"
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}}"
329 sed -e 's/^x-\([a-zA-Z0-9]\)/opt_OPT="\1";/' \
330 -e 's/";\(.*\)$/"; opt_ARG="\1"/'`
333 opt_OPT=`echo "x$1" | cut -c3-`
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
358 eval "opt_${opt_OPT}=yes"
361 # option with argument (multiple occurances override)
362 eval "opt_${opt_OPT}=\"\$opt_ARG\""
365 # option with argument (multiple occurances append)
366 eval "opt_${opt_OPT}=\"\$opt_${opt_OPT}\${ASC_NL}\$opt_ARG\""
369 echo "$msgprefix:Error: unknown option: \`$opt_OPT'" 1>&2
370 echo "$msgprefix:Hint: run \`$toolcmdhelp -h' or \`man shtool' for details" 1>&2
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
381 # process help option
382 if [ ".$opt_h" = .yes ]; then
383 echo "Usage: $toolcmdhelp $str_usage"
387 # complain about incorrect number of arguments
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
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
405 # establish a temporary file on request
406 if [ ".$gen_tmpfile" = .yes ]; then
407 if [ ".$TMPDIR" != . ]; then
409 elif [ ".$TEMPDIR" != . ]; then
414 tmpfile="$tmpdir/.shtool.$$"
415 rm -f $tmpfile >/dev/null 2>&1
420 # utility function: map string to lower case
422 echo "$1" | tr 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 'abcdefghijklmnopqrstuvwxyz'
425 # utility function: map string to upper case
427 echo "$1" | tr 'abcdefghijklmnopqrstuvwxyz' 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
433 if [ ".$gen_tmpfile" = .yes ]; then
434 rm -f $tmpfile >/dev/null 2>&1 || true
440 ## DISPATCH INTO SCRIPT BODY
447 ## echo -- Print string with optional construct expansion
448 ## Copyright (c) 1998-2004 Ralf S. Engelschall <rse@engelschall.com>
453 # check for broken escape sequence expansion
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
463 # check for existing -n option (to suppress newline)
465 bytes=`echo -n 123 2>/dev/null | wc -c | awk '{ printf("%s", $1); }'`
466 if [ ".$bytes" = .3 ]; then
470 # determine terminal bold sequence
473 if [ ".$opt_e" = .yes ] && [ ".`echo $text | grep '%[Bb]'`" != . ]; then
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`
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`
484 # for all others, we try to use a possibly existing `tput' or `tcout' utility
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
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
509 if [ ".$term_bold" != . ] && [ ".$term_norm" != . ]; then
515 if [ ".$term_bold" = . ] || [ ".$term_norm" = . ]; then
516 echo "$msgprefix:Warning: unable to determine terminal sequence for bold mode" 1>&2
522 # determine user name
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/).*$//'`
531 if [ ".$username" = . ]; then
533 if [ ".$username" = . ]; then
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
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/(.*$//'`
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
582 # determine (primary) group id
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/(.*$//'`
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
609 # determine (primary) group name
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/).*$//'`
618 if [ ".$groupname" = . ]; then
619 groupname=`(getent group) 2>/dev/null | \
620 grep "^[^:]*:[^:]*:${groupid}:" | \
622 if [ ".$groupname" = . ]; then
623 groupname=`grep "^[^:]*:[^:]*:${groupid}:" /etc/group 2>/dev/null | \
625 if [ ".$groupname" = . ]; then
626 groupname=`(ypcat group) 2>/dev/null | \
627 grep "^[^:]*:[^:]*:${groupid}:" | \
629 if [ ".$groupname" = . ]; then
638 # determine host and domain name
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
653 domainname=".`echo $hostname | cut -d. -f2-`"
654 hostname="`echo $hostname | cut -d. -f1`"
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); }'`"
678 # determine current time
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'`
690 [5-9][0-9]) time_year="19$time_year" ;;
691 [0-4][0-9]) time_year="20$time_year" ;;
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' ;;
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`
728 if [ .$opt_n = .no ]; then
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"
736 echo dummy | awk '{ printf("%s", TEXT); }' TEXT="$text"
745 ## move -- Move files with simultaneous substitution
746 ## Copyright (c) 1999-2004 Ralf S. Engelschall <rse@engelschall.com>
753 if [ ".$src" = . ] || [ ".$dst" = . ]; then
754 echo "$msgprefix:Error: Invalid arguments" 1>&2
757 if [ ".$src" = ".$dst" ]; then
758 echo "$msgprefix:Error: Source and destination files are the same" 1>&2
762 if [ ".$opt_e" = .yes ]; then
763 expsrc="`echo $expsrc`"
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
770 if [ ".`echo "$dst" | sed -e 's;^.*%[1-9].*$;;'`" = ".$dst" ]; then
771 echo "$msgprefix:Error: Destination doesn't contain substitution ('%N'): $dst" 1>&2
774 if [ ".$expsrc" = ".$src" ]; then
775 echo "$msgprefix:Error: Sources not found or no asterisk : $src" 1>&2
779 if [ ! -r "$src" ]; then
780 echo "$msgprefix:Error: Source not found: $src" 1>&2
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'`
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;"`
799 if [ ".$opt_v" = .yes ]; then
800 echo "$onesrc -> $onedst"
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
808 rm -f $onesrc || errorstatus=$?
810 if [ ".$opt_t" = .yes ]; then
811 echo "mv -f $onesrc $onedst" 1>&2
813 mv -f $onesrc $onedst || errorstatus=$?
816 if [ ".$opt_t" = .yes ]; then
817 echo "mv -f $onesrc $onedst" 1>&2
819 mv -f $onesrc $onedst || errorstatus=$?
822 if [ ".$opt_t" = .yes ]; then
823 echo "mv -f $onesrc $onedst" 1>&2
825 mv -f $onesrc $onedst || errorstatus=$?
827 if [ $errorstatus -ne 0 ]; then
832 shtool_exit $errorstatus
837 ## install -- Install a program, script or datafile
838 ## Copyright (c) 1997-2004 Ralf S. Engelschall <rse@engelschall.com>
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'"
848 if [ ".$opt_g" != . ]; then
849 cmd="$cmd -g '$opt_g'"
851 if [ ".$opt_v" = .yes ]; then
854 if [ ".$opt_t" = .yes ]; then
858 eval "$cmd $dir" || shtool_exit $?
863 # determine source(s) and destination
866 while [ $# -gt 1 ]; do
872 # type check for destination
874 if [ -d $dstpath ]; then
875 dstpath=`echo "$dstpath" | sed -e 's:/$::'`
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
885 # iterate over all source(s)
889 # if destination is a directory, append the input filename
890 if [ $dstisdir = 1 ]; then
891 dstfile=`echo "$src" | sed -e 's;.*/\([^/]*\)$;\1;'`
895 # check for correct arguments
896 if [ ".$src" = ".$dst" ]; then
897 echo "$msgprefix:Warning: source and destination are the same - skipped" 1>&2
900 if [ -d "$src" ]; then
901 echo "$msgprefix:Warning: source \`$src' is a directory - skipped" 1>&2
905 # make a temp file name in the destination directory
907 sed -e 's;[^/]*$;;' -e 's;\(.\)/$;\1;' -e 's;^$;.;' \
908 -e "s;\$;/#INST@$$#;"`
911 if [ ".$opt_v" = .yes ]; then
912 echo "$src -> $dst" 1>&2
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
920 if [ ".$opt_c" = .yes ]; then
921 if [ ".$opt_t" = .yes ]; then
922 echo "cp $src $dsttmp" 1>&2
924 cp $src $dsttmp || shtool_exit $?
926 if [ ".$opt_t" = .yes ]; then
927 echo "mv $src $dsttmp" 1>&2
929 mv $src $dsttmp || shtool_exit $?
932 # adjust the target file
933 if [ ".$opt_e" != . ]; then
935 OIFS="$IFS"; IFS="$ASC_NL"; set -- $opt_e; IFS="$OIFS"
940 cp $dsttmp $dsttmp.old
942 eval "$sed <$dsttmp.old >$dsttmp" || shtool_exit $?
945 if [ ".$opt_s" = .yes ]; then
946 if [ ".$opt_t" = .yes ]; then
947 echo "strip $dsttmp" 1>&2
949 strip $dsttmp || shtool_exit $?
951 if [ ".$opt_o" != . ]; then
952 if [ ".$opt_t" = .yes ]; then
953 echo "chown $opt_o $dsttmp" 1>&2
955 chown $opt_o $dsttmp || shtool_exit $?
957 if [ ".$opt_g" != . ]; then
958 if [ ".$opt_t" = .yes ]; then
959 echo "chgrp $opt_g $dsttmp" 1>&2
961 chgrp $opt_g $dsttmp || shtool_exit $?
963 if [ ".$opt_m" != ".-" ]; then
964 if [ ".$opt_t" = .yes ]; then
965 echo "chmod $opt_m $dsttmp" 1>&2
967 chmod $opt_m $dsttmp || shtool_exit $?
970 # determine whether to do a quick install
971 # (has to be done _after_ the strip was already done)
973 if [ ".$opt_C" = .yes ]; then
975 if cmp -s $src $dst; then
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
988 if [ ".$opt_t" = .yes ]; then
989 echo "rm -f $dst && mv $dsttmp $dst" 1>&2
991 rm -f $dst && mv $dsttmp $dst
1000 ## mkdir -- Make one or more directories
1001 ## Copyright (c) 1996-2004 Ralf S. Engelschall <rse@engelschall.com>
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
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
1021 mkdir $p || errstatus=$?
1022 if [ ".$opt_o" != . ]; then
1023 if [ ".$opt_t" = .yes ]; then
1024 echo "chown $opt_o $p" 1>&2
1026 chown $opt_o $p || errstatus=$?
1028 if [ ".$opt_g" != . ]; then
1029 if [ ".$opt_t" = .yes ]; then
1030 echo "chgrp $opt_g $p" 1>&2
1032 chgrp $opt_g $p || errstatus=$?
1034 if [ ".$opt_m" != . ]; then
1035 if [ ".$opt_t" = .yes ]; then
1036 echo "chmod $opt_m $p" 1>&2
1038 chmod $opt_m $p || errstatus=$?
1041 # the smart situation
1042 set fnord `echo ":$p" |\
1043 sed -e 's/^:\//%/' \
1049 for d in ${1+"$@"}; do
1050 pathcomp="$pathcomp$d"
1052 -* ) pathcomp="./$pathcomp" ;;
1054 if [ ! -d "$pathcomp" ]; then
1055 if [ ".$opt_t" = .yes ]; then
1056 echo "mkdir $pathcomp" 1>&2
1058 mkdir $pathcomp || errstatus=$?
1059 if [ ".$opt_o" != . ]; then
1060 if [ ".$opt_t" = .yes ]; then
1061 echo "chown $opt_o $pathcomp" 1>&2
1063 chown $opt_o $pathcomp || errstatus=$?
1065 if [ ".$opt_g" != . ]; then
1066 if [ ".$opt_t" = .yes ]; then
1067 echo "chgrp $opt_g $pathcomp" 1>&2
1069 chgrp $opt_g $pathcomp || errstatus=$?
1071 if [ ".$opt_m" != . ]; then
1072 if [ ".$opt_t" = .yes ]; then
1073 echo "chmod $opt_m $pathcomp" 1>&2
1075 chmod $opt_m $pathcomp || errstatus=$?
1078 pathcomp="$pathcomp/"
1083 shtool_exit $errstatus
1088 ## mkln -- Make link with calculation of relative paths
1089 ## Copyright (c) 1998-2004 Ralf S. Engelschall <rse@engelschall.com>
1092 # determine source(s) and destination
1095 while [ $# -gt 1 ]; do
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
1107 # determine link options
1109 if [ ".$opt_f" = .yes ]; then
1112 if [ ".$opt_s" = .yes ]; then
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
1124 /* ) oneisabs=1; srcisabs=1 ;;
1127 /* ) oneisabs=1; dstisabs=1 ;;
1130 # split source and destination into dir and base name
1131 if [ -d $src ]; then
1132 srcdir=`echo $src | sed -e 's;/*$;;'`
1135 srcdir=`echo $src | sed -e 's;^[^/]*$;;' -e 's;^\(.*/\)[^/]*$;\1;' -e 's;\(.\)/$;\1;'`
1136 srcbase=`echo $src | sed -e 's;.*/\([^/]*\)$;\1;'`
1138 if [ -d $dst ]; then
1139 dstdir=`echo $dst | sed -e 's;/*$;;'`
1142 dstdir=`echo $dst | sed -e 's;^[^/]*$;;' -e 's;^\(.*/\)[^/]*$;\1;' -e 's;\(.\)/$;\1;'`
1143 dstbase=`echo $dst | sed -e 's;.*/\([^/]*\)$;\1;'`
1147 if [ ".$dstdir" != . ]; then
1148 if [ ! -d $dstdir ]; then
1149 echo "$msgprefix:Error: destination directory not found: $dstdir" 1>&2
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;/*$;;'`"
1161 elif [ -d $srcdir ]; then
1162 srcdir="`cd $srcdir; pwd | sed -e 's;/*$;;'`"
1169 # split away a common prefix
1171 if [ ".$srcdir" = ".$dstdir" ] && [ ".$srcdir" != . ]; then
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
1182 prefix="$prefix$presrc/"
1183 srcdir=`echo $srcdir | sed -e 's;^[^/]*/*;;'`
1184 dstdir=`echo $dstdir | sed -e 's;^[^/]*/*;;'`
1188 # destination prefix is just the common prefix
1191 # determine source prefix which is the reverse directory
1192 # step-up corresponding to the destination directory
1196 if [ ".$prefix" = . ] || [ ".$prefix" = ./ ]; then
1199 if [ $oneisabs = 0 ] || [ $isroot = 0 ]; then
1201 OIFS="$IFS"; IFS='/'
1203 [ ".$pe" = . ] && continue
1204 [ ".$pe" = .. ] && continue
1209 if [ $srcisabs = 1 ]; then
1214 # determine destination symlink name
1215 if [ ".$dstbase" = . ]; then
1216 if [ ".$srcbase" != . ]; then
1219 dstbase=`echo "$prefix$srcdir" | sed -e 's;/*$;;' -e 's;.*/\([^/]*\)$;\1;'`
1223 # now finalize source and destination directory paths
1224 srcdir=`echo $srcdir | sed -e 's;\([^/]\)$;\1/;'`
1225 dstdir=`echo $dstdir | sed -e 's;\([^/]\)$;\1/;'`
1227 # run the final link command
1228 if [ ".$opt_t" = .yes ]; then
1229 echo "ln$lnopt $srcpre$srcdir$srcbase $dstpre$dstdir$dstbase"
1231 eval ln$lnopt $srcpre$srcdir$srcbase $dstpre$dstdir$dstbase
1239 ## subst -- Apply sed(1) substitution operations
1240 ## Copyright (c) 2001-2004 Ralf S. Engelschall <rse@engelschall.com>
1243 # remember optional list of file(s)
1247 # parameter consistency check
1248 if [ $# -eq 0 ] && [ ".$opt_b" != . ]; then
1249 echo "$msgprefix:Error: option -b cannot be applied to stdin" 1>&2
1252 if [ $# -eq 0 ] && [ ".$opt_s" = .yes ]; then
1253 echo "$msgprefix:Error: option -s cannot be applied to stdin" 1>&2
1257 # build underlying sed(1) command
1259 if [ ".$opt_e" != . ]; then
1260 OIFS="$IFS"; IFS="$ASC_NL"; set -- $opt_e; IFS="$OIFS"
1263 sedcmd="$sedcmd -e '$e'"
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
1270 sedcmd="$sedcmd -f '$opt_f'"
1272 echo "$msgprefix:Error: either -e option(s) or -f option required" 1>&2
1276 # determine extension for original file
1278 if [ ".$opt_b" != . ]; then
1282 # apply sed(1) operation(s)
1283 if [ ".$files" != . ]; then
1284 # apply operation(s) to files
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
1293 # handle interactive mode
1294 if [ ".$opt_i" = .yes ]; then
1295 eval "$sedcmd <$file >$file.new"
1297 if cmp $file $file.new >/dev/null 2>&1; then
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
1311 echo dummy | awk '{ printf("%s", TEXT); }' TEXT=">>> Apply [Y/n]: "
1313 if [ ".$input" != .Y ] &&\
1314 [ ".$input" != .y ] &&\
1315 [ ".$input" != . ]; then
1319 if [ ".$skip" = .yes ]; then
1320 if [ ".$opt_v" = .yes ]; then
1321 echo "file \`$file' -- skipped" 1>&2
1327 # apply sed(1) operation(s)
1328 if [ ".$opt_v" = .yes ]; then
1329 echo "patching \`$file'" 1>&2
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"
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"
1342 # optionally fix timestamp
1343 if [ ".$opt_s" = .yes ]; then
1344 if [ ".$opt_t" = .yes ]; then
1345 echo "\$ touch -r $file$orig $file"
1347 if [ ".$opt_n" = .no ]; then
1348 touch -r $file$orig $file
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
1363 # optionally remove preserved original file
1364 if [ ".$opt_b" = . ]; then
1365 if [ ".$opt_t" = .yes ]; then
1366 echo "\$ rm -f $file$orig"
1368 if [ ".$opt_n" = .no ]; then
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
1378 echo "$msgprefix:Warning: substitution resulted in no content change on any file" 1>&2
1383 # apply operation(s) to stdin/stdout
1384 if [ ".$opt_v" = .yes ]; then
1385 echo "patching <stdin>" 1>&2
1387 if [ ".$opt_t" = .yes ]; then
1390 if [ ".$opt_n" = .no ]; then