114c0a534Smrg#!/bin/sh
214c0a534Smrg# install - install a program, script, or datafile
314c0a534Smrg
47015785aSmrgscriptversion=2024-06-19.01; # UTC
514c0a534Smrg
614c0a534Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
714c0a534Smrg# later released in X11R6 (xc/config/util/install.sh) with the
814c0a534Smrg# following copyright and license.
914c0a534Smrg#
1014c0a534Smrg# Copyright (C) 1994 X Consortium
1114c0a534Smrg#
1214c0a534Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
1314c0a534Smrg# of this software and associated documentation files (the "Software"), to
1414c0a534Smrg# deal in the Software without restriction, including without limitation the
1514c0a534Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
1614c0a534Smrg# sell copies of the Software, and to permit persons to whom the Software is
1714c0a534Smrg# furnished to do so, subject to the following conditions:
1814c0a534Smrg#
1914c0a534Smrg# The above copyright notice and this permission notice shall be included in
2014c0a534Smrg# all copies or substantial portions of the Software.
2114c0a534Smrg#
2214c0a534Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2314c0a534Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2414c0a534Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
2514c0a534Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
2614c0a534Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
2714c0a534Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2814c0a534Smrg#
2914c0a534Smrg# Except as contained in this notice, the name of the X Consortium shall not
3014c0a534Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
3114c0a534Smrg# ings in this Software without prior written authorization from the X Consor-
3214c0a534Smrg# tium.
3314c0a534Smrg#
3414c0a534Smrg#
3514c0a534Smrg# FSF changes to this file are in the public domain.
3614c0a534Smrg#
3714c0a534Smrg# Calling this script install-sh is preferred over install.sh, to prevent
3824047306Smrg# 'make' implicit rules from creating a file called install from it
3914c0a534Smrg# when there is no Makefile.
4014c0a534Smrg#
4114c0a534Smrg# This script is compatible with the BSD install script, but was written
42bf2eeab3Smrg# from scratch.
43bf2eeab3Smrg
4424047306Smrgtab='	'
45bf2eeab3Smrgnl='
46bf2eeab3Smrg'
4724047306SmrgIFS=" $tab$nl"
4814c0a534Smrg
4924047306Smrg# Set DOITPROG to "echo" to test this script.
5014c0a534Smrg
51bf2eeab3Smrgdoit=${DOITPROG-}
5224047306Smrgdoit_exec=${doit:-exec}
5314c0a534Smrg
54bf2eeab3Smrg# Put in absolute file names if you don't have them in your path;
55bf2eeab3Smrg# or use environment vars.
56bf2eeab3Smrg
57bf2eeab3Smrgchgrpprog=${CHGRPPROG-chgrp}
58bf2eeab3Smrgchmodprog=${CHMODPROG-chmod}
59bf2eeab3Smrgchownprog=${CHOWNPROG-chown}
60bf2eeab3Smrgcmpprog=${CMPPROG-cmp}
61bf2eeab3Smrgcpprog=${CPPROG-cp}
62bf2eeab3Smrgmkdirprog=${MKDIRPROG-mkdir}
63bf2eeab3Smrgmvprog=${MVPROG-mv}
64bf2eeab3Smrgrmprog=${RMPROG-rm}
65bf2eeab3Smrgstripprog=${STRIPPROG-strip}
66bf2eeab3Smrg
67bf2eeab3Smrgposix_mkdir=
68bf2eeab3Smrg
69bf2eeab3Smrg# Desired mode of installed file.
70bf2eeab3Smrgmode=0755
7114c0a534Smrg
72bdc460c5Smrg# Create dirs (including intermediate dirs) using mode 755.
73bdc460c5Smrg# This is like GNU 'install' as of coreutils 8.32 (2020).
74bdc460c5Smrgmkdir_umask=22
75bdc460c5Smrg
76bdc460c5Smrgbackupsuffix=
7714c0a534Smrgchgrpcmd=
78bf2eeab3Smrgchmodcmd=$chmodprog
79bf2eeab3Smrgchowncmd=
80bf2eeab3Smrgmvcmd=$mvprog
8114c0a534Smrgrmcmd="$rmprog -f"
82bf2eeab3Smrgstripcmd=
83bf2eeab3Smrg
8414c0a534Smrgsrc=
8514c0a534Smrgdst=
8614c0a534Smrgdir_arg=
87bf2eeab3Smrgdst_arg=
88bf2eeab3Smrg
89bf2eeab3Smrgcopy_on_change=false
9024047306Smrgis_target_a_directory=possibly
9114c0a534Smrg
92bf2eeab3Smrgusage="\
93bf2eeab3SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
9414c0a534Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
9514c0a534Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
9614c0a534Smrg   or: $0 [OPTION]... -d DIRECTORIES...
9714c0a534Smrg
9814c0a534SmrgIn the 1st form, copy SRCFILE to DSTFILE.
9914c0a534SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
10014c0a534SmrgIn the 4th, create DIRECTORIES.
10114c0a534Smrg
10214c0a534SmrgOptions:
103bf2eeab3Smrg     --help     display this help and exit.
104bf2eeab3Smrg     --version  display version info and exit.
105bf2eeab3Smrg
106bf2eeab3Smrg  -c            (ignored)
107bdc460c5Smrg  -C            install only if different (preserve data modification time)
108bf2eeab3Smrg  -d            create directories instead of installing files.
109bf2eeab3Smrg  -g GROUP      $chgrpprog installed files to GROUP.
110bf2eeab3Smrg  -m MODE       $chmodprog installed files to MODE.
111bf2eeab3Smrg  -o USER       $chownprog installed files to USER.
112bdc460c5Smrg  -p            pass -p to $cpprog.
113bf2eeab3Smrg  -s            $stripprog installed files.
114bdc460c5Smrg  -S SUFFIX     attempt to back up existing files, with suffix SUFFIX.
115bf2eeab3Smrg  -t DIRECTORY  install into DIRECTORY.
116bf2eeab3Smrg  -T            report an error if DSTFILE is a directory.
11714c0a534Smrg
11814c0a534SmrgEnvironment variables override the default commands:
119bf2eeab3Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
120bf2eeab3Smrg  RMPROG STRIPPROG
121bdc460c5Smrg
122bdc460c5SmrgBy default, rm is invoked with -f; when overridden with RMPROG,
123bdc460c5Smrgit's up to you to specify -f if you want it.
124bdc460c5Smrg
125bdc460c5SmrgIf -S is not specified, no backups are attempted.
126bdc460c5Smrg
1277015785aSmrgReport bugs to <bug-automake@gnu.org>.
1287015785aSmrgGNU Automake home page: <https://www.gnu.org/software/automake/>.
1297015785aSmrgGeneral help using GNU software: <https://www.gnu.org/gethelp/>."
13014c0a534Smrg
131bf2eeab3Smrgwhile test $# -ne 0; do
13214c0a534Smrg  case $1 in
133bf2eeab3Smrg    -c) ;;
134bf2eeab3Smrg
135bf2eeab3Smrg    -C) copy_on_change=true;;
13614c0a534Smrg
137bf2eeab3Smrg    -d) dir_arg=true;;
13814c0a534Smrg
13914c0a534Smrg    -g) chgrpcmd="$chgrpprog $2"
14024047306Smrg        shift;;
14114c0a534Smrg
14214c0a534Smrg    --help) echo "$usage"; exit $?;;
14314c0a534Smrg
144bf2eeab3Smrg    -m) mode=$2
14524047306Smrg        case $mode in
14624047306Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
14724047306Smrg            echo "$0: invalid mode: $mode" >&2
14824047306Smrg            exit 1;;
14924047306Smrg        esac
15024047306Smrg        shift;;
15114c0a534Smrg
15214c0a534Smrg    -o) chowncmd="$chownprog $2"
15324047306Smrg        shift;;
15414c0a534Smrg
155bdc460c5Smrg    -p) cpprog="$cpprog -p";;
156bdc460c5Smrg
157bf2eeab3Smrg    -s) stripcmd=$stripprog;;
15814c0a534Smrg
159bdc460c5Smrg    -S) backupsuffix="$2"
160bdc460c5Smrg        shift;;
161bdc460c5Smrg
16224047306Smrg    -t)
16324047306Smrg        is_target_a_directory=always
16424047306Smrg        dst_arg=$2
16524047306Smrg        # Protect names problematic for 'test' and other utilities.
16624047306Smrg        case $dst_arg in
16724047306Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
16824047306Smrg        esac
16924047306Smrg        shift;;
17014c0a534Smrg
17124047306Smrg    -T) is_target_a_directory=never;;
17214c0a534Smrg
1737015785aSmrg    --version) echo "$0 (GNU Automake) $scriptversion"; exit $?;;
17414c0a534Smrg
17524047306Smrg    --) shift
17624047306Smrg        break;;
177bf2eeab3Smrg
17824047306Smrg    -*) echo "$0: invalid option: $1" >&2
17924047306Smrg        exit 1;;
180bf2eeab3Smrg
181bf2eeab3Smrg    *)  break;;
18214c0a534Smrg  esac
183bf2eeab3Smrg  shift
18414c0a534Smrgdone
18514c0a534Smrg
18624047306Smrg# We allow the use of options -d and -T together, by making -d
18724047306Smrg# take the precedence; this is for compatibility with GNU install.
18824047306Smrg
18924047306Smrgif test -n "$dir_arg"; then
19024047306Smrg  if test -n "$dst_arg"; then
19124047306Smrg    echo "$0: target directory not allowed when installing a directory." >&2
19224047306Smrg    exit 1
19324047306Smrg  fi
19424047306Smrgfi
19524047306Smrg
196bf2eeab3Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
197bf2eeab3Smrg  # When -d is used, all remaining arguments are directories to create.
198bf2eeab3Smrg  # When -t is used, the destination is already specified.
199bf2eeab3Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
200bf2eeab3Smrg  for arg
201bf2eeab3Smrg  do
202bf2eeab3Smrg    if test -n "$dst_arg"; then
203bf2eeab3Smrg      # $@ is not empty: it contains at least $arg.
204bf2eeab3Smrg      set fnord "$@" "$dst_arg"
205bf2eeab3Smrg      shift # fnord
206bf2eeab3Smrg    fi
207bf2eeab3Smrg    shift # arg
208bf2eeab3Smrg    dst_arg=$arg
20924047306Smrg    # Protect names problematic for 'test' and other utilities.
21024047306Smrg    case $dst_arg in
21124047306Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
21224047306Smrg    esac
213bf2eeab3Smrg  done
214bf2eeab3Smrgfi
215bf2eeab3Smrg
216bf2eeab3Smrgif test $# -eq 0; then
21714c0a534Smrg  if test -z "$dir_arg"; then
21814c0a534Smrg    echo "$0: no input file specified." >&2
21914c0a534Smrg    exit 1
22014c0a534Smrg  fi
22124047306Smrg  # It's OK to call 'install-sh -d' without argument.
22214c0a534Smrg  # This can happen when creating conditional directories.
22314c0a534Smrg  exit 0
22414c0a534Smrgfi
22514c0a534Smrg
226bf2eeab3Smrgif test -z "$dir_arg"; then
22724047306Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
22824047306Smrg    if test ! -d "$dst_arg"; then
22924047306Smrg      echo "$0: $dst_arg: Is not a directory." >&2
23024047306Smrg      exit 1
23124047306Smrg    fi
23224047306Smrg  fi
23324047306Smrgfi
23424047306Smrg
23524047306Smrgif test -z "$dir_arg"; then
23624047306Smrg  do_exit='(exit $ret); exit $ret'
23724047306Smrg  trap "ret=129; $do_exit" 1
23824047306Smrg  trap "ret=130; $do_exit" 2
23924047306Smrg  trap "ret=141; $do_exit" 13
24024047306Smrg  trap "ret=143; $do_exit" 15
241bf2eeab3Smrg
242bf2eeab3Smrg  # Set umask so as not to create temps with too-generous modes.
243bf2eeab3Smrg  # However, 'strip' requires both read and write access to temps.
244bf2eeab3Smrg  case $mode in
245bf2eeab3Smrg    # Optimize common cases.
246bf2eeab3Smrg    *644) cp_umask=133;;
247bf2eeab3Smrg    *755) cp_umask=22;;
248bf2eeab3Smrg
249bf2eeab3Smrg    *[0-7])
250bf2eeab3Smrg      if test -z "$stripcmd"; then
25124047306Smrg        u_plus_rw=
252bf2eeab3Smrg      else
25324047306Smrg        u_plus_rw='% 200'
254bf2eeab3Smrg      fi
255bf2eeab3Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
256bf2eeab3Smrg    *)
257bf2eeab3Smrg      if test -z "$stripcmd"; then
25824047306Smrg        u_plus_rw=
259bf2eeab3Smrg      else
26024047306Smrg        u_plus_rw=,u+rw
261bf2eeab3Smrg      fi
262bf2eeab3Smrg      cp_umask=$mode$u_plus_rw;;
263bf2eeab3Smrg  esac
264bf2eeab3Smrgfi
265bf2eeab3Smrg
26614c0a534Smrgfor src
26714c0a534Smrgdo
26824047306Smrg  # Protect names problematic for 'test' and other utilities.
26914c0a534Smrg  case $src in
27024047306Smrg    -* | [=\(\)!]) src=./$src;;
27114c0a534Smrg  esac
27214c0a534Smrg
27314c0a534Smrg  if test -n "$dir_arg"; then
27414c0a534Smrg    dst=$src
275bf2eeab3Smrg    dstdir=$dst
276bf2eeab3Smrg    test -d "$dstdir"
277bf2eeab3Smrg    dstdir_status=$?
278bdc460c5Smrg    # Don't chown directories that already exist.
279bdc460c5Smrg    if test $dstdir_status = 0; then
280bdc460c5Smrg      chowncmd=""
281bdc460c5Smrg    fi
28214c0a534Smrg  else
283bf2eeab3Smrg
28414c0a534Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
28514c0a534Smrg    # might cause directories to be created, which would be especially bad
28614c0a534Smrg    # if $src (and thus $dsttmp) contains '*'.
28714c0a534Smrg    if test ! -f "$src" && test ! -d "$src"; then
28814c0a534Smrg      echo "$0: $src does not exist." >&2
28914c0a534Smrg      exit 1
29014c0a534Smrg    fi
29114c0a534Smrg
292bf2eeab3Smrg    if test -z "$dst_arg"; then
29314c0a534Smrg      echo "$0: no destination specified." >&2
29414c0a534Smrg      exit 1
29514c0a534Smrg    fi
296bf2eeab3Smrg    dst=$dst_arg
29714c0a534Smrg
298bdc460c5Smrg    # If destination is a directory, append the input filename.
29914c0a534Smrg    if test -d "$dst"; then
30024047306Smrg      if test "$is_target_a_directory" = never; then
30124047306Smrg        echo "$0: $dst_arg: Is a directory" >&2
30224047306Smrg        exit 1
30314c0a534Smrg      fi
304bf2eeab3Smrg      dstdir=$dst
305bdc460c5Smrg      dstbase=`basename "$src"`
306bdc460c5Smrg      case $dst in
307bdc460c5Smrg	*/) dst=$dst$dstbase;;
308bdc460c5Smrg	*)  dst=$dst/$dstbase;;
309bdc460c5Smrg      esac
310bf2eeab3Smrg      dstdir_status=0
311bf2eeab3Smrg    else
31224047306Smrg      dstdir=`dirname "$dst"`
313bf2eeab3Smrg      test -d "$dstdir"
314bf2eeab3Smrg      dstdir_status=$?
31514c0a534Smrg    fi
31614c0a534Smrg  fi
31714c0a534Smrg
318bdc460c5Smrg  case $dstdir in
319bdc460c5Smrg    */) dstdirslash=$dstdir;;
320bdc460c5Smrg    *)  dstdirslash=$dstdir/;;
321bdc460c5Smrg  esac
322bdc460c5Smrg
323bf2eeab3Smrg  obsolete_mkdir_used=false
324bf2eeab3Smrg
325bf2eeab3Smrg  if test $dstdir_status != 0; then
326bf2eeab3Smrg    case $posix_mkdir in
327bf2eeab3Smrg      '')
32824047306Smrg        # With -d, create the new directory with the user-specified mode.
32924047306Smrg        # Otherwise, rely on $mkdir_umask.
33024047306Smrg        if test -n "$dir_arg"; then
33124047306Smrg          mkdir_mode=-m$mode
33224047306Smrg        else
33324047306Smrg          mkdir_mode=
33424047306Smrg        fi
33524047306Smrg
33624047306Smrg        posix_mkdir=false
337bdc460c5Smrg	# The $RANDOM variable is not portable (e.g., dash).  Use it
338bdc460c5Smrg	# here however when possible just to lower collision chance.
339bdc460c5Smrg	tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
340bdc460c5Smrg
341bdc460c5Smrg	trap '
342bdc460c5Smrg	  ret=$?
343bdc460c5Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null
344bdc460c5Smrg	  exit $ret
345bdc460c5Smrg	' 0
346bdc460c5Smrg
347bdc460c5Smrg	# Because "mkdir -p" follows existing symlinks and we likely work
3487015785aSmrg	# directly in world-writable /tmp, make sure that the '$tmpdir'
349bdc460c5Smrg	# directory is successfully created first before we actually test
350bdc460c5Smrg	# 'mkdir -p'.
351bdc460c5Smrg	if (umask $mkdir_umask &&
352bdc460c5Smrg	    $mkdirprog $mkdir_mode "$tmpdir" &&
353bdc460c5Smrg	    exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
354bdc460c5Smrg	then
355bdc460c5Smrg	  if test -z "$dir_arg" || {
3567015785aSmrg	       # Check for POSIX incompatibility with -m.
357bdc460c5Smrg	       # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
358bdc460c5Smrg	       # other-writable bit of parent directory when it shouldn't.
359bdc460c5Smrg	       # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
360bdc460c5Smrg	       test_tmpdir="$tmpdir/a"
361bdc460c5Smrg	       ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
362bdc460c5Smrg	       case $ls_ld_tmpdir in
363bdc460c5Smrg		 d????-?r-*) different_mode=700;;
364bdc460c5Smrg		 d????-?--*) different_mode=755;;
365bdc460c5Smrg		 *) false;;
366bdc460c5Smrg	       esac &&
367bdc460c5Smrg	       $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
368bdc460c5Smrg		 ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
369bdc460c5Smrg		 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
370bdc460c5Smrg	       }
371bdc460c5Smrg	     }
372bdc460c5Smrg	  then posix_mkdir=:
373bdc460c5Smrg	  fi
374bdc460c5Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
375bdc460c5Smrg	else
376bdc460c5Smrg	  # Remove any dirs left behind by ancient mkdir implementations.
377bdc460c5Smrg	  rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
378bdc460c5Smrg	fi
379bdc460c5Smrg	trap '' 0;;
380bf2eeab3Smrg    esac
38114c0a534Smrg
382bf2eeab3Smrg    if
383bf2eeab3Smrg      $posix_mkdir && (
38424047306Smrg        umask $mkdir_umask &&
38524047306Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
386bf2eeab3Smrg      )
387bf2eeab3Smrg    then :
388bf2eeab3Smrg    else
38914c0a534Smrg
390bdc460c5Smrg      # mkdir does not conform to POSIX,
391bf2eeab3Smrg      # or it failed possibly due to a race condition.  Create the
392bf2eeab3Smrg      # directory the slow way, step by step, checking for races as we go.
39314c0a534Smrg
394bf2eeab3Smrg      case $dstdir in
39524047306Smrg        /*) prefix='/';;
39624047306Smrg        [-=\(\)!]*) prefix='./';;
39724047306Smrg        *)  prefix='';;
398bf2eeab3Smrg      esac
39914c0a534Smrg
400bf2eeab3Smrg      oIFS=$IFS
401bf2eeab3Smrg      IFS=/
40224047306Smrg      set -f
403bf2eeab3Smrg      set fnord $dstdir
40414c0a534Smrg      shift
40524047306Smrg      set +f
406bf2eeab3Smrg      IFS=$oIFS
407bf2eeab3Smrg
408bf2eeab3Smrg      prefixes=
409bf2eeab3Smrg
410bf2eeab3Smrg      for d
411bf2eeab3Smrg      do
41224047306Smrg        test X"$d" = X && continue
41324047306Smrg
41424047306Smrg        prefix=$prefix$d
41524047306Smrg        if test -d "$prefix"; then
41624047306Smrg          prefixes=
41724047306Smrg        else
41824047306Smrg          if $posix_mkdir; then
419bdc460c5Smrg            (umask $mkdir_umask &&
42024047306Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
42124047306Smrg            # Don't fail if two instances are running concurrently.
42224047306Smrg            test -d "$prefix" || exit 1
42324047306Smrg          else
42424047306Smrg            case $prefix in
42524047306Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
42624047306Smrg              *) qprefix=$prefix;;
42724047306Smrg            esac
42824047306Smrg            prefixes="$prefixes '$qprefix'"
42924047306Smrg          fi
43024047306Smrg        fi
43124047306Smrg        prefix=$prefix/
432bf2eeab3Smrg      done
433bf2eeab3Smrg
434bf2eeab3Smrg      if test -n "$prefixes"; then
43524047306Smrg        # Don't fail if two instances are running concurrently.
43624047306Smrg        (umask $mkdir_umask &&
43724047306Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
43824047306Smrg          test -d "$dstdir" || exit 1
43924047306Smrg        obsolete_mkdir_used=true
44014c0a534Smrg      fi
441bf2eeab3Smrg    fi
44214c0a534Smrg  fi
44314c0a534Smrg
44414c0a534Smrg  if test -n "$dir_arg"; then
445bf2eeab3Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
446bf2eeab3Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
447bf2eeab3Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
448bf2eeab3Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
44914c0a534Smrg  else
45014c0a534Smrg
45114c0a534Smrg    # Make a couple of temp file names in the proper directory.
452bdc460c5Smrg    dsttmp=${dstdirslash}_inst.$$_
453bdc460c5Smrg    rmtmp=${dstdirslash}_rm.$$_
45414c0a534Smrg
45514c0a534Smrg    # Trap to clean up those temp files at exit.
45614c0a534Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
45714c0a534Smrg
45814c0a534Smrg    # Copy the file name to the temp name.
459bdc460c5Smrg    (umask $cp_umask &&
460bdc460c5Smrg     { test -z "$stripcmd" || {
461bdc460c5Smrg	 # Create $dsttmp read-write so that cp doesn't create it read-only,
462bdc460c5Smrg	 # which would cause strip to fail.
463bdc460c5Smrg	 if test -z "$doit"; then
464bdc460c5Smrg	   : >"$dsttmp" # No need to fork-exec 'touch'.
465bdc460c5Smrg	 else
466bdc460c5Smrg	   $doit touch "$dsttmp"
467bdc460c5Smrg	 fi
468bdc460c5Smrg       }
469bdc460c5Smrg     } &&
470bdc460c5Smrg     $doit_exec $cpprog "$src" "$dsttmp") &&
47114c0a534Smrg
47214c0a534Smrg    # and set any options; do chmod last to preserve setuid bits.
47314c0a534Smrg    #
47414c0a534Smrg    # If any of these fail, we abort the whole thing.  If we want to
47514c0a534Smrg    # ignore errors from any of these, just make sure not to ignore
47614c0a534Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
47714c0a534Smrg    #
478bf2eeab3Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
479bf2eeab3Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
480bf2eeab3Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
481bf2eeab3Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
482bf2eeab3Smrg
483bf2eeab3Smrg    # If -C, don't bother to copy if it wouldn't change the file.
484bf2eeab3Smrg    if $copy_on_change &&
48524047306Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
48624047306Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
48724047306Smrg       set -f &&
488bf2eeab3Smrg       set X $old && old=:$2:$4:$5:$6 &&
489bf2eeab3Smrg       set X $new && new=:$2:$4:$5:$6 &&
49024047306Smrg       set +f &&
491bf2eeab3Smrg       test "$old" = "$new" &&
492bf2eeab3Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
493bf2eeab3Smrg    then
494bf2eeab3Smrg      rm -f "$dsttmp"
495bf2eeab3Smrg    else
496bdc460c5Smrg      # If $backupsuffix is set, and the file being installed
497bdc460c5Smrg      # already exists, attempt a backup.  Don't worry if it fails,
498bdc460c5Smrg      # e.g., if mv doesn't support -f.
499bdc460c5Smrg      if test -n "$backupsuffix" && test -f "$dst"; then
500bdc460c5Smrg        $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null
501bdc460c5Smrg      fi
502bdc460c5Smrg
503bf2eeab3Smrg      # Rename the file to the real destination.
504bf2eeab3Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
505bf2eeab3Smrg
506bf2eeab3Smrg      # The rename failed, perhaps because mv can't rename something else
507bf2eeab3Smrg      # to itself, or perhaps because mv is so ancient that it does not
508bf2eeab3Smrg      # support -f.
509bf2eeab3Smrg      {
51024047306Smrg        # Now remove or move aside any old file at destination location.
51124047306Smrg        # We try this two ways since rm can't unlink itself on some
51224047306Smrg        # systems and the destination file might be busy for other
51324047306Smrg        # reasons.  In this case, the final cleanup might fail but the new
51424047306Smrg        # file should still install successfully.
51524047306Smrg        {
51624047306Smrg          test ! -f "$dst" ||
517bdc460c5Smrg          $doit $rmcmd "$dst" 2>/dev/null ||
51824047306Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
519bdc460c5Smrg            { $doit $rmcmd "$rmtmp" 2>/dev/null; :; }
52024047306Smrg          } ||
52124047306Smrg          { echo "$0: cannot unlink or rename $dst" >&2
52224047306Smrg            (exit 1); exit 1
52324047306Smrg          }
52424047306Smrg        } &&
52524047306Smrg
52624047306Smrg        # Now rename the file to the real destination.
52724047306Smrg        $doit $mvcmd "$dsttmp" "$dst"
528bf2eeab3Smrg      }
529bf2eeab3Smrg    fi || exit 1
530bf2eeab3Smrg
531bf2eeab3Smrg    trap '' 0
532bf2eeab3Smrg  fi
53314c0a534Smrgdone
53414c0a534Smrg
53514c0a534Smrg# Local variables:
536bdc460c5Smrg# eval: (add-hook 'before-save-hook 'time-stamp)
53714c0a534Smrg# time-stamp-start: "scriptversion="
53814c0a534Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
539bdc460c5Smrg# time-stamp-time-zone: "UTC0"
540bf2eeab3Smrg# time-stamp-end: "; # UTC"
54114c0a534Smrg# End:
542