Blame config/install-sh

Packit 994f1a
#!/bin/sh
Packit 994f1a
# install - install a program, script, or datafile
Packit 994f1a
Packit 994f1a
scriptversion=2005-11-07.23
Packit 994f1a
Packit 994f1a
# This originates from X11R5 (mit/util/scripts/install.sh), which was
Packit 994f1a
# later released in X11R6 (xc/config/util/install.sh) with the
Packit 994f1a
# following copyright and license.
Packit 994f1a
#
Packit 994f1a
# Copyright (C) 1994 X Consortium
Packit 994f1a
#
Packit 994f1a
# Permission is hereby granted, free of charge, to any person obtaining a copy
Packit 994f1a
# of this software and associated documentation files (the "Software"), to
Packit 994f1a
# deal in the Software without restriction, including without limitation the
Packit 994f1a
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
Packit 994f1a
# sell copies of the Software, and to permit persons to whom the Software is
Packit 994f1a
# furnished to do so, subject to the following conditions:
Packit 994f1a
#
Packit 994f1a
# The above copyright notice and this permission notice shall be included in
Packit 994f1a
# all copies or substantial portions of the Software.
Packit 994f1a
#
Packit 994f1a
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Packit 994f1a
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Packit 994f1a
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
Packit 994f1a
# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
Packit 994f1a
# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
Packit 994f1a
# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Packit 994f1a
#
Packit 994f1a
# Except as contained in this notice, the name of the X Consortium shall not
Packit 994f1a
# be used in advertising or otherwise to promote the sale, use or other deal-
Packit 994f1a
# ings in this Software without prior written authorization from the X Consor-
Packit 994f1a
# tium.
Packit 994f1a
#
Packit 994f1a
#
Packit 994f1a
# FSF changes to this file are in the public domain.
Packit 994f1a
#
Packit 994f1a
# Calling this script install-sh is preferred over install.sh, to prevent
Packit 994f1a
# `make' implicit rules from creating a file called install from it
Packit 994f1a
# when there is no Makefile.
Packit 994f1a
#
Packit 994f1a
# This script is compatible with the BSD install script, but was written
Packit 994f1a
# from scratch.  It can only install one file at a time, a restriction
Packit 994f1a
# shared with many OS's install programs.
Packit 994f1a
Packit 994f1a
# set DOITPROG to echo to test this script
Packit 994f1a
Packit 994f1a
# Don't use :- since 4.3BSD and earlier shells don't like it.
Packit 994f1a
doit="${DOITPROG-}"
Packit 994f1a
Packit 994f1a
# put in absolute paths if you don't have them in your path; or use env. vars.
Packit 994f1a
Packit 994f1a
mvprog="${MVPROG-mv}"
Packit 994f1a
cpprog="${CPPROG-cp}"
Packit 994f1a
chmodprog="${CHMODPROG-chmod}"
Packit 994f1a
chownprog="${CHOWNPROG-chown}"
Packit 994f1a
chgrpprog="${CHGRPPROG-chgrp}"
Packit 994f1a
stripprog="${STRIPPROG-strip}"
Packit 994f1a
rmprog="${RMPROG-rm}"
Packit 994f1a
mkdirprog="${MKDIRPROG-mkdir}"
Packit 994f1a
Packit 994f1a
posix_glob=
Packit 994f1a
posix_mkdir=
Packit 994f1a
Packit 994f1a
# Symbolic mode for testing mkdir with directories.
Packit 994f1a
# It is the same as 755, but also tests that "u+" works.
Packit 994f1a
test_mode=u=rwx,g=rx,o=rx,u+wx
Packit 994f1a
Packit 994f1a
# Desired mode of installed file.
Packit 994f1a
mode=0755
Packit 994f1a
Packit 994f1a
# Desired mode of newly created intermediate directories.
Packit 994f1a
# It is empty if not known yet.
Packit 994f1a
intermediate_mode=
Packit 994f1a
Packit 994f1a
chmodcmd=$chmodprog
Packit 994f1a
chowncmd=
Packit 994f1a
chgrpcmd=
Packit 994f1a
stripcmd=
Packit 994f1a
rmcmd="$rmprog -f"
Packit 994f1a
mvcmd="$mvprog"
Packit 994f1a
src=
Packit 994f1a
dst=
Packit 994f1a
dir_arg=
Packit 994f1a
dstarg=
Packit 994f1a
no_target_directory=
Packit 994f1a
Packit 994f1a
usage="Usage: $0 [OPTION]... [-T] SRCFILE DSTFILE
Packit 994f1a
   or: $0 [OPTION]... SRCFILES... DIRECTORY
