Chris@1: #!/bin/sh Chris@1: # Chris@1: # install - install a program, script, or datafile Chris@1: # Chris@1: # This originates from X11R5 (mit/util/scripts/install.sh), which was Chris@1: # later released in X11R6 (xc/config/util/install.sh) with the Chris@1: # following copyright and license. Chris@1: # Chris@1: # Copyright (C) 1994 X Consortium Chris@1: # Chris@1: # Permission is hereby granted, free of charge, to any person obtaining a copy Chris@1: # of this software and associated documentation files (the "Software"), to Chris@1: # deal in the Software without restriction, including without limitation the Chris@1: # rights to use, copy, modify, merge, publish, distribute, sublicense, and/or Chris@1: # sell copies of the Software, and to permit persons to whom the Software is Chris@1: # furnished to do so, subject to the following conditions: Chris@1: # Chris@1: # The above copyright notice and this permission notice shall be included in Chris@1: # all copies or substantial portions of the Software. Chris@1: # Chris@1: # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR Chris@1: # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, Chris@1: # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE Chris@1: # X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN Chris@1: # AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC- Chris@1: # TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. Chris@1: # Chris@1: # Except as contained in this notice, the name of the X Consortium shall not Chris@1: # be used in advertising or otherwise to promote the sale, use or other deal- Chris@1: # ings in this Software without prior written authorization from the X Consor- Chris@1: # tium. Chris@1: # Chris@1: # Chris@1: # FSF changes to this file are in the public domain. Chris@1: # Chris@1: # Calling this script install-sh is preferred over install.sh, to prevent Chris@1: # `make' implicit rules from creating a file called install from it Chris@1: # when there is no Makefile. Chris@1: # Chris@1: # This script is compatible with the BSD install script, but was written Chris@1: # from scratch. It can only install one file at a time, a restriction Chris@1: # shared with many OS's install programs. Chris@1: Chris@1: Chris@1: # set DOITPROG to echo to test this script Chris@1: Chris@1: # Don't use :- since 4.3BSD and earlier shells don't like it. Chris@1: doit="${DOITPROG-}" Chris@1: Chris@1: Chris@1: # put in absolute paths if you don't have them in your path; or use env. vars. Chris@1: Chris@1: mvprog="${MVPROG-mv}" Chris@1: cpprog="${CPPROG-cp}" Chris@1: chmodprog="${CHMODPROG-chmod}" Chris@1: chownprog="${CHOWNPROG-chown}" Chris@1: chgrpprog="${CHGRPPROG-chgrp}" Chris@1: stripprog="${STRIPPROG-strip}" Chris@1: rmprog="${RMPROG-rm}" Chris@1: mkdirprog="${MKDIRPROG-mkdir}" Chris@1: Chris@1: transformbasename="" Chris@1: transform_arg="" Chris@1: instcmd="$mvprog" Chris@1: chmodcmd="$chmodprog 0755" Chris@1: chowncmd="" Chris@1: chgrpcmd="" Chris@1: stripcmd="" Chris@1: rmcmd="$rmprog -f" Chris@1: mvcmd="$mvprog" Chris@1: src="" Chris@1: dst="" Chris@1: dir_arg="" Chris@1: Chris@1: while [ x"$1" != x ]; do Chris@1: case $1 in Chris@1: -c) instcmd=$cpprog Chris@1: shift Chris@1: continue;; Chris@1: Chris@1: -d) dir_arg=true Chris@1: shift Chris@1: continue;; Chris@1: Chris@1: -m) chmodcmd="$chmodprog $2" Chris@1: shift Chris@1: shift Chris@1: continue;; Chris@1: Chris@1: -o) chowncmd="$chownprog $2" Chris@1: shift Chris@1: shift Chris@1: continue;; Chris@1: Chris@1: -g) chgrpcmd="$chgrpprog $2" Chris@1: shift Chris@1: shift Chris@1: continue;; Chris@1: Chris@1: -s) stripcmd=$stripprog Chris@1: shift Chris@1: continue;; Chris@1: Chris@1: -t=*) transformarg=`echo $1 | sed 's/-t=//'` Chris@1: shift Chris@1: continue;; Chris@1: Chris@1: -b=*) transformbasename=`echo $1 | sed 's/-b=//'` Chris@1: shift Chris@1: continue;; Chris@1: Chris@1: *) if [ x"$src" = x ] Chris@1: then Chris@1: src=$1 Chris@1: else Chris@1: # this colon is to work around a 386BSD /bin/sh bug Chris@1: : Chris@1: dst=$1 Chris@1: fi Chris@1: shift Chris@1: continue;; Chris@1: esac Chris@1: done Chris@1: Chris@1: if [ x"$src" = x ] Chris@1: then Chris@1: echo "$0: no input file specified" >&2 Chris@1: exit 1 Chris@1: else Chris@1: : Chris@1: fi Chris@1: Chris@1: if [ x"$dir_arg" != x ]; then Chris@1: dst=$src Chris@1: src="" Chris@1: Chris@1: if [ -d "$dst" ]; then Chris@1: instcmd=: Chris@1: chmodcmd="" Chris@1: else Chris@1: instcmd=$mkdirprog Chris@1: fi Chris@1: else Chris@1: Chris@1: # Waiting for this to be detected by the "$instcmd $src $dsttmp" command Chris@1: # might cause directories to be created, which would be especially bad Chris@1: # if $src (and thus $dsttmp) contains '*'. Chris@1: Chris@1: if [ -f "$src" ] || [ -d "$src" ] Chris@1: then Chris@1: : Chris@1: else Chris@1: echo "$0: $src does not exist" >&2 Chris@1: exit 1 Chris@1: fi Chris@1: Chris@1: if [ x"$dst" = x ] Chris@1: then Chris@1: echo "$0: no destination specified" >&2 Chris@1: exit 1 Chris@1: else Chris@1: : Chris@1: fi Chris@1: Chris@1: # If destination is a directory, append the input filename; if your system Chris@1: # does not like double slashes in filenames, you may need to add some logic Chris@1: Chris@1: if [ -d "$dst" ] Chris@1: then Chris@1: dst=$dst/`basename "$src"` Chris@1: else Chris@1: : Chris@1: fi Chris@1: fi Chris@1: Chris@1: ## this sed command emulates the dirname command Chris@1: dstdir=`echo "$dst" | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'` Chris@1: Chris@1: # Make sure that the destination directory exists. Chris@1: # this part is taken from Noah Friedman's mkinstalldirs script Chris@1: Chris@1: # Skip lots of stat calls in the usual case. Chris@1: if [ ! -d "$dstdir" ]; then Chris@1: defaultIFS=' Chris@1: ' Chris@1: IFS="${IFS-$defaultIFS}" Chris@1: Chris@1: oIFS=$IFS Chris@1: # Some sh's can't handle IFS=/ for some reason. Chris@1: IFS='%' Chris@1: set - `echo "$dstdir" | sed -e 's@/@%@g' -e 's@^%@/@'` Chris@1: IFS=$oIFS Chris@1: Chris@1: pathcomp='' Chris@1: Chris@1: while [ $# -ne 0 ] ; do Chris@1: pathcomp=$pathcomp$1 Chris@1: shift Chris@1: Chris@1: if [ ! -d "$pathcomp" ] ; Chris@1: then Chris@1: $mkdirprog "$pathcomp" Chris@1: else Chris@1: : Chris@1: fi Chris@1: Chris@1: pathcomp=$pathcomp/ Chris@1: done Chris@1: fi Chris@1: Chris@1: if [ x"$dir_arg" != x ] Chris@1: then Chris@1: $doit $instcmd "$dst" && Chris@1: Chris@1: if [ x"$chowncmd" != x ]; then $doit $chowncmd "$dst"; else : ; fi && Chris@1: if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd "$dst"; else : ; fi && Chris@1: if [ x"$stripcmd" != x ]; then $doit $stripcmd "$dst"; else : ; fi && Chris@1: if [ x"$chmodcmd" != x ]; then $doit $chmodcmd "$dst"; else : ; fi Chris@1: else Chris@1: Chris@1: # If we're going to rename the final executable, determine the name now. Chris@1: Chris@1: if [ x"$transformarg" = x ] Chris@1: then Chris@1: dstfile=`basename "$dst"` Chris@1: else Chris@1: dstfile=`basename "$dst" $transformbasename | Chris@1: sed $transformarg`$transformbasename Chris@1: fi Chris@1: Chris@1: # don't allow the sed command to completely eliminate the filename Chris@1: Chris@1: if [ x"$dstfile" = x ] Chris@1: then Chris@1: dstfile=`basename "$dst"` Chris@1: else Chris@1: : Chris@1: fi Chris@1: Chris@1: # Make a couple of temp file names in the proper directory. Chris@1: Chris@1: dsttmp=$dstdir/_inst.$$_ Chris@1: rmtmp=$dstdir/_rm.$$_ Chris@1: Chris@1: # Trap to clean up temp files at exit. Chris@1: Chris@1: trap 'status=$?; rm -f "$dsttmp" "$rmtmp" && exit $status' 0 Chris@1: trap '(exit $?); exit' 1 2 13 15 Chris@1: Chris@1: # Move or copy the file name to the temp name Chris@1: Chris@1: $doit $instcmd "$src" "$dsttmp" && Chris@1: Chris@1: # and set any options; do chmod last to preserve setuid bits Chris@1: Chris@1: # If any of these fail, we abort the whole thing. If we want to Chris@1: # ignore errors from any of these, just make sure not to ignore Chris@1: # errors from the above "$doit $instcmd $src $dsttmp" command. Chris@1: Chris@1: if [ x"$chowncmd" != x ]; then $doit $chowncmd "$dsttmp"; else :;fi && Chris@1: if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd "$dsttmp"; else :;fi && Chris@1: if [ x"$stripcmd" != x ]; then $doit $stripcmd "$dsttmp"; else :;fi && Chris@1: if [ x"$chmodcmd" != x ]; then $doit $chmodcmd "$dsttmp"; else :;fi && Chris@1: Chris@1: # Now remove or move aside any old file at destination location. We try this Chris@1: # two ways since rm can't unlink itself on some systems and the destination Chris@1: # file might be busy for other reasons. In this case, the final cleanup Chris@1: # might fail but the new file should still install successfully. Chris@1: Chris@1: { Chris@1: if [ -f "$dstdir/$dstfile" ] Chris@1: then Chris@1: $doit $rmcmd -f "$dstdir/$dstfile" 2>/dev/null || Chris@1: $doit $mvcmd -f "$dstdir/$dstfile" "$rmtmp" 2>/dev/null || Chris@1: { Chris@1: echo "$0: cannot unlink or rename $dstdir/$dstfile" >&2 Chris@1: (exit 1); exit Chris@1: } Chris@1: else Chris@1: : Chris@1: fi Chris@1: } && Chris@1: Chris@1: # Now rename the file to the real destination. Chris@1: Chris@1: $doit $mvcmd "$dsttmp" "$dstdir/$dstfile" Chris@1: Chris@1: fi && Chris@1: Chris@1: # The final little trick to "correctly" pass the exit status to the exit trap. Chris@1: Chris@1: { Chris@1: (exit 0); exit Chris@1: }