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