melunko@917: #!/bin/sh
melunko@917: # install - install a program, script, or datafile
melunko@917: 
melunko@917: scriptversion=2005-05-14.22
melunko@917: 
melunko@917: # This originates from X11R5 (mit/util/scripts/install.sh), which was
melunko@917: # later released in X11R6 (xc/config/util/install.sh) with the
melunko@917: # following copyright and license.
melunko@917: #
melunko@917: # Copyright (C) 1994 X Consortium
melunko@917: #
melunko@917: # Permission is hereby granted, free of charge, to any person obtaining a copy
melunko@917: # of this software and associated documentation files (the "Software"), to
melunko@917: # deal in the Software without restriction, including without limitation the
melunko@917: # rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
melunko@917: # sell copies of the Software, and to permit persons to whom the Software is
melunko@917: # furnished to do so, subject to the following conditions:
melunko@917: #
melunko@917: # The above copyright notice and this permission notice shall be included in
melunko@917: # all copies or substantial portions of the Software.
melunko@917: #
melunko@917: # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
melunko@917: # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
melunko@917: # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
melunko@917: # X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
melunko@917: # AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
melunko@917: # TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
melunko@917: #
melunko@917: # Except as contained in this notice, the name of the X Consortium shall not
melunko@917: # be used in advertising or otherwise to promote the sale, use or other deal-
melunko@917: # ings in this Software without prior written authorization from the X Consor-
melunko@917: # tium.
melunko@917: #
melunko@917: #
melunko@917: # FSF changes to this file are in the public domain.
melunko@917: #
melunko@917: # Calling this script install-sh is preferred over install.sh, to prevent
melunko@917: # `make' implicit rules from creating a file called install from it
melunko@917: # when there is no Makefile.
melunko@917: #
melunko@917: # This script is compatible with the BSD install script, but was written
melunko@917: # from scratch.  It can only install one file at a time, a restriction
melunko@917: # shared with many OS's install programs.
melunko@917: 
melunko@917: # set DOITPROG to echo to test this script
melunko@917: 
melunko@917: # Don't use :- since 4.3BSD and earlier shells don't like it.
melunko@917: doit="${DOITPROG-}"
melunko@917: 
melunko@917: # put in absolute paths if you don't have them in your path; or use env. vars.
melunko@917: 
melunko@917: mvprog="${MVPROG-mv}"
melunko@917: cpprog="${CPPROG-cp}"
melunko@917: chmodprog="${CHMODPROG-chmod}"
melunko@917: chownprog="${CHOWNPROG-chown}"
melunko@917: chgrpprog="${CHGRPPROG-chgrp}"
melunko@917: stripprog="${STRIPPROG-strip}"
melunko@917: rmprog="${RMPROG-rm}"
melunko@917: mkdirprog="${MKDIRPROG-mkdir}"
melunko@917: 
melunko@917: chmodcmd="$chmodprog 0755"
melunko@917: chowncmd=
melunko@917: chgrpcmd=
melunko@917: stripcmd=
melunko@917: rmcmd="$rmprog -f"
melunko@917: mvcmd="$mvprog"
melunko@917: src=
melunko@917: dst=
melunko@917: dir_arg=
melunko@917: dstarg=
melunko@917: no_target_directory=
melunko@917: 
melunko@917: usage="Usage: $0 [OPTION]... [-T] SRCFILE DSTFILE
melunko@917:    or: $0 [OPTION]... SRCFILES... DIRECTORY
melunko@917:    or: $0 [OPTION]... -t DIRECTORY SRCFILES...
melunko@917:    or: $0 [OPTION]... -d DIRECTORIES...
melunko@917: 
melunko@917: In the 1st form, copy SRCFILE to DSTFILE.
melunko@917: In the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
melunko@917: In the 4th, create DIRECTORIES.
melunko@917: 
melunko@917: Options:
melunko@917: -c         (ignored)
melunko@917: -d         create directories instead of installing files.
melunko@917: -g GROUP   $chgrpprog installed files to GROUP.
melunko@917: -m MODE    $chmodprog installed files to MODE.
melunko@917: -o USER    $chownprog installed files to USER.
melunko@917: -s         $stripprog installed files.
melunko@917: -t DIRECTORY  install into DIRECTORY.
melunko@917: -T         report an error if DSTFILE is a directory.
melunko@917: --help     display this help and exit.
melunko@917: --version  display version info and exit.
melunko@917: 
melunko@917: Environment variables override the default commands:
melunko@917:   CHGRPPROG CHMODPROG CHOWNPROG CPPROG MKDIRPROG MVPROG RMPROG STRIPPROG
melunko@917: "
melunko@917: 
melunko@917: while test -n "$1"; do
melunko@917:   case $1 in
melunko@917:     -c) shift
melunko@917:         continue;;
melunko@917: 
melunko@917:     -d) dir_arg=true
melunko@917:         shift
melunko@917:         continue;;
melunko@917: 
melunko@917:     -g) chgrpcmd="$chgrpprog $2"
melunko@917:         shift
melunko@917:         shift
melunko@917:         continue;;
melunko@917: 
melunko@917:     --help) echo "$usage"; exit $?;;
melunko@917: 
melunko@917:     -m) chmodcmd="$chmodprog $2"
melunko@917:         shift
melunko@917:         shift
melunko@917:         continue;;
melunko@917: 
melunko@917:     -o) chowncmd="$chownprog $2"
melunko@917:         shift
melunko@917:         shift
melunko@917:         continue;;
melunko@917: 
melunko@917:     -s) stripcmd=$stripprog
melunko@917:         shift
melunko@917:         continue;;
melunko@917: 
melunko@917:     -t) dstarg=$2
melunko@917: 	shift
melunko@917: 	shift
melunko@917: 	continue;;
melunko@917: 
melunko@917:     -T) no_target_directory=true
melunko@917: 	shift
melunko@917: 	continue;;
melunko@917: 
melunko@917:     --version) echo "$0 $scriptversion"; exit $?;;
melunko@917: 
melunko@917:     *)  # When -d is used, all remaining arguments are directories to create.
melunko@917: 	# When -t is used, the destination is already specified.
melunko@917: 	test -n "$dir_arg$dstarg" && break
melunko@917:         # Otherwise, the last argument is the destination.  Remove it from $@.
melunko@917: 	for arg
melunko@917: 	do
melunko@917:           if test -n "$dstarg"; then
melunko@917: 	    # $@ is not empty: it contains at least $arg.
melunko@917: 	    set fnord "$@" "$dstarg"
melunko@917: 	    shift # fnord
melunko@917: 	  fi
melunko@917: 	  shift # arg
melunko@917: 	  dstarg=$arg
melunko@917: 	done
melunko@917: 	break;;
melunko@917:   esac
melunko@917: done
melunko@917: 
melunko@917: if test -z "$1"; then
melunko@917:   if test -z "$dir_arg"; then
melunko@917:     echo "$0: no input file specified." >&2
melunko@917:     exit 1
melunko@917:   fi
melunko@917:   # It's OK to call `install-sh -d' without argument.
melunko@917:   # This can happen when creating conditional directories.
melunko@917:   exit 0
melunko@917: fi
melunko@917: 
melunko@917: for src
melunko@917: do
melunko@917:   # Protect names starting with `-'.
melunko@917:   case $src in
melunko@917:     -*) src=./$src ;;
melunko@917:   esac
melunko@917: 
melunko@917:   if test -n "$dir_arg"; then
melunko@917:     dst=$src
melunko@917:     src=
melunko@917: 
melunko@917:     if test -d "$dst"; then
melunko@917:       mkdircmd=:
melunko@917:       chmodcmd=
melunko@917:     else
melunko@917:       mkdircmd=$mkdirprog
melunko@917:     fi
melunko@917:   else
melunko@917:     # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
melunko@917:     # might cause directories to be created, which would be especially bad
melunko@917:     # if $src (and thus $dsttmp) contains '*'.
melunko@917:     if test ! -f "$src" && test ! -d "$src"; then
melunko@917:       echo "$0: $src does not exist." >&2
melunko@917:       exit 1
melunko@917:     fi
melunko@917: 
melunko@917:     if test -z "$dstarg"; then
melunko@917:       echo "$0: no destination specified." >&2
melunko@917:       exit 1
melunko@917:     fi
melunko@917: 
melunko@917:     dst=$dstarg
melunko@917:     # Protect names starting with `-'.
melunko@917:     case $dst in
melunko@917:       -*) dst=./$dst ;;
melunko@917:     esac
melunko@917: 
melunko@917:     # If destination is a directory, append the input filename; won't work
melunko@917:     # if double slashes aren't ignored.
melunko@917:     if test -d "$dst"; then
melunko@917:       if test -n "$no_target_directory"; then
melunko@917: 	echo "$0: $dstarg: Is a directory" >&2
melunko@917: 	exit 1
melunko@917:       fi
melunko@917:       dst=$dst/`basename "$src"`
melunko@917:     fi
melunko@917:   fi
melunko@917: 
melunko@917:   # This sed command emulates the dirname command.
melunko@917:   dstdir=`echo "$dst" | sed -e 's,/*$,,;s,[^/]*$,,;s,/*$,,;s,^$,.,'`
melunko@917: 
melunko@917:   # Make sure that the destination directory exists.
melunko@917: 
melunko@917:   # Skip lots of stat calls in the usual case.
melunko@917:   if test ! -d "$dstdir"; then
melunko@917:     defaultIFS='
melunko@917: 	 '
melunko@917:     IFS="${IFS-$defaultIFS}"
melunko@917: 
melunko@917:     oIFS=$IFS
melunko@917:     # Some sh's can't handle IFS=/ for some reason.
melunko@917:     IFS='%'
melunko@917:     set x `echo "$dstdir" | sed -e 's@/@%@g' -e 's@^%@/@'`
melunko@917:     shift
melunko@917:     IFS=$oIFS
melunko@917: 
melunko@917:     pathcomp=
melunko@917: 
melunko@917:     while test $# -ne 0 ; do
melunko@917:       pathcomp=$pathcomp$1
melunko@917:       shift
melunko@917:       if test ! -d "$pathcomp"; then
melunko@917:         $mkdirprog "$pathcomp"
melunko@917: 	# mkdir can fail with a `File exist' error in case several
melunko@917: 	# install-sh are creating the directory concurrently.  This
melunko@917: 	# is OK.
melunko@917: 	test -d "$pathcomp" || exit
melunko@917:       fi
melunko@917:       pathcomp=$pathcomp/
melunko@917:     done
melunko@917:   fi
melunko@917: 
melunko@917:   if test -n "$dir_arg"; then
melunko@917:     $doit $mkdircmd "$dst" \
melunko@917:       && { test -z "$chowncmd" || $doit $chowncmd "$dst"; } \
melunko@917:       && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } \
melunko@917:       && { test -z "$stripcmd" || $doit $stripcmd "$dst"; } \
melunko@917:       && { test -z "$chmodcmd" || $doit $chmodcmd "$dst"; }
melunko@917: 
melunko@917:   else
melunko@917:     dstfile=`basename "$dst"`
melunko@917: 
melunko@917:     # Make a couple of temp file names in the proper directory.
melunko@917:     dsttmp=$dstdir/_inst.$$_
melunko@917:     rmtmp=$dstdir/_rm.$$_
melunko@917: 
melunko@917:     # Trap to clean up those temp files at exit.
melunko@917:     trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
melunko@917:     trap '(exit $?); exit' 1 2 13 15
melunko@917: 
melunko@917:     # Copy the file name to the temp name.
melunko@917:     $doit $cpprog "$src" "$dsttmp" &&
melunko@917: 
melunko@917:     # and set any options; do chmod last to preserve setuid bits.
melunko@917:     #
melunko@917:     # If any of these fail, we abort the whole thing.  If we want to
melunko@917:     # ignore errors from any of these, just make sure not to ignore
melunko@917:     # errors from the above "$doit $cpprog $src $dsttmp" command.
melunko@917:     #
melunko@917:     { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } \
melunko@917:       && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } \
melunko@917:       && { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } \
melunko@917:       && { test -z "$chmodcmd" || $doit $chmodcmd "$dsttmp"; } &&
melunko@917: 
melunko@917:     # Now rename the file to the real destination.
melunko@917:     { $doit $mvcmd -f "$dsttmp" "$dstdir/$dstfile" 2>/dev/null \
melunko@917:       || {
melunko@917: 	   # The rename failed, perhaps because mv can't rename something else
melunko@917: 	   # to itself, or perhaps because mv is so ancient that it does not
melunko@917: 	   # support -f.
melunko@917: 
melunko@917: 	   # Now remove or move aside any old file at destination location.
melunko@917: 	   # We try this two ways since rm can't unlink itself on some
melunko@917: 	   # systems and the destination file might be busy for other
melunko@917: 	   # reasons.  In this case, the final cleanup might fail but the new
melunko@917: 	   # file should still install successfully.
melunko@917: 	   {
melunko@917: 	     if test -f "$dstdir/$dstfile"; then
melunko@917: 	       $doit $rmcmd -f "$dstdir/$dstfile" 2>/dev/null \
melunko@917: 	       || $doit $mvcmd -f "$dstdir/$dstfile" "$rmtmp" 2>/dev/null \
melunko@917: 	       || {
melunko@917: 		 echo "$0: cannot unlink or rename $dstdir/$dstfile" >&2
melunko@917: 		 (exit 1); exit 1
melunko@917: 	       }
melunko@917: 	     else
melunko@917: 	       :
melunko@917: 	     fi
melunko@917: 	   } &&
melunko@917: 
melunko@917: 	   # Now rename the file to the real destination.
melunko@917: 	   $doit $mvcmd "$dsttmp" "$dstdir/$dstfile"
melunko@917: 	 }
melunko@917:     }
melunko@917:   fi || { (exit 1); exit 1; }
melunko@917: done
melunko@917: 
melunko@917: # The final little trick to "correctly" pass the exit status to the exit trap.
melunko@917: {
melunko@917:   (exit 0); exit 0
melunko@917: }
melunko@917: 
melunko@917: # Local variables:
melunko@917: # eval: (add-hook 'write-file-hooks 'time-stamp)
melunko@917: # time-stamp-start: "scriptversion="
melunko@917: # time-stamp-format: "%:y-%02m-%02d.%02H"
melunko@917: # time-stamp-end: "$"
melunko@917: # End: