joachim99@2: #!/bin/sh joachim99@69: # install - install a program, script, or datafile joachim99@69: joachim99@77: scriptversion=2005-11-07.23 joachim99@69: joachim99@69: # This originates from X11R5 (mit/util/scripts/install.sh), which was joachim99@69: # later released in X11R6 (xc/config/util/install.sh) with the joachim99@69: # following copyright and license. joachim99@2: # joachim99@69: # Copyright (C) 1994 X Consortium joachim99@2: # joachim99@69: # Permission is hereby granted, free of charge, to any person obtaining a copy joachim99@69: # of this software and associated documentation files (the "Software"), to joachim99@69: # deal in the Software without restriction, including without limitation the joachim99@69: # rights to use, copy, modify, merge, publish, distribute, sublicense, and/or joachim99@69: # sell copies of the Software, and to permit persons to whom the Software is joachim99@69: # furnished to do so, subject to the following conditions: joachim99@2: # joachim99@69: # The above copyright notice and this permission notice shall be included in joachim99@69: # all copies or substantial portions of the Software. joachim99@69: # joachim99@69: # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR joachim99@69: # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, joachim99@69: # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE joachim99@69: # X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN joachim99@69: # AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC- joachim99@69: # TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. joachim99@69: # joachim99@69: # Except as contained in this notice, the name of the X Consortium shall not joachim99@69: # be used in advertising or otherwise to promote the sale, use or other deal- joachim99@69: # ings in this Software without prior written authorization from the X Consor- joachim99@69: # tium. joachim99@69: # joachim99@69: # joachim99@69: # FSF changes to this file are in the public domain. joachim99@2: # joachim99@2: # Calling this script install-sh is preferred over install.sh, to prevent joachim99@2: # `make' implicit rules from creating a file called install from it joachim99@2: # when there is no Makefile. joachim99@2: # joachim99@2: # This script is compatible with the BSD install script, but was written joachim99@2: # from scratch. It can only install one file at a time, a restriction joachim99@2: # shared with many OS's install programs. joachim99@2: joachim99@2: # set DOITPROG to echo to test this script joachim99@2: joachim99@2: # Don't use :- since 4.3BSD and earlier shells don't like it. joachim99@2: doit="${DOITPROG-}" joachim99@2: joachim99@2: # put in absolute paths if you don't have them in your path; or use env. vars. joachim99@2: joachim99@2: mvprog="${MVPROG-mv}" joachim99@2: cpprog="${CPPROG-cp}" joachim99@2: chmodprog="${CHMODPROG-chmod}" joachim99@2: chownprog="${CHOWNPROG-chown}" joachim99@2: chgrpprog="${CHGRPPROG-chgrp}" joachim99@2: stripprog="${STRIPPROG-strip}" joachim99@2: rmprog="${RMPROG-rm}" joachim99@2: mkdirprog="${MKDIRPROG-mkdir}" joachim99@2: joachim99@77: posix_glob= joachim99@77: posix_mkdir= joachim99@77: joachim99@77: # Symbolic mode for testing mkdir with directories. joachim99@77: # It is the same as 755, but also tests that "u+" works. joachim99@77: test_mode=u=rwx,g=rx,o=rx,u+wx joachim99@77: joachim99@77: # Desired mode of installed file. joachim99@77: mode=0755 joachim99@77: joachim99@77: # Desired mode of newly created intermediate directories. joachim99@77: # It is empty if not known yet. joachim99@77: intermediate_mode= joachim99@77: joachim99@77: chmodcmd=$chmodprog joachim99@69: chowncmd= joachim99@69: chgrpcmd= joachim99@69: stripcmd= joachim99@2: rmcmd="$rmprog -f" joachim99@2: mvcmd="$mvprog" joachim99@69: src= joachim99@69: dst= joachim99@69: dir_arg= joachim99@69: dstarg= joachim99@69: no_target_directory= joachim99@2: joachim99@69: usage="Usage: $0 [OPTION]... [-T] SRCFILE DSTFILE joachim99@69: or: $0 [OPTION]... SRCFILES... DIRECTORY joachim99@69: or: $0 [OPTION]... -t DIRECTORY SRCFILES... joachim99@69: or: $0 [OPTION]... -d DIRECTORIES... joachim99@2: joachim99@69: In the 1st form, copy SRCFILE to DSTFILE. joachim99@69: In the 2nd and 3rd, copy all SRCFILES to DIRECTORY. joachim99@69: In the 4th, create DIRECTORIES. joachim99@2: joachim99@69: Options: joachim99@69: -c (ignored) joachim99@69: -d create directories instead of installing files. joachim99@69: -g GROUP $chgrpprog installed files to GROUP. joachim99@69: -m MODE $chmodprog installed files to MODE. joachim99@69: -o USER $chownprog installed files to USER. joachim99@69: -s $stripprog installed files. joachim99@69: -t DIRECTORY install into DIRECTORY. joachim99@69: -T report an error if DSTFILE is a directory. joachim99@69: --help display this help and exit. joachim99@69: --version display version info and exit. joachim99@2: joachim99@69: Environment variables override the default commands: joachim99@69: CHGRPPROG CHMODPROG CHOWNPROG CPPROG MKDIRPROG MVPROG RMPROG STRIPPROG joachim99@69: " joachim99@2: joachim99@69: while test -n "$1"; do joachim99@69: case $1 in joachim99@69: -c) shift joachim99@69: continue;; joachim99@2: joachim99@69: -d) dir_arg=true joachim99@69: shift joachim99@69: continue;; joachim99@2: joachim99@69: -g) chgrpcmd="$chgrpprog $2" joachim99@69: shift joachim99@69: shift joachim99@69: continue;; joachim99@2: joachim99@69: --help) echo "$usage"; exit $?;; joachim99@2: joachim99@77: -m) mode=$2 joachim99@69: shift joachim99@69: shift joachim99@69: continue;; joachim99@69: joachim99@69: -o) chowncmd="$chownprog $2" joachim99@69: shift joachim99@69: shift joachim99@69: continue;; joachim99@69: joachim99@69: -s) stripcmd=$stripprog joachim99@69: shift joachim99@69: continue;; joachim99@69: joachim99@69: -t) dstarg=$2 joachim99@69: shift joachim99@69: shift joachim99@69: continue;; joachim99@69: joachim99@69: -T) no_target_directory=true joachim99@69: shift joachim99@69: continue;; joachim99@69: joachim99@69: --version) echo "$0 $scriptversion"; exit $?;; joachim99@69: joachim99@69: *) # When -d is used, all remaining arguments are directories to create. joachim99@69: # When -t is used, the destination is already specified. joachim99@69: test -n "$dir_arg$dstarg" && break joachim99@69: # Otherwise, the last argument is the destination. Remove it from $@. joachim99@69: for arg joachim99@69: do joachim99@69: if test -n "$dstarg"; then joachim99@69: # $@ is not empty: it contains at least $arg. joachim99@69: set fnord "$@" "$dstarg" joachim99@69: shift # fnord joachim99@69: fi joachim99@69: shift # arg joachim99@69: dstarg=$arg joachim99@69: done joachim99@69: break;; joachim99@69: esac joachim99@2: done joachim99@2: joachim99@69: if test -z "$1"; then joachim99@69: if test -z "$dir_arg"; then joachim99@69: echo "$0: no input file specified." >&2 joachim99@69: exit 1 joachim99@69: fi joachim99@69: # It's OK to call `install-sh -d' without argument. joachim99@69: # This can happen when creating conditional directories. joachim99@69: exit 0 joachim99@2: fi joachim99@2: joachim99@77: test -n "$dir_arg" || trap '(exit $?); exit' 1 2 13 15 joachim99@77: joachim99@69: for src joachim99@69: do joachim99@69: # Protect names starting with `-'. joachim99@69: case $src in joachim99@69: -*) src=./$src ;; joachim99@69: esac joachim99@66: joachim99@69: if test -n "$dir_arg"; then joachim99@69: dst=$src joachim99@77: dstdir=$dst joachim99@77: test -d "$dstdir" joachim99@77: dstdir_status=$? joachim99@77: else joachim99@2: joachim99@69: # Waiting for this to be detected by the "$cpprog $src $dsttmp" command joachim99@69: # might cause directories to be created, which would be especially bad joachim99@69: # if $src (and thus $dsttmp) contains '*'. joachim99@69: if test ! -f "$src" && test ! -d "$src"; then joachim99@69: echo "$0: $src does not exist." >&2 joachim99@69: exit 1 joachim99@69: fi joachim99@2: joachim99@69: if test -z "$dstarg"; then joachim99@69: echo "$0: no destination specified." >&2 joachim99@69: exit 1 joachim99@69: fi joachim99@66: joachim99@69: dst=$dstarg joachim99@69: # Protect names starting with `-'. joachim99@69: case $dst in joachim99@69: -*) dst=./$dst ;; joachim99@69: esac joachim99@2: joachim99@69: # If destination is a directory, append the input filename; won't work joachim99@69: # if double slashes aren't ignored. joachim99@69: if test -d "$dst"; then joachim99@69: if test -n "$no_target_directory"; then joachim99@69: echo "$0: $dstarg: Is a directory" >&2 joachim99@69: exit 1 joachim99@69: fi joachim99@77: dstdir=$dst joachim99@77: dst=$dstdir/`basename "$src"` joachim99@77: dstdir_status=0 joachim99@77: else joachim99@77: # Prefer dirname, but fall back on a substitute if dirname fails. joachim99@77: dstdir=` joachim99@77: (dirname "$dst") 2>/dev/null || joachim99@77: expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \ joachim99@77: X"$dst" : 'X\(//\)[^/]' \| \ joachim99@77: X"$dst" : 'X\(//\)$' \| \ joachim99@77: X"$dst" : 'X\(/\)' \| \ joachim99@77: . : '\(.\)' 2>/dev/null || joachim99@77: echo X"$dst" | joachim99@77: sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{ s//\1/; q; } joachim99@77: /^X\(\/\/\)[^/].*/{ s//\1/; q; } joachim99@77: /^X\(\/\/\)$/{ s//\1/; q; } joachim99@77: /^X\(\/\).*/{ s//\1/; q; } joachim99@77: s/.*/./; q' joachim99@77: ` joachim99@77: joachim99@77: test -d "$dstdir" joachim99@77: dstdir_status=$? joachim99@69: fi joachim99@69: fi joachim99@2: joachim99@77: obsolete_mkdir_used=false joachim99@2: joachim99@77: if test $dstdir_status != 0; then joachim99@77: case $posix_mkdir in joachim99@77: '') joachim99@77: posix_mkdir=false joachim99@77: if $mkdirprog -m $test_mode -p -- / >/dev/null 2>&1; then joachim99@77: posix_mkdir=true joachim99@77: else joachim99@77: # Remove any dirs left behind by ancient mkdir implementations. joachim99@77: rmdir ./-m "$test_mode" ./-p ./-- 2>/dev/null joachim99@77: fi ;; joachim99@77: esac joachim99@2: joachim99@77: if joachim99@77: $posix_mkdir && { joachim99@2: joachim99@77: # With -d, create the new directory with the user-specified mode. joachim99@77: # Otherwise, create it using the same intermediate mode that joachim99@77: # mkdir -p would use when creating intermediate directories. joachim99@77: # POSIX says that this mode is "$(umask -S),u+wx", so use that joachim99@77: # if umask -S works. joachim99@2: joachim99@77: if test -n "$dir_arg"; then joachim99@77: mkdir_mode=$mode joachim99@77: else joachim99@77: case $intermediate_mode in joachim99@77: '') joachim99@77: if umask_S=`(umask -S) 2>/dev/null`; then joachim99@77: intermediate_mode=$umask_S,u+wx joachim99@77: else joachim99@77: intermediate_mode=$test_mode joachim99@77: fi ;; joachim99@77: esac joachim99@77: mkdir_mode=$intermediate_mode joachim99@77: fi joachim99@2: joachim99@77: $mkdirprog -m "$mkdir_mode" -p -- "$dstdir" joachim99@77: } joachim99@77: then : joachim99@77: else joachim99@77: joachim99@77: # mkdir does not conform to POSIX, or it failed possibly due to joachim99@77: # a race condition. Create the directory the slow way, step by joachim99@77: # step, checking for races as we go. joachim99@77: joachim99@77: case $dstdir in joachim99@77: /*) pathcomp=/ ;; joachim99@77: -*) pathcomp=./ ;; joachim99@77: *) pathcomp= ;; joachim99@77: esac joachim99@77: joachim99@77: case $posix_glob in joachim99@77: '') joachim99@77: if (set -f) 2>/dev/null; then joachim99@77: posix_glob=true joachim99@77: else joachim99@77: posix_glob=false joachim99@77: fi ;; joachim99@77: esac joachim99@77: joachim99@77: oIFS=$IFS joachim99@77: IFS=/ joachim99@77: $posix_glob && set -f joachim99@77: set fnord $dstdir joachim99@69: shift joachim99@77: $posix_glob && set +f joachim99@77: IFS=$oIFS joachim99@77: joachim99@77: for d joachim99@77: do joachim99@77: test "x$d" = x && continue joachim99@77: joachim99@77: pathcomp=$pathcomp$d joachim99@77: if test ! -d "$pathcomp"; then joachim99@77: $mkdirprog "$pathcomp" joachim99@77: # Don't fail if two instances are running concurrently. joachim99@77: test -d "$pathcomp" || exit 1 joachim99@77: fi joachim99@77: pathcomp=$pathcomp/ joachim99@77: done joachim99@77: obsolete_mkdir_used=true joachim99@77: fi joachim99@69: fi joachim99@2: joachim99@69: if test -n "$dir_arg"; then joachim99@77: { test -z "$chowncmd" || $doit $chowncmd "$dst"; } && joachim99@77: { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } && joachim99@77: { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false || joachim99@77: test -z "$chmodcmd" || $doit $chmodcmd "$mode" "$dst"; } || exit 1 joachim99@69: else joachim99@2: joachim99@69: # Make a couple of temp file names in the proper directory. joachim99@69: dsttmp=$dstdir/_inst.$$_ joachim99@69: rmtmp=$dstdir/_rm.$$_ joachim99@69: joachim99@69: # Trap to clean up those temp files at exit. joachim99@69: trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0 joachim99@69: joachim99@69: # Copy the file name to the temp name. joachim99@69: $doit $cpprog "$src" "$dsttmp" && joachim99@69: joachim99@69: # and set any options; do chmod last to preserve setuid bits. joachim99@69: # joachim99@69: # If any of these fail, we abort the whole thing. If we want to joachim99@69: # ignore errors from any of these, just make sure not to ignore joachim99@69: # errors from the above "$doit $cpprog $src $dsttmp" command. joachim99@69: # joachim99@69: { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } \ joachim99@69: && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } \ joachim99@69: && { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } \ joachim99@77: && { test -z "$chmodcmd" || $doit $chmodcmd "$mode" "$dsttmp"; } && joachim99@69: joachim99@69: # Now rename the file to the real destination. joachim99@77: { $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null \ joachim99@69: || { joachim99@69: # The rename failed, perhaps because mv can't rename something else joachim99@69: # to itself, or perhaps because mv is so ancient that it does not joachim99@69: # support -f. joachim99@69: joachim99@69: # Now remove or move aside any old file at destination location. joachim99@69: # We try this two ways since rm can't unlink itself on some joachim99@69: # systems and the destination file might be busy for other joachim99@69: # reasons. In this case, the final cleanup might fail but the new joachim99@69: # file should still install successfully. joachim99@69: { joachim99@77: if test -f "$dst"; then joachim99@77: $doit $rmcmd -f "$dst" 2>/dev/null \ joachim99@77: || { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null \ joachim99@77: && { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }; }\ joachim99@69: || { joachim99@77: echo "$0: cannot unlink or rename $dst" >&2 joachim99@69: (exit 1); exit 1 joachim99@69: } joachim99@69: else joachim99@69: : joachim99@69: fi joachim99@69: } && joachim99@69: joachim99@69: # Now rename the file to the real destination. joachim99@77: $doit $mvcmd "$dsttmp" "$dst" joachim99@69: } joachim99@77: } || exit 1 joachim99@77: joachim99@77: trap '' 0 joachim99@77: fi joachim99@2: done joachim99@2: joachim99@69: # Local variables: joachim99@69: # eval: (add-hook 'write-file-hooks 'time-stamp) joachim99@69: # time-stamp-start: "scriptversion=" joachim99@69: # time-stamp-format: "%:y-%02m-%02d.%02H" joachim99@69: # time-stamp-end: "$" joachim99@69: # End: