cannam@85: #!/bin/sh cannam@85: # cannam@85: # install - install a program, script, or datafile cannam@85: # This comes from X11R5 (mit/util/scripts/install.sh). cannam@85: # cannam@85: # Copyright 1991 by the Massachusetts Institute of Technology cannam@85: # cannam@85: # Permission to use, copy, modify, distribute, and sell this software and its cannam@85: # documentation for any purpose is hereby granted without fee, provided that cannam@85: # the above copyright notice appear in all copies and that both that cannam@85: # copyright notice and this permission notice appear in supporting cannam@85: # documentation, and that the name of M.I.T. not be used in advertising or cannam@85: # publicity pertaining to distribution of the software without specific, cannam@85: # written prior permission. M.I.T. makes no representations about the cannam@85: # suitability of this software for any purpose. It is provided "as is" cannam@85: # without express or implied warranty. cannam@85: # cannam@85: # Calling this script install-sh is preferred over install.sh, to prevent cannam@85: # `make' implicit rules from creating a file called install from it cannam@85: # when there is no Makefile. cannam@85: # cannam@85: # This script is compatible with the BSD install script, but was written cannam@85: # from scratch. It can only install one file at a time, a restriction cannam@85: # shared with many OS's install programs. cannam@85: cannam@85: cannam@85: # set DOITPROG to echo to test this script cannam@85: cannam@85: # Don't use :- since 4.3BSD and earlier shells don't like it. cannam@85: doit="${DOITPROG-}" cannam@85: cannam@85: cannam@85: # put in absolute paths if you don't have them in your path; or use env. vars. cannam@85: cannam@85: mvprog="${MVPROG-mv}" cannam@85: cpprog="${CPPROG-cp}" cannam@85: chmodprog="${CHMODPROG-chmod}" cannam@85: chownprog="${CHOWNPROG-chown}" cannam@85: chgrpprog="${CHGRPPROG-chgrp}" cannam@85: stripprog="${STRIPPROG-strip}" cannam@85: rmprog="${RMPROG-rm}" cannam@85: mkdirprog="${MKDIRPROG-mkdir}" cannam@85: cannam@85: transformbasename="" cannam@85: transform_arg="" cannam@85: instcmd="$mvprog" cannam@85: chmodcmd="$chmodprog 0755" cannam@85: chowncmd="" cannam@85: chgrpcmd="" cannam@85: stripcmd="" cannam@85: rmcmd="$rmprog -f" cannam@85: mvcmd="$mvprog" cannam@85: src="" cannam@85: dst="" cannam@85: dir_arg="" cannam@85: cannam@85: while [ x"$1" != x ]; do cannam@85: case $1 in cannam@85: -c) instcmd=$cpprog cannam@85: shift cannam@85: continue;; cannam@85: cannam@85: -d) dir_arg=true cannam@85: shift cannam@85: continue;; cannam@85: cannam@85: -m) chmodcmd="$chmodprog $2" cannam@85: shift cannam@85: shift cannam@85: continue;; cannam@85: cannam@85: -o) chowncmd="$chownprog $2" cannam@85: shift cannam@85: shift cannam@85: continue;; cannam@85: cannam@85: -g) chgrpcmd="$chgrpprog $2" cannam@85: shift cannam@85: shift cannam@85: continue;; cannam@85: cannam@85: -s) stripcmd=$stripprog cannam@85: shift cannam@85: continue;; cannam@85: cannam@85: -t=*) transformarg=`echo $1 | sed 's/-t=//'` cannam@85: shift cannam@85: continue;; cannam@85: cannam@85: -b=*) transformbasename=`echo $1 | sed 's/-b=//'` cannam@85: shift cannam@85: continue;; cannam@85: cannam@85: *) if [ x"$src" = x ] cannam@85: then cannam@85: src=$1 cannam@85: else cannam@85: # this colon is to work around a 386BSD /bin/sh bug cannam@85: : cannam@85: dst=$1 cannam@85: fi cannam@85: shift cannam@85: continue;; cannam@85: esac cannam@85: done cannam@85: cannam@85: if [ x"$src" = x ] cannam@85: then cannam@85: echo "$0: no input file specified" >&2 cannam@85: exit 1 cannam@85: else cannam@85: : cannam@85: fi cannam@85: cannam@85: if [ x"$dir_arg" != x ]; then cannam@85: dst=$src cannam@85: src="" cannam@85: cannam@85: if [ -d "$dst" ]; then cannam@85: instcmd=: cannam@85: chmodcmd="" cannam@85: else cannam@85: instcmd=$mkdirprog cannam@85: fi cannam@85: else cannam@85: cannam@85: # Waiting for this to be detected by the "$instcmd $src $dsttmp" command cannam@85: # might cause directories to be created, which would be especially bad cannam@85: # if $src (and thus $dsttmp) contains '*'. cannam@85: cannam@85: if [ -f "$src" ] || [ -d "$src" ] cannam@85: then cannam@85: : cannam@85: else cannam@85: echo "$0: $src does not exist" >&2 cannam@85: exit 1 cannam@85: fi cannam@85: cannam@85: if [ x"$dst" = x ] cannam@85: then cannam@85: echo "$0: no destination specified" >&2 cannam@85: exit 1 cannam@85: else cannam@85: : cannam@85: fi cannam@85: cannam@85: # If destination is a directory, append the input filename; if your system cannam@85: # does not like double slashes in filenames, you may need to add some logic cannam@85: cannam@85: if [ -d "$dst" ] cannam@85: then cannam@85: dst=$dst/`basename "$src"` cannam@85: else cannam@85: : cannam@85: fi cannam@85: fi cannam@85: cannam@85: ## this sed command emulates the dirname command cannam@85: dstdir=`echo "$dst" | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'` cannam@85: cannam@85: # Make sure that the destination directory exists. cannam@85: # this part is taken from Noah Friedman's mkinstalldirs script cannam@85: cannam@85: # Skip lots of stat calls in the usual case. cannam@85: if [ ! -d "$dstdir" ]; then cannam@85: defaultIFS=' cannam@85: ' cannam@85: IFS="${IFS-$defaultIFS}" cannam@85: cannam@85: oIFS=$IFS cannam@85: # Some sh's can't handle IFS=/ for some reason. cannam@85: IFS='%' cannam@85: set - `echo "$dstdir" | sed -e 's@/@%@g' -e 's@^%@/@'` cannam@85: IFS=$oIFS cannam@85: cannam@85: pathcomp='' cannam@85: cannam@85: while [ $# -ne 0 ] ; do cannam@85: pathcomp=$pathcomp$1 cannam@85: shift cannam@85: cannam@85: if [ ! -d "$pathcomp" ] ; cannam@85: then cannam@85: $mkdirprog "$pathcomp" cannam@85: else cannam@85: : cannam@85: fi cannam@85: cannam@85: pathcomp=$pathcomp/ cannam@85: done cannam@85: fi cannam@85: cannam@85: if [ x"$dir_arg" != x ] cannam@85: then cannam@85: $doit $instcmd "$dst" && cannam@85: cannam@85: if [ x"$chowncmd" != x ]; then $doit $chowncmd "$dst"; else : ; fi && cannam@85: if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd "$dst"; else : ; fi && cannam@85: if [ x"$stripcmd" != x ]; then $doit $stripcmd "$dst"; else : ; fi && cannam@85: if [ x"$chmodcmd" != x ]; then $doit $chmodcmd "$dst"; else : ; fi cannam@85: else cannam@85: cannam@85: # If we're going to rename the final executable, determine the name now. cannam@85: cannam@85: if [ x"$transformarg" = x ] cannam@85: then cannam@85: dstfile=`basename "$dst"` cannam@85: else cannam@85: dstfile=`basename "$dst" $transformbasename | cannam@85: sed $transformarg`$transformbasename cannam@85: fi cannam@85: cannam@85: # don't allow the sed command to completely eliminate the filename cannam@85: cannam@85: if [ x"$dstfile" = x ] cannam@85: then cannam@85: dstfile=`basename "$dst"` cannam@85: else cannam@85: : cannam@85: fi cannam@85: cannam@85: # Make a couple of temp file names in the proper directory. cannam@85: cannam@85: dsttmp=$dstdir/#inst.$$# cannam@85: rmtmp=$dstdir/#rm.$$# cannam@85: cannam@85: # Trap to clean up temp files at exit. cannam@85: cannam@85: trap 'status=$?; rm -f "$dsttmp" "$rmtmp" && exit $status' 0 cannam@85: trap '(exit $?); exit' 1 2 13 15 cannam@85: cannam@85: # Move or copy the file name to the temp name cannam@85: cannam@85: $doit $instcmd "$src" "$dsttmp" && cannam@85: cannam@85: # and set any options; do chmod last to preserve setuid bits cannam@85: cannam@85: # If any of these fail, we abort the whole thing. If we want to cannam@85: # ignore errors from any of these, just make sure not to ignore cannam@85: # errors from the above "$doit $instcmd $src $dsttmp" command. cannam@85: cannam@85: if [ x"$chowncmd" != x ]; then $doit $chowncmd "$dsttmp"; else :;fi && cannam@85: if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd "$dsttmp"; else :;fi && cannam@85: if [ x"$stripcmd" != x ]; then $doit $stripcmd "$dsttmp"; else :;fi && cannam@85: if [ x"$chmodcmd" != x ]; then $doit $chmodcmd "$dsttmp"; else :;fi && cannam@85: cannam@85: # Now remove or move aside any old file at destination location. We try this cannam@85: # two ways since rm can't unlink itself on some systems and the destination cannam@85: # file might be busy for other reasons. In this case, the final cleanup cannam@85: # might fail but the new file should still install successfully. cannam@85: cannam@85: { cannam@85: if [ -f "$dstdir/$dstfile" ] cannam@85: then cannam@85: $doit $rmcmd -f "$dstdir/$dstfile" 2>/dev/null || cannam@85: $doit $mvcmd -f "$dstdir/$dstfile" "$rmtmp" 2>/dev/null || cannam@85: { cannam@85: echo "$0: cannot unlink or rename $dstdir/$dstfile" >&2 cannam@85: (exit 1); exit cannam@85: } cannam@85: else cannam@85: : cannam@85: fi cannam@85: } && cannam@85: cannam@85: # Now rename the file to the real destination. cannam@85: cannam@85: $doit $mvcmd "$dsttmp" "$dstdir/$dstfile" cannam@85: cannam@85: fi && cannam@85: cannam@85: # The final little trick to "correctly" pass the exit status to the exit trap. cannam@85: cannam@85: { cannam@85: (exit 0); exit cannam@85: }