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