1 |
#!/bin/sh |
#!/bin/sh |
2 |
# install - install a program, script, or datafile |
# install - install a program, script, or datafile |
3 |
|
|
4 |
scriptversion=2004-01-12.10 |
scriptversion=2004-12-17.09 |
5 |
|
|
6 |
# This originates from X11R5 (mit/util/scripts/install.sh), which was |
# This originates from X11R5 (mit/util/scripts/install.sh), which was |
7 |
# later released in X11R6 (xc/config/util/install.sh) with the |
# later released in X11R6 (xc/config/util/install.sh) with the |
58 |
rmprog="${RMPROG-rm}" |
rmprog="${RMPROG-rm}" |
59 |
mkdirprog="${MKDIRPROG-mkdir}" |
mkdirprog="${MKDIRPROG-mkdir}" |
60 |
|
|
|
transformbasename= |
|
|
transform_arg= |
|
|
instcmd="$mvprog" |
|
61 |
chmodcmd="$chmodprog 0755" |
chmodcmd="$chmodprog 0755" |
62 |
chowncmd= |
chowncmd= |
63 |
chgrpcmd= |
chgrpcmd= |
67 |
src= |
src= |
68 |
dst= |
dst= |
69 |
dir_arg= |
dir_arg= |
70 |
|
dstarg= |
71 |
|
no_target_directory= |
72 |
|
|
73 |
usage="Usage: $0 [OPTION]... SRCFILE DSTFILE |
usage="Usage: $0 [OPTION]... [-T] SRCFILE DSTFILE |
74 |
or: $0 [OPTION]... SRCFILES... DIRECTORY |
or: $0 [OPTION]... SRCFILES... DIRECTORY |
75 |
or: $0 -d DIRECTORIES... |
or: $0 [OPTION]... -t DIRECTORY SRCFILES... |
76 |
|
or: $0 [OPTION]... -d DIRECTORIES... |
77 |
|
|
78 |
In the first form, install SRCFILE to DSTFILE, removing SRCFILE by default. |
In the 1st form, copy SRCFILE to DSTFILE. |
79 |
In the second, create the directory path DIR. |
In the 2nd and 3rd, copy all SRCFILES to DIRECTORY. |
80 |
|
In the 4th, create DIRECTORIES. |
81 |
|
|
82 |
Options: |
Options: |
83 |
-b=TRANSFORMBASENAME |
-c (ignored) |
|
-c copy source (using $cpprog) instead of moving (using $mvprog). |
|
84 |
-d create directories instead of installing files. |
-d create directories instead of installing files. |
85 |
-g GROUP $chgrp installed files to GROUP. |
-g GROUP $chgrpprog installed files to GROUP. |
86 |
-m MODE $chmod installed files to MODE. |
-m MODE $chmodprog installed files to MODE. |
87 |
-o USER $chown installed files to USER. |
-o USER $chownprog installed files to USER. |
88 |
-s strip installed files (using $stripprog). |
-s $stripprog installed files. |
89 |
-t=TRANSFORM |
-t DIRECTORY install into DIRECTORY. |
90 |
|
-T report an error if DSTFILE is a directory. |
91 |
--help display this help and exit. |
--help display this help and exit. |
92 |
--version display version info and exit. |
--version display version info and exit. |
93 |
|
|
97 |
|
|
98 |
while test -n "$1"; do |
while test -n "$1"; do |
99 |
case $1 in |
case $1 in |
100 |
-b=*) transformbasename=`echo $1 | sed 's/-b=//'` |
-c) shift |
|
shift |
|
|
continue;; |
|
|
|
|
|
-c) instcmd=$cpprog |
|
|
shift |
|
101 |
continue;; |
continue;; |
102 |
|
|
103 |
-d) dir_arg=true |
-d) dir_arg=true |
125 |
shift |
shift |
126 |
continue;; |
continue;; |
127 |
|
|
128 |
-t=*) transformarg=`echo $1 | sed 's/-t=//'` |
-t) dstarg=$2 |
129 |
shift |
shift |
130 |
continue;; |
shift |
131 |
|
continue;; |
132 |
|
|
133 |
|
-T) no_target_directory=true |
134 |
|
shift |
135 |
|
continue;; |
136 |
|
|
137 |
--version) echo "$0 $scriptversion"; exit 0;; |
--version) echo "$0 $scriptversion"; exit 0;; |
138 |
|
|
139 |
*) # When -d is used, all remaining arguments are directories to create. |
*) # When -d is used, all remaining arguments are directories to create. |
140 |
test -n "$dir_arg" && break |
# When -t is used, the destination is already specified. |
141 |
|
test -n "$dir_arg$dstarg" && break |
142 |
# Otherwise, the last argument is the destination. Remove it from $@. |
# Otherwise, the last argument is the destination. Remove it from $@. |
143 |
for arg |
for arg |
144 |
do |
do |
176 |
src= |
src= |
177 |
|
|
178 |
if test -d "$dst"; then |
if test -d "$dst"; then |
179 |
instcmd=: |
mkdircmd=: |
180 |
chmodcmd= |
chmodcmd= |
181 |
else |
else |
182 |
instcmd=$mkdirprog |
mkdircmd=$mkdirprog |
183 |
fi |
fi |
184 |
else |
else |
185 |
# Waiting for this to be detected by the "$instcmd $src $dsttmp" command |
# Waiting for this to be detected by the "$cpprog $src $dsttmp" command |
186 |
# might cause directories to be created, which would be especially bad |
# might cause directories to be created, which would be especially bad |
187 |
# if $src (and thus $dsttmp) contains '*'. |
# if $src (and thus $dsttmp) contains '*'. |
188 |
if test ! -f "$src" && test ! -d "$src"; then |
if test ! -f "$src" && test ! -d "$src"; then |
204 |
# If destination is a directory, append the input filename; won't work |
# If destination is a directory, append the input filename; won't work |
205 |
# if double slashes aren't ignored. |
# if double slashes aren't ignored. |
206 |
if test -d "$dst"; then |
if test -d "$dst"; then |
207 |
|
if test -n "$no_target_directory"; then |
208 |
|
echo "$0: $dstarg: Is a directory" >&2 |
209 |
|
exit 1 |
210 |
|
fi |
211 |
dst=$dst/`basename "$src"` |
dst=$dst/`basename "$src"` |
212 |
fi |
fi |
213 |
fi |
fi |
214 |
|
|
215 |
# This sed command emulates the dirname command. |
# This sed command emulates the dirname command. |
216 |
dstdir=`echo "$dst" | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'` |
dstdir=`echo "$dst" | sed -e 's,/*$,,;s,[^/]*$,,;s,/*$,,;s,^$,.,'` |
217 |
|
|
218 |
# Make sure that the destination directory exists. |
# Make sure that the destination directory exists. |
219 |
|
|
220 |
# Skip lots of stat calls in the usual case. |
# Skip lots of stat calls in the usual case. |
221 |
if test ! -d "$dstdir"; then |
if test ! -d "$dstdir"; then |
222 |
defaultIFS=' |
defaultIFS=' |
223 |
' |
' |
224 |
IFS="${IFS-$defaultIFS}" |
IFS="${IFS-$defaultIFS}" |
225 |
|
|
226 |
oIFS=$IFS |
oIFS=$IFS |
227 |
# Some sh's can't handle IFS=/ for some reason. |
# Some sh's can't handle IFS=/ for some reason. |
228 |
IFS='%' |
IFS='%' |
229 |
set - `echo "$dstdir" | sed -e 's@/@%@g' -e 's@^%@/@'` |
set x `echo "$dstdir" | sed -e 's@/@%@g' -e 's@^%@/@'` |
230 |
|
shift |
231 |
IFS=$oIFS |
IFS=$oIFS |
232 |
|
|
233 |
pathcomp= |
pathcomp= |
235 |
while test $# -ne 0 ; do |
while test $# -ne 0 ; do |
236 |
pathcomp=$pathcomp$1 |
pathcomp=$pathcomp$1 |
237 |
shift |
shift |
238 |
test -d "$pathcomp" || $mkdirprog "$pathcomp" |
if test ! -d "$pathcomp"; then |
239 |
|
$mkdirprog "$pathcomp" |
240 |
|
# mkdir can fail with a `File exist' error in case several |
241 |
|
# install-sh are creating the directory concurrently. This |
242 |
|
# is OK. |
243 |
|
test -d "$pathcomp" || exit |
244 |
|
fi |
245 |
pathcomp=$pathcomp/ |
pathcomp=$pathcomp/ |
246 |
done |
done |
247 |
fi |
fi |
248 |
|
|
249 |
if test -n "$dir_arg"; then |
if test -n "$dir_arg"; then |
250 |
$doit $instcmd "$dst" \ |
$doit $mkdircmd "$dst" \ |
251 |
&& { test -z "$chowncmd" || $doit $chowncmd "$dst"; } \ |
&& { test -z "$chowncmd" || $doit $chowncmd "$dst"; } \ |
252 |
&& { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } \ |
&& { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } \ |
253 |
&& { test -z "$stripcmd" || $doit $stripcmd "$dst"; } \ |
&& { test -z "$stripcmd" || $doit $stripcmd "$dst"; } \ |
254 |
&& { test -z "$chmodcmd" || $doit $chmodcmd "$dst"; } |
&& { test -z "$chmodcmd" || $doit $chmodcmd "$dst"; } |
255 |
|
|
256 |
else |
else |
257 |
# If we're going to rename the final executable, determine the name now. |
dstfile=`basename "$dst"` |
|
if test -z "$transformarg"; then |
|
|
dstfile=`basename "$dst"` |
|
|
else |
|
|
dstfile=`basename "$dst" $transformbasename \ |
|
|
| sed $transformarg`$transformbasename |
|
|
fi |
|
|
|
|
|
# don't allow the sed command to completely eliminate the filename. |
|
|
test -z "$dstfile" && dstfile=`basename "$dst"` |
|
258 |
|
|
259 |
# Make a couple of temp file names in the proper directory. |
# Make a couple of temp file names in the proper directory. |
260 |
dsttmp=$dstdir/_inst.$$_ |
dsttmp=$dstdir/_inst.$$_ |
261 |
rmtmp=$dstdir/_rm.$$_ |
rmtmp=$dstdir/_rm.$$_ |
262 |
|
|
263 |
# Trap to clean up those temp files at exit. |
# Trap to clean up those temp files at exit. |
264 |
trap 'status=$?; rm -f "$dsttmp" "$rmtmp" && exit $status' 0 |
trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0 |
265 |
trap '(exit $?); exit' 1 2 13 15 |
trap '(exit $?); exit' 1 2 13 15 |
266 |
|
|
267 |
# Move or copy the file name to the temp name |
# Copy the file name to the temp name. |
268 |
$doit $instcmd "$src" "$dsttmp" && |
$doit $cpprog "$src" "$dsttmp" && |
269 |
|
|
270 |
# and set any options; do chmod last to preserve setuid bits. |
# and set any options; do chmod last to preserve setuid bits. |
271 |
# |
# |
272 |
# If any of these fail, we abort the whole thing. If we want to |
# If any of these fail, we abort the whole thing. If we want to |
273 |
# ignore errors from any of these, just make sure not to ignore |
# ignore errors from any of these, just make sure not to ignore |
274 |
# errors from the above "$doit $instcmd $src $dsttmp" command. |
# errors from the above "$doit $cpprog $src $dsttmp" command. |
275 |
# |
# |
276 |
{ test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } \ |
{ test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } \ |
277 |
&& { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } \ |
&& { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } \ |
278 |
&& { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } \ |
&& { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } \ |
279 |
&& { test -z "$chmodcmd" || $doit $chmodcmd "$dsttmp"; } && |
&& { test -z "$chmodcmd" || $doit $chmodcmd "$dsttmp"; } && |
280 |
|
|
|
# Now remove or move aside any old file at destination location. We |
|
|
# try this two ways since rm can't unlink itself on some systems and |
|
|
# the destination file might be busy for other reasons. In this case, |
|
|
# the final cleanup might fail but the new file should still install |
|
|
# successfully. |
|
|
{ |
|
|
if test -f "$dstdir/$dstfile"; then |
|
|
$doit $rmcmd -f "$dstdir/$dstfile" 2>/dev/null \ |
|
|
|| $doit $mvcmd -f "$dstdir/$dstfile" "$rmtmp" 2>/dev/null \ |
|
|
|| { |
|
|
echo "$0: cannot unlink or rename $dstdir/$dstfile" >&2 |
|
|
(exit 1); exit |
|
|
} |
|
|
else |
|
|
: |
|
|
fi |
|
|
} && |
|
|
|
|
281 |
# Now rename the file to the real destination. |
# Now rename the file to the real destination. |
282 |
$doit $mvcmd "$dsttmp" "$dstdir/$dstfile" |
{ $doit $mvcmd -f "$dsttmp" "$dstdir/$dstfile" 2>/dev/null \ |
283 |
fi || { (exit 1); exit; } |
|| { |
284 |
|
# The rename failed, perhaps because mv can't rename something else |
285 |
|
# to itself, or perhaps because mv is so ancient that it does not |
286 |
|
# support -f. |
287 |
|
|
288 |
|
# Now remove or move aside any old file at destination location. |
289 |
|
# We try this two ways since rm can't unlink itself on some |
290 |
|
# systems and the destination file might be busy for other |
291 |
|
# reasons. In this case, the final cleanup might fail but the new |
292 |
|
# file should still install successfully. |
293 |
|
{ |
294 |
|
if test -f "$dstdir/$dstfile"; then |
295 |
|
$doit $rmcmd -f "$dstdir/$dstfile" 2>/dev/null \ |
296 |
|
|| $doit $mvcmd -f "$dstdir/$dstfile" "$rmtmp" 2>/dev/null \ |
297 |
|
|| { |
298 |
|
echo "$0: cannot unlink or rename $dstdir/$dstfile" >&2 |
299 |
|
(exit 1); exit 1 |
300 |
|
} |
301 |
|
else |
302 |
|
: |
303 |
|
fi |
304 |
|
} && |
305 |
|
|
306 |
|
# Now rename the file to the real destination. |
307 |
|
$doit $mvcmd "$dsttmp" "$dstdir/$dstfile" |
308 |
|
} |
309 |
|
} |
310 |
|
fi || { (exit 1); exit 1; } |
311 |
done |
done |
312 |
|
|
313 |
# The final little trick to "correctly" pass the exit status to the exit trap. |
# The final little trick to "correctly" pass the exit status to the exit trap. |
314 |
{ |
{ |
315 |
(exit 0); exit |
(exit 0); exit 0 |
316 |
} |
} |
317 |
|
|
318 |
# Local variables: |
# Local variables: |