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