2N/A#!/bin/sh
2N/A# install - install a program, script, or datafile
2N/A
2N/Ascriptversion=2006-10-14.15
2N/A
2N/A# This originates from X11R5 (mit/util/scripts/install.sh), which was
2N/A# later released in X11R6 (xc/config/util/install.sh) with the
2N/A# following copyright and license.
2N/A#
2N/A# Copyright (C) 1994 X Consortium
2N/A#
2N/A# Permission is hereby granted, free of charge, to any person obtaining a copy
2N/A# of this software and associated documentation files (the "Software"), to
2N/A# deal in the Software without restriction, including without limitation the
2N/A# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
2N/A# sell copies of the Software, and to permit persons to whom the Software is
2N/A# furnished to do so, subject to the following conditions:
2N/A#
2N/A# The above copyright notice and this permission notice shall be included in
2N/A# all copies or substantial portions of the Software.
2N/A#
2N/A# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2N/A# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2N/A# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
2N/A# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
2N/A# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
2N/A# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2N/A#
2N/A# Except as contained in this notice, the name of the X Consortium shall not
2N/A# be used in advertising or otherwise to promote the sale, use or other deal-
2N/A# ings in this Software without prior written authorization from the X Consor-
2N/A# tium.
2N/A#
2N/A#
2N/A# FSF changes to this file are in the public domain.
2N/A#
2N/A# Calling this script install-sh is preferred over install.sh, to prevent
2N/A# `make' implicit rules from creating a file called install from it
2N/A# when there is no Makefile.
2N/A#
2N/A# This script is compatible with the BSD install script, but was written
2N/A# from scratch.
2N/A
2N/Anl='
2N/A'
2N/AIFS=" "" $nl"
2N/A
2N/A# set DOITPROG to echo to test this script
2N/A
2N/A# Don't use :- since 4.3BSD and earlier shells don't like it.
2N/Adoit="${DOITPROG-}"
2N/Aif test -z "$doit"; then
2N/A doit_exec=exec
2N/Aelse
2N/A doit_exec=$doit
2N/Afi
2N/A
2N/A# Put in absolute file names if you don't have them in your path;
2N/A# or use environment vars.
2N/A
2N/Amvprog="${MVPROG-mv}"
2N/Acpprog="${CPPROG-cp}"
2N/Achmodprog="${CHMODPROG-chmod}"
2N/Achownprog="${CHOWNPROG-chown}"
2N/Achgrpprog="${CHGRPPROG-chgrp}"
2N/Astripprog="${STRIPPROG-strip}"
2N/Armprog="${RMPROG-rm}"
2N/Amkdirprog="${MKDIRPROG-mkdir}"
2N/A
2N/Aposix_glob=
2N/Aposix_mkdir=
2N/A
2N/A# Desired mode of installed file.
2N/Amode=0755
2N/A
2N/Achmodcmd=$chmodprog
2N/Achowncmd=
2N/Achgrpcmd=
2N/Astripcmd=
2N/Armcmd="$rmprog -f"
2N/Amvcmd="$mvprog"
2N/Asrc=
2N/Adst=
2N/Adir_arg=
2N/Adstarg=
2N/Ano_target_directory=
2N/A
2N/Ausage="Usage: $0 [OPTION]... [-T] SRCFILE DSTFILE
2N/A or: $0 [OPTION]... SRCFILES... DIRECTORY
2N/A or: $0 [OPTION]... -t DIRECTORY SRCFILES...
2N/A or: $0 [OPTION]... -d DIRECTORIES...
2N/A
2N/AIn the 1st form, copy SRCFILE to DSTFILE.
2N/AIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
2N/AIn the 4th, create DIRECTORIES.
2N/A
2N/AOptions:
2N/A-c (ignored)
2N/A-d create directories instead of installing files.
2N/A-g GROUP $chgrpprog installed files to GROUP.
2N/A-m MODE $chmodprog installed files to MODE.
2N/A-o USER $chownprog installed files to USER.
2N/A-s $stripprog installed files.
2N/A-t DIRECTORY install into DIRECTORY.
2N/A-T report an error if DSTFILE is a directory.
2N/A--help display this help and exit.
2N/A--version display version info and exit.
2N/A
2N/AEnvironment variables override the default commands:
2N/A CHGRPPROG CHMODPROG CHOWNPROG CPPROG MKDIRPROG MVPROG RMPROG STRIPPROG
2N/A"
2N/A
2N/Awhile test $# -ne 0; do
2N/A case $1 in
2N/A -c) shift
2N/A continue;;
2N/A
2N/A -d) dir_arg=true
2N/A shift
2N/A continue;;
2N/A
2N/A -g) chgrpcmd="$chgrpprog $2"
2N/A shift
2N/A shift
2N/A continue;;
2N/A
2N/A --help) echo "$usage"; exit $?;;
2N/A
2N/A -m) mode=$2
2N/A shift
2N/A shift
2N/A case $mode in
2N/A *' '* | *' '* | *'
2N/A'* | *'*'* | *'?'* | *'['*)
2N/A echo "$0: invalid mode: $mode" >&2
2N/A exit 1;;
2N/A esac
2N/A continue;;
2N/A
2N/A -o) chowncmd="$chownprog $2"
2N/A shift
2N/A shift
2N/A continue;;
2N/A
2N/A -s) stripcmd=$stripprog
2N/A shift
2N/A continue;;
2N/A
2N/A -t) dstarg=$2
2N/A shift
2N/A shift
2N/A continue;;
2N/A
2N/A -T) no_target_directory=true
2N/A shift
2N/A continue;;
2N/A
2N/A --version) echo "$0 $scriptversion"; exit $?;;
2N/A
2N/A --) shift
2N/A break;;
2N/A
2N/A -*) echo "$0: invalid option: $1" >&2
2N/A exit 1;;
2N/A
2N/A *) break;;
2N/A esac
2N/Adone
2N/A
2N/Aif test $# -ne 0 && test -z "$dir_arg$dstarg"; then
2N/A # When -d is used, all remaining arguments are directories to create.
2N/A # When -t is used, the destination is already specified.
2N/A # Otherwise, the last argument is the destination. Remove it from $@.
2N/A for arg
2N/A do
2N/A if test -n "$dstarg"; then
2N/A # $@ is not empty: it contains at least $arg.
2N/A set fnord "$@" "$dstarg"
2N/A shift # fnord
2N/A fi
2N/A shift # arg
2N/A dstarg=$arg
2N/A done
2N/Afi
2N/A
2N/Aif test $# -eq 0; then
2N/A if test -z "$dir_arg"; then
2N/A echo "$0: no input file specified." >&2
2N/A exit 1
2N/A fi
2N/A # It's OK to call `install-sh -d' without argument.
2N/A # This can happen when creating conditional directories.
2N/A exit 0
2N/Afi
2N/A
2N/Aif test -z "$dir_arg"; then
2N/A trap '(exit $?); exit' 1 2 13 15
2N/A
2N/A # Set umask so as not to create temps with too-generous modes.
2N/A # However, 'strip' requires both read and write access to temps.
2N/A case $mode in
2N/A # Optimize common cases.
2N/A *644) cp_umask=133;;
2N/A *755) cp_umask=22;;
2N/A
2N/A *[0-7])
2N/A if test -z "$stripcmd"; then
2N/A u_plus_rw=
2N/A else
2N/A u_plus_rw='% 200'
2N/A fi
2N/A cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2N/A *)
2N/A if test -z "$stripcmd"; then
2N/A u_plus_rw=
2N/A else
2N/A u_plus_rw=,u+rw
2N/A fi
2N/A cp_umask=$mode$u_plus_rw;;
2N/A esac
2N/Afi
2N/A
2N/Afor src
2N/Ado
2N/A # Protect names starting with `-'.
2N/A case $src in
2N/A -*) src=./$src ;;
2N/A esac
2N/A
2N/A if test -n "$dir_arg"; then
2N/A dst=$src
2N/A dstdir=$dst
2N/A test -d "$dstdir"
2N/A dstdir_status=$?
2N/A else
2N/A
2N/A # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
2N/A # might cause directories to be created, which would be especially bad
2N/A # if $src (and thus $dsttmp) contains '*'.
2N/A if test ! -f "$src" && test ! -d "$src"; then
2N/A echo "$0: $src does not exist." >&2
2N/A exit 1
2N/A fi
2N/A
2N/A if test -z "$dstarg"; then
2N/A echo "$0: no destination specified." >&2
2N/A exit 1
2N/A fi
2N/A
2N/A dst=$dstarg
2N/A # Protect names starting with `-'.
2N/A case $dst in
2N/A -*) dst=./$dst ;;
2N/A esac
2N/A
2N/A # If destination is a directory, append the input filename; won't work
2N/A # if double slashes aren't ignored.
2N/A if test -d "$dst"; then
2N/A if test -n "$no_target_directory"; then
2N/A echo "$0: $dstarg: Is a directory" >&2
2N/A exit 1
2N/A fi
2N/A dstdir=$dst
2N/A dst=$dstdir/`basename "$src"`
2N/A dstdir_status=0
2N/A else
2N/A # Prefer dirname, but fall back on a substitute if dirname fails.
2N/A dstdir=`
2N/A (dirname "$dst") 2>/dev/null ||
2N/A expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
2N/A X"$dst" : 'X\(//\)[^/]' \| \
2N/A X"$dst" : 'X\(//\)$' \| \
2N/A X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
2N/A echo X"$dst" |
2N/A sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
2N/A s//\1/
2N/A q
2N/A }
2N/A /^X\(\/\/\)[^/].*/{
2N/A s//\1/
2N/A q
2N/A }
2N/A /^X\(\/\/\)$/{
2N/A s//\1/
2N/A q
2N/A }
2N/A /^X\(\/\).*/{
2N/A s//\1/
2N/A q
2N/A }
2N/A s/.*/./; q'
2N/A `
2N/A
2N/A test -d "$dstdir"
2N/A dstdir_status=$?
2N/A fi
2N/A fi
2N/A
2N/A obsolete_mkdir_used=false
2N/A
2N/A if test $dstdir_status != 0; then
2N/A case $posix_mkdir in
2N/A '')
2N/A # Create intermediate dirs using mode 755 as modified by the umask.
2N/A # This is like FreeBSD 'install' as of 1997-10-28.
2N/A umask=`umask`
2N/A case $stripcmd.$umask in
2N/A # Optimize common cases.
2N/A *[2367][2367]) mkdir_umask=$umask;;
2N/A .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
2N/A
2N/A *[0-7])
2N/A mkdir_umask=`expr $umask + 22 \
2N/A - $umask % 100 % 40 + $umask % 20 \
2N/A - $umask % 10 % 4 + $umask % 2
2N/A `;;
2N/A *) mkdir_umask=$umask,go-w;;
2N/A esac
2N/A
2N/A # With -d, create the new directory with the user-specified mode.
2N/A # Otherwise, rely on $mkdir_umask.
2N/A if test -n "$dir_arg"; then
2N/A mkdir_mode=-m$mode
2N/A else
2N/A mkdir_mode=
2N/A fi
2N/A
2N/A posix_mkdir=false
2N/A case $umask in
2N/A *[123567][0-7][0-7])
2N/A # POSIX mkdir -p sets u+wx bits regardless of umask, which
2N/A # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
2N/A ;;
2N/A *)
2N/A tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
2N/A trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
2N/A
2N/A if (umask $mkdir_umask &&
2N/A exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
2N/A then
2N/A if test -z "$dir_arg" || {
2N/A # Check for POSIX incompatibilities with -m.
2N/A # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
2N/A # other-writeable bit of parent directory when it shouldn't.
2N/A # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
2N/A ls_ld_tmpdir=`ls -ld "$tmpdir"`
2N/A case $ls_ld_tmpdir in
2N/A d????-?r-*) different_mode=700;;
2N/A d????-?--*) different_mode=755;;
2N/A *) false;;
2N/A esac &&
2N/A $mkdirprog -m$different_mode -p -- "$tmpdir" && {
2N/A ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
2N/A test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
2N/A }
2N/A }
2N/A then posix_mkdir=:
2N/A fi
2N/A rmdir "$tmpdir/d" "$tmpdir"
2N/A else
2N/A # Remove any dirs left behind by ancient mkdir implementations.
2N/A rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
2N/A fi
2N/A trap '' 0;;
2N/A esac;;
2N/A esac
2N/A
2N/A if
2N/A $posix_mkdir && (
2N/A umask $mkdir_umask &&
2N/A $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
2N/A )
2N/A then :
2N/A else
2N/A
2N/A # The umask is ridiculous, or mkdir does not conform to POSIX,
2N/A # or it failed possibly due to a race condition. Create the
2N/A # directory the slow way, step by step, checking for races as we go.
2N/A
2N/A case $dstdir in
2N/A /*) prefix=/ ;;
2N/A -*) prefix=./ ;;
2N/A *) prefix= ;;
2N/A esac
2N/A
2N/A case $posix_glob in
2N/A '')
2N/A if (set -f) 2>/dev/null; then
2N/A posix_glob=true
2N/A else
2N/A posix_glob=false
2N/A fi ;;
2N/A esac
2N/A
2N/A oIFS=$IFS
2N/A IFS=/
2N/A $posix_glob && set -f
2N/A set fnord $dstdir
2N/A shift
2N/A $posix_glob && set +f
2N/A IFS=$oIFS
2N/A
2N/A prefixes=
2N/A
2N/A for d
2N/A do
2N/A test -z "$d" && continue
2N/A
2N/A prefix=$prefix$d
2N/A if test -d "$prefix"; then
2N/A prefixes=
2N/A else
2N/A if $posix_mkdir; then
2N/A (umask=$mkdir_umask &&
2N/A $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
2N/A # Don't fail if two instances are running concurrently.
2N/A test -d "$prefix" || exit 1
2N/A else
2N/A case $prefix in
2N/A *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
2N/A *) qprefix=$prefix;;
2N/A esac
2N/A prefixes="$prefixes '$qprefix'"
2N/A fi
2N/A fi
2N/A prefix=$prefix/
2N/A done
2N/A
2N/A if test -n "$prefixes"; then
2N/A # Don't fail if two instances are running concurrently.
2N/A (umask $mkdir_umask &&
2N/A eval "\$doit_exec \$mkdirprog $prefixes") ||
2N/A test -d "$dstdir" || exit 1
2N/A obsolete_mkdir_used=true
2N/A fi
2N/A fi
2N/A fi
2N/A
2N/A if test -n "$dir_arg"; then
2N/A { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
2N/A { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
2N/A { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
2N/A test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
2N/A else
2N/A
2N/A # Make a couple of temp file names in the proper directory.
2N/A dsttmp=$dstdir/_inst.$$_
2N/A rmtmp=$dstdir/_rm.$$_
2N/A
2N/A # Trap to clean up those temp files at exit.
2N/A trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
2N/A
2N/A # Copy the file name to the temp name.
2N/A (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
2N/A
2N/A # and set any options; do chmod last to preserve setuid bits.
2N/A #
2N/A # If any of these fail, we abort the whole thing. If we want to
2N/A # ignore errors from any of these, just make sure not to ignore
2N/A # errors from the above "$doit $cpprog $src $dsttmp" command.
2N/A #
2N/A { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } \
2N/A && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } \
2N/A && { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } \
2N/A && { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
2N/A
2N/A # Now rename the file to the real destination.
2N/A { $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null \
2N/A || {
2N/A # The rename failed, perhaps because mv can't rename something else
2N/A # to itself, or perhaps because mv is so ancient that it does not
2N/A # support -f.
2N/A
2N/A # Now remove or move aside any old file at destination location.
2N/A # We try this two ways since rm can't unlink itself on some
2N/A # systems and the destination file might be busy for other
2N/A # reasons. In this case, the final cleanup might fail but the new
2N/A # file should still install successfully.
2N/A {
2N/A if test -f "$dst"; then
2N/A $doit $rmcmd -f "$dst" 2>/dev/null \
2N/A || { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null \
2N/A && { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }; }\
2N/A || {
2N/A echo "$0: cannot unlink or rename $dst" >&2
2N/A (exit 1); exit 1
2N/A }
2N/A else
2N/A :
2N/A fi
2N/A } &&
2N/A
2N/A # Now rename the file to the real destination.
2N/A $doit $mvcmd "$dsttmp" "$dst"
2N/A }
2N/A } || exit 1
2N/A
2N/A trap '' 0
2N/A fi
2N/Adone
2N/A
2N/A# Local variables:
2N/A# eval: (add-hook 'write-file-hooks 'time-stamp)
2N/A# time-stamp-start: "scriptversion="
2N/A# time-stamp-format: "%:y-%02m-%02d.%02H"
2N/A# time-stamp-end: "$"
2N/A# End: