1191cded7Smrg#!/bin/sh
2191cded7Smrg# install - install a program, script, or datafile
3191cded7Smrg
48f9fac4aSmrgscriptversion=2024-06-19.01; # UTC
5191cded7Smrg
6191cded7Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7191cded7Smrg# later released in X11R6 (xc/config/util/install.sh) with the
8191cded7Smrg# following copyright and license.
9191cded7Smrg#
10191cded7Smrg# Copyright (C) 1994 X Consortium
11191cded7Smrg#
12191cded7Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13191cded7Smrg# of this software and associated documentation files (the "Software"), to
14191cded7Smrg# deal in the Software without restriction, including without limitation the
15191cded7Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16191cded7Smrg# sell copies of the Software, and to permit persons to whom the Software is
17191cded7Smrg# furnished to do so, subject to the following conditions:
18191cded7Smrg#
19191cded7Smrg# The above copyright notice and this permission notice shall be included in
20191cded7Smrg# all copies or substantial portions of the Software.
21191cded7Smrg#
22191cded7Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23191cded7Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24191cded7Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25191cded7Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26191cded7Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27191cded7Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28191cded7Smrg#
29191cded7Smrg# Except as contained in this notice, the name of the X Consortium shall not
30191cded7Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
31191cded7Smrg# ings in this Software without prior written authorization from the X Consor-
32191cded7Smrg# tium.
33191cded7Smrg#
34191cded7Smrg#
35191cded7Smrg# FSF changes to this file are in the public domain.
36191cded7Smrg#
37191cded7Smrg# Calling this script install-sh is preferred over install.sh, to prevent
38d9dcd5aeSmrg# 'make' implicit rules from creating a file called install from it
39191cded7Smrg# when there is no Makefile.
40191cded7Smrg#
41191cded7Smrg# This script is compatible with the BSD install script, but was written
42b290cf36Smrg# from scratch.
43b290cf36Smrg
4458deefebSmrgtab='	'
45b290cf36Smrgnl='
46b290cf36Smrg'
4758deefebSmrgIFS=" $tab$nl"
48191cded7Smrg
4958deefebSmrg# Set DOITPROG to "echo" to test this script.
50191cded7Smrg
51b290cf36Smrgdoit=${DOITPROG-}
5258deefebSmrgdoit_exec=${doit:-exec}
53191cded7Smrg
54b290cf36Smrg# Put in absolute file names if you don't have them in your path;
55b290cf36Smrg# or use environment vars.
56b290cf36Smrg
57b290cf36Smrgchgrpprog=${CHGRPPROG-chgrp}
58b290cf36Smrgchmodprog=${CHMODPROG-chmod}
59b290cf36Smrgchownprog=${CHOWNPROG-chown}
60b290cf36Smrgcmpprog=${CMPPROG-cmp}
61b290cf36Smrgcpprog=${CPPROG-cp}
62b290cf36Smrgmkdirprog=${MKDIRPROG-mkdir}
63b290cf36Smrgmvprog=${MVPROG-mv}
64b290cf36Smrgrmprog=${RMPROG-rm}
65b290cf36Smrgstripprog=${STRIPPROG-strip}
66b290cf36Smrg
67b290cf36Smrgposix_mkdir=
68b290cf36Smrg
69b290cf36Smrg# Desired mode of installed file.
70b290cf36Smrgmode=0755
71191cded7Smrg
72ee3e8c14Smrg# Create dirs (including intermediate dirs) using mode 755.
73ee3e8c14Smrg# This is like GNU 'install' as of coreutils 8.32 (2020).
74ee3e8c14Smrgmkdir_umask=22
75ee3e8c14Smrg
76ee3e8c14Smrgbackupsuffix=
77191cded7Smrgchgrpcmd=
78b290cf36Smrgchmodcmd=$chmodprog
79b290cf36Smrgchowncmd=
80b290cf36Smrgmvcmd=$mvprog
81191cded7Smrgrmcmd="$rmprog -f"
82b290cf36Smrgstripcmd=
83b290cf36Smrg
84191cded7Smrgsrc=
85191cded7Smrgdst=
86191cded7Smrgdir_arg=
87b290cf36Smrgdst_arg=
88b290cf36Smrg
89b290cf36Smrgcopy_on_change=false
9058deefebSmrgis_target_a_directory=possibly
91191cded7Smrg
92b290cf36Smrgusage="\
93b290cf36SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
94191cded7Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
95191cded7Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
96191cded7Smrg   or: $0 [OPTION]... -d DIRECTORIES...
97191cded7Smrg
98191cded7SmrgIn the 1st form, copy SRCFILE to DSTFILE.
99191cded7SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
100191cded7SmrgIn the 4th, create DIRECTORIES.
101191cded7Smrg
102191cded7SmrgOptions:
103b290cf36Smrg     --help     display this help and exit.
104b290cf36Smrg     --version  display version info and exit.
105b290cf36Smrg
106b290cf36Smrg  -c            (ignored)
107ee3e8c14Smrg  -C            install only if different (preserve data modification time)
108b290cf36Smrg  -d            create directories instead of installing files.
109b290cf36Smrg  -g GROUP      $chgrpprog installed files to GROUP.
110b290cf36Smrg  -m MODE       $chmodprog installed files to MODE.
111b290cf36Smrg  -o USER       $chownprog installed files to USER.
112ee3e8c14Smrg  -p            pass -p to $cpprog.
113b290cf36Smrg  -s            $stripprog installed files.
114ee3e8c14Smrg  -S SUFFIX     attempt to back up existing files, with suffix SUFFIX.
115b290cf36Smrg  -t DIRECTORY  install into DIRECTORY.
116b290cf36Smrg  -T            report an error if DSTFILE is a directory.
117191cded7Smrg
118191cded7SmrgEnvironment variables override the default commands:
119b290cf36Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
120b290cf36Smrg  RMPROG STRIPPROG
121ee3e8c14Smrg
122ee3e8c14SmrgBy default, rm is invoked with -f; when overridden with RMPROG,
123ee3e8c14Smrgit's up to you to specify -f if you want it.
124ee3e8c14Smrg
125ee3e8c14SmrgIf -S is not specified, no backups are attempted.
126ee3e8c14Smrg
1278f9fac4aSmrgReport bugs to <bug-automake@gnu.org>.
1288f9fac4aSmrgGNU Automake home page: <https://www.gnu.org/software/automake/>.
1298f9fac4aSmrgGeneral help using GNU software: <https://www.gnu.org/gethelp/>."
130191cded7Smrg
131b290cf36Smrgwhile test $# -ne 0; do
132191cded7Smrg  case $1 in
133b290cf36Smrg    -c) ;;
134b290cf36Smrg
135b290cf36Smrg    -C) copy_on_change=true;;
136191cded7Smrg
137b290cf36Smrg    -d) dir_arg=true;;
138191cded7Smrg
139191cded7Smrg    -g) chgrpcmd="$chgrpprog $2"
14058deefebSmrg        shift;;
141191cded7Smrg
142191cded7Smrg    --help) echo "$usage"; exit $?;;
143191cded7Smrg
144b290cf36Smrg    -m) mode=$2
14558deefebSmrg        case $mode in
14658deefebSmrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
14758deefebSmrg            echo "$0: invalid mode: $mode" >&2
14858deefebSmrg            exit 1;;
14958deefebSmrg        esac
15058deefebSmrg        shift;;
151191cded7Smrg
152191cded7Smrg    -o) chowncmd="$chownprog $2"
15358deefebSmrg        shift;;
154191cded7Smrg
155ee3e8c14Smrg    -p) cpprog="$cpprog -p";;
156ee3e8c14Smrg
157b290cf36Smrg    -s) stripcmd=$stripprog;;
158191cded7Smrg
159ee3e8c14Smrg    -S) backupsuffix="$2"
160ee3e8c14Smrg        shift;;
161ee3e8c14Smrg
16258deefebSmrg    -t)
16358deefebSmrg        is_target_a_directory=always
16458deefebSmrg        dst_arg=$2
16558deefebSmrg        # Protect names problematic for 'test' and other utilities.
16658deefebSmrg        case $dst_arg in
16758deefebSmrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
16858deefebSmrg        esac
16958deefebSmrg        shift;;
170191cded7Smrg
17158deefebSmrg    -T) is_target_a_directory=never;;
172191cded7Smrg
1738f9fac4aSmrg    --version) echo "$0 (GNU Automake) $scriptversion"; exit $?;;
174191cded7Smrg
17558deefebSmrg    --) shift
17658deefebSmrg        break;;
177b290cf36Smrg
17858deefebSmrg    -*) echo "$0: invalid option: $1" >&2
17958deefebSmrg        exit 1;;
180b290cf36Smrg
181b290cf36Smrg    *)  break;;
182191cded7Smrg  esac
183b290cf36Smrg  shift
184191cded7Smrgdone
185191cded7Smrg
18658deefebSmrg# We allow the use of options -d and -T together, by making -d
18758deefebSmrg# take the precedence; this is for compatibility with GNU install.
18858deefebSmrg
18958deefebSmrgif test -n "$dir_arg"; then
19058deefebSmrg  if test -n "$dst_arg"; then
19158deefebSmrg    echo "$0: target directory not allowed when installing a directory." >&2
19258deefebSmrg    exit 1
19358deefebSmrg  fi
19458deefebSmrgfi
19558deefebSmrg
196b290cf36Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
197b290cf36Smrg  # When -d is used, all remaining arguments are directories to create.
198b290cf36Smrg  # When -t is used, the destination is already specified.
199b290cf36Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
200b290cf36Smrg  for arg
201b290cf36Smrg  do
202b290cf36Smrg    if test -n "$dst_arg"; then
203b290cf36Smrg      # $@ is not empty: it contains at least $arg.
204b290cf36Smrg      set fnord "$@" "$dst_arg"
205b290cf36Smrg      shift # fnord
206b290cf36Smrg    fi
207b290cf36Smrg    shift # arg
208b290cf36Smrg    dst_arg=$arg
209d9dcd5aeSmrg    # Protect names problematic for 'test' and other utilities.
210d9dcd5aeSmrg    case $dst_arg in
211d9dcd5aeSmrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
212d9dcd5aeSmrg    esac
213b290cf36Smrg  done
214b290cf36Smrgfi
215b290cf36Smrg
216b290cf36Smrgif test $# -eq 0; then
217191cded7Smrg  if test -z "$dir_arg"; then
218191cded7Smrg    echo "$0: no input file specified." >&2
219191cded7Smrg    exit 1
220191cded7Smrg  fi
221d9dcd5aeSmrg  # It's OK to call 'install-sh -d' without argument.
222191cded7Smrg  # This can happen when creating conditional directories.
223191cded7Smrg  exit 0
224191cded7Smrgfi
225191cded7Smrg
22658deefebSmrgif test -z "$dir_arg"; then
22758deefebSmrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
22858deefebSmrg    if test ! -d "$dst_arg"; then
22958deefebSmrg      echo "$0: $dst_arg: Is not a directory." >&2
23058deefebSmrg      exit 1
23158deefebSmrg    fi
23258deefebSmrg  fi
23358deefebSmrgfi
23458deefebSmrg
235b290cf36Smrgif test -z "$dir_arg"; then
236d9dcd5aeSmrg  do_exit='(exit $ret); exit $ret'
237d9dcd5aeSmrg  trap "ret=129; $do_exit" 1
238d9dcd5aeSmrg  trap "ret=130; $do_exit" 2
239d9dcd5aeSmrg  trap "ret=141; $do_exit" 13
240d9dcd5aeSmrg  trap "ret=143; $do_exit" 15
241b290cf36Smrg
242b290cf36Smrg  # Set umask so as not to create temps with too-generous modes.
243b290cf36Smrg  # However, 'strip' requires both read and write access to temps.
244b290cf36Smrg  case $mode in
245b290cf36Smrg    # Optimize common cases.
246b290cf36Smrg    *644) cp_umask=133;;
247b290cf36Smrg    *755) cp_umask=22;;
248b290cf36Smrg
249b290cf36Smrg    *[0-7])
250b290cf36Smrg      if test -z "$stripcmd"; then
25158deefebSmrg        u_plus_rw=
252b290cf36Smrg      else
25358deefebSmrg        u_plus_rw='% 200'
254b290cf36Smrg      fi
255b290cf36Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
256b290cf36Smrg    *)
257b290cf36Smrg      if test -z "$stripcmd"; then
25858deefebSmrg        u_plus_rw=
259b290cf36Smrg      else
26058deefebSmrg        u_plus_rw=,u+rw
261b290cf36Smrg      fi
262b290cf36Smrg      cp_umask=$mode$u_plus_rw;;
263b290cf36Smrg  esac
264b290cf36Smrgfi
265b290cf36Smrg
266191cded7Smrgfor src
267191cded7Smrgdo
268d9dcd5aeSmrg  # Protect names problematic for 'test' and other utilities.
269191cded7Smrg  case $src in
270d9dcd5aeSmrg    -* | [=\(\)!]) src=./$src;;
271191cded7Smrg  esac
272191cded7Smrg
273191cded7Smrg  if test -n "$dir_arg"; then
274191cded7Smrg    dst=$src
275b290cf36Smrg    dstdir=$dst
276b290cf36Smrg    test -d "$dstdir"
277b290cf36Smrg    dstdir_status=$?
278ee3e8c14Smrg    # Don't chown directories that already exist.
279ee3e8c14Smrg    if test $dstdir_status = 0; then
280ee3e8c14Smrg      chowncmd=""
281ee3e8c14Smrg    fi
282191cded7Smrg  else
283b290cf36Smrg
284191cded7Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
285191cded7Smrg    # might cause directories to be created, which would be especially bad
286191cded7Smrg    # if $src (and thus $dsttmp) contains '*'.
287191cded7Smrg    if test ! -f "$src" && test ! -d "$src"; then
288191cded7Smrg      echo "$0: $src does not exist." >&2
289191cded7Smrg      exit 1
290191cded7Smrg    fi
291191cded7Smrg
292b290cf36Smrg    if test -z "$dst_arg"; then
293191cded7Smrg      echo "$0: no destination specified." >&2
294191cded7Smrg      exit 1
295191cded7Smrg    fi
296b290cf36Smrg    dst=$dst_arg
297191cded7Smrg
298ee3e8c14Smrg    # If destination is a directory, append the input filename.
299191cded7Smrg    if test -d "$dst"; then
30058deefebSmrg      if test "$is_target_a_directory" = never; then
30158deefebSmrg        echo "$0: $dst_arg: Is a directory" >&2
30258deefebSmrg        exit 1
303191cded7Smrg      fi
304b290cf36Smrg      dstdir=$dst
305ee3e8c14Smrg      dstbase=`basename "$src"`
306ee3e8c14Smrg      case $dst in
307ee3e8c14Smrg	*/) dst=$dst$dstbase;;
308ee3e8c14Smrg	*)  dst=$dst/$dstbase;;
309ee3e8c14Smrg      esac
310b290cf36Smrg      dstdir_status=0
311b290cf36Smrg    else
31258deefebSmrg      dstdir=`dirname "$dst"`
313b290cf36Smrg      test -d "$dstdir"
314b290cf36Smrg      dstdir_status=$?
315191cded7Smrg    fi
316191cded7Smrg  fi
317191cded7Smrg
318ee3e8c14Smrg  case $dstdir in
319ee3e8c14Smrg    */) dstdirslash=$dstdir;;
320ee3e8c14Smrg    *)  dstdirslash=$dstdir/;;
321ee3e8c14Smrg  esac
322ee3e8c14Smrg
323b290cf36Smrg  obsolete_mkdir_used=false
324b290cf36Smrg
325b290cf36Smrg  if test $dstdir_status != 0; then
326b290cf36Smrg    case $posix_mkdir in
327b290cf36Smrg      '')
32858deefebSmrg        # With -d, create the new directory with the user-specified mode.
32958deefebSmrg        # Otherwise, rely on $mkdir_umask.
33058deefebSmrg        if test -n "$dir_arg"; then
33158deefebSmrg          mkdir_mode=-m$mode
33258deefebSmrg        else
33358deefebSmrg          mkdir_mode=
33458deefebSmrg        fi
33558deefebSmrg
33658deefebSmrg        posix_mkdir=false
337ee3e8c14Smrg	# The $RANDOM variable is not portable (e.g., dash).  Use it
338ee3e8c14Smrg	# here however when possible just to lower collision chance.
339ee3e8c14Smrg	tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
340ee3e8c14Smrg
341ee3e8c14Smrg	trap '
342ee3e8c14Smrg	  ret=$?
343ee3e8c14Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null
344ee3e8c14Smrg	  exit $ret
345ee3e8c14Smrg	' 0
346ee3e8c14Smrg
347ee3e8c14Smrg	# Because "mkdir -p" follows existing symlinks and we likely work
3488f9fac4aSmrg	# directly in world-writable /tmp, make sure that the '$tmpdir'
349ee3e8c14Smrg	# directory is successfully created first before we actually test
350ee3e8c14Smrg	# 'mkdir -p'.
351ee3e8c14Smrg	if (umask $mkdir_umask &&
352ee3e8c14Smrg	    $mkdirprog $mkdir_mode "$tmpdir" &&
353ee3e8c14Smrg	    exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
354ee3e8c14Smrg	then
355ee3e8c14Smrg	  if test -z "$dir_arg" || {
3568f9fac4aSmrg	       # Check for POSIX incompatibility with -m.
357ee3e8c14Smrg	       # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
358ee3e8c14Smrg	       # other-writable bit of parent directory when it shouldn't.
359ee3e8c14Smrg	       # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
360ee3e8c14Smrg	       test_tmpdir="$tmpdir/a"
361ee3e8c14Smrg	       ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
362ee3e8c14Smrg	       case $ls_ld_tmpdir in
363ee3e8c14Smrg		 d????-?r-*) different_mode=700;;
364ee3e8c14Smrg		 d????-?--*) different_mode=755;;
365ee3e8c14Smrg		 *) false;;
366ee3e8c14Smrg	       esac &&
367ee3e8c14Smrg	       $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
368ee3e8c14Smrg		 ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
369ee3e8c14Smrg		 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
370ee3e8c14Smrg	       }
371ee3e8c14Smrg	     }
372ee3e8c14Smrg	  then posix_mkdir=:
373ee3e8c14Smrg	  fi
374ee3e8c14Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
375ee3e8c14Smrg	else
376ee3e8c14Smrg	  # Remove any dirs left behind by ancient mkdir implementations.
377ee3e8c14Smrg	  rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
378ee3e8c14Smrg	fi
379ee3e8c14Smrg	trap '' 0;;
380b290cf36Smrg    esac
381191cded7Smrg
382b290cf36Smrg    if
383b290cf36Smrg      $posix_mkdir && (
38458deefebSmrg        umask $mkdir_umask &&
38558deefebSmrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
386b290cf36Smrg      )
387b290cf36Smrg    then :
388b290cf36Smrg    else
389191cded7Smrg
390ee3e8c14Smrg      # mkdir does not conform to POSIX,
391b290cf36Smrg      # or it failed possibly due to a race condition.  Create the
392b290cf36Smrg      # directory the slow way, step by step, checking for races as we go.
393191cded7Smrg
394b290cf36Smrg      case $dstdir in
39558deefebSmrg        /*) prefix='/';;
39658deefebSmrg        [-=\(\)!]*) prefix='./';;
39758deefebSmrg        *)  prefix='';;
398b290cf36Smrg      esac
399191cded7Smrg
400b290cf36Smrg      oIFS=$IFS
401b290cf36Smrg      IFS=/
40258deefebSmrg      set -f
403b290cf36Smrg      set fnord $dstdir
404191cded7Smrg      shift
40558deefebSmrg      set +f
406b290cf36Smrg      IFS=$oIFS
407b290cf36Smrg
408b290cf36Smrg      prefixes=
409b290cf36Smrg
410b290cf36Smrg      for d
411b290cf36Smrg      do
41258deefebSmrg        test X"$d" = X && continue
41358deefebSmrg
41458deefebSmrg        prefix=$prefix$d
41558deefebSmrg        if test -d "$prefix"; then
41658deefebSmrg          prefixes=
41758deefebSmrg        else
41858deefebSmrg          if $posix_mkdir; then
419ee3e8c14Smrg            (umask $mkdir_umask &&
42058deefebSmrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
42158deefebSmrg            # Don't fail if two instances are running concurrently.
42258deefebSmrg            test -d "$prefix" || exit 1
42358deefebSmrg          else
42458deefebSmrg            case $prefix in
42558deefebSmrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
42658deefebSmrg              *) qprefix=$prefix;;
42758deefebSmrg            esac
42858deefebSmrg            prefixes="$prefixes '$qprefix'"
42958deefebSmrg          fi
43058deefebSmrg        fi
43158deefebSmrg        prefix=$prefix/
432b290cf36Smrg      done
433b290cf36Smrg
434b290cf36Smrg      if test -n "$prefixes"; then
43558deefebSmrg        # Don't fail if two instances are running concurrently.
43658deefebSmrg        (umask $mkdir_umask &&
43758deefebSmrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
43858deefebSmrg          test -d "$dstdir" || exit 1
43958deefebSmrg        obsolete_mkdir_used=true
440191cded7Smrg      fi
441b290cf36Smrg    fi
442191cded7Smrg  fi
443191cded7Smrg
444191cded7Smrg  if test -n "$dir_arg"; then
445b290cf36Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
446b290cf36Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
447b290cf36Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
448b290cf36Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
449191cded7Smrg  else
450191cded7Smrg
451191cded7Smrg    # Make a couple of temp file names in the proper directory.
452ee3e8c14Smrg    dsttmp=${dstdirslash}_inst.$$_
453ee3e8c14Smrg    rmtmp=${dstdirslash}_rm.$$_
454191cded7Smrg
455191cded7Smrg    # Trap to clean up those temp files at exit.
456191cded7Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
457191cded7Smrg
458191cded7Smrg    # Copy the file name to the temp name.
459ee3e8c14Smrg    (umask $cp_umask &&
460ee3e8c14Smrg     { test -z "$stripcmd" || {
461ee3e8c14Smrg	 # Create $dsttmp read-write so that cp doesn't create it read-only,
462ee3e8c14Smrg	 # which would cause strip to fail.
463ee3e8c14Smrg	 if test -z "$doit"; then
464ee3e8c14Smrg	   : >"$dsttmp" # No need to fork-exec 'touch'.
465ee3e8c14Smrg	 else
466ee3e8c14Smrg	   $doit touch "$dsttmp"
467ee3e8c14Smrg	 fi
468ee3e8c14Smrg       }
469ee3e8c14Smrg     } &&
470ee3e8c14Smrg     $doit_exec $cpprog "$src" "$dsttmp") &&
471191cded7Smrg
472191cded7Smrg    # and set any options; do chmod last to preserve setuid bits.
473191cded7Smrg    #
474191cded7Smrg    # If any of these fail, we abort the whole thing.  If we want to
475191cded7Smrg    # ignore errors from any of these, just make sure not to ignore
476191cded7Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
477191cded7Smrg    #
478b290cf36Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
479b290cf36Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
480b290cf36Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
481b290cf36Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
482b290cf36Smrg
483b290cf36Smrg    # If -C, don't bother to copy if it wouldn't change the file.
484b290cf36Smrg    if $copy_on_change &&
48558deefebSmrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
48658deefebSmrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
48758deefebSmrg       set -f &&
488b290cf36Smrg       set X $old && old=:$2:$4:$5:$6 &&
489b290cf36Smrg       set X $new && new=:$2:$4:$5:$6 &&
49058deefebSmrg       set +f &&
491b290cf36Smrg       test "$old" = "$new" &&
492b290cf36Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
493b290cf36Smrg    then
494b290cf36Smrg      rm -f "$dsttmp"
495b290cf36Smrg    else
496ee3e8c14Smrg      # If $backupsuffix is set, and the file being installed
497ee3e8c14Smrg      # already exists, attempt a backup.  Don't worry if it fails,
498ee3e8c14Smrg      # e.g., if mv doesn't support -f.
499ee3e8c14Smrg      if test -n "$backupsuffix" && test -f "$dst"; then
500ee3e8c14Smrg        $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null
501ee3e8c14Smrg      fi
502ee3e8c14Smrg
503b290cf36Smrg      # Rename the file to the real destination.
504b290cf36Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
505b290cf36Smrg
506b290cf36Smrg      # The rename failed, perhaps because mv can't rename something else
507b290cf36Smrg      # to itself, or perhaps because mv is so ancient that it does not
508b290cf36Smrg      # support -f.
509b290cf36Smrg      {
51058deefebSmrg        # Now remove or move aside any old file at destination location.
51158deefebSmrg        # We try this two ways since rm can't unlink itself on some
51258deefebSmrg        # systems and the destination file might be busy for other
51358deefebSmrg        # reasons.  In this case, the final cleanup might fail but the new
51458deefebSmrg        # file should still install successfully.
51558deefebSmrg        {
51658deefebSmrg          test ! -f "$dst" ||
517ee3e8c14Smrg          $doit $rmcmd "$dst" 2>/dev/null ||
51858deefebSmrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
519ee3e8c14Smrg            { $doit $rmcmd "$rmtmp" 2>/dev/null; :; }
52058deefebSmrg          } ||
52158deefebSmrg          { echo "$0: cannot unlink or rename $dst" >&2
52258deefebSmrg            (exit 1); exit 1
52358deefebSmrg          }
52458deefebSmrg        } &&
52558deefebSmrg
52658deefebSmrg        # Now rename the file to the real destination.
52758deefebSmrg        $doit $mvcmd "$dsttmp" "$dst"
528b290cf36Smrg      }
529b290cf36Smrg    fi || exit 1
530b290cf36Smrg
531b290cf36Smrg    trap '' 0
532b290cf36Smrg  fi
533191cded7Smrgdone
534191cded7Smrg
535191cded7Smrg# Local variables:
536ee3e8c14Smrg# eval: (add-hook 'before-save-hook 'time-stamp)
537191cded7Smrg# time-stamp-start: "scriptversion="
538191cded7Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
539ee3e8c14Smrg# time-stamp-time-zone: "UTC0"
540ba064abeSmrg# time-stamp-end: "; # UTC"
541191cded7Smrg# End:
542