Packit 994f1a
   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
Packit 994f1a
   or: $0 [OPTION]... -d DIRECTORIES...
Packit 994f1a
Packit 994f1a
In the 1st form, copy SRCFILE to DSTFILE.
Packit 994f1a
In the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
Packit 994f1a
In the 4th, create DIRECTORIES.
Packit 994f1a
Packit 994f1a
Options:
Packit 994f1a
-c         (ignored)
Packit 994f1a
-d         create directories instead of installing files.
Packit 994f1a
-g GROUP   $chgrpprog installed files to GROUP.
Packit 994f1a
-m MODE    $chmodprog installed files to MODE.
Packit 994f1a
-o USER    $chownprog installed files to USER.
Packit 994f1a
-s         $stripprog installed files.
Packit 994f1a
-t DIRECTORY  install into DIRECTORY.
Packit 994f1a
-T         report an error if DSTFILE is a directory.
Packit 994f1a
--help     display this help and exit.
Packit 994f1a
--version  display version info and exit.
Packit 994f1a
Packit 994f1a
Environment variables override the default commands:
Packit 994f1a
  CHGRPPROG CHMODPROG CHOWNPROG CPPROG MKDIRPROG MVPROG RMPROG STRIPPROG
Packit 994f1a
"
Packit 994f1a
Packit 994f1a
while test -n "$1"; do
Packit 994f1a
  case $1 in
Packit 994f1a
    -c) shift
Packit 994f1a
        continue;;
Packit 994f1a
Packit 994f1a
    -d) dir_arg=true
Packit 994f1a
        shift
Packit 994f1a
        continue;;
Packit 994f1a
Packit 994f1a
    -g) chgrpcmd="$chgrpprog $2"
Packit 994f1a
        shift
Packit 994f1a
        shift
Packit 994f1a
        continue;;
Packit 994f1a
Packit 994f1a
    --help) echo "$usage"; exit $?;;
Packit 994f1a
Packit 994f1a
    -m) mode=$2
Packit 994f1a
        shift
Packit 994f1a
        shift
Packit 994f1a
        continue;;
Packit 994f1a
Packit 994f1a
    -o) chowncmd="$chownprog $2"
Packit 994f1a
        shift
Packit 994f1a
        shift
Packit 994f1a
        continue;;
Packit 994f1a
Packit 994f1a
    -s) stripcmd=$stripprog
Packit 994f1a
        shift
Packit 994f1a
        continue;;
Packit 994f1a
Packit 994f1a
    -t) dstarg=$2
Packit 994f1a
	shift
Packit 994f1a
	shift
Packit 994f1a
	continue;;
Packit 994f1a
Packit 994f1a
    -T) no_target_directory=true
Packit 994f1a
	shift
Packit 994f1a
	continue;;
Packit 994f1a
Packit 994f1a
    --version) echo "$0 $scriptversion"; exit $?;;
Packit 994f1a
Packit 994f1a
    *)  # When -d is used, all remaining arguments are directories to create.
Packit 994f1a
	# When -t is used, the destination is already specified.
Packit 994f1a
	test -n "$dir_arg$dstarg" && break
Packit 994f1a
        # Otherwise, the last argument is the destination.  Remove it from $@.
Packit 994f1a
	for arg
Packit 994f1a
	do
Packit 994f1a
          if test -n "$dstarg"; then
Packit 994f1a
	    # $@ is not empty: it contains at least $arg.
Packit 994f1a
	    set fnord "$@" "$dstarg"
