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