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