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