11abf7346Smrg#!/bin/sh
21abf7346Smrg# install - install a program, script, or datafile
31abf7346Smrg
4a5a2a776Smrgscriptversion=2020-11-14.01; # UTC
51abf7346Smrg
61abf7346Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
71abf7346Smrg# later released in X11R6 (xc/config/util/install.sh) with the
81abf7346Smrg# following copyright and license.
91abf7346Smrg#
101abf7346Smrg# Copyright (C) 1994 X Consortium
111abf7346Smrg#
121abf7346Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
131abf7346Smrg# of this software and associated documentation files (the "Software"), to
141abf7346Smrg# deal in the Software without restriction, including without limitation the
151abf7346Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
161abf7346Smrg# sell copies of the Software, and to permit persons to whom the Software is
171abf7346Smrg# furnished to do so, subject to the following conditions:
181abf7346Smrg#
191abf7346Smrg# The above copyright notice and this permission notice shall be included in
201abf7346Smrg# all copies or substantial portions of the Software.
211abf7346Smrg#
221abf7346Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
231abf7346Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
241abf7346Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
251abf7346Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
261abf7346Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
271abf7346Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
281abf7346Smrg#
291abf7346Smrg# Except as contained in this notice, the name of the X Consortium shall not
301abf7346Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
311abf7346Smrg# ings in this Software without prior written authorization from the X Consor-
321abf7346Smrg# tium.
331abf7346Smrg#
341abf7346Smrg#
351abf7346Smrg# FSF changes to this file are in the public domain.
361abf7346Smrg#
371abf7346Smrg# Calling this script install-sh is preferred over install.sh, to prevent
386d36ef34Smrg# 'make' implicit rules from creating a file called install from it
391abf7346Smrg# when there is no Makefile.
401abf7346Smrg#
411abf7346Smrg# This script is compatible with the BSD install script, but was written
421abf7346Smrg# from scratch.
431abf7346Smrg
44da4a0041Smrgtab='	'
451abf7346Smrgnl='
461abf7346Smrg'
47da4a0041SmrgIFS=" $tab$nl"
481abf7346Smrg
49da4a0041Smrg# Set DOITPROG to "echo" to test this script.
501abf7346Smrg
518b6d6341Smrgdoit=${DOITPROG-}
52da4a0041Smrgdoit_exec=${doit:-exec}
531abf7346Smrg
541abf7346Smrg# Put in absolute file names if you don't have them in your path;
551abf7346Smrg# or use environment vars.
561abf7346Smrg
578b6d6341Smrgchgrpprog=${CHGRPPROG-chgrp}
588b6d6341Smrgchmodprog=${CHMODPROG-chmod}
598b6d6341Smrgchownprog=${CHOWNPROG-chown}
608b6d6341Smrgcmpprog=${CMPPROG-cmp}
618b6d6341Smrgcpprog=${CPPROG-cp}
628b6d6341Smrgmkdirprog=${MKDIRPROG-mkdir}
638b6d6341Smrgmvprog=${MVPROG-mv}
648b6d6341Smrgrmprog=${RMPROG-rm}
658b6d6341Smrgstripprog=${STRIPPROG-strip}
668b6d6341Smrg
671abf7346Smrgposix_mkdir=
681abf7346Smrg
691abf7346Smrg# Desired mode of installed file.
701abf7346Smrgmode=0755
711abf7346Smrg
72a5a2a776Smrg# Create dirs (including intermediate dirs) using mode 755.
73a5a2a776Smrg# This is like GNU 'install' as of coreutils 8.32 (2020).
74a5a2a776Smrgmkdir_umask=22
75a5a2a776Smrg
76a5a2a776Smrgbackupsuffix=
778b6d6341Smrgchgrpcmd=
781abf7346Smrgchmodcmd=$chmodprog
791abf7346Smrgchowncmd=
808b6d6341Smrgmvcmd=$mvprog
811abf7346Smrgrmcmd="$rmprog -f"
828b6d6341Smrgstripcmd=
838b6d6341Smrg
841abf7346Smrgsrc=
851abf7346Smrgdst=
861abf7346Smrgdir_arg=
878b6d6341Smrgdst_arg=
888b6d6341Smrg
898b6d6341Smrgcopy_on_change=false
90da4a0041Smrgis_target_a_directory=possibly
911abf7346Smrg
928b6d6341Smrgusage="\
938b6d6341SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
941abf7346Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
951abf7346Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
961abf7346Smrg   or: $0 [OPTION]... -d DIRECTORIES...
971abf7346Smrg
981abf7346SmrgIn the 1st form, copy SRCFILE to DSTFILE.
991abf7346SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
1001abf7346SmrgIn the 4th, create DIRECTORIES.
1011abf7346Smrg
1021abf7346SmrgOptions:
1038b6d6341Smrg     --help     display this help and exit.
1048b6d6341Smrg     --version  display version info and exit.
1058b6d6341Smrg
1068b6d6341Smrg  -c            (ignored)
107a5a2a776Smrg  -C            install only if different (preserve data modification time)
1088b6d6341Smrg  -d            create directories instead of installing files.
1098b6d6341Smrg  -g GROUP      $chgrpprog installed files to GROUP.
1108b6d6341Smrg  -m MODE       $chmodprog installed files to MODE.
1118b6d6341Smrg  -o USER       $chownprog installed files to USER.
112a5a2a776Smrg  -p            pass -p to $cpprog.
1138b6d6341Smrg  -s            $stripprog installed files.
114a5a2a776Smrg  -S SUFFIX     attempt to back up existing files, with suffix SUFFIX.
1158b6d6341Smrg  -t DIRECTORY  install into DIRECTORY.
1168b6d6341Smrg  -T            report an error if DSTFILE is a directory.
1171abf7346Smrg
1181abf7346SmrgEnvironment variables override the default commands:
1198b6d6341Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
1208b6d6341Smrg  RMPROG STRIPPROG
121a5a2a776Smrg
122a5a2a776SmrgBy default, rm is invoked with -f; when overridden with RMPROG,
123a5a2a776Smrgit's up to you to specify -f if you want it.
124a5a2a776Smrg
125a5a2a776SmrgIf -S is not specified, no backups are attempted.
126a5a2a776Smrg
127a5a2a776SmrgEmail bug reports to bug-automake@gnu.org.
128a5a2a776SmrgAutomake home page: https://www.gnu.org/software/automake/
1291abf7346Smrg"
1301abf7346Smrg
1311abf7346Smrgwhile test $# -ne 0; do
1321abf7346Smrg  case $1 in
1338b6d6341Smrg    -c) ;;
1348b6d6341Smrg
1358b6d6341Smrg    -C) copy_on_change=true;;
1361abf7346Smrg
1378b6d6341Smrg    -d) dir_arg=true;;
1381abf7346Smrg
1391abf7346Smrg    -g) chgrpcmd="$chgrpprog $2"
140da4a0041Smrg        shift;;
1411abf7346Smrg
1421abf7346Smrg    --help) echo "$usage"; exit $?;;
1431abf7346Smrg
1441abf7346Smrg    -m) mode=$2
145da4a0041Smrg        case $mode in
146da4a0041Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
147da4a0041Smrg            echo "$0: invalid mode: $mode" >&2
148da4a0041Smrg            exit 1;;
149da4a0041Smrg        esac
150da4a0041Smrg        shift;;
1511abf7346Smrg
1521abf7346Smrg    -o) chowncmd="$chownprog $2"
153da4a0041Smrg        shift;;
1541abf7346Smrg
155a5a2a776Smrg    -p) cpprog="$cpprog -p";;
156a5a2a776Smrg
1578b6d6341Smrg    -s) stripcmd=$stripprog;;
1581abf7346Smrg
159a5a2a776Smrg    -S) backupsuffix="$2"
160a5a2a776Smrg        shift;;
161a5a2a776Smrg
162da4a0041Smrg    -t)
163da4a0041Smrg        is_target_a_directory=always
164da4a0041Smrg        dst_arg=$2
165da4a0041Smrg        # Protect names problematic for 'test' and other utilities.
166da4a0041Smrg        case $dst_arg in
167da4a0041Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
168da4a0041Smrg        esac
169da4a0041Smrg        shift;;
1701abf7346Smrg
171da4a0041Smrg    -T) is_target_a_directory=never;;
1721abf7346Smrg
1731abf7346Smrg    --version) echo "$0 $scriptversion"; exit $?;;
1741abf7346Smrg
175da4a0041Smrg    --) shift
176da4a0041Smrg        break;;
1771abf7346Smrg
178da4a0041Smrg    -*) echo "$0: invalid option: $1" >&2
179da4a0041Smrg        exit 1;;
1801abf7346Smrg
1811abf7346Smrg    *)  break;;
1821abf7346Smrg  esac
1838b6d6341Smrg  shift
1841abf7346Smrgdone
1851abf7346Smrg
186da4a0041Smrg# We allow the use of options -d and -T together, by making -d
187da4a0041Smrg# take the precedence; this is for compatibility with GNU install.
188da4a0041Smrg
189da4a0041Smrgif test -n "$dir_arg"; then
190da4a0041Smrg  if test -n "$dst_arg"; then
191da4a0041Smrg    echo "$0: target directory not allowed when installing a directory." >&2
192da4a0041Smrg    exit 1
193da4a0041Smrg  fi
194da4a0041Smrgfi
195da4a0041Smrg
1968b6d6341Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1971abf7346Smrg  # When -d is used, all remaining arguments are directories to create.
1981abf7346Smrg  # When -t is used, the destination is already specified.
1991abf7346Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
2001abf7346Smrg  for arg
2011abf7346Smrg  do
2028b6d6341Smrg    if test -n "$dst_arg"; then
2031abf7346Smrg      # $@ is not empty: it contains at least $arg.
2048b6d6341Smrg      set fnord "$@" "$dst_arg"
2051abf7346Smrg      shift # fnord
2061abf7346Smrg    fi
2071abf7346Smrg    shift # arg
2088b6d6341Smrg    dst_arg=$arg
2096d36ef34Smrg    # Protect names problematic for 'test' and other utilities.
2106d36ef34Smrg    case $dst_arg in
2116d36ef34Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
2126d36ef34Smrg    esac
2131abf7346Smrg  done
2141abf7346Smrgfi
2151abf7346Smrg
2161abf7346Smrgif test $# -eq 0; then
2171abf7346Smrg  if test -z "$dir_arg"; then
2181abf7346Smrg    echo "$0: no input file specified." >&2
2191abf7346Smrg    exit 1
2201abf7346Smrg  fi
2216d36ef34Smrg  # It's OK to call 'install-sh -d' without argument.
2221abf7346Smrg  # This can happen when creating conditional directories.
2231abf7346Smrg  exit 0
2241abf7346Smrgfi
2251abf7346Smrg
226da4a0041Smrgif test -z "$dir_arg"; then
227da4a0041Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
228da4a0041Smrg    if test ! -d "$dst_arg"; then
229da4a0041Smrg      echo "$0: $dst_arg: Is not a directory." >&2
230da4a0041Smrg      exit 1
231da4a0041Smrg    fi
232da4a0041Smrg  fi
233da4a0041Smrgfi
234da4a0041Smrg
2351abf7346Smrgif test -z "$dir_arg"; then
2366d36ef34Smrg  do_exit='(exit $ret); exit $ret'
2376d36ef34Smrg  trap "ret=129; $do_exit" 1
2386d36ef34Smrg  trap "ret=130; $do_exit" 2
2396d36ef34Smrg  trap "ret=141; $do_exit" 13
2406d36ef34Smrg  trap "ret=143; $do_exit" 15
2411abf7346Smrg
2421abf7346Smrg  # Set umask so as not to create temps with too-generous modes.
2431abf7346Smrg  # However, 'strip' requires both read and write access to temps.
2441abf7346Smrg  case $mode in
2451abf7346Smrg    # Optimize common cases.
2461abf7346Smrg    *644) cp_umask=133;;
2471abf7346Smrg    *755) cp_umask=22;;
2481abf7346Smrg
2491abf7346Smrg    *[0-7])
2501abf7346Smrg      if test -z "$stripcmd"; then
251da4a0041Smrg        u_plus_rw=
2521abf7346Smrg      else
253da4a0041Smrg        u_plus_rw='% 200'
2541abf7346Smrg      fi
2551abf7346Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2561abf7346Smrg    *)
2571abf7346Smrg      if test -z "$stripcmd"; then
258da4a0041Smrg        u_plus_rw=
2591abf7346Smrg      else
260da4a0041Smrg        u_plus_rw=,u+rw
2611abf7346Smrg      fi
2621abf7346Smrg      cp_umask=$mode$u_plus_rw;;
2631abf7346Smrg  esac
2641abf7346Smrgfi
2651abf7346Smrg
2661abf7346Smrgfor src
2671abf7346Smrgdo
2686d36ef34Smrg  # Protect names problematic for 'test' and other utilities.
2691abf7346Smrg  case $src in
2706d36ef34Smrg    -* | [=\(\)!]) src=./$src;;
2711abf7346Smrg  esac
2721abf7346Smrg
2731abf7346Smrg  if test -n "$dir_arg"; then
2741abf7346Smrg    dst=$src
2751abf7346Smrg    dstdir=$dst
2761abf7346Smrg    test -d "$dstdir"
2771abf7346Smrg    dstdir_status=$?
278a5a2a776Smrg    # Don't chown directories that already exist.
279a5a2a776Smrg    if test $dstdir_status = 0; then
280a5a2a776Smrg      chowncmd=""
281a5a2a776Smrg    fi
2821abf7346Smrg  else
2831abf7346Smrg
2841abf7346Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
2851abf7346Smrg    # might cause directories to be created, which would be especially bad
2861abf7346Smrg    # if $src (and thus $dsttmp) contains '*'.
2871abf7346Smrg    if test ! -f "$src" && test ! -d "$src"; then
2881abf7346Smrg      echo "$0: $src does not exist." >&2
2891abf7346Smrg      exit 1
2901abf7346Smrg    fi
2911abf7346Smrg
2928b6d6341Smrg    if test -z "$dst_arg"; then
2931abf7346Smrg      echo "$0: no destination specified." >&2
2941abf7346Smrg      exit 1
2951abf7346Smrg    fi
2968b6d6341Smrg    dst=$dst_arg
2971abf7346Smrg
298a5a2a776Smrg    # If destination is a directory, append the input filename.
2991abf7346Smrg    if test -d "$dst"; then
300da4a0041Smrg      if test "$is_target_a_directory" = never; then
301da4a0041Smrg        echo "$0: $dst_arg: Is a directory" >&2
302da4a0041Smrg        exit 1
3031abf7346Smrg      fi
3041abf7346Smrg      dstdir=$dst
305a5a2a776Smrg      dstbase=`basename "$src"`
306a5a2a776Smrg      case $dst in
307a5a2a776Smrg	*/) dst=$dst$dstbase;;
308a5a2a776Smrg	*)  dst=$dst/$dstbase;;
309a5a2a776Smrg      esac
3101abf7346Smrg      dstdir_status=0
3111abf7346Smrg    else
312da4a0041Smrg      dstdir=`dirname "$dst"`
3131abf7346Smrg      test -d "$dstdir"
3141abf7346Smrg      dstdir_status=$?
3151abf7346Smrg    fi
3161abf7346Smrg  fi
3171abf7346Smrg
318a5a2a776Smrg  case $dstdir in
319a5a2a776Smrg    */) dstdirslash=$dstdir;;
320a5a2a776Smrg    *)  dstdirslash=$dstdir/;;
321a5a2a776Smrg  esac
322a5a2a776Smrg
3231abf7346Smrg  obsolete_mkdir_used=false
3241abf7346Smrg
3251abf7346Smrg  if test $dstdir_status != 0; then
3261abf7346Smrg    case $posix_mkdir in
3271abf7346Smrg      '')
328da4a0041Smrg        # With -d, create the new directory with the user-specified mode.
329da4a0041Smrg        # Otherwise, rely on $mkdir_umask.
330da4a0041Smrg        if test -n "$dir_arg"; then
331da4a0041Smrg          mkdir_mode=-m$mode
332da4a0041Smrg        else
333da4a0041Smrg          mkdir_mode=
334da4a0041Smrg        fi
335da4a0041Smrg
336da4a0041Smrg        posix_mkdir=false
337a5a2a776Smrg	# The $RANDOM variable is not portable (e.g., dash).  Use it
338a5a2a776Smrg	# here however when possible just to lower collision chance.
339a5a2a776Smrg	tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
340a5a2a776Smrg
341a5a2a776Smrg	trap '
342a5a2a776Smrg	  ret=$?
343a5a2a776Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null
344a5a2a776Smrg	  exit $ret
345a5a2a776Smrg	' 0
346a5a2a776Smrg
347a5a2a776Smrg	# Because "mkdir -p" follows existing symlinks and we likely work
348a5a2a776Smrg	# directly in world-writeable /tmp, make sure that the '$tmpdir'
349a5a2a776Smrg	# directory is successfully created first before we actually test
350a5a2a776Smrg	# 'mkdir -p'.
351a5a2a776Smrg	if (umask $mkdir_umask &&
352a5a2a776Smrg	    $mkdirprog $mkdir_mode "$tmpdir" &&
353a5a2a776Smrg	    exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
354a5a2a776Smrg	then
355a5a2a776Smrg	  if test -z "$dir_arg" || {
356a5a2a776Smrg	       # Check for POSIX incompatibilities with -m.
357a5a2a776Smrg	       # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
358a5a2a776Smrg	       # other-writable bit of parent directory when it shouldn't.
359a5a2a776Smrg	       # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
360a5a2a776Smrg	       test_tmpdir="$tmpdir/a"
361a5a2a776Smrg	       ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
362a5a2a776Smrg	       case $ls_ld_tmpdir in
363a5a2a776Smrg		 d????-?r-*) different_mode=700;;
364a5a2a776Smrg		 d????-?--*) different_mode=755;;
365a5a2a776Smrg		 *) false;;
366a5a2a776Smrg	       esac &&
367a5a2a776Smrg	       $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
368a5a2a776Smrg		 ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
369a5a2a776Smrg		 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
370a5a2a776Smrg	       }
371a5a2a776Smrg	     }
372a5a2a776Smrg	  then posix_mkdir=:
373a5a2a776Smrg	  fi
374a5a2a776Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
375a5a2a776Smrg	else
376a5a2a776Smrg	  # Remove any dirs left behind by ancient mkdir implementations.
377a5a2a776Smrg	  rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
378a5a2a776Smrg	fi
379a5a2a776Smrg	trap '' 0;;
3801abf7346Smrg    esac
3811abf7346Smrg
3821abf7346Smrg    if
3831abf7346Smrg      $posix_mkdir && (
384da4a0041Smrg        umask $mkdir_umask &&
385da4a0041Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3861abf7346Smrg      )
3871abf7346Smrg    then :
3881abf7346Smrg    else
3891abf7346Smrg
390a5a2a776Smrg      # mkdir does not conform to POSIX,
3911abf7346Smrg      # or it failed possibly due to a race condition.  Create the
3921abf7346Smrg      # directory the slow way, step by step, checking for races as we go.
3931abf7346Smrg
3941abf7346Smrg      case $dstdir in
395da4a0041Smrg        /*) prefix='/';;
396da4a0041Smrg        [-=\(\)!]*) prefix='./';;
397da4a0041Smrg        *)  prefix='';;
3981abf7346Smrg      esac
3991abf7346Smrg
4001abf7346Smrg      oIFS=$IFS
4011abf7346Smrg      IFS=/
402da4a0041Smrg      set -f
4031abf7346Smrg      set fnord $dstdir
4041abf7346Smrg      shift
405da4a0041Smrg      set +f
4061abf7346Smrg      IFS=$oIFS
4071abf7346Smrg
4081abf7346Smrg      prefixes=
4091abf7346Smrg
4101abf7346Smrg      for d
4111abf7346Smrg      do
412da4a0041Smrg        test X"$d" = X && continue
413da4a0041Smrg
414da4a0041Smrg        prefix=$prefix$d
415da4a0041Smrg        if test -d "$prefix"; then
416da4a0041Smrg          prefixes=
417da4a0041Smrg        else
418da4a0041Smrg          if $posix_mkdir; then
419a5a2a776Smrg            (umask $mkdir_umask &&
420da4a0041Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
421da4a0041Smrg            # Don't fail if two instances are running concurrently.
422da4a0041Smrg            test -d "$prefix" || exit 1
423da4a0041Smrg          else
424da4a0041Smrg            case $prefix in
425da4a0041Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
426da4a0041Smrg              *) qprefix=$prefix;;
427da4a0041Smrg            esac
428da4a0041Smrg            prefixes="$prefixes '$qprefix'"
429da4a0041Smrg          fi
430da4a0041Smrg        fi
431da4a0041Smrg        prefix=$prefix/
4321abf7346Smrg      done
4331abf7346Smrg
4341abf7346Smrg      if test -n "$prefixes"; then
435da4a0041Smrg        # Don't fail if two instances are running concurrently.
436da4a0041Smrg        (umask $mkdir_umask &&
437da4a0041Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
438da4a0041Smrg          test -d "$dstdir" || exit 1
439da4a0041Smrg        obsolete_mkdir_used=true
4401abf7346Smrg      fi
4411abf7346Smrg    fi
4421abf7346Smrg  fi
4431abf7346Smrg
4441abf7346Smrg  if test -n "$dir_arg"; then
4451abf7346Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4461abf7346Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4471abf7346Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4481abf7346Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
4491abf7346Smrg  else
4501abf7346Smrg
4511abf7346Smrg    # Make a couple of temp file names in the proper directory.
452a5a2a776Smrg    dsttmp=${dstdirslash}_inst.$$_
453a5a2a776Smrg    rmtmp=${dstdirslash}_rm.$$_
4541abf7346Smrg
4551abf7346Smrg    # Trap to clean up those temp files at exit.
4561abf7346Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
4571abf7346Smrg
4581abf7346Smrg    # Copy the file name to the temp name.
459a5a2a776Smrg    (umask $cp_umask &&
460a5a2a776Smrg     { test -z "$stripcmd" || {
461a5a2a776Smrg	 # Create $dsttmp read-write so that cp doesn't create it read-only,
462a5a2a776Smrg	 # which would cause strip to fail.
463a5a2a776Smrg	 if test -z "$doit"; then
464a5a2a776Smrg	   : >"$dsttmp" # No need to fork-exec 'touch'.
465a5a2a776Smrg	 else
466a5a2a776Smrg	   $doit touch "$dsttmp"
467a5a2a776Smrg	 fi
468a5a2a776Smrg       }
469a5a2a776Smrg     } &&
470a5a2a776Smrg     $doit_exec $cpprog "$src" "$dsttmp") &&
4711abf7346Smrg
4721abf7346Smrg    # and set any options; do chmod last to preserve setuid bits.
4731abf7346Smrg    #
4741abf7346Smrg    # If any of these fail, we abort the whole thing.  If we want to
4751abf7346Smrg    # ignore errors from any of these, just make sure not to ignore
4761abf7346Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
4771abf7346Smrg    #
4788b6d6341Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
4798b6d6341Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
4808b6d6341Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
4818b6d6341Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4828b6d6341Smrg
4838b6d6341Smrg    # If -C, don't bother to copy if it wouldn't change the file.
4848b6d6341Smrg    if $copy_on_change &&
485da4a0041Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
486da4a0041Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
487da4a0041Smrg       set -f &&
4888b6d6341Smrg       set X $old && old=:$2:$4:$5:$6 &&
4898b6d6341Smrg       set X $new && new=:$2:$4:$5:$6 &&
490da4a0041Smrg       set +f &&
4918b6d6341Smrg       test "$old" = "$new" &&
4928b6d6341Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
4938b6d6341Smrg    then
4948b6d6341Smrg      rm -f "$dsttmp"
4958b6d6341Smrg    else
496a5a2a776Smrg      # If $backupsuffix is set, and the file being installed
497a5a2a776Smrg      # already exists, attempt a backup.  Don't worry if it fails,
498a5a2a776Smrg      # e.g., if mv doesn't support -f.
499a5a2a776Smrg      if test -n "$backupsuffix" && test -f "$dst"; then
500a5a2a776Smrg        $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null
501a5a2a776Smrg      fi
502a5a2a776Smrg
5038b6d6341Smrg      # Rename the file to the real destination.
5048b6d6341Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
5058b6d6341Smrg
5068b6d6341Smrg      # The rename failed, perhaps because mv can't rename something else
5078b6d6341Smrg      # to itself, or perhaps because mv is so ancient that it does not
5088b6d6341Smrg      # support -f.
5098b6d6341Smrg      {
510da4a0041Smrg        # Now remove or move aside any old file at destination location.
511da4a0041Smrg        # We try this two ways since rm can't unlink itself on some
512da4a0041Smrg        # systems and the destination file might be busy for other
513da4a0041Smrg        # reasons.  In this case, the final cleanup might fail but the new
514da4a0041Smrg        # file should still install successfully.
515da4a0041Smrg        {
516da4a0041Smrg          test ! -f "$dst" ||
517a5a2a776Smrg          $doit $rmcmd "$dst" 2>/dev/null ||
518da4a0041Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
519a5a2a776Smrg            { $doit $rmcmd "$rmtmp" 2>/dev/null; :; }
520da4a0041Smrg          } ||
521da4a0041Smrg          { echo "$0: cannot unlink or rename $dst" >&2
522da4a0041Smrg            (exit 1); exit 1
523da4a0041Smrg          }
524da4a0041Smrg        } &&
525da4a0041Smrg
526da4a0041Smrg        # Now rename the file to the real destination.
527da4a0041Smrg        $doit $mvcmd "$dsttmp" "$dst"
5288b6d6341Smrg      }
5298b6d6341Smrg    fi || exit 1
5301abf7346Smrg
5311abf7346Smrg    trap '' 0
5321abf7346Smrg  fi
5331abf7346Smrgdone
5341abf7346Smrg
5351abf7346Smrg# Local variables:
536a5a2a776Smrg# eval: (add-hook 'before-save-hook 'time-stamp)
5371abf7346Smrg# time-stamp-start: "scriptversion="
5381abf7346Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
539a5a2a776Smrg# time-stamp-time-zone: "UTC0"
5408b6d6341Smrg# time-stamp-end: "; # UTC"
5411abf7346Smrg# End:
542