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