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