leo_sobral@2: #!/bin/sh leo_sobral@2: # install - install a program, script, or datafile leo_sobral@2: leo_sobral@2: scriptversion=2004-04-01.17 leo_sobral@2: leo_sobral@2: # This originates from X11R5 (mit/util/scripts/install.sh), which was leo_sobral@2: # later released in X11R6 (xc/config/util/install.sh) with the leo_sobral@2: # following copyright and license. leo_sobral@2: # leo_sobral@2: # Copyright (C) 1994 X Consortium leo_sobral@2: # leo_sobral@2: # Permission is hereby granted, free of charge, to any person obtaining a copy leo_sobral@2: # of this software and associated documentation files (the "Software"), to leo_sobral@2: # deal in the Software without restriction, including without limitation the leo_sobral@2: # rights to use, copy, modify, merge, publish, distribute, sublicense, and/or leo_sobral@2: # sell copies of the Software, and to permit persons to whom the Software is leo_sobral@2: # furnished to do so, subject to the following conditions: leo_sobral@2: # leo_sobral@2: # The above copyright notice and this permission notice shall be included in leo_sobral@2: # all copies or substantial portions of the Software. leo_sobral@2: # leo_sobral@2: # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR leo_sobral@2: # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, leo_sobral@2: # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE leo_sobral@2: # X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN leo_sobral@2: # AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC- leo_sobral@2: # TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. leo_sobral@2: # leo_sobral@2: # Except as contained in this notice, the name of the X Consortium shall not leo_sobral@2: # be used in advertising or otherwise to promote the sale, use or other deal- leo_sobral@2: # ings in this Software without prior written authorization from the X Consor- leo_sobral@2: # tium. leo_sobral@2: # leo_sobral@2: # leo_sobral@2: # FSF changes to this file are in the public domain. leo_sobral@2: # leo_sobral@2: # Calling this script install-sh is preferred over install.sh, to prevent leo_sobral@2: # `make' implicit rules from creating a file called install from it leo_sobral@2: # when there is no Makefile. leo_sobral@2: # leo_sobral@2: # This script is compatible with the BSD install script, but was written leo_sobral@2: # from scratch. It can only install one file at a time, a restriction leo_sobral@2: # shared with many OS's install programs. leo_sobral@2: leo_sobral@2: # set DOITPROG to echo to test this script leo_sobral@2: leo_sobral@2: # Don't use :- since 4.3BSD and earlier shells don't like it. leo_sobral@2: doit="${DOITPROG-}" leo_sobral@2: leo_sobral@2: # put in absolute paths if you don't have them in your path; or use env. vars. leo_sobral@2: leo_sobral@2: mvprog="${MVPROG-mv}" leo_sobral@2: cpprog="${CPPROG-cp}" leo_sobral@2: chmodprog="${CHMODPROG-chmod}" leo_sobral@2: chownprog="${CHOWNPROG-chown}" leo_sobral@2: chgrpprog="${CHGRPPROG-chgrp}" leo_sobral@2: stripprog="${STRIPPROG-strip}" leo_sobral@2: rmprog="${RMPROG-rm}" leo_sobral@2: mkdirprog="${MKDIRPROG-mkdir}" leo_sobral@2: leo_sobral@2: transformbasename= leo_sobral@2: transform_arg= leo_sobral@2: instcmd="$mvprog" leo_sobral@2: chmodcmd="$chmodprog 0755" leo_sobral@2: chowncmd= leo_sobral@2: chgrpcmd= leo_sobral@2: stripcmd= leo_sobral@2: rmcmd="$rmprog -f" leo_sobral@2: mvcmd="$mvprog" leo_sobral@2: src= leo_sobral@2: dst= leo_sobral@2: dir_arg= leo_sobral@2: leo_sobral@2: usage="Usage: $0 [OPTION]... SRCFILE DSTFILE leo_sobral@2: or: $0 [OPTION]... SRCFILES... DIRECTORY leo_sobral@2: or: $0 -d DIRECTORIES... leo_sobral@2: leo_sobral@2: In the first form, install SRCFILE to DSTFILE, removing SRCFILE by default. leo_sobral@2: In the second, create the directory path DIR. leo_sobral@2: leo_sobral@2: Options: leo_sobral@2: -b=TRANSFORMBASENAME leo_sobral@2: -c copy source (using $cpprog) instead of moving (using $mvprog). leo_sobral@2: -d create directories instead of installing files. leo_sobral@2: -g GROUP $chgrp installed files to GROUP. leo_sobral@2: -m MODE $chmod installed files to MODE. leo_sobral@2: -o USER $chown installed files to USER. leo_sobral@2: -s strip installed files (using $stripprog). leo_sobral@2: -t=TRANSFORM leo_sobral@2: --help display this help and exit. leo_sobral@2: --version display version info and exit. leo_sobral@2: leo_sobral@2: Environment variables override the default commands: leo_sobral@2: CHGRPPROG CHMODPROG CHOWNPROG CPPROG MKDIRPROG MVPROG RMPROG STRIPPROG leo_sobral@2: " leo_sobral@2: leo_sobral@2: while test -n "$1"; do leo_sobral@2: case $1 in leo_sobral@2: -b=*) transformbasename=`echo $1 | sed 's/-b=//'` leo_sobral@2: shift leo_sobral@2: continue;; leo_sobral@2: leo_sobral@2: -c) instcmd=$cpprog leo_sobral@2: shift leo_sobral@2: continue;; leo_sobral@2: leo_sobral@2: -d) dir_arg=true leo_sobral@2: shift leo_sobral@2: continue;; leo_sobral@2: leo_sobral@2: -g) chgrpcmd="$chgrpprog $2" leo_sobral@2: shift leo_sobral@2: shift leo_sobral@2: continue;; leo_sobral@2: leo_sobral@2: --help) echo "$usage"; exit 0;; leo_sobral@2: leo_sobral@2: -m) chmodcmd="$chmodprog $2" leo_sobral@2: shift leo_sobral@2: shift leo_sobral@2: continue;; leo_sobral@2: leo_sobral@2: -o) chowncmd="$chownprog $2" leo_sobral@2: shift leo_sobral@2: shift leo_sobral@2: continue;; leo_sobral@2: leo_sobral@2: -s) stripcmd=$stripprog leo_sobral@2: shift leo_sobral@2: continue;; leo_sobral@2: leo_sobral@2: -t=*) transformarg=`echo $1 | sed 's/-t=//'` leo_sobral@2: shift leo_sobral@2: continue;; leo_sobral@2: leo_sobral@2: --version) echo "$0 $scriptversion"; exit 0;; leo_sobral@2: leo_sobral@2: *) # When -d is used, all remaining arguments are directories to create. leo_sobral@2: test -n "$dir_arg" && break leo_sobral@2: # Otherwise, the last argument is the destination. Remove it from $@. leo_sobral@2: for arg leo_sobral@2: do leo_sobral@2: if test -n "$dstarg"; then leo_sobral@2: # $@ is not empty: it contains at least $arg. leo_sobral@2: set fnord "$@" "$dstarg" leo_sobral@2: shift # fnord leo_sobral@2: fi leo_sobral@2: shift # arg leo_sobral@2: dstarg=$arg leo_sobral@2: done leo_sobral@2: break;; leo_sobral@2: esac leo_sobral@2: done leo_sobral@2: leo_sobral@2: if test -z "$1"; then leo_sobral@2: if test -z "$dir_arg"; then leo_sobral@2: echo "$0: no input file specified." >&2 leo_sobral@2: exit 1 leo_sobral@2: fi leo_sobral@2: # It's OK to call `install-sh -d' without argument. leo_sobral@2: # This can happen when creating conditional directories. leo_sobral@2: exit 0 leo_sobral@2: fi leo_sobral@2: leo_sobral@2: for src leo_sobral@2: do leo_sobral@2: # Protect names starting with `-'. leo_sobral@2: case $src in leo_sobral@2: -*) src=./$src ;; leo_sobral@2: esac leo_sobral@2: leo_sobral@2: if test -n "$dir_arg"; then leo_sobral@2: dst=$src leo_sobral@2: src= leo_sobral@2: leo_sobral@2: if test -d "$dst"; then leo_sobral@2: instcmd=: leo_sobral@2: chmodcmd= leo_sobral@2: else leo_sobral@2: instcmd=$mkdirprog leo_sobral@2: fi leo_sobral@2: else leo_sobral@2: # Waiting for this to be detected by the "$instcmd $src $dsttmp" command leo_sobral@2: # might cause directories to be created, which would be especially bad leo_sobral@2: # if $src (and thus $dsttmp) contains '*'. leo_sobral@2: if test ! -f "$src" && test ! -d "$src"; then leo_sobral@2: echo "$0: $src does not exist." >&2 leo_sobral@2: exit 1 leo_sobral@2: fi leo_sobral@2: leo_sobral@2: if test -z "$dstarg"; then leo_sobral@2: echo "$0: no destination specified." >&2 leo_sobral@2: exit 1 leo_sobral@2: fi leo_sobral@2: leo_sobral@2: dst=$dstarg leo_sobral@2: # Protect names starting with `-'. leo_sobral@2: case $dst in leo_sobral@2: -*) dst=./$dst ;; leo_sobral@2: esac leo_sobral@2: leo_sobral@2: # If destination is a directory, append the input filename; won't work leo_sobral@2: # if double slashes aren't ignored. leo_sobral@2: if test -d "$dst"; then leo_sobral@2: dst=$dst/`basename "$src"` leo_sobral@2: fi leo_sobral@2: fi leo_sobral@2: leo_sobral@2: # This sed command emulates the dirname command. leo_sobral@2: dstdir=`echo "$dst" | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'` leo_sobral@2: leo_sobral@2: # Make sure that the destination directory exists. leo_sobral@2: leo_sobral@2: # Skip lots of stat calls in the usual case. leo_sobral@2: if test ! -d "$dstdir"; then leo_sobral@2: defaultIFS=' leo_sobral@2: ' leo_sobral@2: IFS="${IFS-$defaultIFS}" leo_sobral@2: leo_sobral@2: oIFS=$IFS leo_sobral@2: # Some sh's can't handle IFS=/ for some reason. leo_sobral@2: IFS='%' leo_sobral@2: set - `echo "$dstdir" | sed -e 's@/@%@g' -e 's@^%@/@'` leo_sobral@2: IFS=$oIFS leo_sobral@2: leo_sobral@2: pathcomp= leo_sobral@2: leo_sobral@2: while test $# -ne 0 ; do leo_sobral@2: pathcomp=$pathcomp$1 leo_sobral@2: shift leo_sobral@2: if test ! -d "$pathcomp"; then leo_sobral@2: $mkdirprog "$pathcomp" || lasterr=$? leo_sobral@2: # mkdir can fail with a `File exist' error in case several leo_sobral@2: # install-sh are creating the directory concurrently. This leo_sobral@2: # is OK. leo_sobral@2: test ! -d "$pathcomp" && { (exit ${lasterr-1}); exit; } leo_sobral@2: fi leo_sobral@2: pathcomp=$pathcomp/ leo_sobral@2: done leo_sobral@2: fi leo_sobral@2: leo_sobral@2: if test -n "$dir_arg"; then leo_sobral@2: $doit $instcmd "$dst" \ leo_sobral@2: && { test -z "$chowncmd" || $doit $chowncmd "$dst"; } \ leo_sobral@2: && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } \ leo_sobral@2: && { test -z "$stripcmd" || $doit $stripcmd "$dst"; } \ leo_sobral@2: && { test -z "$chmodcmd" || $doit $chmodcmd "$dst"; } leo_sobral@2: leo_sobral@2: else leo_sobral@2: # If we're going to rename the final executable, determine the name now. leo_sobral@2: if test -z "$transformarg"; then leo_sobral@2: dstfile=`basename "$dst"` leo_sobral@2: else leo_sobral@2: dstfile=`basename "$dst" $transformbasename \ leo_sobral@2: | sed $transformarg`$transformbasename leo_sobral@2: fi leo_sobral@2: leo_sobral@2: # don't allow the sed command to completely eliminate the filename. leo_sobral@2: test -z "$dstfile" && dstfile=`basename "$dst"` leo_sobral@2: leo_sobral@2: # Make a couple of temp file names in the proper directory. leo_sobral@2: dsttmp=$dstdir/_inst.$$_ leo_sobral@2: rmtmp=$dstdir/_rm.$$_ leo_sobral@2: leo_sobral@2: # Trap to clean up those temp files at exit. leo_sobral@2: trap 'status=$?; rm -f "$dsttmp" "$rmtmp" && exit $status' 0 leo_sobral@2: trap '(exit $?); exit' 1 2 13 15 leo_sobral@2: leo_sobral@2: # Move or copy the file name to the temp name leo_sobral@2: $doit $instcmd "$src" "$dsttmp" && leo_sobral@2: leo_sobral@2: # and set any options; do chmod last to preserve setuid bits. leo_sobral@2: # leo_sobral@2: # If any of these fail, we abort the whole thing. If we want to leo_sobral@2: # ignore errors from any of these, just make sure not to ignore leo_sobral@2: # errors from the above "$doit $instcmd $src $dsttmp" command. leo_sobral@2: # leo_sobral@2: { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } \ leo_sobral@2: && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } \ leo_sobral@2: && { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } \ leo_sobral@2: && { test -z "$chmodcmd" || $doit $chmodcmd "$dsttmp"; } && leo_sobral@2: leo_sobral@2: # Now rename the file to the real destination. leo_sobral@2: { $doit $mvcmd -f "$dsttmp" "$dstdir/$dstfile" 2>/dev/null \ leo_sobral@2: || { leo_sobral@2: # The rename failed, perhaps because mv can't rename something else leo_sobral@2: # to itself, or perhaps because mv is so ancient that it does not leo_sobral@2: # support -f. leo_sobral@2: leo_sobral@2: # Now remove or move aside any old file at destination location. leo_sobral@2: # We try this two ways since rm can't unlink itself on some leo_sobral@2: # systems and the destination file might be busy for other leo_sobral@2: # reasons. In this case, the final cleanup might fail but the new leo_sobral@2: # file should still install successfully. leo_sobral@2: { leo_sobral@2: if test -f "$dstdir/$dstfile"; then leo_sobral@2: $doit $rmcmd -f "$dstdir/$dstfile" 2>/dev/null \ leo_sobral@2: || $doit $mvcmd -f "$dstdir/$dstfile" "$rmtmp" 2>/dev/null \ leo_sobral@2: || { leo_sobral@2: echo "$0: cannot unlink or rename $dstdir/$dstfile" >&2 leo_sobral@2: (exit 1); exit leo_sobral@2: } leo_sobral@2: else leo_sobral@2: : leo_sobral@2: fi leo_sobral@2: } && leo_sobral@2: leo_sobral@2: # Now rename the file to the real destination. leo_sobral@2: $doit $mvcmd "$dsttmp" "$dstdir/$dstfile" leo_sobral@2: } leo_sobral@2: } leo_sobral@2: fi || { (exit 1); exit; } leo_sobral@2: done leo_sobral@2: leo_sobral@2: # The final little trick to "correctly" pass the exit status to the exit trap. leo_sobral@2: { leo_sobral@2: (exit 0); exit leo_sobral@2: } leo_sobral@2: leo_sobral@2: # Local variables: leo_sobral@2: # eval: (add-hook 'write-file-hooks 'time-stamp) leo_sobral@2: # time-stamp-start: "scriptversion=" leo_sobral@2: # time-stamp-format: "%:y-%02m-%02d.%02H" leo_sobral@2: # time-stamp-end: "$" leo_sobral@2: # End: