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