Packit 994f1a
	    shift # fnord
Packit 994f1a
	  fi
Packit 994f1a
	  shift # arg
Packit 994f1a
	  dstarg=$arg
Packit 994f1a
	done
Packit 994f1a
	break;;
Packit 994f1a
  esac
Packit 994f1a
done
Packit 994f1a
Packit 994f1a
if test -z "$1"; then
Packit 994f1a
  if test -z "$dir_arg"; then
Packit 994f1a
    echo "$0: no input file specified." >&2
Packit 994f1a
    exit 1
Packit 994f1a
  fi
Packit 994f1a
  # It's OK to call `install-sh -d' without argument.
Packit 994f1a
  # This can happen when creating conditional directories.
Packit 994f1a
  exit 0
Packit 994f1a
fi
Packit 994f1a
Packit 994f1a
test -n "$dir_arg" || trap '(exit $?); exit' 1 2 13 15
Packit 994f1a
Packit 994f1a
for src
Packit 994f1a
do
Packit 994f1a
  # Protect names starting with `-'.
Packit 994f1a
  case $src in
Packit 994f1a
    -*) src=./$src ;;
Packit 994f1a
  esac
Packit 994f1a
Packit 994f1a
  if test -n "$dir_arg"; then
Packit 994f1a
    dst=$src
Packit 994f1a
    dstdir=$dst
Packit 994f1a
    test -d "$dstdir"
Packit 994f1a
    dstdir_status=$?
Packit 994f1a
  else
Packit 994f1a
Packit 994f1a
    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
Packit 994f1a
    # might cause directories to be created, which would be especially bad
Packit 994f1a
    # if $src (and thus $dsttmp) contains '*'.
Packit 994f1a
    if test ! -f "$src" && test ! -d "$src"; then
Packit 994f1a
      echo "$0: $src does not exist." >&2
Packit 994f1a
      exit 1
Packit 994f1a
    fi
Packit 994f1a
Packit 994f1a
    if test -z "$dstarg"; then
Packit 994f1a
      echo "$0: no destination specified." >&2
Packit 994f1a
      exit 1
Packit 994f1a
    fi
Packit 994f1a
Packit 994f1a
    dst=$dstarg
Packit 994f1a
    # Protect names starting with `-'.
Packit 994f1a
    case $dst in
Packit 994f1a
      -*) dst=./$dst ;;
Packit 994f1a
    esac
Packit 994f1a
Packit 994f1a
    # If destination is a directory, append the input filename; won't work
Packit 994f1a
    # if double slashes aren't ignored.
Packit 994f1a
    if test -d "$dst"; then
Packit 994f1a
      if test -n "$no_target_directory"; then
Packit 994f1a
	echo "$0: $dstarg: Is a directory" >&2
Packit 994f1a
	exit 1
Packit 994f1a
      fi
Packit 994f1a
      dstdir=$dst
Packit 994f1a
      dst=$dstdir/`basename "$src"`
Packit 994f1a
      dstdir_status=0
Packit 994f1a
    else
Packit 994f1a
      # Prefer dirname, but fall back on a substitute if dirname fails.
Packit 994f1a
      dstdir=`
Packit 994f1a
	(dirname "$dst") 2>/dev/null ||
Packit 994f1a
	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
Packit 994f1a
	     X"$dst" : 'X\(//\)[^/]' \| \
Packit 994f1a
	     X"$dst" : 'X\(//\)$' \| \
Packit 994f1a
	     X"$dst" : 'X\(/\)' \| \
Packit 994f1a
	     .       : '\(.\)' 2>/dev/null ||
Packit 994f1a
	echo X"$dst" |
Packit 994f1a
	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{ s//\1/; q; }
Packit 994f1a
		  /^X\(\/\/\)[^/].*/{ s//\1/; q; }
Packit 994f1a
		  /^X\(\/\/\)$/{ s//\1/; q; }
Packit 994f1a
		  /^X\(\/\).*/{ s//\1/; q; }
Packit 994f1a
		  s/.*/./; q'
Packit 994f1a
      `
