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