install-sh revision 6c19a6d3
198049353Smrg#!/bin/sh
298049353Smrg# install - install a program, script, or datafile
30ad07f78Smrg
46c19a6d3Smrgscriptversion=2020-11-14.01; # UTC
50ad07f78Smrg
698049353Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
798049353Smrg# later released in X11R6 (xc/config/util/install.sh) with the
898049353Smrg# following copyright and license.
998049353Smrg#
1098049353Smrg# Copyright (C) 1994 X Consortium
1198049353Smrg#
1298049353Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
1398049353Smrg# of this software and associated documentation files (the "Software"), to
1498049353Smrg# deal in the Software without restriction, including without limitation the
1598049353Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
1698049353Smrg# sell copies of the Software, and to permit persons to whom the Software is
1798049353Smrg# furnished to do so, subject to the following conditions:
1898049353Smrg#
1998049353Smrg# The above copyright notice and this permission notice shall be included in
2098049353Smrg# all copies or substantial portions of the Software.
2198049353Smrg#
2298049353Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2398049353Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2498049353Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
2598049353Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
2698049353Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
2798049353Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2898049353Smrg#
2998049353Smrg# Except as contained in this notice, the name of the X Consortium shall not
3098049353Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
3198049353Smrg# ings in this Software without prior written authorization from the X Consor-
3298049353Smrg# tium.
3398049353Smrg#
3498049353Smrg#
3598049353Smrg# FSF changes to this file are in the public domain.
3698049353Smrg#
3798049353Smrg# Calling this script install-sh is preferred over install.sh, to prevent
386c19a6d3Smrg# 'make' implicit rules from creating a file called install from it
3998049353Smrg# when there is no Makefile.
4098049353Smrg#
4198049353Smrg# This script is compatible with the BSD install script, but was written
420ad07f78Smrg# from scratch.
4398049353Smrg
446c19a6d3Smrgtab='	'
450ad07f78Smrgnl='
460ad07f78Smrg'
476c19a6d3SmrgIFS=" $tab$nl"
4898049353Smrg
496c19a6d3Smrg# Set DOITPROG to "echo" to test this script.
5098049353Smrg
510ad07f78Smrgdoit=${DOITPROG-}
526c19a6d3Smrgdoit_exec=${doit:-exec}
5398049353Smrg
540ad07f78Smrg# Put in absolute file names if you don't have them in your path;
550ad07f78Smrg# or use environment vars.
560ad07f78Smrg
570ad07f78Smrgchgrpprog=${CHGRPPROG-chgrp}
580ad07f78Smrgchmodprog=${CHMODPROG-chmod}
590ad07f78Smrgchownprog=${CHOWNPROG-chown}
600ad07f78Smrgcmpprog=${CMPPROG-cmp}
610ad07f78Smrgcpprog=${CPPROG-cp}
620ad07f78Smrgmkdirprog=${MKDIRPROG-mkdir}
630ad07f78Smrgmvprog=${MVPROG-mv}
640ad07f78Smrgrmprog=${RMPROG-rm}
650ad07f78Smrgstripprog=${STRIPPROG-strip}
660ad07f78Smrg
670ad07f78Smrgposix_mkdir=
680ad07f78Smrg
690ad07f78Smrg# Desired mode of installed file.
700ad07f78Smrgmode=0755
710ad07f78Smrg
726c19a6d3Smrg# Create dirs (including intermediate dirs) using mode 755.
736c19a6d3Smrg# This is like GNU 'install' as of coreutils 8.32 (2020).
746c19a6d3Smrgmkdir_umask=22
756c19a6d3Smrg
766c19a6d3Smrgbackupsuffix=
770ad07f78Smrgchgrpcmd=
780ad07f78Smrgchmodcmd=$chmodprog
790ad07f78Smrgchowncmd=
800ad07f78Smrgmvcmd=$mvprog
810ad07f78Smrgrmcmd="$rmprog -f"
820ad07f78Smrgstripcmd=
8398049353Smrg
840ad07f78Smrgsrc=
850ad07f78Smrgdst=
860ad07f78Smrgdir_arg=
870ad07f78Smrgdst_arg=
8898049353Smrg
890ad07f78Smrgcopy_on_change=false
906c19a6d3Smrgis_target_a_directory=possibly
9198049353Smrg
920ad07f78Smrgusage="\
930ad07f78SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
940ad07f78Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
950ad07f78Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
960ad07f78Smrg   or: $0 [OPTION]... -d DIRECTORIES...
9798049353Smrg
980ad07f78SmrgIn the 1st form, copy SRCFILE to DSTFILE.
990ad07f78SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
1000ad07f78SmrgIn the 4th, create DIRECTORIES.
10198049353Smrg
1020ad07f78SmrgOptions:
1030ad07f78Smrg     --help     display this help and exit.
1040ad07f78Smrg     --version  display version info and exit.
10598049353Smrg
1060ad07f78Smrg  -c            (ignored)
1076c19a6d3Smrg  -C            install only if different (preserve data modification time)
1080ad07f78Smrg  -d            create directories instead of installing files.
1090ad07f78Smrg  -g GROUP      $chgrpprog installed files to GROUP.
1100ad07f78Smrg  -m MODE       $chmodprog installed files to MODE.
1110ad07f78Smrg  -o USER       $chownprog installed files to USER.
1126c19a6d3Smrg  -p            pass -p to $cpprog.
1130ad07f78Smrg  -s            $stripprog installed files.
1146c19a6d3Smrg  -S SUFFIX     attempt to back up existing files, with suffix SUFFIX.
1150ad07f78Smrg  -t DIRECTORY  install into DIRECTORY.
1160ad07f78Smrg  -T            report an error if DSTFILE is a directory.
11798049353Smrg
1180ad07f78SmrgEnvironment variables override the default commands:
1190ad07f78Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
1200ad07f78Smrg  RMPROG STRIPPROG
1216c19a6d3Smrg
1226c19a6d3SmrgBy default, rm is invoked with -f; when overridden with RMPROG,
1236c19a6d3Smrgit's up to you to specify -f if you want it.
1246c19a6d3Smrg
1256c19a6d3SmrgIf -S is not specified, no backups are attempted.
1266c19a6d3Smrg
1276c19a6d3SmrgEmail bug reports to bug-automake@gnu.org.
1286c19a6d3SmrgAutomake home page: https://www.gnu.org/software/automake/
1290ad07f78Smrg"
13098049353Smrg
1310ad07f78Smrgwhile test $# -ne 0; do
1320ad07f78Smrg  case $1 in
1330ad07f78Smrg    -c) ;;
13498049353Smrg
1350ad07f78Smrg    -C) copy_on_change=true;;
13698049353Smrg
1370ad07f78Smrg    -d) dir_arg=true;;
13898049353Smrg
1390ad07f78Smrg    -g) chgrpcmd="$chgrpprog $2"
1406c19a6d3Smrg        shift;;
14198049353Smrg
1420ad07f78Smrg    --help) echo "$usage"; exit $?;;
14398049353Smrg
1440ad07f78Smrg    -m) mode=$2
1456c19a6d3Smrg        case $mode in
1466c19a6d3Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
1476c19a6d3Smrg            echo "$0: invalid mode: $mode" >&2
1486c19a6d3Smrg            exit 1;;
1496c19a6d3Smrg        esac
1506c19a6d3Smrg        shift;;
15198049353Smrg
1520ad07f78Smrg    -o) chowncmd="$chownprog $2"
1536c19a6d3Smrg        shift;;
1546c19a6d3Smrg
1556c19a6d3Smrg    -p) cpprog="$cpprog -p";;
15698049353Smrg
1570ad07f78Smrg    -s) stripcmd=$stripprog;;
15898049353Smrg
1596c19a6d3Smrg    -S) backupsuffix="$2"
1606c19a6d3Smrg        shift;;
1616c19a6d3Smrg
1626c19a6d3Smrg    -t)
1636c19a6d3Smrg        is_target_a_directory=always
1646c19a6d3Smrg        dst_arg=$2
1656c19a6d3Smrg        # Protect names problematic for 'test' and other utilities.
1666c19a6d3Smrg        case $dst_arg in
1676c19a6d3Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
1686c19a6d3Smrg        esac
1696c19a6d3Smrg        shift;;
17098049353Smrg
1716c19a6d3Smrg    -T) is_target_a_directory=never;;
17298049353Smrg
1730ad07f78Smrg    --version) echo "$0 $scriptversion"; exit $?;;
17498049353Smrg
1756c19a6d3Smrg    --) shift
1766c19a6d3Smrg        break;;
17798049353Smrg
1786c19a6d3Smrg    -*) echo "$0: invalid option: $1" >&2
1796c19a6d3Smrg        exit 1;;
18098049353Smrg
1810ad07f78Smrg    *)  break;;
1820ad07f78Smrg  esac
1830ad07f78Smrg  shift
1840ad07f78Smrgdone
18598049353Smrg
1866c19a6d3Smrg# We allow the use of options -d and -T together, by making -d
1876c19a6d3Smrg# take the precedence; this is for compatibility with GNU install.
1886c19a6d3Smrg
1896c19a6d3Smrgif test -n "$dir_arg"; then
1906c19a6d3Smrg  if test -n "$dst_arg"; then
1916c19a6d3Smrg    echo "$0: target directory not allowed when installing a directory." >&2
1926c19a6d3Smrg    exit 1
1936c19a6d3Smrg  fi
1946c19a6d3Smrgfi
1956c19a6d3Smrg
1960ad07f78Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1970ad07f78Smrg  # When -d is used, all remaining arguments are directories to create.
1980ad07f78Smrg  # When -t is used, the destination is already specified.
1990ad07f78Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
2000ad07f78Smrg  for arg
2010ad07f78Smrg  do
2020ad07f78Smrg    if test -n "$dst_arg"; then
2030ad07f78Smrg      # $@ is not empty: it contains at least $arg.
2040ad07f78Smrg      set fnord "$@" "$dst_arg"
2050ad07f78Smrg      shift # fnord
2060ad07f78Smrg    fi
2070ad07f78Smrg    shift # arg
2080ad07f78Smrg    dst_arg=$arg
2096c19a6d3Smrg    # Protect names problematic for 'test' and other utilities.
2106c19a6d3Smrg    case $dst_arg in
2116c19a6d3Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
2126c19a6d3Smrg    esac
2130ad07f78Smrg  done
2140ad07f78Smrgfi
21598049353Smrg
2160ad07f78Smrgif test $# -eq 0; then
2170ad07f78Smrg  if test -z "$dir_arg"; then
2180ad07f78Smrg    echo "$0: no input file specified." >&2
2190ad07f78Smrg    exit 1
2200ad07f78Smrg  fi
2216c19a6d3Smrg  # It's OK to call 'install-sh -d' without argument.
2220ad07f78Smrg  # This can happen when creating conditional directories.
2230ad07f78Smrg  exit 0
2240ad07f78Smrgfi
22598049353Smrg
2260ad07f78Smrgif test -z "$dir_arg"; then
2276c19a6d3Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
2286c19a6d3Smrg    if test ! -d "$dst_arg"; then
2296c19a6d3Smrg      echo "$0: $dst_arg: Is not a directory." >&2
2306c19a6d3Smrg      exit 1
2316c19a6d3Smrg    fi
2326c19a6d3Smrg  fi
2336c19a6d3Smrgfi
2346c19a6d3Smrg
2356c19a6d3Smrgif test -z "$dir_arg"; then
2366c19a6d3Smrg  do_exit='(exit $ret); exit $ret'
2376c19a6d3Smrg  trap "ret=129; $do_exit" 1
2386c19a6d3Smrg  trap "ret=130; $do_exit" 2
2396c19a6d3Smrg  trap "ret=141; $do_exit" 13
2406c19a6d3Smrg  trap "ret=143; $do_exit" 15
2410ad07f78Smrg
2420ad07f78Smrg  # Set umask so as not to create temps with too-generous modes.
2430ad07f78Smrg  # However, 'strip' requires both read and write access to temps.
2440ad07f78Smrg  case $mode in
2450ad07f78Smrg    # Optimize common cases.
2460ad07f78Smrg    *644) cp_umask=133;;
2470ad07f78Smrg    *755) cp_umask=22;;
2480ad07f78Smrg
2490ad07f78Smrg    *[0-7])
2500ad07f78Smrg      if test -z "$stripcmd"; then
2516c19a6d3Smrg        u_plus_rw=
2520ad07f78Smrg      else
2536c19a6d3Smrg        u_plus_rw='% 200'
2540ad07f78Smrg      fi
2550ad07f78Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2560ad07f78Smrg    *)
2570ad07f78Smrg      if test -z "$stripcmd"; then
2586c19a6d3Smrg        u_plus_rw=
2590ad07f78Smrg      else
2606c19a6d3Smrg        u_plus_rw=,u+rw
2610ad07f78Smrg      fi
2620ad07f78Smrg      cp_umask=$mode$u_plus_rw;;
2630ad07f78Smrg  esac
2640ad07f78Smrgfi
26598049353Smrg
2660ad07f78Smrgfor src
2670ad07f78Smrgdo
2686c19a6d3Smrg  # Protect names problematic for 'test' and other utilities.
2690ad07f78Smrg  case $src in
2706c19a6d3Smrg    -* | [=\(\)!]) src=./$src;;
2710ad07f78Smrg  esac
2720ad07f78Smrg
2730ad07f78Smrg  if test -n "$dir_arg"; then
2740ad07f78Smrg    dst=$src
2750ad07f78Smrg    dstdir=$dst
2760ad07f78Smrg    test -d "$dstdir"
2770ad07f78Smrg    dstdir_status=$?
2786c19a6d3Smrg    # Don't chown directories that already exist.
2796c19a6d3Smrg    if test $dstdir_status = 0; then
2806c19a6d3Smrg      chowncmd=""
2816c19a6d3Smrg    fi
2820ad07f78Smrg  else
2830ad07f78Smrg
2840ad07f78Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
2850ad07f78Smrg    # might cause directories to be created, which would be especially bad
2860ad07f78Smrg    # if $src (and thus $dsttmp) contains '*'.
2870ad07f78Smrg    if test ! -f "$src" && test ! -d "$src"; then
2880ad07f78Smrg      echo "$0: $src does not exist." >&2
2890ad07f78Smrg      exit 1
2900ad07f78Smrg    fi
2910ad07f78Smrg
2920ad07f78Smrg    if test -z "$dst_arg"; then
2930ad07f78Smrg      echo "$0: no destination specified." >&2
2940ad07f78Smrg      exit 1
2950ad07f78Smrg    fi
2960ad07f78Smrg    dst=$dst_arg
29798049353Smrg
2986c19a6d3Smrg    # If destination is a directory, append the input filename.
2990ad07f78Smrg    if test -d "$dst"; then
3006c19a6d3Smrg      if test "$is_target_a_directory" = never; then
3016c19a6d3Smrg        echo "$0: $dst_arg: Is a directory" >&2
3026c19a6d3Smrg        exit 1
3030ad07f78Smrg      fi
3040ad07f78Smrg      dstdir=$dst
3056c19a6d3Smrg      dstbase=`basename "$src"`
3066c19a6d3Smrg      case $dst in
3076c19a6d3Smrg	*/) dst=$dst$dstbase;;
3086c19a6d3Smrg	*)  dst=$dst/$dstbase;;
3096c19a6d3Smrg      esac
3100ad07f78Smrg      dstdir_status=0
3110ad07f78Smrg    else
3126c19a6d3Smrg      dstdir=`dirname "$dst"`
3130ad07f78Smrg      test -d "$dstdir"
3140ad07f78Smrg      dstdir_status=$?
3150ad07f78Smrg    fi
3160ad07f78Smrg  fi
3170ad07f78Smrg
3186c19a6d3Smrg  case $dstdir in
3196c19a6d3Smrg    */) dstdirslash=$dstdir;;
3206c19a6d3Smrg    *)  dstdirslash=$dstdir/;;
3216c19a6d3Smrg  esac
3226c19a6d3Smrg
3230ad07f78Smrg  obsolete_mkdir_used=false
3240ad07f78Smrg
3250ad07f78Smrg  if test $dstdir_status != 0; then
3260ad07f78Smrg    case $posix_mkdir in
3270ad07f78Smrg      '')
3286c19a6d3Smrg        # With -d, create the new directory with the user-specified mode.
3296c19a6d3Smrg        # Otherwise, rely on $mkdir_umask.
3306c19a6d3Smrg        if test -n "$dir_arg"; then
3316c19a6d3Smrg          mkdir_mode=-m$mode
3326c19a6d3Smrg        else
3336c19a6d3Smrg          mkdir_mode=
3346c19a6d3Smrg        fi
3356c19a6d3Smrg
3366c19a6d3Smrg        posix_mkdir=false
3376c19a6d3Smrg	# The $RANDOM variable is not portable (e.g., dash).  Use it
3386c19a6d3Smrg	# here however when possible just to lower collision chance.
3396c19a6d3Smrg	tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
3406c19a6d3Smrg
3416c19a6d3Smrg	trap '
3426c19a6d3Smrg	  ret=$?
3436c19a6d3Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null
3446c19a6d3Smrg	  exit $ret
3456c19a6d3Smrg	' 0
3466c19a6d3Smrg
3476c19a6d3Smrg	# Because "mkdir -p" follows existing symlinks and we likely work
3486c19a6d3Smrg	# directly in world-writeable /tmp, make sure that the '$tmpdir'
3496c19a6d3Smrg	# directory is successfully created first before we actually test
3506c19a6d3Smrg	# 'mkdir -p'.
3516c19a6d3Smrg	if (umask $mkdir_umask &&
3526c19a6d3Smrg	    $mkdirprog $mkdir_mode "$tmpdir" &&
3536c19a6d3Smrg	    exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
3546c19a6d3Smrg	then
3556c19a6d3Smrg	  if test -z "$dir_arg" || {
3566c19a6d3Smrg	       # Check for POSIX incompatibilities with -m.
3576c19a6d3Smrg	       # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
3586c19a6d3Smrg	       # other-writable bit of parent directory when it shouldn't.
3596c19a6d3Smrg	       # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
3606c19a6d3Smrg	       test_tmpdir="$tmpdir/a"
3616c19a6d3Smrg	       ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
3626c19a6d3Smrg	       case $ls_ld_tmpdir in
3636c19a6d3Smrg		 d????-?r-*) different_mode=700;;
3646c19a6d3Smrg		 d????-?--*) different_mode=755;;
3656c19a6d3Smrg		 *) false;;
3666c19a6d3Smrg	       esac &&
3676c19a6d3Smrg	       $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
3686c19a6d3Smrg		 ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
3696c19a6d3Smrg		 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
3706c19a6d3Smrg	       }
3716c19a6d3Smrg	     }
3726c19a6d3Smrg	  then posix_mkdir=:
3736c19a6d3Smrg	  fi
3746c19a6d3Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
37598049353Smrg	else
3766c19a6d3Smrg	  # Remove any dirs left behind by ancient mkdir implementations.
3776c19a6d3Smrg	  rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
37898049353Smrg	fi
3796c19a6d3Smrg	trap '' 0;;
3800ad07f78Smrg    esac
38198049353Smrg
3820ad07f78Smrg    if
3830ad07f78Smrg      $posix_mkdir && (
3846c19a6d3Smrg        umask $mkdir_umask &&
3856c19a6d3Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3860ad07f78Smrg      )
3870ad07f78Smrg    then :
3880ad07f78Smrg    else
3890ad07f78Smrg
3906c19a6d3Smrg      # mkdir does not conform to POSIX,
3910ad07f78Smrg      # or it failed possibly due to a race condition.  Create the
3920ad07f78Smrg      # directory the slow way, step by step, checking for races as we go.
3930ad07f78Smrg
3940ad07f78Smrg      case $dstdir in
3956c19a6d3Smrg        /*) prefix='/';;
3966c19a6d3Smrg        [-=\(\)!]*) prefix='./';;
3976c19a6d3Smrg        *)  prefix='';;
3980ad07f78Smrg      esac
3990ad07f78Smrg
4000ad07f78Smrg      oIFS=$IFS
4010ad07f78Smrg      IFS=/
4026c19a6d3Smrg      set -f
4030ad07f78Smrg      set fnord $dstdir
4040ad07f78Smrg      shift
4056c19a6d3Smrg      set +f
4060ad07f78Smrg      IFS=$oIFS
4070ad07f78Smrg
4080ad07f78Smrg      prefixes=
4090ad07f78Smrg
4100ad07f78Smrg      for d
4110ad07f78Smrg      do
4126c19a6d3Smrg        test X"$d" = X && continue
4136c19a6d3Smrg
4146c19a6d3Smrg        prefix=$prefix$d
4156c19a6d3Smrg        if test -d "$prefix"; then
4166c19a6d3Smrg          prefixes=
4176c19a6d3Smrg        else
4186c19a6d3Smrg          if $posix_mkdir; then
4196c19a6d3Smrg            (umask $mkdir_umask &&
4206c19a6d3Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
4216c19a6d3Smrg            # Don't fail if two instances are running concurrently.
4226c19a6d3Smrg            test -d "$prefix" || exit 1
4236c19a6d3Smrg          else
4246c19a6d3Smrg            case $prefix in
4256c19a6d3Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
4266c19a6d3Smrg              *) qprefix=$prefix;;
4276c19a6d3Smrg            esac
4286c19a6d3Smrg            prefixes="$prefixes '$qprefix'"
4296c19a6d3Smrg          fi
4306c19a6d3Smrg        fi
4316c19a6d3Smrg        prefix=$prefix/
4320ad07f78Smrg      done
4330ad07f78Smrg
4340ad07f78Smrg      if test -n "$prefixes"; then
4356c19a6d3Smrg        # Don't fail if two instances are running concurrently.
4366c19a6d3Smrg        (umask $mkdir_umask &&
4376c19a6d3Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
4386c19a6d3Smrg          test -d "$dstdir" || exit 1
4396c19a6d3Smrg        obsolete_mkdir_used=true
4400ad07f78Smrg      fi
4410ad07f78Smrg    fi
4420ad07f78Smrg  fi
4430ad07f78Smrg
4440ad07f78Smrg  if test -n "$dir_arg"; then
4450ad07f78Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4460ad07f78Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4470ad07f78Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4480ad07f78Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
4490ad07f78Smrg  else
4500ad07f78Smrg
4510ad07f78Smrg    # Make a couple of temp file names in the proper directory.
4526c19a6d3Smrg    dsttmp=${dstdirslash}_inst.$$_
4536c19a6d3Smrg    rmtmp=${dstdirslash}_rm.$$_
4540ad07f78Smrg
4550ad07f78Smrg    # Trap to clean up those temp files at exit.
4560ad07f78Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
4570ad07f78Smrg
4580ad07f78Smrg    # Copy the file name to the temp name.
4596c19a6d3Smrg    (umask $cp_umask &&
4606c19a6d3Smrg     { test -z "$stripcmd" || {
4616c19a6d3Smrg	 # Create $dsttmp read-write so that cp doesn't create it read-only,
4626c19a6d3Smrg	 # which would cause strip to fail.
4636c19a6d3Smrg	 if test -z "$doit"; then
4646c19a6d3Smrg	   : >"$dsttmp" # No need to fork-exec 'touch'.
4656c19a6d3Smrg	 else
4666c19a6d3Smrg	   $doit touch "$dsttmp"
4676c19a6d3Smrg	 fi
4686c19a6d3Smrg       }
4696c19a6d3Smrg     } &&
4706c19a6d3Smrg     $doit_exec $cpprog "$src" "$dsttmp") &&
4710ad07f78Smrg
4720ad07f78Smrg    # and set any options; do chmod last to preserve setuid bits.
4730ad07f78Smrg    #
4740ad07f78Smrg    # If any of these fail, we abort the whole thing.  If we want to
4750ad07f78Smrg    # ignore errors from any of these, just make sure not to ignore
4760ad07f78Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
4770ad07f78Smrg    #
4780ad07f78Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
4790ad07f78Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
4800ad07f78Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
4810ad07f78Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4820ad07f78Smrg
4830ad07f78Smrg    # If -C, don't bother to copy if it wouldn't change the file.
4840ad07f78Smrg    if $copy_on_change &&
4856c19a6d3Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
4866c19a6d3Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
4876c19a6d3Smrg       set -f &&
4880ad07f78Smrg       set X $old && old=:$2:$4:$5:$6 &&
4890ad07f78Smrg       set X $new && new=:$2:$4:$5:$6 &&
4906c19a6d3Smrg       set +f &&
4910ad07f78Smrg       test "$old" = "$new" &&
4920ad07f78Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
4930ad07f78Smrg    then
4940ad07f78Smrg      rm -f "$dsttmp"
4950ad07f78Smrg    else
4966c19a6d3Smrg      # If $backupsuffix is set, and the file being installed
4976c19a6d3Smrg      # already exists, attempt a backup.  Don't worry if it fails,
4986c19a6d3Smrg      # e.g., if mv doesn't support -f.
4996c19a6d3Smrg      if test -n "$backupsuffix" && test -f "$dst"; then
5006c19a6d3Smrg        $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null
5016c19a6d3Smrg      fi
5026c19a6d3Smrg
5030ad07f78Smrg      # Rename the file to the real destination.
5040ad07f78Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
5050ad07f78Smrg
5060ad07f78Smrg      # The rename failed, perhaps because mv can't rename something else
5070ad07f78Smrg      # to itself, or perhaps because mv is so ancient that it does not
5080ad07f78Smrg      # support -f.
5090ad07f78Smrg      {
5106c19a6d3Smrg        # Now remove or move aside any old file at destination location.
5116c19a6d3Smrg        # We try this two ways since rm can't unlink itself on some
5126c19a6d3Smrg        # systems and the destination file might be busy for other
5136c19a6d3Smrg        # reasons.  In this case, the final cleanup might fail but the new
5146c19a6d3Smrg        # file should still install successfully.
5156c19a6d3Smrg        {
5166c19a6d3Smrg          test ! -f "$dst" ||
5176c19a6d3Smrg          $doit $rmcmd "$dst" 2>/dev/null ||
5186c19a6d3Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
5196c19a6d3Smrg            { $doit $rmcmd "$rmtmp" 2>/dev/null; :; }
5206c19a6d3Smrg          } ||
5216c19a6d3Smrg          { echo "$0: cannot unlink or rename $dst" >&2
5226c19a6d3Smrg            (exit 1); exit 1
5236c19a6d3Smrg          }
5246c19a6d3Smrg        } &&
5256c19a6d3Smrg
5266c19a6d3Smrg        # Now rename the file to the real destination.
5276c19a6d3Smrg        $doit $mvcmd "$dsttmp" "$dst"
5280ad07f78Smrg      }
5290ad07f78Smrg    fi || exit 1
5300ad07f78Smrg
5310ad07f78Smrg    trap '' 0
5320ad07f78Smrg  fi
5330ad07f78Smrgdone
53498049353Smrg
5350ad07f78Smrg# Local variables:
5366c19a6d3Smrg# eval: (add-hook 'before-save-hook 'time-stamp)
5370ad07f78Smrg# time-stamp-start: "scriptversion="
5380ad07f78Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
5396c19a6d3Smrg# time-stamp-time-zone: "UTC0"
5400ad07f78Smrg# time-stamp-end: "; # UTC"
5410ad07f78Smrg# End:
542