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