cannam@86: #!/bin/sh cannam@86: # cannam@86: # install - install a program, script, or datafile 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: 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: 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: transformbasename="" cannam@86: transform_arg="" cannam@86: instcmd="$mvprog" 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: cannam@86: while [ x"$1" != x ]; do cannam@86: case $1 in cannam@86: -c) instcmd=$cpprog cannam@86: shift cannam@86: continue;; cannam@86: cannam@86: -d) dir_arg=true cannam@86: shift cannam@86: continue;; 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: -g) chgrpcmd="$chgrpprog $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=*) transformarg=`echo $1 | sed 's/-t=//'` cannam@86: shift cannam@86: continue;; cannam@86: cannam@86: -b=*) transformbasename=`echo $1 | sed 's/-b=//'` cannam@86: shift cannam@86: continue;; cannam@86: cannam@86: *) if [ x"$src" = x ] cannam@86: then cannam@86: src=$1 cannam@86: else cannam@86: # this colon is to work around a 386BSD /bin/sh bug cannam@86: : cannam@86: dst=$1 cannam@86: fi cannam@86: shift cannam@86: continue;; cannam@86: esac cannam@86: done cannam@86: cannam@86: if [ x"$src" = x ] cannam@86: then cannam@86: echo "$0: no input file specified" >&2 cannam@86: exit 1 cannam@86: else cannam@86: : cannam@86: fi cannam@86: cannam@86: if [ x"$dir_arg" != x ]; then cannam@86: dst=$src cannam@86: src="" cannam@86: cannam@86: if [ -d "$dst" ]; then cannam@86: instcmd=: cannam@86: chmodcmd="" cannam@86: else cannam@86: instcmd=$mkdirprog cannam@86: fi cannam@86: else cannam@86: cannam@86: # Waiting for this to be detected by the "$instcmd $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: cannam@86: if [ -f "$src" ] || [ -d "$src" ] cannam@86: then cannam@86: : cannam@86: else cannam@86: echo "$0: $src does not exist" >&2 cannam@86: exit 1 cannam@86: fi cannam@86: cannam@86: if [ x"$dst" = x ] cannam@86: then cannam@86: echo "$0: no destination specified" >&2 cannam@86: exit 1 cannam@86: else cannam@86: : cannam@86: fi cannam@86: cannam@86: # If destination is a directory, append the input filename; if your system cannam@86: # does not like double slashes in filenames, you may need to add some logic cannam@86: cannam@86: if [ -d "$dst" ] cannam@86: then cannam@86: dst=$dst/`basename "$src"` cannam@86: else cannam@86: : 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,^$,.,'` cannam@86: cannam@86: # Make sure that the destination directory exists. cannam@86: # this part is taken from Noah Friedman's mkinstalldirs script cannam@86: cannam@86: # Skip lots of stat calls in the usual case. cannam@86: if [ ! -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 - `echo "$dstdir" | sed -e 's@/@%@g' -e 's@^%@/@'` cannam@86: IFS=$oIFS cannam@86: cannam@86: pathcomp='' cannam@86: cannam@86: while [ $# -ne 0 ] ; do cannam@86: pathcomp=$pathcomp$1 cannam@86: shift cannam@86: cannam@86: if [ ! -d "$pathcomp" ] ; cannam@86: then cannam@86: $mkdirprog "$pathcomp" cannam@86: else cannam@86: : cannam@86: fi cannam@86: cannam@86: pathcomp=$pathcomp/ cannam@86: done cannam@86: fi cannam@86: cannam@86: if [ x"$dir_arg" != x ] cannam@86: then cannam@86: $doit $instcmd "$dst" && cannam@86: cannam@86: if [ x"$chowncmd" != x ]; then $doit $chowncmd "$dst"; else : ; fi && cannam@86: if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd "$dst"; else : ; fi && cannam@86: if [ x"$stripcmd" != x ]; then $doit $stripcmd "$dst"; else : ; fi && cannam@86: if [ x"$chmodcmd" != x ]; then $doit $chmodcmd "$dst"; else : ; fi cannam@86: else cannam@86: cannam@86: # If we're going to rename the final executable, determine the name now. cannam@86: cannam@86: if [ x"$transformarg" = x ] cannam@86: then cannam@86: dstfile=`basename "$dst"` cannam@86: else cannam@86: dstfile=`basename "$dst" $transformbasename | cannam@86: sed $transformarg`$transformbasename cannam@86: fi cannam@86: cannam@86: # don't allow the sed command to completely eliminate the filename cannam@86: cannam@86: if [ x"$dstfile" = x ] cannam@86: then cannam@86: dstfile=`basename "$dst"` cannam@86: else cannam@86: : cannam@86: fi cannam@86: cannam@86: # Make a couple of temp file names in the proper directory. cannam@86: cannam@86: dsttmp=$dstdir/_inst.$$_ cannam@86: rmtmp=$dstdir/_rm.$$_ cannam@86: cannam@86: # Trap to clean up temp files at exit. cannam@86: cannam@86: trap 'status=$?; rm -f "$dsttmp" "$rmtmp" && exit $status' 0 cannam@86: trap '(exit $?); exit' 1 2 13 15 cannam@86: cannam@86: # Move or copy the file name to the temp name cannam@86: cannam@86: $doit $instcmd "$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 $instcmd $src $dsttmp" command. cannam@86: cannam@86: if [ x"$chowncmd" != x ]; then $doit $chowncmd "$dsttmp"; else :;fi && cannam@86: if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd "$dsttmp"; else :;fi && cannam@86: if [ x"$stripcmd" != x ]; then $doit $stripcmd "$dsttmp"; else :;fi && cannam@86: if [ x"$chmodcmd" != x ]; then $doit $chmodcmd "$dsttmp"; else :;fi && cannam@86: cannam@86: # Now remove or move aside any old file at destination location. We try this cannam@86: # two ways since rm can't unlink itself on some systems and the destination cannam@86: # file might be busy for other reasons. In this case, the final cleanup cannam@86: # might fail but the new file should still install successfully. cannam@86: cannam@86: { cannam@86: if [ -f "$dstdir/$dstfile" ] cannam@86: 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 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: cannam@86: $doit $mvcmd "$dsttmp" "$dstdir/$dstfile" cannam@86: cannam@86: fi && cannam@86: cannam@86: # The final little trick to "correctly" pass the exit status to the exit trap. cannam@86: cannam@86: { cannam@86: (exit 0); exit cannam@86: }