2 # install - install a program, script, or datafile
3 # This originally came from X11R5 (mit/util/scripts/install.sh).
5 scriptversion
=2003-01-17.15
7 # Copyright 1991 by the Massachusetts Institute of Technology
8 # (FSF changes in the public domain.)
10 # Permission to use, copy, modify, distribute, and sell this software and its
11 # documentation for any purpose is hereby granted without fee, provided that
12 # the above copyright notice appear in all copies and that both that
13 # copyright notice and this permission notice appear in supporting
14 # documentation, and that the name of M.I.T. not be used in advertising or
15 # publicity pertaining to distribution of the software without specific,
16 # written prior permission. M.I.T. makes no representations about the
17 # suitability of this software for any purpose. It is provided "as is"
18 # without express or implied warranty.
20 # Calling this script install-sh is preferred over install.sh, to prevent
21 # `make' implicit rules from creating a file called install from it
22 # when there is no Makefile.
24 # This script is compatible with the BSD install script, but was written
25 # from scratch. It can only install one file at a time, a restriction
26 # shared with many OS's install programs.
28 # set DOITPROG to echo to test this script
30 # Don't use :- since 4.3BSD and earlier shells don't like it.
33 # put in absolute paths if you don't have them in your path; or use env. vars.
37 chmodprog
="${CHMODPROG-chmod}"
38 chownprog
="${CHOWNPROG-chown}"
39 chgrpprog
="${CHGRPPROG-chgrp}"
40 stripprog
="${STRIPPROG-strip}"
42 mkdirprog
="${MKDIRPROG-mkdir}"
47 chmodcmd
="$chmodprog 0755"
57 usage
="Usage: $0 [OPTION]... SRCFILE DSTFILE
58 or: $0 -d DIR1 DIR2...
60 In the first form, install SRCFILE to DSTFILE, removing SRCFILE by default.
61 In the second, create the directory path DIR.
65 -c copy source (using $cpprog) instead of moving (using $mvprog).
66 -d create directories instead of installing files.
67 -g GROUP $chgrp installed files to GROUP.
68 -m MODE $chmod installed files to MODE.
69 -o USER $chown installed files to USER.
70 -s strip installed files (using $stripprog).
72 --help display this help and exit.
73 --version display version info and exit.
75 Environment variables override the default commands:
76 CHGRPPROG CHMODPROG CHOWNPROG CPPROG MKDIRPROG MVPROG RMPROG STRIPPROG
79 while test -n "$1"; do
81 -b=*) transformbasename
=`echo $1 | sed 's/-b=//'`
93 -g) chgrpcmd
="$chgrpprog $2"
98 --help) echo "$usage"; exit 0;;
100 -m) chmodcmd
="$chmodprog $2"
105 -o) chowncmd
="$chownprog $2"
110 -s) stripcmd
=$stripprog
114 -t=*) transformarg
=`echo $1 | sed 's/-t=//'`
118 --version) echo "$0 $scriptversion"; exit 0;;
120 *) if test -z "$src"; then
123 # this colon is to work around a 386BSD /bin/sh bug
132 if test -z "$src"; then
133 echo "$0: no input file specified." >&2
137 if test -n "$dir_arg"; then
141 if test -d "$dst"; then
148 # Waiting for this to be detected by the "$instcmd $src $dsttmp" command
149 # might cause directories to be created, which would be especially bad
150 # if $src (and thus $dsttmp) contains '*'.
151 if test ! -f "$src" && test ! -d "$src"; then
152 echo "$0: $src does not exist." >&2
156 if test -z "$dst"; then
157 echo "$0: no destination specified." >&2
161 # If destination is a directory, append the input filename; won't work
162 # if double slashes aren't ignored.
163 if test -d "$dst"; then
164 dst
=$dst/`basename "$src"`
168 ## this sed command emulates the dirname command
169 dstdir
=`echo "$dst" | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'`
171 # Make sure that the destination directory exists.
172 # (this part is taken from Noah Friedman's mkinstalldirs script.)
174 # Skip lots of stat calls in the usual case.
175 if test ! -d "$dstdir"; then
178 IFS
="${IFS-$defaultIFS}"
181 # Some sh's can't handle IFS=/ for some reason.
183 set - `echo "$dstdir" | sed -e 's@/@%@g' -e 's@^%@/@'`
188 while test $# -ne 0 ; do
191 test -d "$pathcomp" ||
$mkdirprog "$pathcomp"
196 if test -n "$dir_arg"; then
197 $doit $instcmd "$dst" \
198 && { test -z "$chowncmd" ||
$doit $chowncmd "$dst"; } \
199 && { test -z "$chgrpcmd" ||
$doit $chgrpcmd "$dst"; } \
200 && { test -z "$stripcmd" ||
$doit $stripcmd "$dst"; } \
201 && { test -z "$chmodcmd" ||
$doit $chmodcmd "$dst"; }
204 # If we're going to rename the final executable, determine the name now.
205 if test -z "$transformarg"; then
206 dstfile
=`basename "$dst"`
208 dstfile
=`basename "$dst" $transformbasename \
209 | sed $transformarg`$transformbasename
212 # don't allow the sed command to completely eliminate the filename.
213 test -z "$dstfile" && dstfile
=`basename "$dst"`
215 # Make a couple of temp file names in the proper directory.
216 dsttmp
=$dstdir/#inst.$$#
217 rmtmp
=$dstdir/#rm.$$#
219 # Trap to clean up those temp files at exit.
220 trap 'status=$?; rm -f "$dsttmp" "$rmtmp" && exit $status' 0
221 trap '(exit $?); exit' 1 2 13 15
223 # Move or copy the file name to the temp name
224 $doit $instcmd "$src" "$dsttmp" &&
226 # and set any options; do chmod last to preserve setuid bits.
228 # If any of these fail, we abort the whole thing. If we want to
229 # ignore errors from any of these, just make sure not to ignore
230 # errors from the above "$doit $instcmd $src $dsttmp" command.
232 { test -z "$chowncmd" ||
$doit $chowncmd "$dsttmp"; } \
233 && { test -z "$chgrpcmd" ||
$doit $chgrpcmd "$dsttmp"; } \
234 && { test -z "$stripcmd" ||
$doit $stripcmd "$dsttmp"; } \
235 && { test -z "$chmodcmd" ||
$doit $chmodcmd "$dsttmp"; } &&
237 # Now remove or move aside any old file at destination location. We
238 # try this two ways since rm can't unlink itself on some systems and
239 # the destination file might be busy for other reasons. In this case,
240 # the final cleanup might fail but the new file should still install
243 if test -f "$dstdir/$dstfile"; then
244 $doit $rmcmd -f "$dstdir/$dstfile" 2>/dev
/null \
245 ||
$doit $mvcmd -f "$dstdir/$dstfile" "$rmtmp" 2>/dev
/null \
247 echo "$0: cannot unlink or rename $dstdir/$dstfile" >&2
255 # Now rename the file to the real destination.
256 $doit $mvcmd "$dsttmp" "$dstdir/$dstfile"
259 # The final little trick to "correctly" pass the exit status to the exit trap.
265 # eval: (add-hook 'write-file-hooks 'time-stamp)
266 # time-stamp-start: "scriptversion="
267 # time-stamp-format: "%:y-%02m-%02d.%02H"
268 # time-stamp-end: "$"