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