cannam@86: #!/bin/sh cannam@86: # install - install a program, script, or datafile cannam@86: cannam@86: scriptversion=2005-05-14.22 cannam@86: cannam@86: # This originates from X11R5 (mit/util/scripts/install.sh), which was cannam@86: # later released in X11R6 (xc/config/util/install.sh) with the cannam@86: # following copyright and license. cannam@86: # cannam@86: # Copyright (C) 1994 X Consortium cannam@86: # cannam@86: # Permission is hereby granted, free of charge, to any person obtaining a copy cannam@86: # of this software and associated documentation files (the "Software"), to cannam@86: # deal in the Software without restriction, including without limitation the cannam@86: # rights to use, copy, modify, merge, publish, distribute, sublicense, and/or cannam@86: # sell copies of the Software, and to permit persons to whom the Software is cannam@86: # furnished to do so, subject to the following conditions: cannam@86: # cannam@86: # The above copyright notice and this permission notice shall be included in cannam@86: # all copies or substantial portions of the Software. cannam@86: # cannam@86: # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR cannam@86: # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, cannam@86: # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE cannam@86: # X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN cannam@86: # AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC- cannam@86: # TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. cannam@86: # cannam@86: # Except as contained in this notice, the name of the X Consortium shall not cannam@86: # be used in advertising or otherwise to promote the sale, use or other deal- cannam@86: # ings in this Software without prior written authorization from the X Consor- cannam@86: # tium. cannam@86: # cannam@86: # cannam@86: # FSF changes to this file are in the public domain. cannam@86: # cannam@86: # Calling this script install-sh is preferred over install.sh, to prevent cannam@86: # `make' implicit rules from creating a file called install from it cannam@86: # when there is no Makefile. cannam@86: # cannam@86: # This script is compatible with the BSD install script, but was written cannam@86: # from scratch. It can only install one file at a time, a restriction cannam@86: # shared with many OS's install programs. cannam@86: cannam@86: # set DOITPROG to echo to test this script cannam@86: cannam@86: # Don't use :- since 4.3BSD and earlier shells don't like it. cannam@86: doit="${DOITPROG-}" cannam@86: cannam@86: # put in absolute paths if you don't have them in your path; or use env. vars. cannam@86: cannam@86: mvprog="${MVPROG-mv}" cannam@86: cpprog="${CPPROG-cp}" cannam@86: chmodprog="${CHMODPROG-chmod}" cannam@86: chownprog="${CHOWNPROG-chown}" cannam@86: chgrpprog="${CHGRPPROG-chgrp}" cannam@86: stripprog="${STRIPPROG-strip}" cannam@86: rmprog="${RMPROG-rm}" cannam@86: mkdirprog="${MKDIRPROG-mkdir}" cannam@86: cannam@86: chmodcmd="$chmodprog 0755" cannam@86: chowncmd= cannam@86: chgrpcmd= cannam@86: stripcmd= cannam@86: rmcmd="$rmprog -f" cannam@86: mvcmd="$mvprog" cannam@86: src= cannam@86: dst= cannam@86: dir_arg= cannam@86: dstarg= cannam@86: no_target_directory= cannam@86: cannam@86: usage="Usage: $0 [OPTION]... [-T] SRCFILE DSTFILE cannam@86: or: $0 [OPTION]... SRCFILES... DIRECTORY cannam@86: or: $0 [OPTION]... -t DIRECTORY SRCFILES... cannam@86: or: $0 [OPTION]... -d DIRECTORIES... cannam@86: cannam@86: In the 1st form, copy SRCFILE to DSTFILE. cannam@86: In the 2nd and 3rd, copy all SRCFILES to DIRECTORY. cannam@86: In the 4th, create DIRECTORIES. cannam@86: cannam@86: Options: cannam@86: -c (ignored) cannam@86: -d create directories instead of installing files. cannam@86: -g GROUP $chgrpprog installed files to GROUP. cannam@86: -m MODE $chmodprog installed files to MODE. cannam@86: -o USER $chownprog installed files to USER. cannam@86: -s $stripprog installed files. cannam@86: -t DIRECTORY install into DIRECTORY. cannam@86: -T report an error if DSTFILE is a directory. cannam@86: --help display this help and exit. cannam@86: --version display version info and exit. cannam@86: cannam@86: Environment variables override the default commands: cannam@86: CHGRPPROG CHMODPROG CHOWNPROG CPPROG MKDIRPROG MVPROG RMPROG STRIPPROG cannam@86: " cannam@86: cannam@86: while test -n "$1"; do cannam@86: case $1 in cannam@86: -c) shift cannam@86: continue;; cannam@86: cannam@86: -d) dir_arg=true cannam@86: shift cannam@86: continue;; cannam@86: cannam@86: -g) chgrpcmd="$chgrpprog $2" cannam@86: shift cannam@86: shift cannam@86: continue;; cannam@86: cannam@86: --help) echo "$usage"; exit $?;; cannam@86: cannam@86: -m) chmodcmd="$chmodprog $2" cannam@86: shift cannam@86: shift cannam@86: continue;; cannam@86: cannam@86: -o) chowncmd="$chownprog $2" cannam@86: shift cannam@86: shift cannam@86: continue;; cannam@86: cannam@86: -s) stripcmd=$stripprog cannam@86: shift cannam@86: continue;; cannam@86: cannam@86: -t) dstarg=$2 cannam@86: shift cannam@86: shift cannam@86: continue;; cannam@86: cannam@86: -T) no_target_directory=true cannam@86: shift cannam@86: continue;; cannam@86: cannam@86: --version) echo "$0 $scriptversion"; exit $?;; cannam@86: cannam@86: *) # When -d is used, all remaining arguments are directories to create. cannam@86: # When -t is used, the destination is already specified. cannam@86: test -n "$dir_arg$dstarg" && break cannam@86: # Otherwise, the last argument is the destination. Remove it from $@. cannam@86: for arg cannam@86: do cannam@86: if test -n "$dstarg"; then cannam@86: # $@ is not empty: it contains at least $arg. cannam@86: set fnord "$@" "$dstarg" cannam@86: shift # fnord cannam@86: fi cannam@86: shift # arg cannam@86: dstarg=$arg cannam@86: done cannam@86: break;; cannam@86: esac cannam@86: done cannam@86: cannam@86: if test -z "$1"; then cannam@86: if test -z "$dir_arg"; then cannam@86: echo "$0: no input file specified." >&2 cannam@86: exit 1 cannam@86: fi cannam@86: # It's OK to call `install-sh -d' without argument. cannam@86: # This can happen when creating conditional directories. cannam@86: exit 0 cannam@86: fi cannam@86: cannam@86: for src cannam@86: do cannam@86: # Protect names starting with `-'. cannam@86: case $src in cannam@86: -*) src=./$src ;; cannam@86: esac cannam@86: cannam@86: if test -n "$dir_arg"; then cannam@86: dst=$src cannam@86: src= cannam@86: cannam@86: if test -d "$dst"; then cannam@86: mkdircmd=: cannam@86: chmodcmd= cannam@86: else cannam@86: mkdircmd=$mkdirprog cannam@86: fi cannam@86: else cannam@86: # Waiting for this to be detected by the "$cpprog $src $dsttmp" command cannam@86: # might cause directories to be created, which would be especially bad cannam@86: # if $src (and thus $dsttmp) contains '*'. cannam@86: if test ! -f "$src" && test ! -d "$src"; then cannam@86: echo "$0: $src does not exist." >&2 cannam@86: exit 1 cannam@86: fi cannam@86: cannam@86: if test -z "$dstarg"; then cannam@86: echo "$0: no destination specified." >&2 cannam@86: exit 1 cannam@86: fi cannam@86: cannam@86: dst=$dstarg cannam@86: # Protect names starting with `-'. cannam@86: case $dst in cannam@86: -*) dst=./$dst ;; cannam@86: esac cannam@86: cannam@86: # If destination is a directory, append the input filename; won't work cannam@86: # if double slashes aren't ignored. cannam@86: if test -d "$dst"; then cannam@86: if test -n "$no_target_directory"; then cannam@86: echo "$0: $dstarg: Is a directory" >&2 cannam@86: exit 1 cannam@86: fi cannam@86: dst=$dst/`basename "$src"` cannam@86: fi cannam@86: fi cannam@86: cannam@86: # This sed command emulates the dirname command. cannam@86: dstdir=`echo "$dst" | sed -e 's,/*$,,;s,[^/]*$,,;s,/*$,,;s,^$,.,'` cannam@86: cannam@86: # Make sure that the destination directory exists. cannam@86: cannam@86: # Skip lots of stat calls in the usual case. cannam@86: if test ! -d "$dstdir"; then cannam@86: defaultIFS=' cannam@86: ' cannam@86: IFS="${IFS-$defaultIFS}" cannam@86: cannam@86: oIFS=$IFS cannam@86: # Some sh's can't handle IFS=/ for some reason. cannam@86: IFS='%' cannam@86: set x `echo "$dstdir" | sed -e 's@/@%@g' -e 's@^%@/@'` cannam@86: shift cannam@86: IFS=$oIFS cannam@86: cannam@86: pathcomp= cannam@86: cannam@86: while test $# -ne 0 ; do cannam@86: pathcomp=$pathcomp$1 cannam@86: shift cannam@86: if test ! -d "$pathcomp"; then cannam@86: $mkdirprog "$pathcomp" cannam@86: # mkdir can fail with a `File exist' error in case several cannam@86: # install-sh are creating the directory concurrently. This cannam@86: # is OK. cannam@86: test -d "$pathcomp" || exit cannam@86: fi cannam@86: pathcomp=$pathcomp/ cannam@86: done cannam@86: fi cannam@86: cannam@86: if test -n "$dir_arg"; then cannam@86: $doit $mkdircmd "$dst" \ cannam@86: && { test -z "$chowncmd" || $doit $chowncmd "$dst"; } \ cannam@86: && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } \ cannam@86: && { test -z "$stripcmd" || $doit $stripcmd "$dst"; } \ cannam@86: && { test -z "$chmodcmd" || $doit $chmodcmd "$dst"; } cannam@86: cannam@86: else cannam@86: dstfile=`basename "$dst"` cannam@86: cannam@86: # Make a couple of temp file names in the proper directory. cannam@86: dsttmp=$dstdir/_inst.$$_ cannam@86: rmtmp=$dstdir/_rm.$$_ cannam@86: cannam@86: # Trap to clean up those temp files at exit. cannam@86: trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0 cannam@86: trap '(exit $?); exit' 1 2 13 15 cannam@86: cannam@86: # Copy the file name to the temp name. cannam@86: $doit $cpprog "$src" "$dsttmp" && cannam@86: cannam@86: # and set any options; do chmod last to preserve setuid bits. cannam@86: # cannam@86: # If any of these fail, we abort the whole thing. If we want to cannam@86: # ignore errors from any of these, just make sure not to ignore cannam@86: # errors from the above "$doit $cpprog $src $dsttmp" command. cannam@86: # cannam@86: { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } \ cannam@86: && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } \ cannam@86: && { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } \ cannam@86: && { test -z "$chmodcmd" || $doit $chmodcmd "$dsttmp"; } && cannam@86: cannam@86: # Now rename the file to the real destination. cannam@86: { $doit $mvcmd -f "$dsttmp" "$dstdir/$dstfile" 2>/dev/null \ cannam@86: || { cannam@86: # The rename failed, perhaps because mv can't rename something else cannam@86: # to itself, or perhaps because mv is so ancient that it does not cannam@86: # support -f. cannam@86: cannam@86: # Now remove or move aside any old file at destination location. cannam@86: # We try this two ways since rm can't unlink itself on some cannam@86: # systems and the destination file might be busy for other cannam@86: # reasons. In this case, the final cleanup might fail but the new cannam@86: # file should still install successfully. cannam@86: { cannam@86: if test -f "$dstdir/$dstfile"; then cannam@86: $doit $rmcmd -f "$dstdir/$dstfile" 2>/dev/null \ cannam@86: || $doit $mvcmd -f "$dstdir/$dstfile" "$rmtmp" 2>/dev/null \ cannam@86: || { cannam@86: echo "$0: cannot unlink or rename $dstdir/$dstfile" >&2 cannam@86: (exit 1); exit 1 cannam@86: } cannam@86: else cannam@86: : cannam@86: fi cannam@86: } && cannam@86: cannam@86: # Now rename the file to the real destination. cannam@86: $doit $mvcmd "$dsttmp" "$dstdir/$dstfile" cannam@86: } cannam@86: } cannam@86: fi || { (exit 1); exit 1; } cannam@86: done cannam@86: cannam@86: # The final little trick to "correctly" pass the exit status to the exit trap. cannam@86: { cannam@86: (exit 0); exit 0 cannam@86: } cannam@86: cannam@86: # Local variables: cannam@86: # eval: (add-hook 'write-file-hooks 'time-stamp) cannam@86: # time-stamp-start: "scriptversion=" cannam@86: # time-stamp-format: "%:y-%02m-%02d.%02H" cannam@86: # time-stamp-end: "$" cannam@86: # End: