joachim99@2: #!/bin/sh joachim99@69: # install - install a program, script, or datafile joachim99@69: joachim99@69: scriptversion=2005-05-14.22 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@2: chmodcmd="$chmodprog 0755" 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@69: -m) chmodcmd="$chmodprog $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@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@69: src= joachim99@2: joachim99@69: if test -d "$dst"; then joachim99@69: mkdircmd=: joachim99@69: chmodcmd= joachim99@69: else joachim99@69: mkdircmd=$mkdirprog joachim99@69: fi joachim99@69: else 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@69: dst=$dst/`basename "$src"` joachim99@69: fi joachim99@69: fi joachim99@2: joachim99@69: # This sed command emulates the dirname command. joachim99@69: dstdir=`echo "$dst" | sed -e 's,/*$,,;s,[^/]*$,,;s,/*$,,;s,^$,.,'` joachim99@2: joachim99@69: # Make sure that the destination directory exists. joachim99@2: joachim99@69: # Skip lots of stat calls in the usual case. joachim99@69: if test ! -d "$dstdir"; then joachim99@69: defaultIFS=' joachim99@69: ' joachim99@69: IFS="${IFS-$defaultIFS}" joachim99@2: joachim99@69: oIFS=$IFS joachim99@69: # Some sh's can't handle IFS=/ for some reason. joachim99@69: IFS='%' joachim99@69: set x `echo "$dstdir" | sed -e 's@/@%@g' -e 's@^%@/@'` joachim99@69: shift joachim99@69: IFS=$oIFS joachim99@2: joachim99@69: pathcomp= joachim99@2: joachim99@69: while test $# -ne 0 ; do joachim99@69: pathcomp=$pathcomp$1 joachim99@69: shift joachim99@69: if test ! -d "$pathcomp"; then joachim99@69: $mkdirprog "$pathcomp" joachim99@69: # mkdir can fail with a `File exist' error in case several joachim99@69: # install-sh are creating the directory concurrently. This joachim99@69: # is OK. joachim99@69: test -d "$pathcomp" || exit joachim99@69: fi joachim99@69: pathcomp=$pathcomp/ joachim99@69: done joachim99@69: fi joachim99@2: joachim99@69: if test -n "$dir_arg"; then joachim99@69: $doit $mkdircmd "$dst" \ joachim99@69: && { test -z "$chowncmd" || $doit $chowncmd "$dst"; } \ joachim99@69: && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } \ joachim99@69: && { test -z "$stripcmd" || $doit $stripcmd "$dst"; } \ joachim99@69: && { test -z "$chmodcmd" || $doit $chmodcmd "$dst"; } joachim99@2: joachim99@69: else joachim99@69: dstfile=`basename "$dst"` 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: trap '(exit $?); exit' 1 2 13 15 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@69: && { test -z "$chmodcmd" || $doit $chmodcmd "$dsttmp"; } && joachim99@69: joachim99@69: # Now rename the file to the real destination. joachim99@69: { $doit $mvcmd -f "$dsttmp" "$dstdir/$dstfile" 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@69: if test -f "$dstdir/$dstfile"; then joachim99@69: $doit $rmcmd -f "$dstdir/$dstfile" 2>/dev/null \ joachim99@69: || $doit $mvcmd -f "$dstdir/$dstfile" "$rmtmp" 2>/dev/null \ joachim99@69: || { joachim99@69: echo "$0: cannot unlink or rename $dstdir/$dstfile" >&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@69: $doit $mvcmd "$dsttmp" "$dstdir/$dstfile" joachim99@69: } joachim99@69: } joachim99@69: fi || { (exit 1); exit 1; } joachim99@2: done joachim99@2: joachim99@66: # The final little trick to "correctly" pass the exit status to the exit trap. joachim99@69: { joachim99@69: (exit 0); exit 0 joachim99@69: } 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: