1 | #!@KSH@ -p |
---|
2 | # |
---|
3 | # Either bash or ksh will be ok for this; requires (( )) arithmetic |
---|
4 | # (-p above just says to not parse $ENV file; makes it faster for |
---|
5 | # those of us who set $ENV) |
---|
6 | # |
---|
7 | # install files if they have changed by running 'cmp', then 'install' |
---|
8 | # as necessary. |
---|
9 | # |
---|
10 | # Optionally, can append a suffix before last existing suffix (if any) |
---|
11 | # |
---|
12 | # NOTE |
---|
13 | # We avoid using typical install(1M) programs since they have |
---|
14 | # large variability across systems and we also need to support ou |
---|
15 | # -V option. |
---|
16 | # So we just copy and chmod by hand. |
---|
17 | # |
---|
18 | # $Id$ |
---|
19 | # |
---|
20 | |
---|
21 | progname=`basename $0` |
---|
22 | #progname=${0##*/} # fast basename hack for ksh, bash |
---|
23 | |
---|
24 | USAGE=\ |
---|
25 | "usage: $progname [ -vmV ] file [ file ... ] dest-directory-or-file |
---|
26 | -v -- verbose |
---|
27 | -V suffix -- suffix to append to targets (before any . suffix) |
---|
28 | eg: -V _g would change 'foo' to 'foo_g' and |
---|
29 | 'libfoo.a' to 'libfoo_g.a' |
---|
30 | -m mode -- mode for new file(s)" |
---|
31 | |
---|
32 | fatal() { |
---|
33 | if [ "$1" ] |
---|
34 | then |
---|
35 | echo $* >&2 |
---|
36 | fi |
---|
37 | echo "$USAGE" 1>&2 |
---|
38 | exit 1 |
---|
39 | } |
---|
40 | |
---|
41 | # |
---|
42 | # process the options |
---|
43 | # |
---|
44 | |
---|
45 | verbose="" |
---|
46 | suffix="" |
---|
47 | mode="" |
---|
48 | |
---|
49 | while getopts vm:V: OPT |
---|
50 | do |
---|
51 | case "$OPT" in |
---|
52 | v) |
---|
53 | verbose="yes";; |
---|
54 | V) |
---|
55 | eval suffix=$OPTARG;; |
---|
56 | m) |
---|
57 | mode="$OPTARG";; |
---|
58 | *) |
---|
59 | fatal |
---|
60 | esac |
---|
61 | done |
---|
62 | |
---|
63 | shiftcount=`expr $OPTIND - 1` |
---|
64 | shift $shiftcount |
---|
65 | |
---|
66 | args=$* |
---|
67 | |
---|
68 | # |
---|
69 | # Separate source file(s) from dest directory or file |
---|
70 | # |
---|
71 | |
---|
72 | files="" |
---|
73 | dest="" |
---|
74 | for d in $args |
---|
75 | do |
---|
76 | files="$files $dest" |
---|
77 | dest=$d |
---|
78 | done |
---|
79 | |
---|
80 | if [ ! "$files" ] || [ ! "$dest" ] |
---|
81 | then |
---|
82 | fatal "missing files or invalid destination" |
---|
83 | fi |
---|
84 | |
---|
85 | # |
---|
86 | # Process the arguments |
---|
87 | # |
---|
88 | |
---|
89 | targets="" |
---|
90 | for f in $files |
---|
91 | do |
---|
92 | # leaf=`basename $f` |
---|
93 | leaf=${f##*/} # fast basename hack for ksh, bash |
---|
94 | |
---|
95 | target=$dest |
---|
96 | if [ -d $dest ] |
---|
97 | then |
---|
98 | # if we were given a suffix, then add it as appropriate |
---|
99 | if [ "$suffix" ] |
---|
100 | then |
---|
101 | case $f in |
---|
102 | *.*) |
---|
103 | # leaf=`echo $leaf | |
---|
104 | # /bin/sed "s/\([~\.]*\)\.\(.*\)$/\1$suffix.\2/"` |
---|
105 | # ksh,bash hack for above sed script |
---|
106 | leaf=${leaf%%.*}$suffix.${leaf#*.} |
---|
107 | |
---|
108 | [ "$verbose" = "yes" ] && |
---|
109 | echo "$progname: $f will be installed as $leaf" |
---|
110 | ;; |
---|
111 | *) |
---|
112 | leaf=$leaf$suffix;; |
---|
113 | esac |
---|
114 | fi |
---|
115 | target=$target/$leaf |
---|
116 | fi |
---|
117 | |
---|
118 | [ ! -r $f ] && fatal "can not read $f" |
---|
119 | |
---|
120 | if cmp -s $f $target |
---|
121 | then |
---|
122 | [ "$verbose" = "yes" ] && echo "'$f' not newer than '$target'" |
---|
123 | else |
---|
124 | [ "$verbose" = "yes" ] && echo "rm -f $target" |
---|
125 | rm -f $target |
---|
126 | echo "cp -p $f $target" |
---|
127 | cp -p $f $target || exit 1 |
---|
128 | targets="$targets $target" # keep list for chmod below |
---|
129 | fi |
---|
130 | done |
---|
131 | |
---|
132 | if [ "$mode" -a "$targets" ] |
---|
133 | then |
---|
134 | [ "$verbose" = "yes" ] && echo "chmod $mode $targets" |
---|
135 | chmod $mode $targets |
---|
136 | fi |
---|
137 | |
---|
138 | exit 0 |
---|
139 | |
---|
140 | # Local Variables: *** |
---|
141 | # mode:ksh *** |
---|
142 | # End: *** |
---|