Blame install-sh

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