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