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