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