Packit 994f1a
Packit 994f1a
      test -d "$dstdir"
Packit 994f1a
      dstdir_status=$?
Packit 994f1a
    fi
Packit 994f1a
  fi
Packit 994f1a
Packit 994f1a
  obsolete_mkdir_used=false
Packit 994f1a
Packit 994f1a
  if test $dstdir_status != 0; then
Packit 994f1a
    case $posix_mkdir in
Packit 994f1a
      '')
Packit 994f1a
	posix_mkdir=false
Packit 994f1a
	if $mkdirprog -m $test_mode -p -- / >/dev/null 2>&1; then
Packit 994f1a
	  posix_mkdir=true
Packit 994f1a
	else
Packit 994f1a
	  # Remove any dirs left behind by ancient mkdir implementations.
Packit 994f1a
	  rmdir ./-m "$test_mode" ./-p ./-- 2>/dev/null
Packit 994f1a
	fi ;;
Packit 994f1a
    esac
Packit 994f1a
Packit 994f1a
    if
Packit 994f1a
      $posix_mkdir && {
Packit 994f1a
Packit 994f1a
	# With -d, create the new directory with the user-specified mode.
Packit 994f1a
	# Otherwise, create it using the same intermediate mode that
Packit 994f1a
	# mkdir -p would use when creating intermediate directories.
Packit 994f1a
	# POSIX says that this mode is "$(umask -S),u+wx", so use that
Packit 994f1a
	# if umask -S works.
Packit 994f1a
Packit 994f1a
	if test -n "$dir_arg"; then
Packit 994f1a
	  mkdir_mode=$mode
Packit 994f1a
	else
Packit 994f1a
	  case $intermediate_mode in
Packit 994f1a
	    '')
Packit 994f1a
	      if umask_S=`(umask -S) 2>/dev/null`; then
Packit 994f1a
		intermediate_mode=$umask_S,u+wx
Packit 994f1a
	      else
Packit 994f1a
		intermediate_mode=$test_mode
Packit 994f1a
	      fi ;;
Packit 994f1a
	  esac
Packit 994f1a
	  mkdir_mode=$intermediate_mode
Packit 994f1a
	fi
Packit 994f1a
Packit 994f1a
	$mkdirprog -m "$mkdir_mode" -p -- "$dstdir"
Packit 994f1a
      }
Packit 994f1a
    then :
Packit 994f1a
    else
Packit 994f1a
Packit 994f1a
      # mkdir does not conform to POSIX, or it failed possibly due to
Packit 994f1a
      # a race condition.  Create the directory the slow way, step by
Packit 994f1a
      # step, checking for races as we go.
Packit 994f1a
Packit 994f1a
      case $dstdir in
