Blame install-sh

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