3 # $NetBSD: install-sh,v 1.4 2011/09/25 18:18:44 christos Exp $
4 # This script now also installs multiple files, but might choke on installing
5 # multiple files with spaces in the file names.
7 # install - install a program, script, or datafile
8 # This comes from X11R5 (mit/util/scripts/install.sh).
10 # Copyright 1991 by the Massachusetts Institute of Technology
12 # Permission to use, copy, modify, distribute, and sell this software and its
13 # documentation for any purpose is hereby granted without fee, provided that
14 # the above copyright notice appear in all copies and that both that
15 # copyright notice and this permission notice appear in supporting
16 # documentation, and that the name of M.I.T. not be used in advertising or
17 # publicity pertaining to distribution of the software without specific,
18 # written prior permission. M.I.T. makes no representations about the
19 # suitability of this software for any purpose. It is provided "as is"
20 # without express or implied warranty.
22 # Calling this script install-sh is preferred over install.sh, to prevent
23 # `make' implicit rules from creating a file called install from it
24 # when there is no Makefile.
26 # This script is compatible with the BSD install script, but was written
29 # set DOITPROG to echo to test this script
31 # Don't use :- since 4.3BSD and earlier shells don't like it.
35 # put in absolute paths if you don't have them in your path; or use env. vars.
37 awkprog
="${AWKPROG-awk}"
40 chmodprog
="${CHMODPROG-chmod}"
41 chownprog
="${CHOWNPROG-chown}"
42 chgrpprog
="${CHGRPPROG-chgrp}"
43 stripprog
="${STRIPPROG-strip}"
45 mkdirprog
="${MKDIRPROG-mkdir}"
48 pathcompchmodcmd
="$chmodprog 755"
49 chmodcmd
="$chmodprog 755"
63 while [ x
"$1" != x
]; do
82 -m) chmodcmd
="$chmodprog $2"
87 -o) chowncmd
="$chownprog $2"
92 -g) chgrpcmd
="$chgrpprog $2"
97 -s) stripcmd
="$stripprog"
101 -S) stripcmd
="$stripprog"
102 stripflags
="-S $2 $stripflags"
107 *) if [ x
"$msrc" = x
]
120 if [ x
"$dir_arg" = x
]
125 if [ x
"$msrc" = x
"$src" ]
129 echo "install: destination is not a directory"
139 echo "install: no destination specified"
143 for srcarg
in $msrc; do
145 if [ x
"$dir_arg" != x
]; then
151 # Waiting for this to be detected by the "$instcmd $srcarg $dsttmp" command
152 # might cause directories to be created, which would be especially bad
153 # if $src (and thus $dsttmp) contains '*'.
158 elif [ -d "$srcarg" ]
160 echo "install: $srcarg: not a regular file"
162 elif [ "$srcarg" = "/dev/null" ]
166 echo "install: $srcarg does not exist"
170 # If destination is a directory, append the input filename; if your system
171 # does not like double slashes in filenames, you may need to add some logic
175 dstarg
="$dstarg"/`basename "$srcarg"`
179 ## this sed command emulates the dirname command
180 dstdir
=`echo "$dstarg" | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'`
182 # Make sure that the destination directory exists.
183 # this part is taken from Noah Friedman's mkinstalldirs script
185 # Skip lots of stat calls in the usual case.
186 if [ ! -d "$dstdir" ]; then
189 IFS
="${IFS-${defaultIFS}}"
192 # Some sh's can't handle IFS=/ for some reason.
194 set - `echo ${dstdir} | sed -e 's@/@%@g' -e 's@^%@/@'`
199 while [ $# -ne 0 ] ; do
200 pathcomp
="${pathcomp}${1}"
203 if [ ! -d "${pathcomp}" ] ;
205 $doit $mkdirprog "${pathcomp}"
206 if [ x
"$chowncmd" != x
]; then $doit $chowncmd "${pathcomp}"; else true
; fi &&
207 if [ x
"$chgrpcmd" != x
]; then $doit $chgrpcmd "${pathcomp}"; else true
; fi &&
208 if [ x
"$pathcompchmodcmd" != x
]; then $doit $pathcompchmodcmd "${pathcomp}"; else true
; fi
214 pathcomp
="${pathcomp}/"
218 if [ x
"$dir_arg" != x
]
220 if [ -d "$dstarg" ]; then
223 $doit $mkdirprog "$dstarg" &&
225 if [ x
"$chowncmd" != x
]; then $doit $chowncmd "$dstarg"; else true
; fi &&
226 if [ x
"$chgrpcmd" != x
]; then $doit $chgrpcmd "$dstarg"; else true
; fi &&
227 if [ x
"$chmodcmd" != x
]; then $doit $chmodcmd "$dstarg"; else true
; fi
231 if [ x
"$dstisfile" = x
]
238 dstfile
=`basename "$file"`
239 dstfinal
="$dstdir/$dstfile"
241 # Make a temp file name in the proper directory.
243 dsttmp
=$dstdir/#inst.$$#
245 # Make a backup file name in the proper directory.
247 *%*) suffix
=`echo x |
248 $awkprog -v bname="$dstfinal" -v fmt="$suffixfmt" '
251 sfx = sprintf(fmt, cnt++);
253 } while (system("test -f " name) == 0);
256 *) suffix
="$suffixfmt";;
258 dstbackup
="$dstfinal$suffix"
260 # Move or copy the file name to the temp name
262 $doit $doinst $srcarg "$dsttmp" &&
264 trap "rm -f ${dsttmp}" 0 &&
266 # and set any options; do chmod last to preserve setuid bits
268 # If any of these fail, we abort the whole thing. If we want to
269 # ignore errors from any of these, just make sure not to ignore
270 # errors from the above "$doit $instcmd $src $dsttmp" command.
272 if [ x
"$chowncmd" != x
]; then $doit $chowncmd "$dsttmp"; else true
;fi &&
273 if [ x
"$chgrpcmd" != x
]; then $doit $chgrpcmd "$dsttmp"; else true
;fi &&
274 if [ x
"$stripcmd" != x
]; then $doit $stripcmd $stripflags "$dsttmp"; else true
;fi &&
275 if [ x
"$chmodcmd" != x
]; then $doit $chmodcmd "$dsttmp"; else true
;fi &&
277 # Now rename the file to the real destination.
279 if [ x
"$suffix" != x
] && [ -f "$dstfinal" ]
281 $doit $mvcmd "$dstfinal" "$dstbackup"
283 $doit $rmcmd -f "$dstfinal"
285 $doit $mvcmd "$dsttmp" "$dstfinal"