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