Blame config/install-sh

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