3 # $NetBSD: install-sh,v 1.10 2014/10/20 22:04:31 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}"
49 pathcompchmodcmd
="$chmodprog 755"
50 chmodcmd
="$chmodprog 755"
64 while [ x
"$1" != x
]; do
83 -m) chmodcmd
="$chmodprog $2"
89 chmodcmd
="$chmodprog ${1#-m}"
93 -o) chowncmd
="$chownprog $2"
98 -g) chgrpcmd
="$chgrpprog $2"
103 -s) stripcmd
="$stripprog"
107 -S) stripcmd
="$stripprog"
108 stripflags
="-S $2 $stripflags"
117 *) if [ x
"$msrc" = x
]
130 if [ x
"$dir_arg" = x
]
135 if [ x
"$msrc" = x
"$src" ]
139 echo "install: destination is not a directory"
149 echo "install: no destination specified"
153 for srcarg
in $msrc; do
155 if [ x
"$dir_arg" != x
]; then
161 # Waiting for this to be detected by the "$instcmd $srcarg $dsttmp" command
162 # might cause directories to be created, which would be especially bad
163 # if $src (and thus $dsttmp) contains '*'.
167 doinst
="$instcmd $instflags"
168 elif [ -d "$srcarg" ]
170 echo "install: $srcarg: not a regular file"
172 elif [ "$srcarg" = "/dev/null" ]
176 echo "install: $srcarg does not exist"
180 # If destination is a directory, append the input filename; if your system
181 # does not like double slashes in filenames, you may need to add some logic
185 dstarg
="$dstarg"/`basename "$srcarg"`
189 ## this sed command emulates the dirname command
190 dstdir
=`echo "$dstarg" | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'`
192 # Make sure that the destination directory exists.
193 # this part is taken from Noah Friedman's mkinstalldirs script
195 # Skip lots of stat calls in the usual case.
196 if [ ! -d "$dstdir" ]; then
199 IFS
="${IFS-${defaultIFS}}"
202 # Some sh's can't handle IFS=/ for some reason.
204 set - `echo ${dstdir} | sed -e 's@/@%@g' -e 's@^%@/@'`
209 while [ $# -ne 0 ] ; do
210 pathcomp
="${pathcomp}${1}"
213 if [ ! -d "${pathcomp}" ] ;
215 $doit $mkdirprog "${pathcomp}"
216 if [ x
"$chowncmd" != x
]; then $doit $chowncmd "${pathcomp}"; else true
; fi &&
217 if [ x
"$chgrpcmd" != x
]; then $doit $chgrpcmd "${pathcomp}"; else true
; fi &&
218 if [ x
"$pathcompchmodcmd" != x
]; then $doit $pathcompchmodcmd "${pathcomp}"; else true
; fi
224 pathcomp
="${pathcomp}/"
228 if [ x
"$dir_arg" != x
]
230 if [ -d "$dstarg" ]; then
233 $doit $mkdirprog "$dstarg" &&
235 if [ x
"$chowncmd" != x
]; then $doit $chowncmd "$dstarg"; else true
; fi &&
236 if [ x
"$chgrpcmd" != x
]; then $doit $chgrpcmd "$dstarg"; else true
; fi &&
237 if [ x
"$chmodcmd" != x
]; then $doit $chmodcmd "$dstarg"; else true
; fi
241 if [ x
"$dstisfile" = x
]
248 dstfile
=`basename "$file"`
249 dstfinal
="$dstdir/$dstfile"
251 # Make a temp file name in the proper directory.
253 dsttmp
=$dstdir/#inst.$$#
255 # Make a backup file name in the proper directory.
257 *%*) suffix
=`echo x |
258 $awkprog -v bname="$dstfinal" -v fmt="$suffixfmt" '
261 sfx = sprintf(fmt, cnt++);
263 } while (system("test -f " name) == 0);
266 *) suffix
="$suffixfmt";;
268 dstbackup
="$dstfinal$suffix"
270 # Move or copy the file name to the temp name
272 $doit $doinst $srcarg "$dsttmp" &&
274 trap "rm -f ${dsttmp}" 0 &&
276 # and set any options; do chmod last to preserve setuid bits
278 # If any of these fail, we abort the whole thing. If we want to
279 # ignore errors from any of these, just make sure not to ignore
280 # errors from the above "$doit $instcmd $src $dsttmp" command.
282 if [ x
"$chowncmd" != x
]; then $doit $chowncmd "$dsttmp"; else true
;fi &&
283 if [ x
"$chgrpcmd" != x
]; then $doit $chgrpcmd "$dsttmp"; else true
;fi &&
284 if [ x
"$stripcmd" != x
]; then $doit $stripcmd $stripflags "$dsttmp"; else true
;fi &&
285 if [ x
"$chmodcmd" != x
]; then $doit $chmodcmd "$dsttmp"; else true
;fi &&
287 # Now rename the file to the real destination.
289 if [ x
"$suffix" != x
] && [ -f "$dstfinal" ]
291 $doit $mvcmd "$dstfinal" "$dstbackup"
293 $doit $rmcmd -f "$dstfinal"
295 $doit $mvcmd "$dsttmp" "$dstfinal"