1659607e0Smrg#!/bin/sh
2659607e0Smrg# install - install a program, script, or datafile
3659607e0Smrg
4370b807fSmrgscriptversion=2020-11-14.01; # UTC
5659607e0Smrg
6659607e0Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7659607e0Smrg# later released in X11R6 (xc/config/util/install.sh) with the
8659607e0Smrg# following copyright and license.
9659607e0Smrg#
10659607e0Smrg# Copyright (C) 1994 X Consortium
11659607e0Smrg#
12659607e0Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13659607e0Smrg# of this software and associated documentation files (the "Software"), to
14659607e0Smrg# deal in the Software without restriction, including without limitation the
15659607e0Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16659607e0Smrg# sell copies of the Software, and to permit persons to whom the Software is
17659607e0Smrg# furnished to do so, subject to the following conditions:
18659607e0Smrg#
19659607e0Smrg# The above copyright notice and this permission notice shall be included in
20659607e0Smrg# all copies or substantial portions of the Software.
21659607e0Smrg#
22659607e0Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23659607e0Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24659607e0Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25659607e0Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26659607e0Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27659607e0Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28659607e0Smrg#
29659607e0Smrg# Except as contained in this notice, the name of the X Consortium shall not
30659607e0Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
31659607e0Smrg# ings in this Software without prior written authorization from the X Consor-
32659607e0Smrg# tium.
33659607e0Smrg#
34659607e0Smrg#
35659607e0Smrg# FSF changes to this file are in the public domain.
36659607e0Smrg#
37659607e0Smrg# Calling this script install-sh is preferred over install.sh, to prevent
38a73597f9Smrg# 'make' implicit rules from creating a file called install from it
39659607e0Smrg# when there is no Makefile.
40659607e0Smrg#
41659607e0Smrg# This script is compatible with the BSD install script, but was written
42659607e0Smrg# from scratch.
43659607e0Smrg
44370b807fSmrgtab='	'
45659607e0Smrgnl='
46659607e0Smrg'
47370b807fSmrgIFS=" $tab$nl"
48659607e0Smrg
49370b807fSmrg# Set DOITPROG to "echo" to test this script.
50659607e0Smrg
51fc27e79cSmrgdoit=${DOITPROG-}
52370b807fSmrgdoit_exec=${doit:-exec}
53659607e0Smrg
54659607e0Smrg# Put in absolute file names if you don't have them in your path;
55659607e0Smrg# or use environment vars.
56659607e0Smrg
57fc27e79cSmrgchgrpprog=${CHGRPPROG-chgrp}
58fc27e79cSmrgchmodprog=${CHMODPROG-chmod}
59fc27e79cSmrgchownprog=${CHOWNPROG-chown}
60fc27e79cSmrgcmpprog=${CMPPROG-cmp}
61fc27e79cSmrgcpprog=${CPPROG-cp}
62fc27e79cSmrgmkdirprog=${MKDIRPROG-mkdir}
63fc27e79cSmrgmvprog=${MVPROG-mv}
64fc27e79cSmrgrmprog=${RMPROG-rm}
65fc27e79cSmrgstripprog=${STRIPPROG-strip}
66fc27e79cSmrg
67659607e0Smrgposix_mkdir=
68659607e0Smrg
69659607e0Smrg# Desired mode of installed file.
70659607e0Smrgmode=0755
71659607e0Smrg
72370b807fSmrg# Create dirs (including intermediate dirs) using mode 755.
73370b807fSmrg# This is like GNU 'install' as of coreutils 8.32 (2020).
74370b807fSmrgmkdir_umask=22
75370b807fSmrg
76370b807fSmrgbackupsuffix=
77fc27e79cSmrgchgrpcmd=
78659607e0Smrgchmodcmd=$chmodprog
79659607e0Smrgchowncmd=
80fc27e79cSmrgmvcmd=$mvprog
81659607e0Smrgrmcmd="$rmprog -f"
82fc27e79cSmrgstripcmd=
83fc27e79cSmrg
84659607e0Smrgsrc=
85659607e0Smrgdst=
86659607e0Smrgdir_arg=
87fc27e79cSmrgdst_arg=
88fc27e79cSmrg
89fc27e79cSmrgcopy_on_change=false
90370b807fSmrgis_target_a_directory=possibly
91659607e0Smrg
92fc27e79cSmrgusage="\
93fc27e79cSmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
94659607e0Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
95659607e0Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
96659607e0Smrg   or: $0 [OPTION]... -d DIRECTORIES...
97659607e0Smrg
98659607e0SmrgIn the 1st form, copy SRCFILE to DSTFILE.
99659607e0SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
100659607e0SmrgIn the 4th, create DIRECTORIES.
101659607e0Smrg
102659607e0SmrgOptions:
103fc27e79cSmrg     --help     display this help and exit.
104fc27e79cSmrg     --version  display version info and exit.
105fc27e79cSmrg
106fc27e79cSmrg  -c            (ignored)
107370b807fSmrg  -C            install only if different (preserve data modification time)
108fc27e79cSmrg  -d            create directories instead of installing files.
109fc27e79cSmrg  -g GROUP      $chgrpprog installed files to GROUP.
110fc27e79cSmrg  -m MODE       $chmodprog installed files to MODE.
111fc27e79cSmrg  -o USER       $chownprog installed files to USER.
112370b807fSmrg  -p            pass -p to $cpprog.
113fc27e79cSmrg  -s            $stripprog installed files.
114370b807fSmrg  -S SUFFIX     attempt to back up existing files, with suffix SUFFIX.
115fc27e79cSmrg  -t DIRECTORY  install into DIRECTORY.
116fc27e79cSmrg  -T            report an error if DSTFILE is a directory.
117659607e0Smrg
118659607e0SmrgEnvironment variables override the default commands:
119fc27e79cSmrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
120fc27e79cSmrg  RMPROG STRIPPROG
121370b807fSmrg
122370b807fSmrgBy default, rm is invoked with -f; when overridden with RMPROG,
123370b807fSmrgit's up to you to specify -f if you want it.
124370b807fSmrg
125370b807fSmrgIf -S is not specified, no backups are attempted.
126370b807fSmrg
127370b807fSmrgEmail bug reports to bug-automake@gnu.org.
128370b807fSmrgAutomake home page: https://www.gnu.org/software/automake/
129659607e0Smrg"
130659607e0Smrg
131659607e0Smrgwhile test $# -ne 0; do
132659607e0Smrg  case $1 in
133fc27e79cSmrg    -c) ;;
134fc27e79cSmrg
135fc27e79cSmrg    -C) copy_on_change=true;;
136659607e0Smrg
137fc27e79cSmrg    -d) dir_arg=true;;
138659607e0Smrg
139659607e0Smrg    -g) chgrpcmd="$chgrpprog $2"
140370b807fSmrg        shift;;
141659607e0Smrg
142659607e0Smrg    --help) echo "$usage"; exit $?;;
143659607e0Smrg
144659607e0Smrg    -m) mode=$2
145370b807fSmrg        case $mode in
146370b807fSmrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
147370b807fSmrg            echo "$0: invalid mode: $mode" >&2
148370b807fSmrg            exit 1;;
149370b807fSmrg        esac
150370b807fSmrg        shift;;
151659607e0Smrg
152659607e0Smrg    -o) chowncmd="$chownprog $2"
153370b807fSmrg        shift;;
154370b807fSmrg
155370b807fSmrg    -p) cpprog="$cpprog -p";;
156659607e0Smrg
157fc27e79cSmrg    -s) stripcmd=$stripprog;;
158659607e0Smrg
159370b807fSmrg    -S) backupsuffix="$2"
160370b807fSmrg        shift;;
161659607e0Smrg
162370b807fSmrg    -t)
163370b807fSmrg        is_target_a_directory=always
164370b807fSmrg        dst_arg=$2
165370b807fSmrg        # Protect names problematic for 'test' and other utilities.
166370b807fSmrg        case $dst_arg in
167370b807fSmrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
168370b807fSmrg        esac
169370b807fSmrg        shift;;
170370b807fSmrg
171370b807fSmrg    -T) is_target_a_directory=never;;
172659607e0Smrg
173659607e0Smrg    --version) echo "$0 $scriptversion"; exit $?;;
174659607e0Smrg
175370b807fSmrg    --) shift
176370b807fSmrg        break;;
177659607e0Smrg
178370b807fSmrg    -*) echo "$0: invalid option: $1" >&2
179370b807fSmrg        exit 1;;
180659607e0Smrg
181659607e0Smrg    *)  break;;
182659607e0Smrg  esac
183fc27e79cSmrg  shift
184659607e0Smrgdone
185659607e0Smrg
186370b807fSmrg# We allow the use of options -d and -T together, by making -d
187370b807fSmrg# take the precedence; this is for compatibility with GNU install.
188370b807fSmrg
189370b807fSmrgif test -n "$dir_arg"; then
190370b807fSmrg  if test -n "$dst_arg"; then
191370b807fSmrg    echo "$0: target directory not allowed when installing a directory." >&2
192370b807fSmrg    exit 1
193370b807fSmrg  fi
194370b807fSmrgfi
195370b807fSmrg
196fc27e79cSmrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
197659607e0Smrg  # When -d is used, all remaining arguments are directories to create.
198659607e0Smrg  # When -t is used, the destination is already specified.
199659607e0Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
200659607e0Smrg  for arg
201659607e0Smrg  do
202fc27e79cSmrg    if test -n "$dst_arg"; then
203659607e0Smrg      # $@ is not empty: it contains at least $arg.
204fc27e79cSmrg      set fnord "$@" "$dst_arg"
205659607e0Smrg      shift # fnord
206659607e0Smrg    fi
207659607e0Smrg    shift # arg
208fc27e79cSmrg    dst_arg=$arg
209a73597f9Smrg    # Protect names problematic for 'test' and other utilities.
210a73597f9Smrg    case $dst_arg in
211a73597f9Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
212a73597f9Smrg    esac
213659607e0Smrg  done
214659607e0Smrgfi
215659607e0Smrg
216659607e0Smrgif test $# -eq 0; then
217659607e0Smrg  if test -z "$dir_arg"; then
218659607e0Smrg    echo "$0: no input file specified." >&2
219659607e0Smrg    exit 1
220659607e0Smrg  fi
221a73597f9Smrg  # It's OK to call 'install-sh -d' without argument.
222659607e0Smrg  # This can happen when creating conditional directories.
223659607e0Smrg  exit 0
224659607e0Smrgfi
225659607e0Smrg
226370b807fSmrgif test -z "$dir_arg"; then
227370b807fSmrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
228370b807fSmrg    if test ! -d "$dst_arg"; then
229370b807fSmrg      echo "$0: $dst_arg: Is not a directory." >&2
230370b807fSmrg      exit 1
231370b807fSmrg    fi
232370b807fSmrg  fi
233370b807fSmrgfi
234370b807fSmrg
235659607e0Smrgif test -z "$dir_arg"; then
236a73597f9Smrg  do_exit='(exit $ret); exit $ret'
237a73597f9Smrg  trap "ret=129; $do_exit" 1
238a73597f9Smrg  trap "ret=130; $do_exit" 2
239a73597f9Smrg  trap "ret=141; $do_exit" 13
240a73597f9Smrg  trap "ret=143; $do_exit" 15
241659607e0Smrg
242659607e0Smrg  # Set umask so as not to create temps with too-generous modes.
243659607e0Smrg  # However, 'strip' requires both read and write access to temps.
244659607e0Smrg  case $mode in
245659607e0Smrg    # Optimize common cases.
246659607e0Smrg    *644) cp_umask=133;;
247659607e0Smrg    *755) cp_umask=22;;
248659607e0Smrg
249659607e0Smrg    *[0-7])
250659607e0Smrg      if test -z "$stripcmd"; then
251370b807fSmrg        u_plus_rw=
252659607e0Smrg      else
253370b807fSmrg        u_plus_rw='% 200'
254659607e0Smrg      fi
255659607e0Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
256659607e0Smrg    *)
257659607e0Smrg      if test -z "$stripcmd"; then
258370b807fSmrg        u_plus_rw=
259659607e0Smrg      else
260370b807fSmrg        u_plus_rw=,u+rw
261659607e0Smrg      fi
262659607e0Smrg      cp_umask=$mode$u_plus_rw;;
263659607e0Smrg  esac
264659607e0Smrgfi
265659607e0Smrg
266659607e0Smrgfor src
267659607e0Smrgdo
268a73597f9Smrg  # Protect names problematic for 'test' and other utilities.
269659607e0Smrg  case $src in
270a73597f9Smrg    -* | [=\(\)!]) src=./$src;;
271659607e0Smrg  esac
272659607e0Smrg
273659607e0Smrg  if test -n "$dir_arg"; then
274659607e0Smrg    dst=$src
275659607e0Smrg    dstdir=$dst
276659607e0Smrg    test -d "$dstdir"
277659607e0Smrg    dstdir_status=$?
278370b807fSmrg    # Don't chown directories that already exist.
279370b807fSmrg    if test $dstdir_status = 0; then
280370b807fSmrg      chowncmd=""
281370b807fSmrg    fi
282659607e0Smrg  else
283659607e0Smrg
284659607e0Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
285659607e0Smrg    # might cause directories to be created, which would be especially bad
286659607e0Smrg    # if $src (and thus $dsttmp) contains '*'.
287659607e0Smrg    if test ! -f "$src" && test ! -d "$src"; then
288659607e0Smrg      echo "$0: $src does not exist." >&2
289659607e0Smrg      exit 1
290659607e0Smrg    fi
291659607e0Smrg
292fc27e79cSmrg    if test -z "$dst_arg"; then
293659607e0Smrg      echo "$0: no destination specified." >&2
294659607e0Smrg      exit 1
295659607e0Smrg    fi
296fc27e79cSmrg    dst=$dst_arg
297659607e0Smrg
298370b807fSmrg    # If destination is a directory, append the input filename.
299659607e0Smrg    if test -d "$dst"; then
300370b807fSmrg      if test "$is_target_a_directory" = never; then
301370b807fSmrg        echo "$0: $dst_arg: Is a directory" >&2
302370b807fSmrg        exit 1
303659607e0Smrg      fi
304659607e0Smrg      dstdir=$dst
305370b807fSmrg      dstbase=`basename "$src"`
306370b807fSmrg      case $dst in
307370b807fSmrg	*/) dst=$dst$dstbase;;
308370b807fSmrg	*)  dst=$dst/$dstbase;;
309370b807fSmrg      esac
310659607e0Smrg      dstdir_status=0
311659607e0Smrg    else
312370b807fSmrg      dstdir=`dirname "$dst"`
313659607e0Smrg      test -d "$dstdir"
314659607e0Smrg      dstdir_status=$?
315659607e0Smrg    fi
316659607e0Smrg  fi
317659607e0Smrg
318370b807fSmrg  case $dstdir in
319370b807fSmrg    */) dstdirslash=$dstdir;;
320370b807fSmrg    *)  dstdirslash=$dstdir/;;
321370b807fSmrg  esac
322370b807fSmrg
323659607e0Smrg  obsolete_mkdir_used=false
324659607e0Smrg
325659607e0Smrg  if test $dstdir_status != 0; then
326659607e0Smrg    case $posix_mkdir in
327659607e0Smrg      '')
328370b807fSmrg        # With -d, create the new directory with the user-specified mode.
329370b807fSmrg        # Otherwise, rely on $mkdir_umask.
330370b807fSmrg        if test -n "$dir_arg"; then
331370b807fSmrg          mkdir_mode=-m$mode
332370b807fSmrg        else
333370b807fSmrg          mkdir_mode=
334370b807fSmrg        fi
335370b807fSmrg
336370b807fSmrg        posix_mkdir=false
337370b807fSmrg	# The $RANDOM variable is not portable (e.g., dash).  Use it
338370b807fSmrg	# here however when possible just to lower collision chance.
339370b807fSmrg	tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
340370b807fSmrg
341370b807fSmrg	trap '
342370b807fSmrg	  ret=$?
343370b807fSmrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null
344370b807fSmrg	  exit $ret
345370b807fSmrg	' 0
346370b807fSmrg
347370b807fSmrg	# Because "mkdir -p" follows existing symlinks and we likely work
348370b807fSmrg	# directly in world-writeable /tmp, make sure that the '$tmpdir'
349370b807fSmrg	# directory is successfully created first before we actually test
350370b807fSmrg	# 'mkdir -p'.
351370b807fSmrg	if (umask $mkdir_umask &&
352370b807fSmrg	    $mkdirprog $mkdir_mode "$tmpdir" &&
353370b807fSmrg	    exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
354370b807fSmrg	then
355370b807fSmrg	  if test -z "$dir_arg" || {
356370b807fSmrg	       # Check for POSIX incompatibilities with -m.
357370b807fSmrg	       # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
358370b807fSmrg	       # other-writable bit of parent directory when it shouldn't.
359370b807fSmrg	       # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
360370b807fSmrg	       test_tmpdir="$tmpdir/a"
361370b807fSmrg	       ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
362370b807fSmrg	       case $ls_ld_tmpdir in
363370b807fSmrg		 d????-?r-*) different_mode=700;;
364370b807fSmrg		 d????-?--*) different_mode=755;;
365370b807fSmrg		 *) false;;
366370b807fSmrg	       esac &&
367370b807fSmrg	       $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
368370b807fSmrg		 ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
369370b807fSmrg		 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
370370b807fSmrg	       }
371370b807fSmrg	     }
372370b807fSmrg	  then posix_mkdir=:
373370b807fSmrg	  fi
374370b807fSmrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
375659607e0Smrg	else
376370b807fSmrg	  # Remove any dirs left behind by ancient mkdir implementations.
377370b807fSmrg	  rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
378659607e0Smrg	fi
379370b807fSmrg	trap '' 0;;
380659607e0Smrg    esac
381659607e0Smrg
382659607e0Smrg    if
383659607e0Smrg      $posix_mkdir && (
384370b807fSmrg        umask $mkdir_umask &&
385370b807fSmrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
386659607e0Smrg      )
387659607e0Smrg    then :
388659607e0Smrg    else
389659607e0Smrg
390370b807fSmrg      # mkdir does not conform to POSIX,
391659607e0Smrg      # or it failed possibly due to a race condition.  Create the
392659607e0Smrg      # directory the slow way, step by step, checking for races as we go.
393659607e0Smrg
394659607e0Smrg      case $dstdir in
395370b807fSmrg        /*) prefix='/';;
396370b807fSmrg        [-=\(\)!]*) prefix='./';;
397370b807fSmrg        *)  prefix='';;
398659607e0Smrg      esac
399659607e0Smrg
400659607e0Smrg      oIFS=$IFS
401659607e0Smrg      IFS=/
402370b807fSmrg      set -f
403659607e0Smrg      set fnord $dstdir
404659607e0Smrg      shift
405370b807fSmrg      set +f
406659607e0Smrg      IFS=$oIFS
407659607e0Smrg
408659607e0Smrg      prefixes=
409659607e0Smrg
410659607e0Smrg      for d
411659607e0Smrg      do
412370b807fSmrg        test X"$d" = X && continue
413370b807fSmrg
414370b807fSmrg        prefix=$prefix$d
415370b807fSmrg        if test -d "$prefix"; then
416370b807fSmrg          prefixes=
417370b807fSmrg        else
418370b807fSmrg          if $posix_mkdir; then
419370b807fSmrg            (umask $mkdir_umask &&
420370b807fSmrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
421370b807fSmrg            # Don't fail if two instances are running concurrently.
422370b807fSmrg            test -d "$prefix" || exit 1
423370b807fSmrg          else
424370b807fSmrg            case $prefix in
425370b807fSmrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
426370b807fSmrg              *) qprefix=$prefix;;
427370b807fSmrg            esac
428370b807fSmrg            prefixes="$prefixes '$qprefix'"
429370b807fSmrg          fi
430370b807fSmrg        fi
431370b807fSmrg        prefix=$prefix/
432659607e0Smrg      done
433659607e0Smrg
434659607e0Smrg      if test -n "$prefixes"; then
435370b807fSmrg        # Don't fail if two instances are running concurrently.
436370b807fSmrg        (umask $mkdir_umask &&
437370b807fSmrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
438370b807fSmrg          test -d "$dstdir" || exit 1
439370b807fSmrg        obsolete_mkdir_used=true
440659607e0Smrg      fi
441659607e0Smrg    fi
442659607e0Smrg  fi
443659607e0Smrg
444659607e0Smrg  if test -n "$dir_arg"; then
445659607e0Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
446659607e0Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
447659607e0Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
448659607e0Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
449659607e0Smrg  else
450659607e0Smrg
451659607e0Smrg    # Make a couple of temp file names in the proper directory.
452370b807fSmrg    dsttmp=${dstdirslash}_inst.$$_
453370b807fSmrg    rmtmp=${dstdirslash}_rm.$$_
454659607e0Smrg
455659607e0Smrg    # Trap to clean up those temp files at exit.
456659607e0Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
457659607e0Smrg
458659607e0Smrg    # Copy the file name to the temp name.
459370b807fSmrg    (umask $cp_umask &&
460370b807fSmrg     { test -z "$stripcmd" || {
461370b807fSmrg	 # Create $dsttmp read-write so that cp doesn't create it read-only,
462370b807fSmrg	 # which would cause strip to fail.
463370b807fSmrg	 if test -z "$doit"; then
464370b807fSmrg	   : >"$dsttmp" # No need to fork-exec 'touch'.
465370b807fSmrg	 else
466370b807fSmrg	   $doit touch "$dsttmp"
467370b807fSmrg	 fi
468370b807fSmrg       }
469370b807fSmrg     } &&
470370b807fSmrg     $doit_exec $cpprog "$src" "$dsttmp") &&
471659607e0Smrg
472659607e0Smrg    # and set any options; do chmod last to preserve setuid bits.
473659607e0Smrg    #
474659607e0Smrg    # If any of these fail, we abort the whole thing.  If we want to
475659607e0Smrg    # ignore errors from any of these, just make sure not to ignore
476659607e0Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
477659607e0Smrg    #
478fc27e79cSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
479fc27e79cSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
480fc27e79cSmrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
481fc27e79cSmrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
482fc27e79cSmrg
483fc27e79cSmrg    # If -C, don't bother to copy if it wouldn't change the file.
484fc27e79cSmrg    if $copy_on_change &&
485370b807fSmrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
486370b807fSmrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
487370b807fSmrg       set -f &&
488fc27e79cSmrg       set X $old && old=:$2:$4:$5:$6 &&
489fc27e79cSmrg       set X $new && new=:$2:$4:$5:$6 &&
490370b807fSmrg       set +f &&
491fc27e79cSmrg       test "$old" = "$new" &&
492fc27e79cSmrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
493fc27e79cSmrg    then
494fc27e79cSmrg      rm -f "$dsttmp"
495fc27e79cSmrg    else
496370b807fSmrg      # If $backupsuffix is set, and the file being installed
497370b807fSmrg      # already exists, attempt a backup.  Don't worry if it fails,
498370b807fSmrg      # e.g., if mv doesn't support -f.
499370b807fSmrg      if test -n "$backupsuffix" && test -f "$dst"; then
500370b807fSmrg        $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null
501370b807fSmrg      fi
502370b807fSmrg
503fc27e79cSmrg      # Rename the file to the real destination.
504fc27e79cSmrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
505fc27e79cSmrg
506fc27e79cSmrg      # The rename failed, perhaps because mv can't rename something else
507fc27e79cSmrg      # to itself, or perhaps because mv is so ancient that it does not
508fc27e79cSmrg      # support -f.
509fc27e79cSmrg      {
510370b807fSmrg        # Now remove or move aside any old file at destination location.
511370b807fSmrg        # We try this two ways since rm can't unlink itself on some
512370b807fSmrg        # systems and the destination file might be busy for other
513370b807fSmrg        # reasons.  In this case, the final cleanup might fail but the new
514370b807fSmrg        # file should still install successfully.
515370b807fSmrg        {
516370b807fSmrg          test ! -f "$dst" ||
517370b807fSmrg          $doit $rmcmd "$dst" 2>/dev/null ||
518370b807fSmrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
519370b807fSmrg            { $doit $rmcmd "$rmtmp" 2>/dev/null; :; }
520370b807fSmrg          } ||
521370b807fSmrg          { echo "$0: cannot unlink or rename $dst" >&2
522370b807fSmrg            (exit 1); exit 1
523370b807fSmrg          }
524370b807fSmrg        } &&
525370b807fSmrg
526370b807fSmrg        # Now rename the file to the real destination.
527370b807fSmrg        $doit $mvcmd "$dsttmp" "$dst"
528fc27e79cSmrg      }
529fc27e79cSmrg    fi || exit 1
530659607e0Smrg
531659607e0Smrg    trap '' 0
532659607e0Smrg  fi
533659607e0Smrgdone
534659607e0Smrg
535659607e0Smrg# Local variables:
536370b807fSmrg# eval: (add-hook 'before-save-hook 'time-stamp)
537659607e0Smrg# time-stamp-start: "scriptversion="
538659607e0Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
539370b807fSmrg# time-stamp-time-zone: "UTC0"
540b73be646Smrg# time-stamp-end: "; # UTC"
541659607e0Smrg# End:
542