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