Blame install-sh

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