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