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