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