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