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