Blame install-sh

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