4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews# install - install a program, script, or datafile
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews# This originates from X11R5 (mit/util/scripts/install.sh), which was
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews# later released in X11R6 (xc/config/util/install.sh) with the
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews# following copyright and license.
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews# Copyright (C) 1994 X Consortium
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews# Permission is hereby granted, free of charge, to any person obtaining a copy
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews# of this software and associated documentation files (the "Software"), to
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews# deal in the Software without restriction, including without limitation the
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews# sell copies of the Software, and to permit persons to whom the Software is
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews# furnished to do so, subject to the following conditions:
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews# The above copyright notice and this permission notice shall be included in
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews# all copies or substantial portions of the Software.
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews# Except as contained in this notice, the name of the X Consortium shall not
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews# be used in advertising or otherwise to promote the sale, use or other deal-
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews# ings in this Software without prior written authorization from the X Consor-
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews# FSF changes to this file are in the public domain.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt# Calling this script install-sh is preferred over install.sh, to prevent
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews# 'make' implicit rules from creating a file called install from it
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt# when there is no Makefile.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt# This script is compatible with the BSD install script, but was written
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt# from scratch.
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews# Set DOITPROG to "echo" to test this script.
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews# Put in absolute file names if you don't have them in your path;
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews# or use environment vars.
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews# Desired mode of installed file.
4a53e3c2b83c476a93148eaee0272649beb221caMark AndrewsUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews or: $0 [OPTION]... SRCFILES... DIRECTORY
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews or: $0 [OPTION]... -t DIRECTORY SRCFILES...
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews or: $0 [OPTION]... -d DIRECTORIES...
4a53e3c2b83c476a93148eaee0272649beb221caMark AndrewsIn the 1st form, copy SRCFILE to DSTFILE.
4a53e3c2b83c476a93148eaee0272649beb221caMark AndrewsIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
4a53e3c2b83c476a93148eaee0272649beb221caMark AndrewsIn the 4th, create DIRECTORIES.
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews --help display this help and exit.
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews --version display version info and exit.
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews -c (ignored)
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews -C install only if different (preserve the last data modification time)
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews -d create directories instead of installing files.
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews -g GROUP $chgrpprog installed files to GROUP.
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews -m MODE $chmodprog installed files to MODE.
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews -o USER $chownprog installed files to USER.
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews -s $stripprog installed files.
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews -t DIRECTORY install into DIRECTORY.
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews -T report an error if DSTFILE is a directory.
4a53e3c2b83c476a93148eaee0272649beb221caMark AndrewsEnvironment variables override the default commands:
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews RMPROG STRIPPROG
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews # Protect names problematic for 'test' and other utilities.
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews# We allow the use of options -d and -T together, by making -d
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews# take the precedence; this is for compatibility with GNU install.
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrewsif test -n "$dir_arg"; then
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews if test -n "$dst_arg"; then
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews echo "$0: target directory not allowed when installing a directory." >&2
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrewsif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews # When -d is used, all remaining arguments are directories to create.
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews # When -t is used, the destination is already specified.
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews # Otherwise, the last argument is the destination. Remove it from $@.
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews if test -n "$dst_arg"; then
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews # $@ is not empty: it contains at least $arg.
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews shift # fnord
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews # Protect names problematic for 'test' and other utilities.
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews if test -z "$dir_arg"; then
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews # It's OK to call 'install-sh -d' without argument.
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews # This can happen when creating conditional directories.
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrewsif test -z "$dir_arg"; then
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews if test $# -gt 1 || test "$is_target_a_directory" = always; then
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews if test ! -d "$dst_arg"; then
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrewsif test -z "$dir_arg"; then
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews # Set umask so as not to create temps with too-generous modes.
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews # However, 'strip' requires both read and write access to temps.
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews # Optimize common cases.
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews if test -z "$stripcmd"; then
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews if test -z "$stripcmd"; then
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews # Protect names problematic for 'test' and other utilities.
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews if test -n "$dir_arg"; then
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews # might cause directories to be created, which would be especially bad
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews # if $src (and thus $dsttmp) contains '*'.
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews if test -z "$dst_arg"; then
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews # If destination is a directory, append the input filename; won't work
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews # if double slashes aren't ignored.
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews if test -d "$dst"; then
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews # Create intermediate dirs using mode 755 as modified by the umask.
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews # This is like FreeBSD 'install' as of 1997-10-28.
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews umask=`umask`
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews # Optimize common cases.
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews - $umask % 100 % 40 + $umask % 20 \
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews - $umask % 10 % 4 + $umask % 2
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews # With -d, create the new directory with the user-specified mode.
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews # Otherwise, rely on $mkdir_umask.
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews if test -n "$dir_arg"; then
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews # POSIX mkdir -p sets u+wx bits regardless of umask, which
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews # other-writable bit of parent directory when it shouldn't.
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews d????-?r-*) different_mode=700;;
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews d????-?--*) different_mode=755;;
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews $mkdirprog -m$different_mode -p -- "$tmpdir" && {
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews # Remove any dirs left behind by ancient mkdir implementations.
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews # The umask is ridiculous, or mkdir does not conform to POSIX,
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews # or it failed possibly due to a race condition. Create the
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews # directory the slow way, step by step, checking for races as we go.
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews test X"$d" = X && continue
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews if test -d "$prefix"; then
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews # Don't fail if two instances are running concurrently.
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews if test -n "$prefixes"; then
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews # Don't fail if two instances are running concurrently.
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews if test -n "$dir_arg"; then
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews # Make a couple of temp file names in the proper directory.
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews # Trap to clean up those temp files at exit.
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews # Copy the file name to the temp name.
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews # and set any options; do chmod last to preserve setuid bits.
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews # If any of these fail, we abort the whole thing. If we want to
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews # ignore errors from any of these, just make sure not to ignore
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews # errors from the above "$doit $cpprog $src $dsttmp" command.
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews # If -C, don't bother to copy if it wouldn't change the file.
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews new=`LC_ALL=C ls -dlL "$dsttmp" 2>/dev/null` &&
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews # Rename the file to the real destination.
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews # The rename failed, perhaps because mv can't rename something else
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews # to itself, or perhaps because mv is so ancient that it does not
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews # support -f.
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews # Now remove or move aside any old file at destination location.
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews # We try this two ways since rm can't unlink itself on some
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews # systems and the destination file might be busy for other
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews # reasons. In this case, the final cleanup might fail but the new
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews fi || exit 1
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews# Local variables:
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews# eval: (add-hook 'write-file-hooks 'time-stamp)
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews# time-stamp-start: "scriptversion="
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews# time-stamp-format: "%:y-%02m-%02d.%02H"
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews# time-stamp-time-zone: "UTC0"
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews# time-stamp-end: "; # UTC"