Packit 994f1a
	/*) pathcomp=/ ;;
Packit 994f1a
	-*) pathcomp=./ ;;
Packit 994f1a
	*)  pathcomp= ;;
Packit 994f1a
      esac
Packit 994f1a
Packit 994f1a
      case $posix_glob in
Packit 994f1a
        '')
Packit 994f1a
	  if (set -f) 2>/dev/null; then
Packit 994f1a
	    posix_glob=true
Packit 994f1a
	  else
Packit 994f1a
	    posix_glob=false
Packit 994f1a
	  fi ;;
Packit 994f1a
      esac
Packit 994f1a
Packit 994f1a
      oIFS=$IFS
Packit 994f1a
      IFS=/
Packit 994f1a
      $posix_glob && set -f
Packit 994f1a
      set fnord $dstdir
Packit 994f1a
      shift
Packit 994f1a
      $posix_glob && set +f
Packit 994f1a
      IFS=$oIFS
Packit 994f1a
Packit 994f1a
      for d
Packit 994f1a
      do
Packit 994f1a
	test "x$d" = x && continue
Packit 994f1a
Packit 994f1a
	pathcomp=$pathcomp$d
Packit 994f1a
	if test ! -d "$pathcomp"; then
Packit 994f1a
	  $mkdirprog "$pathcomp"
Packit 994f1a
	  # Don't fail if two instances are running concurrently.
Packit 994f1a
	  test -d "$pathcomp" || exit 1
Packit 994f1a
	fi
Packit 994f1a
	pathcomp=$pathcomp/
Packit 994f1a
      done
Packit 994f1a
      obsolete_mkdir_used=true
Packit 994f1a
    fi
Packit 994f1a
  fi
Packit 994f1a
Packit 994f1a
  if test -n "$dir_arg"; then
Packit 994f1a
    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
Packit 994f1a
    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
Packit 994f1a
    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
Packit 994f1a
      test -z "$chmodcmd" || $doit $chmodcmd "$mode" "$dst"; } || exit 1
Packit 994f1a
  else
Packit 994f1a
Packit 994f1a
    # Make a couple of temp file names in the proper directory.
Packit 994f1a
    dsttmp=$dstdir/_inst.$$_
Packit 994f1a
    rmtmp=$dstdir/_rm.$$_
Packit 994f1a
Packit 994f1a
    # Trap to clean up those temp files at exit.
Packit 994f1a
    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
Packit 994f1a
Packit 994f1a
    # Copy the file name to the temp name.
Packit 994f1a
    $doit $cpprog "$src" "$dsttmp" &&
Packit 994f1a
Packit 994f1a
    # and set any options; do chmod last to preserve setuid bits.
Packit 994f1a
    #
Packit 994f1a
    # If any of these fail, we abort the whole thing.  If we want to
Packit 994f1a
    # ignore errors from any of these, just make sure not to ignore
Packit 994f1a
    # errors from the above "$doit $cpprog $src $dsttmp" command.
Packit 994f1a
    #
Packit 994f1a
    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } \
Packit 994f1a
      && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } \
Packit 994f1a
      && { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } \
Packit 994f1a
      && { test -z "$chmodcmd" || $doit $chmodcmd "$mode" "$dsttmp"; } &&
Packit 994f1a
Packit 994f1a
    # Now rename the file to the real destination.
Packit 994f1a
    { $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null \
Packit 994f1a
      || {
Packit 994f1a
	   # The rename failed, perhaps because mv can't rename something else
Packit 994f1a
	   # to itself, or perhaps because mv is so ancient that it does not
Packit 994f1a
	   # support -f.
Packit 994f1a
Packit 994f1a
	   # Now remove or move aside any old file at destination location.
Packit 994f1a
	   # We try this two ways since rm can't unlink itself on some
Packit 994f1a
	   # systems and the destination file might be busy for other
Packit 994f1a
	   # reasons.  In this case, the final cleanup might fail but the new
Packit 994f1a
	   # file should still install successfully.
Packit 994f1a
	   {
Packit 994f1a
	     if test -f "$dst"; then
Packit 994f1a
	       $doit $rmcmd -f "$dst" 2>/dev/null \
Packit 994f1a
	       || { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null \
Packit 994f1a
		     && { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }; }\
Packit 994f1a
	       || {
Packit 994f1a
		 echo "$0: cannot unlink or rename $dst" >&2
Packit 994f1a
		 (exit 1); exit 1
Packit 994f1a
	       }
Packit 994f1a
	     else
Packit 994f1a
	       :
Packit 994f1a
	     fi
Packit 994f1a
	   } &&
Packit 994f1a
Packit 994f1a
	   # Now rename the file to the real destination.
Packit 994f1a
	   $doit $mvcmd "$dsttmp" "$dst"
Packit 994f1a
	 }
Packit 994f1a
    } || exit 1
Packit 994f1a
Packit 994f1a
    trap '' 0
Packit 994f1a
  fi
Packit 994f1a
done
Packit 994f1a
Packit 994f1a
# Local variables:
Packit 994f1a
# eval: (add-hook 'write-file-hooks 'time-stamp)
Packit 994f1a
# time-stamp-start: "scriptversion="
Packit 994f1a
# time-stamp-format: "%:y-%02m-%02d.%02H"
Packit 994f1a
# time-stamp-end: "$"
Packit 994f1a
# End: