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