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