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