Blame install-sh

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