3 ## GNU shtool -- The GNU Portable Shell Tool
4 ## Copyright (c) 1994-2005 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.2 (15-Jun-2005)
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.2 (15-Jun-2005)"
71 echo "Copyright (c) 1994-2005 Ralf S. Engelschall <rse@engelschall.com>"
72 echo "Report bugs to <bug-shtool@gnu.org>"
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.2 (15-Jun-2005)"
142 if [ ".$1" = ".-r" ] || [ ".$1" = ".--recreate" ]; then
143 shtoolize -oshtool 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 # create (explicitly) secure temporary directory
408 if [ ".$TMPDIR" != . ]; then
410 elif [ ".$TEMPDIR" != . ]; then
415 tmpdir="$tmpdir/.shtool.$$"
417 rm -rf "$tmpdir" >/dev/null 2>&1 || true
418 mkdir "$tmpdir" >/dev/null 2>&1
419 if [ $? -ne 0 ]; then
420 echo "$msgprefix:Error: failed to create temporary directory \`$tmpdir'" 1>&2
425 # create (implicitly) secure temporary file
426 tmpfile="$tmpdir/shtool.tmp"
430 # utility function: map string to lower case
432 echo "$1" | tr 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 'abcdefghijklmnopqrstuvwxyz'
435 # utility function: map string to upper case
437 echo "$1" | tr 'abcdefghijklmnopqrstuvwxyz' 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
443 if [ ".$gen_tmpfile" = .yes ]; then
444 rm -rf "$tmpdir" >/dev/null 2>&1 || true
450 ## DISPATCH INTO SCRIPT BODY
457 ## echo -- Print string with optional construct expansion
458 ## Copyright (c) 1998-2005 Ralf S. Engelschall <rse@engelschall.com>
463 # check for broken escape sequence expansion
465 bytes=`echo '\1' | wc -c | awk '{ printf("%s", $1); }'`
466 if [ ".$bytes" != .3 ]; then
467 bytes=`echo -E '\1' | wc -c | awk '{ printf("%s", $1); }'`
468 if [ ".$bytes" = .3 ]; then
473 # check for existing -n option (to suppress newline)
475 bytes=`echo -n 123 2>/dev/null | wc -c | awk '{ printf("%s", $1); }'`
476 if [ ".$bytes" = .3 ]; then
480 # determine terminal bold sequence
483 if [ ".$opt_e" = .yes ] && [ ".`echo $text | grep '%[Bb]'`" != . ]; then
485 # for the most important terminal types we directly know the sequences
486 xterm|xterm*|vt220|vt220*)
487 term_bold=`awk 'BEGIN { printf("%c%c%c%c", 27, 91, 49, 109); }' </dev/null 2>/dev/null`
488 term_norm=`awk 'BEGIN { printf("%c%c%c", 27, 91, 109); }' </dev/null 2>/dev/null`
491 term_bold=`awk 'BEGIN { printf("%c%c%c%c%c%c", 27, 91, 49, 109, 0, 0); }' </dev/null 2>/dev/null`
492 term_norm=`awk 'BEGIN { printf("%c%c%c%c%c", 27, 91, 109, 0, 0); }' </dev/null 2>/dev/null`
494 # for all others, we try to use a possibly existing `tput' or `tcout' utility
496 paths=`echo $PATH | sed -e 's/:/ /g'`
497 for tool in tput tcout; do
498 for dir in $paths; do
499 if [ -r "$dir/$tool" ]; then
500 for seq in bold md smso; do # 'smso' is last
501 bold="`$dir/$tool $seq 2>/dev/null`"
502 if [ ".$bold" != . ]; then
507 if [ ".$term_bold" != . ]; then
508 for seq in sgr0 me rmso init reset; do # 'reset' is last
509 norm="`$dir/$tool $seq 2>/dev/null`"
510 if [ ".$norm" != . ]; then
519 if [ ".$term_bold" != . ] && [ ".$term_norm" != . ]; then
525 if [ ".$term_bold" = . ] || [ ".$term_norm" = . ]; then
526 echo "$msgprefix:Warning: unable to determine terminal sequence for bold mode" 1>&2
532 # determine user name
534 if [ ".$opt_e" = .yes ] && [ ".`echo $text | grep '%[uUgG]'`" != . ]; then
535 username="`(id -un) 2>/dev/null`"
536 if [ ".$username" = . ]; then
537 str="`(id) 2>/dev/null`"
538 if [ ".`echo $str | grep '^uid[ ]*=[ ]*[0-9]*('`" != . ]; then
539 username=`echo $str | sed -e 's/^uid[ ]*=[ ]*[0-9]*(//' -e 's/).*$//'`
541 if [ ".$username" = . ]; then
543 if [ ".$username" = . ]; then
545 if [ ".$username" = . ]; then
546 username="`(whoami) 2>/dev/null |\
547 awk '{ printf("%s", $1); }'`"
548 if [ ".$username" = . ]; then
549 username="`(who am i) 2>/dev/null |\
550 awk '{ printf("%s", $1); }'`"
551 if [ ".$username" = . ]; then
563 if [ ".$opt_e" = .yes ] && [ ".`echo $text | grep '%U'`" != . ]; then
564 userid="`(id -u) 2>/dev/null`"
565 if [ ".$userid" = . ]; then
566 userid="`(id -u ${username}) 2>/dev/null`"
567 if [ ".$userid" = . ]; then
568 str="`(id) 2>/dev/null`"
569 if [ ".`echo $str | grep '^uid[ ]*=[ ]*[0-9]*('`" != . ]; then
570 userid=`echo $str | sed -e 's/^uid[ ]*=[ ]*//' -e 's/(.*$//'`
572 if [ ".$userid" = . ]; then
573 userid=`(getent passwd ${username}) 2>/dev/null | \
574 sed -e 's/[^:]*:[^:]*://' -e 's/:.*$//'`
575 if [ ".$userid" = . ]; then
576 userid=`grep "^${username}:" /etc/passwd 2>/dev/null | \
577 sed -e 's/[^:]*:[^:]*://' -e 's/:.*$//'`
578 if [ ".$userid" = . ]; then
579 userid=`(ypcat passwd) 2>/dev/null |
580 grep "^${username}:" | \
581 sed -e 's/[^:]*:[^:]*://' -e 's/:.*$//'`
582 if [ ".$userid" = . ]; then
592 # determine (primary) group id
594 if [ ".$opt_e" = .yes ] && [ ".`echo $text | grep '%[gG]'`" != . ]; then
595 groupid="`(id -g ${username}) 2>/dev/null`"
596 if [ ".$groupid" = . ]; then
597 str="`(id) 2>/dev/null`"
598 if [ ".`echo $str | grep 'gid[ ]*=[ ]*[0-9]*('`" != . ]; then
599 groupid=`echo $str | sed -e 's/^.*gid[ ]*=[ ]*//' -e 's/(.*$//'`
601 if [ ".$groupid" = . ]; then
602 groupid=`(getent passwd ${username}) 2>/dev/null | \
603 sed -e 's/[^:]*:[^:]*:[^:]*://' -e 's/:.*$//'`
604 if [ ".$groupid" = . ]; then
605 groupid=`grep "^${username}:" /etc/passwd 2>/dev/null | \
606 sed -e 's/[^:]*:[^:]*:[^:]*://' -e 's/:.*$//'`
607 if [ ".$groupid" = . ]; then
608 groupid=`(ypcat passwd) 2>/dev/null | grep "^${username}:" | \
609 sed -e 's/[^:]*:[^:]*:[^:]*://' -e 's/:.*$//'`
610 if [ ".$groupid" = . ]; then
619 # determine (primary) group name
621 if [ ".$opt_e" = .yes ] && [ ".`echo $text | grep '%g'`" != . ]; then
622 groupname="`(id -gn ${username}) 2>/dev/null`"
623 if [ ".$groupname" = . ]; then
624 str="`(id) 2>/dev/null`"
625 if [ ".`echo $str | grep 'gid[ ]*=[ ]*[0-9]*('`" != . ]; then
626 groupname=`echo $str | sed -e 's/^.*gid[ ]*=[ ]*[0-9]*(//' -e 's/).*$//'`
628 if [ ".$groupname" = . ]; then
629 groupname=`(getent group) 2>/dev/null | \
630 grep "^[^:]*:[^:]*:${groupid}:" | \
632 if [ ".$groupname" = . ]; then
633 groupname=`grep "^[^:]*:[^:]*:${groupid}:" /etc/group 2>/dev/null | \
635 if [ ".$groupname" = . ]; then
636 groupname=`(ypcat group) 2>/dev/null | \
637 grep "^[^:]*:[^:]*:${groupid}:" | \
639 if [ ".$groupname" = . ]; then
648 # determine host and domain name
651 if [ ".$opt_e" = .yes ] && [ ".`echo $text | grep '%h'`" != . ]; then
652 hostname="`(uname -n) 2>/dev/null |\
653 awk '{ printf("%s", $1); }'`"
654 if [ ".$hostname" = . ]; then
655 hostname="`(hostname) 2>/dev/null |\
656 awk '{ printf("%s", $1); }'`"
657 if [ ".$hostname" = . ]; then
663 domainname=".`echo $hostname | cut -d. -f2-`"
664 hostname="`echo $hostname | cut -d. -f1`"
668 if [ ".$opt_e" = .yes ] && [ ".`echo $text | grep '%d'`" != . ]; then
669 if [ ".$domainname" = . ]; then
670 if [ -f /etc/resolv.conf ]; then
671 domainname="`grep '^[ ]*domain' /etc/resolv.conf | sed -e 'q' |\
672 sed -e 's/.*domain//' \
673 -e 's/^[ ]*//' -e 's/^ *//' -e 's/^ *//' \
674 -e 's/^\.//' -e 's/^/./' |\
675 awk '{ printf("%s", $1); }'`"
676 if [ ".$domainname" = . ]; then
677 domainname="`grep '^[ ]*search' /etc/resolv.conf | sed -e 'q' |\
678 sed -e 's/.*search//' \
679 -e 's/^[ ]*//' -e 's/^ *//' -e 's/^ *//' \
680 -e 's/ .*//' -e 's/ .*//' \
681 -e 's/^\.//' -e 's/^/./' |\
682 awk '{ printf("%s", $1); }'`"
688 # determine current time
693 if [ ".$opt_e" = .yes ] && [ ".`echo $text | grep '%[DMYm]'`" != . ]; then
694 time_day=`date '+%d'`
695 time_month=`date '+%m'`
696 time_year=`date '+%Y' 2>/dev/null`
697 if [ ".$time_year" = . ]; then
698 time_year=`date '+%y'`
700 [5-9][0-9]) time_year="19$time_year" ;;
701 [0-4][0-9]) time_year="20$time_year" ;;
705 1|01) time_monthname='Jan' ;;
706 2|02) time_monthname='Feb' ;;
707 3|03) time_monthname='Mar' ;;
708 4|04) time_monthname='Apr' ;;
709 5|05) time_monthname='May' ;;
710 6|06) time_monthname='Jun' ;;
711 7|07) time_monthname='Jul' ;;
712 8|08) time_monthname='Aug' ;;
713 9|09) time_monthname='Sep' ;;
714 10) time_monthname='Oct' ;;
715 11) time_monthname='Nov' ;;
716 12) time_monthname='Dec' ;;
720 # expand special ``%x'' constructs
721 if [ ".$opt_e" = .yes ]; then
722 text=`echo $seo "$text" |\
723 sed -e "s/%B/${term_bold}/g" \
724 -e "s/%b/${term_norm}/g" \
725 -e "s/%u/${username}/g" \
726 -e "s/%U/${userid}/g" \
727 -e "s/%g/${groupname}/g" \
728 -e "s/%G/${groupid}/g" \
729 -e "s/%h/${hostname}/g" \
730 -e "s/%d/${domainname}/g" \
731 -e "s/%D/${time_day}/g" \
732 -e "s/%M/${time_month}/g" \
733 -e "s/%Y/${time_year}/g" \
734 -e "s/%m/${time_monthname}/g" 2>/dev/null`
738 if [ .$opt_n = .no ]; then
741 # the harder part: echo -n is best, because
742 # awk may complain about some \xx sequences.
743 if [ ".$minusn" != . ]; then
744 echo $seo $minusn "$text"
746 echo dummy | awk '{ printf("%s", TEXT); }' TEXT="$text"
755 ## move -- Move files with simultaneous substitution
756 ## Copyright (c) 1999-2005 Ralf S. Engelschall <rse@engelschall.com>
763 if [ ".$src" = . ] || [ ".$dst" = . ]; then
764 echo "$msgprefix:Error: Invalid arguments" 1>&2
767 if [ ".$src" = ".$dst" ]; then
768 echo "$msgprefix:Error: Source and destination files are the same" 1>&2
772 if [ ".$opt_e" = .yes ]; then
773 expsrc="`echo $expsrc`"
775 if [ ".$opt_e" = .yes ]; then
776 if [ ".`echo "$src" | sed -e 's;^.*\\*.*$;;'`" = ".$src" ]; then
777 echo "$msgprefix:Error: Source doesn't contain wildcard ('*'): $dst" 1>&2
780 if [ ".`echo "$dst" | sed -e 's;^.*%[1-9].*$;;'`" = ".$dst" ]; then
781 echo "$msgprefix:Error: Destination doesn't contain substitution ('%N'): $dst" 1>&2
784 if [ ".$expsrc" = ".$src" ]; then
785 echo "$msgprefix:Error: Sources not found or no asterisk : $src" 1>&2
789 if [ ! -r "$src" ]; then
790 echo "$msgprefix:Error: Source not found: $src" 1>&2
795 # determine substitution patterns
796 if [ ".$opt_e" = .yes ]; then
797 srcpat=`echo "$src" | sed -e 's/\\./\\\\./g' -e 's/;/\\;/g' -e 's;\\*;\\\\(.*\\\\);g'`
798 dstpat=`echo "$dst" | sed -e 's;%\([1-9]\);\\\\\1;g'`
801 # iterate over source(s)
802 for onesrc in $expsrc; do
803 if [ .$opt_e = .yes ]; then
804 onedst=`echo $onesrc | sed -e "s;$srcpat;$dstpat;"`
809 if [ ".$opt_v" = .yes ]; then
810 echo "$onesrc -> $onedst"
812 if [ ".$opt_p" = .yes ]; then
813 if [ -r $onedst ]; then
814 if cmp -s $onesrc $onedst; then
815 if [ ".$opt_t" = .yes ]; then
816 echo "rm -f $onesrc" 1>&2
818 rm -f $onesrc || errorstatus=$?
820 if [ ".$opt_t" = .yes ]; then
821 echo "mv -f $onesrc $onedst" 1>&2
823 mv -f $onesrc $onedst || errorstatus=$?
826 if [ ".$opt_t" = .yes ]; then
827 echo "mv -f $onesrc $onedst" 1>&2
829 mv -f $onesrc $onedst || errorstatus=$?
832 if [ ".$opt_t" = .yes ]; then
833 echo "mv -f $onesrc $onedst" 1>&2
835 mv -f $onesrc $onedst || errorstatus=$?
837 if [ $errorstatus -ne 0 ]; then
842 shtool_exit $errorstatus
847 ## install -- Install a program, script or datafile
848 ## Copyright (c) 1997-2005 Ralf S. Engelschall <rse@engelschall.com>
851 # special case: "shtool install -d <dir> [...]" internally
852 # maps to "shtool mkdir -f -p -m 755 <dir> [...]"
853 if [ "$opt_d" = yes ]; then
854 cmd="$0 mkdir -f -p -m 755"
855 if [ ".$opt_o" != . ]; then
856 cmd="$cmd -o '$opt_o'"
858 if [ ".$opt_g" != . ]; then
859 cmd="$cmd -g '$opt_g'"
861 if [ ".$opt_v" = .yes ]; then
864 if [ ".$opt_t" = .yes ]; then
868 eval "$cmd $dir" || shtool_exit $?
873 # determine source(s) and destination
876 while [ $# -gt 1 ]; do
882 # type check for destination
884 if [ -d $dstpath ]; then
885 dstpath=`echo "$dstpath" | sed -e 's:/$::'`
889 # consistency check for destination
890 if [ $argc -gt 2 ] && [ $dstisdir = 0 ]; then
891 echo "$msgprefix:Error: multiple sources require destination to be directory" 1>&2
895 # iterate over all source(s)
899 # if destination is a directory, append the input filename
900 if [ $dstisdir = 1 ]; then
901 dstfile=`echo "$src" | sed -e 's;.*/\([^/]*\)$;\1;'`
905 # check for correct arguments
906 if [ ".$src" = ".$dst" ]; then
907 echo "$msgprefix:Warning: source and destination are the same - skipped" 1>&2
910 if [ -d "$src" ]; then
911 echo "$msgprefix:Warning: source \`$src' is a directory - skipped" 1>&2
915 # make a temp file name in the destination directory
917 sed -e 's;[^/]*$;;' -e 's;\(.\)/$;\1;' -e 's;^$;.;' \
918 -e "s;\$;/#INST@$$#;"`
921 if [ ".$opt_v" = .yes ]; then
922 echo "$src -> $dst" 1>&2
925 # copy or move the file name to the temp name
926 # (because we might be not allowed to change the source)
927 if [ ".$opt_C" = .yes ]; then
930 if [ ".$opt_c" = .yes ]; then
931 if [ ".$opt_t" = .yes ]; then
932 echo "cp $src $dsttmp" 1>&2
934 cp $src $dsttmp || shtool_exit $?
936 if [ ".$opt_t" = .yes ]; then
937 echo "mv $src $dsttmp" 1>&2
939 mv $src $dsttmp || shtool_exit $?
942 # adjust the target file
943 if [ ".$opt_e" != . ]; then
945 OIFS="$IFS"; IFS="$ASC_NL"; set -- $opt_e; IFS="$OIFS"
950 cp $dsttmp $dsttmp.old
952 eval "$sed <$dsttmp.old >$dsttmp" || shtool_exit $?
955 if [ ".$opt_s" = .yes ]; then
956 if [ ".$opt_t" = .yes ]; then
957 echo "strip $dsttmp" 1>&2
959 strip $dsttmp || shtool_exit $?
961 if [ ".$opt_o" != . ]; then
962 if [ ".$opt_t" = .yes ]; then
963 echo "chown $opt_o $dsttmp" 1>&2
965 chown $opt_o $dsttmp || shtool_exit $?
967 if [ ".$opt_g" != . ]; then
968 if [ ".$opt_t" = .yes ]; then
969 echo "chgrp $opt_g $dsttmp" 1>&2
971 chgrp $opt_g $dsttmp || shtool_exit $?
973 if [ ".$opt_m" != ".-" ]; then
974 if [ ".$opt_t" = .yes ]; then
975 echo "chmod $opt_m $dsttmp" 1>&2
977 chmod $opt_m $dsttmp || shtool_exit $?
980 # determine whether to do a quick install
981 # (has to be done _after_ the strip was already done)
983 if [ ".$opt_C" = .yes ]; then
985 if cmp -s $src $dst; then
991 # finally, install the file to the real destination
992 if [ $quick = yes ]; then
993 if [ ".$opt_t" = .yes ]; then
994 echo "rm -f $dsttmp" 1>&2
998 if [ ".$opt_t" = .yes ]; then
999 echo "rm -f $dst && mv $dsttmp $dst" 1>&2
1001 rm -f $dst && mv $dsttmp $dst
1010 ## mkdir -- Make one or more directories
1011 ## Copyright (c) 1996-2005 Ralf S. Engelschall <rse@engelschall.com>
1015 for p in ${1+"$@"}; do
1016 # if the directory already exists...
1017 if [ -d "$p" ]; then
1018 if [ ".$opt_f" = .no ] && [ ".$opt_p" = .no ]; then
1019 echo "$msgprefix:Error: directory already exists: $p" 1>&2
1026 # if the directory has to be created...
1027 if [ ".$opt_p" = .no ]; then
1028 if [ ".$opt_t" = .yes ]; then
1029 echo "mkdir $p" 1>&2
1031 mkdir $p || errstatus=$?
1032 if [ ".$opt_o" != . ]; then
1033 if [ ".$opt_t" = .yes ]; then
1034 echo "chown $opt_o $p" 1>&2
1036 chown $opt_o $p || errstatus=$?
1038 if [ ".$opt_g" != . ]; then
1039 if [ ".$opt_t" = .yes ]; then
1040 echo "chgrp $opt_g $p" 1>&2
1042 chgrp $opt_g $p || errstatus=$?
1044 if [ ".$opt_m" != . ]; then
1045 if [ ".$opt_t" = .yes ]; then
1046 echo "chmod $opt_m $p" 1>&2
1048 chmod $opt_m $p || errstatus=$?
1051 # the smart situation
1052 set fnord `echo ":$p" |\
1053 sed -e 's/^:\//%/' \
1059 for d in ${1+"$@"}; do
1060 pathcomp="$pathcomp$d"
1062 -* ) pathcomp="./$pathcomp" ;;
1064 if [ ! -d "$pathcomp" ]; then
1065 if [ ".$opt_t" = .yes ]; then
1066 echo "mkdir $pathcomp" 1>&2
1068 mkdir $pathcomp || errstatus=$?
1069 if [ ".$opt_o" != . ]; then
1070 if [ ".$opt_t" = .yes ]; then
1071 echo "chown $opt_o $pathcomp" 1>&2
1073 chown $opt_o $pathcomp || errstatus=$?
1075 if [ ".$opt_g" != . ]; then
1076 if [ ".$opt_t" = .yes ]; then
1077 echo "chgrp $opt_g $pathcomp" 1>&2
1079 chgrp $opt_g $pathcomp || errstatus=$?
1081 if [ ".$opt_m" != . ]; then
1082 if [ ".$opt_t" = .yes ]; then
1083 echo "chmod $opt_m $pathcomp" 1>&2
1085 chmod $opt_m $pathcomp || errstatus=$?
1088 pathcomp="$pathcomp/"
1093 shtool_exit $errstatus
1098 ## mkln -- Make link with calculation of relative paths
1099 ## Copyright (c) 1998-2005 Ralf S. Engelschall <rse@engelschall.com>
1102 # determine source(s) and destination
1105 while [ $# -gt 1 ]; do
1110 if [ ! -d $dst ]; then
1111 if [ $args -gt 2 ]; then
1112 echo "$msgprefix:Error: multiple sources not allowed when target isn't a directory" 1>&2
1117 # determine link options
1119 if [ ".$opt_f" = .yes ]; then
1122 if [ ".$opt_s" = .yes ]; then
1126 # iterate over sources
1127 for src in $srcs; do
1128 # determine if one of the paths is an absolute path,
1129 # because then we _have_ to use an absolute symlink
1134 /* ) oneisabs=1; srcisabs=1 ;;
1137 /* ) oneisabs=1; dstisabs=1 ;;
1140 # split source and destination into dir and base name
1141 if [ -d $src ]; then
1142 srcdir=`echo $src | sed -e 's;/*$;;'`
1145 srcdir=`echo $src | sed -e 's;^[^/]*$;;' -e 's;^\(.*/\)[^/]*$;\1;' -e 's;\(.\)/$;\1;'`
1146 srcbase=`echo $src | sed -e 's;.*/\([^/]*\)$;\1;'`
1148 if [ -d $dst ]; then
1149 dstdir=`echo $dst | sed -e 's;/*$;;'`
1152 dstdir=`echo $dst | sed -e 's;^[^/]*$;;' -e 's;^\(.*/\)[^/]*$;\1;' -e 's;\(.\)/$;\1;'`
1153 dstbase=`echo $dst | sed -e 's;.*/\([^/]*\)$;\1;'`
1157 if [ ".$dstdir" != . ]; then
1158 if [ ! -d $dstdir ]; then
1159 echo "$msgprefix:Error: destination directory not found: $dstdir" 1>&2
1164 # make sure the source is reachable from the destination
1165 if [ $dstisabs = 1 ]; then
1166 if [ $srcisabs = 0 ]; then
1167 if [ ".$srcdir" = . ]; then
1168 srcdir="`pwd | sed -e 's;/*$;;'`"
1171 elif [ -d $srcdir ]; then
1172 srcdir="`cd $srcdir; pwd | sed -e 's;/*$;;'`"
1179 # split away a common prefix
1181 if [ ".$srcdir" = ".$dstdir" ] && [ ".$srcdir" != . ]; then
1186 while [ ".$srcdir" != . ] && [ ".$dstdir" != . ]; do
1187 presrc=`echo $srcdir | sed -e 's;^\([^/]*\)/.*;\1;'`
1188 predst=`echo $dstdir | sed -e 's;^\([^/]*\)/.*;\1;'`
1189 if [ ".$presrc" != ".$predst" ]; then
1192 prefix="$prefix$presrc/"
1193 srcdir=`echo $srcdir | sed -e 's;^[^/]*/*;;'`
1194 dstdir=`echo $dstdir | sed -e 's;^[^/]*/*;;'`
1198 # destination prefix is just the common prefix
1201 # determine source prefix which is the reverse directory
1202 # step-up corresponding to the destination directory
1206 if [ ".$prefix" = . ] || [ ".$prefix" = ./ ]; then
1209 if [ $oneisabs = 0 ] || [ $isroot = 0 ]; then
1211 OIFS="$IFS"; IFS='/'
1213 [ ".$pe" = . ] && continue
1214 [ ".$pe" = .. ] && continue
1219 if [ $srcisabs = 1 ]; then
1224 # determine destination symlink name
1225 if [ ".$dstbase" = . ]; then
1226 if [ ".$srcbase" != . ]; then
1229 dstbase=`echo "$prefix$srcdir" | sed -e 's;/*$;;' -e 's;.*/\([^/]*\)$;\1;'`
1233 # now finalize source and destination directory paths
1234 srcdir=`echo $srcdir | sed -e 's;\([^/]\)$;\1/;'`
1235 dstdir=`echo $dstdir | sed -e 's;\([^/]\)$;\1/;'`
1237 # run the final link command
1238 if [ ".$opt_t" = .yes ]; then
1239 echo "ln$lnopt $srcpre$srcdir$srcbase $dstpre$dstdir$dstbase"
1241 eval ln$lnopt $srcpre$srcdir$srcbase $dstpre$dstdir$dstbase
1249 ## subst -- Apply sed(1) substitution operations
1250 ## Copyright (c) 2001-2005 Ralf S. Engelschall <rse@engelschall.com>
1253 # remember optional list of file(s)
1257 # parameter consistency check
1258 if [ $# -eq 0 ] && [ ".$opt_b" != . ]; then
1259 echo "$msgprefix:Error: option -b cannot be applied to stdin" 1>&2
1262 if [ $# -eq 0 ] && [ ".$opt_s" = .yes ]; then
1263 echo "$msgprefix:Error: option -s cannot be applied to stdin" 1>&2
1267 # build underlying sed(1) command
1269 if [ ".$opt_e" != . ]; then
1270 OIFS="$IFS"; IFS="$ASC_NL"; set -- $opt_e; IFS="$OIFS"
1273 sedcmd="$sedcmd -e '$e'"
1275 elif [ ".$opt_f" != . ]; then
1276 if [ ! -f $opt_f ]; then
1277 echo "$msgprefix:Error: command file \`$opt_f' not found or not a regular file" 1>&2
1280 sedcmd="$sedcmd -f '$opt_f'"
1282 echo "$msgprefix:Error: either -e option(s) or -f option required" 1>&2
1286 # determine extension for original file
1288 if [ ".$opt_b" != . ]; then
1292 # apply sed(1) operation(s)
1293 if [ ".$files" != . ]; then
1294 # apply operation(s) to files
1296 for file in $files; do
1297 test ".$file" = . && continue
1298 if [ ! -f $file ]; then
1299 echo "$msgprefix:Warning: file \`$file' not found or not a regular file" 1>&2
1303 # handle interactive mode
1304 if [ ".$opt_i" = .yes ]; then
1305 eval "$sedcmd <$file >$file.new"
1307 if cmp $file $file.new >/dev/null 2>&1; then
1311 (diff -U1 $file $file.new >$tmpfile) 2>/dev/null
1312 if [ ".`cat $tmpfile`" = . ]; then
1313 (diff -C1 $file $file.new >$tmpfile) 2>/dev/null
1314 if [ ".`cat $tmpfile`" = . ]; then
1315 echo "$msgprefix:Warning: unable to show difference for file \`$file'" 1>&2
1316 cp /dev/null $tmpfile
1321 echo dummy | awk '{ printf("%s", TEXT); }' TEXT=">>> Apply [Y/n]: "
1323 if [ ".$input" != .Y ] &&\
1324 [ ".$input" != .y ] &&\
1325 [ ".$input" != . ]; then
1329 if [ ".$skip" = .yes ]; then
1330 if [ ".$opt_v" = .yes ]; then
1331 echo "file \`$file' -- skipped" 1>&2
1337 # apply sed(1) operation(s)
1338 if [ ".$opt_v" = .yes ]; then
1339 echo "patching \`$file'" 1>&2
1341 if [ ".$opt_t" = .yes ]; then
1342 echo "\$ cp -p $file $file$orig"
1343 echo "\$ chmod u+w $file"
1344 echo "\$ $sedcmd <$file$orig >$file"
1346 if [ ".$opt_n" = .no ]; then
1347 cp -p $file $file$orig
1348 chmod u+w $file >/dev/null 2>&1 || true
1349 eval "$sedcmd <$file$orig >$file"
1352 # optionally fix timestamp
1353 if [ ".$opt_s" = .yes ]; then
1354 if [ ".$opt_t" = .yes ]; then
1355 echo "\$ touch -r $file$orig $file"
1357 if [ ".$opt_n" = .no ]; then
1358 touch -r $file$orig $file
1362 # optionally check whether any content change actually occurred
1363 if [ ".$opt_q" = .no ]; then
1364 if cmp $file$orig $file >/dev/null 2>&1; then
1365 if [ ".$opt_w" = .yes ]; then
1366 echo "$msgprefix:Warning: substitution resulted in no content change on file \"$file\"" 1>&2
1373 # optionally remove preserved original file
1374 if [ ".$opt_b" = . ]; then
1375 if [ ".$opt_t" = .yes ]; then
1376 echo "\$ rm -f $file$orig"
1378 if [ ".$opt_n" = .no ]; then
1383 if [ ".$opt_q" = .no ] && [ ".$opt_w" = .no ]; then
1384 if [ ".$substdone" = .no ]; then
1385 if [ ".$files_num" = .1 ]; then
1386 echo "$msgprefix:Warning: substitution resulted in no content change on file \"$file\"" 1>&2
1388 echo "$msgprefix:Warning: substitution resulted in no content change on any file" 1>&2
1393 # apply operation(s) to stdin/stdout
1394 if [ ".$opt_v" = .yes ]; then
1395 echo "patching <stdin>" 1>&2
1397 if [ ".$opt_t" = .yes ]; then
1400 if [ ".$opt_n" = .no ]; then