1e0963edeSmrg#!/bin/sh
2e0963edeSmrg# install - install a program, script, or datafile
3e0963edeSmrg
4c30f5000Smrgscriptversion=2020-11-14.01; # UTC
5e0963edeSmrg
6e0963edeSmrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7e0963edeSmrg# later released in X11R6 (xc/config/util/install.sh) with the
8e0963edeSmrg# following copyright and license.
9e0963edeSmrg#
10e0963edeSmrg# Copyright (C) 1994 X Consortium
11e0963edeSmrg#
12e0963edeSmrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13e0963edeSmrg# of this software and associated documentation files (the "Software"), to
14e0963edeSmrg# deal in the Software without restriction, including without limitation the
15e0963edeSmrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16e0963edeSmrg# sell copies of the Software, and to permit persons to whom the Software is
17e0963edeSmrg# furnished to do so, subject to the following conditions:
18e0963edeSmrg#
19e0963edeSmrg# The above copyright notice and this permission notice shall be included in
20e0963edeSmrg# all copies or substantial portions of the Software.
21e0963edeSmrg#
22e0963edeSmrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23e0963edeSmrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24e0963edeSmrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25e0963edeSmrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26e0963edeSmrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27e0963edeSmrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28e0963edeSmrg#
29e0963edeSmrg# Except as contained in this notice, the name of the X Consortium shall not
30e0963edeSmrg# be used in advertising or otherwise to promote the sale, use or other deal-
31e0963edeSmrg# ings in this Software without prior written authorization from the X Consor-
32e0963edeSmrg# tium.
33e0963edeSmrg#
34e0963edeSmrg#
35e0963edeSmrg# FSF changes to this file are in the public domain.
36e0963edeSmrg#
37e0963edeSmrg# Calling this script install-sh is preferred over install.sh, to prevent
38e494fffdSmrg# 'make' implicit rules from creating a file called install from it
39e0963edeSmrg# when there is no Makefile.
40e0963edeSmrg#
41e0963edeSmrg# This script is compatible with the BSD install script, but was written
42e0963edeSmrg# from scratch.
43e0963edeSmrg
44b3a827e2Smrgtab='	'
45e0963edeSmrgnl='
46e0963edeSmrg'
47b3a827e2SmrgIFS=" $tab$nl"
48e0963edeSmrg
49b3a827e2Smrg# Set DOITPROG to "echo" to test this script.
50e0963edeSmrg
51e0963edeSmrgdoit=${DOITPROG-}
52b3a827e2Smrgdoit_exec=${doit:-exec}
53e0963edeSmrg
54e0963edeSmrg# Put in absolute file names if you don't have them in your path;
55e0963edeSmrg# or use environment vars.
56e0963edeSmrg
57e0963edeSmrgchgrpprog=${CHGRPPROG-chgrp}
58e0963edeSmrgchmodprog=${CHMODPROG-chmod}
59e0963edeSmrgchownprog=${CHOWNPROG-chown}
60e0963edeSmrgcmpprog=${CMPPROG-cmp}
61e0963edeSmrgcpprog=${CPPROG-cp}
62e0963edeSmrgmkdirprog=${MKDIRPROG-mkdir}
63e0963edeSmrgmvprog=${MVPROG-mv}
64e0963edeSmrgrmprog=${RMPROG-rm}
65e0963edeSmrgstripprog=${STRIPPROG-strip}
66e0963edeSmrg
67e0963edeSmrgposix_mkdir=
68e0963edeSmrg
69e0963edeSmrg# Desired mode of installed file.
70e0963edeSmrgmode=0755
71e0963edeSmrg
72c30f5000Smrg# Create dirs (including intermediate dirs) using mode 755.
73c30f5000Smrg# This is like GNU 'install' as of coreutils 8.32 (2020).
74c30f5000Smrgmkdir_umask=22
75c30f5000Smrg
76c30f5000Smrgbackupsuffix=
77e0963edeSmrgchgrpcmd=
78e0963edeSmrgchmodcmd=$chmodprog
79e0963edeSmrgchowncmd=
80e0963edeSmrgmvcmd=$mvprog
81e0963edeSmrgrmcmd="$rmprog -f"
82e0963edeSmrgstripcmd=
83e0963edeSmrg
84e0963edeSmrgsrc=
85e0963edeSmrgdst=
86e0963edeSmrgdir_arg=
87e0963edeSmrgdst_arg=
88e0963edeSmrg
89e0963edeSmrgcopy_on_change=false
90b3a827e2Smrgis_target_a_directory=possibly
91e0963edeSmrg
92e0963edeSmrgusage="\
93e0963edeSmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
94e0963edeSmrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
95e0963edeSmrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
96e0963edeSmrg   or: $0 [OPTION]... -d DIRECTORIES...
97e0963edeSmrg
98e0963edeSmrgIn the 1st form, copy SRCFILE to DSTFILE.
99e0963edeSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
100e0963edeSmrgIn the 4th, create DIRECTORIES.
101e0963edeSmrg
102e0963edeSmrgOptions:
103e0963edeSmrg     --help     display this help and exit.
104e0963edeSmrg     --version  display version info and exit.
105e0963edeSmrg
106e0963edeSmrg  -c            (ignored)
107c30f5000Smrg  -C            install only if different (preserve data modification time)
108e0963edeSmrg  -d            create directories instead of installing files.
109e0963edeSmrg  -g GROUP      $chgrpprog installed files to GROUP.
110e0963edeSmrg  -m MODE       $chmodprog installed files to MODE.
111e0963edeSmrg  -o USER       $chownprog installed files to USER.
112c30f5000Smrg  -p            pass -p to $cpprog.
113e0963edeSmrg  -s            $stripprog installed files.
114c30f5000Smrg  -S SUFFIX     attempt to back up existing files, with suffix SUFFIX.
115e0963edeSmrg  -t DIRECTORY  install into DIRECTORY.
116e0963edeSmrg  -T            report an error if DSTFILE is a directory.
117e0963edeSmrg
118e0963edeSmrgEnvironment variables override the default commands:
119e0963edeSmrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
120e0963edeSmrg  RMPROG STRIPPROG
121c30f5000Smrg
122c30f5000SmrgBy default, rm is invoked with -f; when overridden with RMPROG,
123c30f5000Smrgit's up to you to specify -f if you want it.
124c30f5000Smrg
125c30f5000SmrgIf -S is not specified, no backups are attempted.
126c30f5000Smrg
127c30f5000SmrgEmail bug reports to bug-automake@gnu.org.
128c30f5000SmrgAutomake home page: https://www.gnu.org/software/automake/
129e0963edeSmrg"
130e0963edeSmrg
131e0963edeSmrgwhile test $# -ne 0; do
132e0963edeSmrg  case $1 in
133e0963edeSmrg    -c) ;;
134e0963edeSmrg
135e0963edeSmrg    -C) copy_on_change=true;;
136e0963edeSmrg
137e0963edeSmrg    -d) dir_arg=true;;
138e0963edeSmrg
139e0963edeSmrg    -g) chgrpcmd="$chgrpprog $2"
140b3a827e2Smrg        shift;;
141e0963edeSmrg
142e0963edeSmrg    --help) echo "$usage"; exit $?;;
143e0963edeSmrg
144e0963edeSmrg    -m) mode=$2
145b3a827e2Smrg        case $mode in
146b3a827e2Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
147b3a827e2Smrg            echo "$0: invalid mode: $mode" >&2
148b3a827e2Smrg            exit 1;;
149b3a827e2Smrg        esac
150b3a827e2Smrg        shift;;
151e0963edeSmrg
152e0963edeSmrg    -o) chowncmd="$chownprog $2"
153b3a827e2Smrg        shift;;
154e0963edeSmrg
155c30f5000Smrg    -p) cpprog="$cpprog -p";;
156c30f5000Smrg
157e0963edeSmrg    -s) stripcmd=$stripprog;;
158e0963edeSmrg
159c30f5000Smrg    -S) backupsuffix="$2"
160c30f5000Smrg        shift;;
161c30f5000Smrg
162b3a827e2Smrg    -t)
163b3a827e2Smrg        is_target_a_directory=always
164b3a827e2Smrg        dst_arg=$2
165b3a827e2Smrg        # Protect names problematic for 'test' and other utilities.
166b3a827e2Smrg        case $dst_arg in
167b3a827e2Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
168b3a827e2Smrg        esac
169b3a827e2Smrg        shift;;
170e0963edeSmrg
171b3a827e2Smrg    -T) is_target_a_directory=never;;
172e0963edeSmrg
173e0963edeSmrg    --version) echo "$0 $scriptversion"; exit $?;;
174e0963edeSmrg
175b3a827e2Smrg    --) shift
176b3a827e2Smrg        break;;
177e0963edeSmrg
178b3a827e2Smrg    -*) echo "$0: invalid option: $1" >&2
179b3a827e2Smrg        exit 1;;
180e0963edeSmrg
181e0963edeSmrg    *)  break;;
182e0963edeSmrg  esac
183e0963edeSmrg  shift
184e0963edeSmrgdone
185e0963edeSmrg
186b3a827e2Smrg# We allow the use of options -d and -T together, by making -d
187b3a827e2Smrg# take the precedence; this is for compatibility with GNU install.
188b3a827e2Smrg
189b3a827e2Smrgif test -n "$dir_arg"; then
190b3a827e2Smrg  if test -n "$dst_arg"; then
191b3a827e2Smrg    echo "$0: target directory not allowed when installing a directory." >&2
192b3a827e2Smrg    exit 1
193b3a827e2Smrg  fi
194b3a827e2Smrgfi
195b3a827e2Smrg
196e0963edeSmrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
197e0963edeSmrg  # When -d is used, all remaining arguments are directories to create.
198e0963edeSmrg  # When -t is used, the destination is already specified.
199e0963edeSmrg  # Otherwise, the last argument is the destination.  Remove it from $@.
200e0963edeSmrg  for arg
201e0963edeSmrg  do
202e0963edeSmrg    if test -n "$dst_arg"; then
203e0963edeSmrg      # $@ is not empty: it contains at least $arg.
204e0963edeSmrg      set fnord "$@" "$dst_arg"
205e0963edeSmrg      shift # fnord
206e0963edeSmrg    fi
207e0963edeSmrg    shift # arg
208e0963edeSmrg    dst_arg=$arg
209e494fffdSmrg    # Protect names problematic for 'test' and other utilities.
210e494fffdSmrg    case $dst_arg in
211e494fffdSmrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
212e494fffdSmrg    esac
213e0963edeSmrg  done
214e0963edeSmrgfi
215e0963edeSmrg
216e0963edeSmrgif test $# -eq 0; then
217e0963edeSmrg  if test -z "$dir_arg"; then
218e0963edeSmrg    echo "$0: no input file specified." >&2
219e0963edeSmrg    exit 1
220e0963edeSmrg  fi
221e494fffdSmrg  # It's OK to call 'install-sh -d' without argument.
222e0963edeSmrg  # This can happen when creating conditional directories.
223e0963edeSmrg  exit 0
224e0963edeSmrgfi
225e0963edeSmrg
226b3a827e2Smrgif test -z "$dir_arg"; then
227b3a827e2Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
228b3a827e2Smrg    if test ! -d "$dst_arg"; then
229b3a827e2Smrg      echo "$0: $dst_arg: Is not a directory." >&2
230b3a827e2Smrg      exit 1
231b3a827e2Smrg    fi
232b3a827e2Smrg  fi
233b3a827e2Smrgfi
234b3a827e2Smrg
235e0963edeSmrgif test -z "$dir_arg"; then
236e494fffdSmrg  do_exit='(exit $ret); exit $ret'
237e494fffdSmrg  trap "ret=129; $do_exit" 1
238e494fffdSmrg  trap "ret=130; $do_exit" 2
239e494fffdSmrg  trap "ret=141; $do_exit" 13
240e494fffdSmrg  trap "ret=143; $do_exit" 15
241e0963edeSmrg
242e0963edeSmrg  # Set umask so as not to create temps with too-generous modes.
243e0963edeSmrg  # However, 'strip' requires both read and write access to temps.
244e0963edeSmrg  case $mode in
245e0963edeSmrg    # Optimize common cases.
246e0963edeSmrg    *644) cp_umask=133;;
247e0963edeSmrg    *755) cp_umask=22;;
248e0963edeSmrg
249e0963edeSmrg    *[0-7])
250e0963edeSmrg      if test -z "$stripcmd"; then
251b3a827e2Smrg        u_plus_rw=
252e0963edeSmrg      else
253b3a827e2Smrg        u_plus_rw='% 200'
254e0963edeSmrg      fi
255e0963edeSmrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
256e0963edeSmrg    *)
257e0963edeSmrg      if test -z "$stripcmd"; then
258b3a827e2Smrg        u_plus_rw=
259e0963edeSmrg      else
260b3a827e2Smrg        u_plus_rw=,u+rw
261e0963edeSmrg      fi
262e0963edeSmrg      cp_umask=$mode$u_plus_rw;;
263e0963edeSmrg  esac
264e0963edeSmrgfi
265e0963edeSmrg
266e0963edeSmrgfor src
267e0963edeSmrgdo
268e494fffdSmrg  # Protect names problematic for 'test' and other utilities.
269e0963edeSmrg  case $src in
270e494fffdSmrg    -* | [=\(\)!]) src=./$src;;
271e0963edeSmrg  esac
272e0963edeSmrg
273e0963edeSmrg  if test -n "$dir_arg"; then
274e0963edeSmrg    dst=$src
275e0963edeSmrg    dstdir=$dst
276e0963edeSmrg    test -d "$dstdir"
277e0963edeSmrg    dstdir_status=$?
278c30f5000Smrg    # Don't chown directories that already exist.
279c30f5000Smrg    if test $dstdir_status = 0; then
280c30f5000Smrg      chowncmd=""
281c30f5000Smrg    fi
282e0963edeSmrg  else
283e0963edeSmrg
284e0963edeSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
285e0963edeSmrg    # might cause directories to be created, which would be especially bad
286e0963edeSmrg    # if $src (and thus $dsttmp) contains '*'.
287e0963edeSmrg    if test ! -f "$src" && test ! -d "$src"; then
288e0963edeSmrg      echo "$0: $src does not exist." >&2
289e0963edeSmrg      exit 1
290e0963edeSmrg    fi
291e0963edeSmrg
292e0963edeSmrg    if test -z "$dst_arg"; then
293e0963edeSmrg      echo "$0: no destination specified." >&2
294e0963edeSmrg      exit 1
295e0963edeSmrg    fi
296e0963edeSmrg    dst=$dst_arg
297e0963edeSmrg
298c30f5000Smrg    # If destination is a directory, append the input filename.
299e0963edeSmrg    if test -d "$dst"; then
300b3a827e2Smrg      if test "$is_target_a_directory" = never; then
301b3a827e2Smrg        echo "$0: $dst_arg: Is a directory" >&2
302b3a827e2Smrg        exit 1
303e0963edeSmrg      fi
304e0963edeSmrg      dstdir=$dst
305c30f5000Smrg      dstbase=`basename "$src"`
306c30f5000Smrg      case $dst in
307c30f5000Smrg	*/) dst=$dst$dstbase;;
308c30f5000Smrg	*)  dst=$dst/$dstbase;;
309c30f5000Smrg      esac
310e0963edeSmrg      dstdir_status=0
311e0963edeSmrg    else
312b3a827e2Smrg      dstdir=`dirname "$dst"`
313e0963edeSmrg      test -d "$dstdir"
314e0963edeSmrg      dstdir_status=$?
315e0963edeSmrg    fi
316e0963edeSmrg  fi
317e0963edeSmrg
318c30f5000Smrg  case $dstdir in
319c30f5000Smrg    */) dstdirslash=$dstdir;;
320c30f5000Smrg    *)  dstdirslash=$dstdir/;;
321c30f5000Smrg  esac
322c30f5000Smrg
323e0963edeSmrg  obsolete_mkdir_used=false
324e0963edeSmrg
325e0963edeSmrg  if test $dstdir_status != 0; then
326e0963edeSmrg    case $posix_mkdir in
327e0963edeSmrg      '')
328b3a827e2Smrg        # With -d, create the new directory with the user-specified mode.
329b3a827e2Smrg        # Otherwise, rely on $mkdir_umask.
330b3a827e2Smrg        if test -n "$dir_arg"; then
331b3a827e2Smrg          mkdir_mode=-m$mode
332b3a827e2Smrg        else
333b3a827e2Smrg          mkdir_mode=
334b3a827e2Smrg        fi
335b3a827e2Smrg
336b3a827e2Smrg        posix_mkdir=false
337c30f5000Smrg	# The $RANDOM variable is not portable (e.g., dash).  Use it
338c30f5000Smrg	# here however when possible just to lower collision chance.
339c30f5000Smrg	tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
340c30f5000Smrg
341c30f5000Smrg	trap '
342c30f5000Smrg	  ret=$?
343c30f5000Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null
344c30f5000Smrg	  exit $ret
345c30f5000Smrg	' 0
346c30f5000Smrg
347c30f5000Smrg	# Because "mkdir -p" follows existing symlinks and we likely work
348c30f5000Smrg	# directly in world-writeable /tmp, make sure that the '$tmpdir'
349c30f5000Smrg	# directory is successfully created first before we actually test
350c30f5000Smrg	# 'mkdir -p'.
351c30f5000Smrg	if (umask $mkdir_umask &&
352c30f5000Smrg	    $mkdirprog $mkdir_mode "$tmpdir" &&
353c30f5000Smrg	    exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
354c30f5000Smrg	then
355c30f5000Smrg	  if test -z "$dir_arg" || {
356c30f5000Smrg	       # Check for POSIX incompatibilities with -m.
357c30f5000Smrg	       # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
358c30f5000Smrg	       # other-writable bit of parent directory when it shouldn't.
359c30f5000Smrg	       # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
360c30f5000Smrg	       test_tmpdir="$tmpdir/a"
361c30f5000Smrg	       ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
362c30f5000Smrg	       case $ls_ld_tmpdir in
363c30f5000Smrg		 d????-?r-*) different_mode=700;;
364c30f5000Smrg		 d????-?--*) different_mode=755;;
365c30f5000Smrg		 *) false;;
366c30f5000Smrg	       esac &&
367c30f5000Smrg	       $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
368c30f5000Smrg		 ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
369c30f5000Smrg		 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
370c30f5000Smrg	       }
371c30f5000Smrg	     }
372c30f5000Smrg	  then posix_mkdir=:
373c30f5000Smrg	  fi
374c30f5000Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
375c30f5000Smrg	else
376c30f5000Smrg	  # Remove any dirs left behind by ancient mkdir implementations.
377c30f5000Smrg	  rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
378c30f5000Smrg	fi
379c30f5000Smrg	trap '' 0;;
380e0963edeSmrg    esac
381e0963edeSmrg
382e0963edeSmrg    if
383e0963edeSmrg      $posix_mkdir && (
384b3a827e2Smrg        umask $mkdir_umask &&
385b3a827e2Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
386e0963edeSmrg      )
387e0963edeSmrg    then :
388e0963edeSmrg    else
389e0963edeSmrg
390c30f5000Smrg      # mkdir does not conform to POSIX,
391e0963edeSmrg      # or it failed possibly due to a race condition.  Create the
392e0963edeSmrg      # directory the slow way, step by step, checking for races as we go.
393e0963edeSmrg
394e0963edeSmrg      case $dstdir in
395b3a827e2Smrg        /*) prefix='/';;
396b3a827e2Smrg        [-=\(\)!]*) prefix='./';;
397b3a827e2Smrg        *)  prefix='';;
398e0963edeSmrg      esac
399e0963edeSmrg
400e0963edeSmrg      oIFS=$IFS
401e0963edeSmrg      IFS=/
402b3a827e2Smrg      set -f
403e0963edeSmrg      set fnord $dstdir
404e0963edeSmrg      shift
405b3a827e2Smrg      set +f
406e0963edeSmrg      IFS=$oIFS
407e0963edeSmrg
408e0963edeSmrg      prefixes=
409e0963edeSmrg
410e0963edeSmrg      for d
411e0963edeSmrg      do
412b3a827e2Smrg        test X"$d" = X && continue
413b3a827e2Smrg
414b3a827e2Smrg        prefix=$prefix$d
415b3a827e2Smrg        if test -d "$prefix"; then
416b3a827e2Smrg          prefixes=
417b3a827e2Smrg        else
418b3a827e2Smrg          if $posix_mkdir; then
419c30f5000Smrg            (umask $mkdir_umask &&
420b3a827e2Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
421b3a827e2Smrg            # Don't fail if two instances are running concurrently.
422b3a827e2Smrg            test -d "$prefix" || exit 1
423b3a827e2Smrg          else
424b3a827e2Smrg            case $prefix in
425b3a827e2Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
426b3a827e2Smrg              *) qprefix=$prefix;;
427b3a827e2Smrg            esac
428b3a827e2Smrg            prefixes="$prefixes '$qprefix'"
429b3a827e2Smrg          fi
430b3a827e2Smrg        fi
431b3a827e2Smrg        prefix=$prefix/
432e0963edeSmrg      done
433e0963edeSmrg
434e0963edeSmrg      if test -n "$prefixes"; then
435b3a827e2Smrg        # Don't fail if two instances are running concurrently.
436b3a827e2Smrg        (umask $mkdir_umask &&
437b3a827e2Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
438b3a827e2Smrg          test -d "$dstdir" || exit 1
439b3a827e2Smrg        obsolete_mkdir_used=true
440e0963edeSmrg      fi
441e0963edeSmrg    fi
442e0963edeSmrg  fi
443e0963edeSmrg
444e0963edeSmrg  if test -n "$dir_arg"; then
445e0963edeSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
446e0963edeSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
447e0963edeSmrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
448e0963edeSmrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
449e0963edeSmrg  else
450e0963edeSmrg
451e0963edeSmrg    # Make a couple of temp file names in the proper directory.
452c30f5000Smrg    dsttmp=${dstdirslash}_inst.$$_
453c30f5000Smrg    rmtmp=${dstdirslash}_rm.$$_
454e0963edeSmrg
455e0963edeSmrg    # Trap to clean up those temp files at exit.
456e0963edeSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
457e0963edeSmrg
458e0963edeSmrg    # Copy the file name to the temp name.
459c30f5000Smrg    (umask $cp_umask &&
460c30f5000Smrg     { test -z "$stripcmd" || {
461c30f5000Smrg	 # Create $dsttmp read-write so that cp doesn't create it read-only,
462c30f5000Smrg	 # which would cause strip to fail.
463c30f5000Smrg	 if test -z "$doit"; then
464c30f5000Smrg	   : >"$dsttmp" # No need to fork-exec 'touch'.
465c30f5000Smrg	 else
466c30f5000Smrg	   $doit touch "$dsttmp"
467c30f5000Smrg	 fi
468c30f5000Smrg       }
469c30f5000Smrg     } &&
470c30f5000Smrg     $doit_exec $cpprog "$src" "$dsttmp") &&
471e0963edeSmrg
472e0963edeSmrg    # and set any options; do chmod last to preserve setuid bits.
473e0963edeSmrg    #
474e0963edeSmrg    # If any of these fail, we abort the whole thing.  If we want to
475e0963edeSmrg    # ignore errors from any of these, just make sure not to ignore
476e0963edeSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
477e0963edeSmrg    #
478e0963edeSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
479e0963edeSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
480e0963edeSmrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
481e0963edeSmrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
482e0963edeSmrg
483e0963edeSmrg    # If -C, don't bother to copy if it wouldn't change the file.
484e0963edeSmrg    if $copy_on_change &&
485b3a827e2Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
486b3a827e2Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
487b3a827e2Smrg       set -f &&
488e0963edeSmrg       set X $old && old=:$2:$4:$5:$6 &&
489e0963edeSmrg       set X $new && new=:$2:$4:$5:$6 &&
490b3a827e2Smrg       set +f &&
491e0963edeSmrg       test "$old" = "$new" &&
492e0963edeSmrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
493e0963edeSmrg    then
494e0963edeSmrg      rm -f "$dsttmp"
495e0963edeSmrg    else
496c30f5000Smrg      # If $backupsuffix is set, and the file being installed
497c30f5000Smrg      # already exists, attempt a backup.  Don't worry if it fails,
498c30f5000Smrg      # e.g., if mv doesn't support -f.
499c30f5000Smrg      if test -n "$backupsuffix" && test -f "$dst"; then
500c30f5000Smrg        $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null
501c30f5000Smrg      fi
502c30f5000Smrg
503e0963edeSmrg      # Rename the file to the real destination.
504e0963edeSmrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
505e0963edeSmrg
506e0963edeSmrg      # The rename failed, perhaps because mv can't rename something else
507e0963edeSmrg      # to itself, or perhaps because mv is so ancient that it does not
508e0963edeSmrg      # support -f.
509e0963edeSmrg      {
510b3a827e2Smrg        # Now remove or move aside any old file at destination location.
511b3a827e2Smrg        # We try this two ways since rm can't unlink itself on some
512b3a827e2Smrg        # systems and the destination file might be busy for other
513b3a827e2Smrg        # reasons.  In this case, the final cleanup might fail but the new
514b3a827e2Smrg        # file should still install successfully.
515b3a827e2Smrg        {
516b3a827e2Smrg          test ! -f "$dst" ||
517c30f5000Smrg          $doit $rmcmd "$dst" 2>/dev/null ||
518b3a827e2Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
519c30f5000Smrg            { $doit $rmcmd "$rmtmp" 2>/dev/null; :; }
520b3a827e2Smrg          } ||
521b3a827e2Smrg          { echo "$0: cannot unlink or rename $dst" >&2
522b3a827e2Smrg            (exit 1); exit 1
523b3a827e2Smrg          }
524b3a827e2Smrg        } &&
525b3a827e2Smrg
526b3a827e2Smrg        # Now rename the file to the real destination.
527b3a827e2Smrg        $doit $mvcmd "$dsttmp" "$dst"
528e0963edeSmrg      }
529e0963edeSmrg    fi || exit 1
530e0963edeSmrg
531e0963edeSmrg    trap '' 0
532e0963edeSmrg  fi
533e0963edeSmrgdone
534e0963edeSmrg
535e0963edeSmrg# Local variables:
536c30f5000Smrg# eval: (add-hook 'before-save-hook 'time-stamp)
537e0963edeSmrg# time-stamp-start: "scriptversion="
538e0963edeSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
539b3a827e2Smrg# time-stamp-time-zone: "UTC0"
540f05b35a2Smrg# time-stamp-end: "; # UTC"
541e0963edeSmrg# End:
542