Blame build-aux/install-sh

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