install-sh revision 14bc0770
1e9554658Smrg#!/bin/sh
2e9554658Smrg# install - install a program, script, or datafile
3e9554658Smrg
414bc0770Smrgscriptversion=2020-11-14.01; # UTC
5e9554658Smrg
6e9554658Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7e9554658Smrg# later released in X11R6 (xc/config/util/install.sh) with the
8e9554658Smrg# following copyright and license.
9e9554658Smrg#
10e9554658Smrg# Copyright (C) 1994 X Consortium
11e9554658Smrg#
12e9554658Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13e9554658Smrg# of this software and associated documentation files (the "Software"), to
14e9554658Smrg# deal in the Software without restriction, including without limitation the
15e9554658Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16e9554658Smrg# sell copies of the Software, and to permit persons to whom the Software is
17e9554658Smrg# furnished to do so, subject to the following conditions:
18e9554658Smrg#
19e9554658Smrg# The above copyright notice and this permission notice shall be included in
20e9554658Smrg# all copies or substantial portions of the Software.
21e9554658Smrg#
22e9554658Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23e9554658Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24e9554658Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25e9554658Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26e9554658Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27e9554658Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28e9554658Smrg#
29e9554658Smrg# Except as contained in this notice, the name of the X Consortium shall not
30e9554658Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
31e9554658Smrg# ings in this Software without prior written authorization from the X Consor-
32e9554658Smrg# tium.
33e9554658Smrg#
34e9554658Smrg#
35e9554658Smrg# FSF changes to this file are in the public domain.
36e9554658Smrg#
37e9554658Smrg# Calling this script install-sh is preferred over install.sh, to prevent
385dd5f640Smrg# 'make' implicit rules from creating a file called install from it
39e9554658Smrg# when there is no Makefile.
40e9554658Smrg#
41e9554658Smrg# This script is compatible with the BSD install script, but was written
42e9554658Smrg# from scratch.
43e9554658Smrg
44688b3aecSmrgtab='	'
45e9554658Smrgnl='
46e9554658Smrg'
47688b3aecSmrgIFS=" $tab$nl"
48e9554658Smrg
49688b3aecSmrg# Set DOITPROG to "echo" to test this script.
50e9554658Smrg
51c2b339b4Smrgdoit=${DOITPROG-}
52688b3aecSmrgdoit_exec=${doit:-exec}
53e9554658Smrg
54e9554658Smrg# Put in absolute file names if you don't have them in your path;
55e9554658Smrg# or use environment vars.
56e9554658Smrg
57c2b339b4Smrgchgrpprog=${CHGRPPROG-chgrp}
58c2b339b4Smrgchmodprog=${CHMODPROG-chmod}
59c2b339b4Smrgchownprog=${CHOWNPROG-chown}
60c2b339b4Smrgcmpprog=${CMPPROG-cmp}
61c2b339b4Smrgcpprog=${CPPROG-cp}
62c2b339b4Smrgmkdirprog=${MKDIRPROG-mkdir}
63c2b339b4Smrgmvprog=${MVPROG-mv}
64c2b339b4Smrgrmprog=${RMPROG-rm}
65c2b339b4Smrgstripprog=${STRIPPROG-strip}
66c2b339b4Smrg
67e9554658Smrgposix_mkdir=
68e9554658Smrg
69e9554658Smrg# Desired mode of installed file.
70e9554658Smrgmode=0755
71e9554658Smrg
7214bc0770Smrg# Create dirs (including intermediate dirs) using mode 755.
7314bc0770Smrg# This is like GNU 'install' as of coreutils 8.32 (2020).
7414bc0770Smrgmkdir_umask=22
7514bc0770Smrg
7614bc0770Smrgbackupsuffix=
77c2b339b4Smrgchgrpcmd=
78e9554658Smrgchmodcmd=$chmodprog
79e9554658Smrgchowncmd=
80c2b339b4Smrgmvcmd=$mvprog
81e9554658Smrgrmcmd="$rmprog -f"
82c2b339b4Smrgstripcmd=
83c2b339b4Smrg
84e9554658Smrgsrc=
85e9554658Smrgdst=
86e9554658Smrgdir_arg=
87c2b339b4Smrgdst_arg=
88c2b339b4Smrg
89c2b339b4Smrgcopy_on_change=false
90688b3aecSmrgis_target_a_directory=possibly
91e9554658Smrg
92c2b339b4Smrgusage="\
93c2b339b4SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
94e9554658Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
95e9554658Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
96e9554658Smrg   or: $0 [OPTION]... -d DIRECTORIES...
97e9554658Smrg
98e9554658SmrgIn the 1st form, copy SRCFILE to DSTFILE.
99e9554658SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
100e9554658SmrgIn the 4th, create DIRECTORIES.
101e9554658Smrg
102e9554658SmrgOptions:
103c2b339b4Smrg     --help     display this help and exit.
104c2b339b4Smrg     --version  display version info and exit.
105c2b339b4Smrg
106c2b339b4Smrg  -c            (ignored)
10714bc0770Smrg  -C            install only if different (preserve data modification time)
108c2b339b4Smrg  -d            create directories instead of installing files.
109c2b339b4Smrg  -g GROUP      $chgrpprog installed files to GROUP.
110c2b339b4Smrg  -m MODE       $chmodprog installed files to MODE.
111c2b339b4Smrg  -o USER       $chownprog installed files to USER.
11214bc0770Smrg  -p            pass -p to $cpprog.
113c2b339b4Smrg  -s            $stripprog installed files.
11414bc0770Smrg  -S SUFFIX     attempt to back up existing files, with suffix SUFFIX.
115c2b339b4Smrg  -t DIRECTORY  install into DIRECTORY.
116c2b339b4Smrg  -T            report an error if DSTFILE is a directory.
117e9554658Smrg
118e9554658SmrgEnvironment variables override the default commands:
119c2b339b4Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
120c2b339b4Smrg  RMPROG STRIPPROG
12114bc0770Smrg
12214bc0770SmrgBy default, rm is invoked with -f; when overridden with RMPROG,
12314bc0770Smrgit's up to you to specify -f if you want it.
12414bc0770Smrg
12514bc0770SmrgIf -S is not specified, no backups are attempted.
12614bc0770Smrg
12714bc0770SmrgEmail bug reports to bug-automake@gnu.org.
12814bc0770SmrgAutomake home page: https://www.gnu.org/software/automake/
129e9554658Smrg"
130e9554658Smrg
131e9554658Smrgwhile test $# -ne 0; do
132e9554658Smrg  case $1 in
133c2b339b4Smrg    -c) ;;
134c2b339b4Smrg
135c2b339b4Smrg    -C) copy_on_change=true;;
136e9554658Smrg
137c2b339b4Smrg    -d) dir_arg=true;;
138e9554658Smrg
139e9554658Smrg    -g) chgrpcmd="$chgrpprog $2"
140688b3aecSmrg        shift;;
141e9554658Smrg
142e9554658Smrg    --help) echo "$usage"; exit $?;;
143e9554658Smrg
144e9554658Smrg    -m) mode=$2
145688b3aecSmrg        case $mode in
146688b3aecSmrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
147688b3aecSmrg            echo "$0: invalid mode: $mode" >&2
148688b3aecSmrg            exit 1;;
149688b3aecSmrg        esac
150688b3aecSmrg        shift;;
151e9554658Smrg
152e9554658Smrg    -o) chowncmd="$chownprog $2"
153688b3aecSmrg        shift;;
154e9554658Smrg
15514bc0770Smrg    -p) cpprog="$cpprog -p";;
15614bc0770Smrg
157c2b339b4Smrg    -s) stripcmd=$stripprog;;
158e9554658Smrg
15914bc0770Smrg    -S) backupsuffix="$2"
16014bc0770Smrg        shift;;
16114bc0770Smrg
162688b3aecSmrg    -t)
163688b3aecSmrg        is_target_a_directory=always
164688b3aecSmrg        dst_arg=$2
165688b3aecSmrg        # Protect names problematic for 'test' and other utilities.
166688b3aecSmrg        case $dst_arg in
167688b3aecSmrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
168688b3aecSmrg        esac
169688b3aecSmrg        shift;;
170e9554658Smrg
171688b3aecSmrg    -T) is_target_a_directory=never;;
172e9554658Smrg
173e9554658Smrg    --version) echo "$0 $scriptversion"; exit $?;;
174e9554658Smrg
175688b3aecSmrg    --) shift
176688b3aecSmrg        break;;
177e9554658Smrg
178688b3aecSmrg    -*) echo "$0: invalid option: $1" >&2
179688b3aecSmrg        exit 1;;
180e9554658Smrg
181e9554658Smrg    *)  break;;
182e9554658Smrg  esac
183c2b339b4Smrg  shift
184e9554658Smrgdone
185e9554658Smrg
186688b3aecSmrg# We allow the use of options -d and -T together, by making -d
187688b3aecSmrg# take the precedence; this is for compatibility with GNU install.
188688b3aecSmrg
189688b3aecSmrgif test -n "$dir_arg"; then
190688b3aecSmrg  if test -n "$dst_arg"; then
191688b3aecSmrg    echo "$0: target directory not allowed when installing a directory." >&2
192688b3aecSmrg    exit 1
193688b3aecSmrg  fi
194688b3aecSmrgfi
195688b3aecSmrg
196c2b339b4Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
197e9554658Smrg  # When -d is used, all remaining arguments are directories to create.
198e9554658Smrg  # When -t is used, the destination is already specified.
199e9554658Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
200e9554658Smrg  for arg
201e9554658Smrg  do
202c2b339b4Smrg    if test -n "$dst_arg"; then
203e9554658Smrg      # $@ is not empty: it contains at least $arg.
204c2b339b4Smrg      set fnord "$@" "$dst_arg"
205e9554658Smrg      shift # fnord
206e9554658Smrg    fi
207e9554658Smrg    shift # arg
208c2b339b4Smrg    dst_arg=$arg
2095dd5f640Smrg    # Protect names problematic for 'test' and other utilities.
2105dd5f640Smrg    case $dst_arg in
2115dd5f640Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
2125dd5f640Smrg    esac
213e9554658Smrg  done
214e9554658Smrgfi
215e9554658Smrg
216e9554658Smrgif test $# -eq 0; then
217e9554658Smrg  if test -z "$dir_arg"; then
218e9554658Smrg    echo "$0: no input file specified." >&2
219e9554658Smrg    exit 1
220e9554658Smrg  fi
2215dd5f640Smrg  # It's OK to call 'install-sh -d' without argument.
222e9554658Smrg  # This can happen when creating conditional directories.
223e9554658Smrg  exit 0
224e9554658Smrgfi
225e9554658Smrg
226688b3aecSmrgif test -z "$dir_arg"; then
227688b3aecSmrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
228688b3aecSmrg    if test ! -d "$dst_arg"; then
229688b3aecSmrg      echo "$0: $dst_arg: Is not a directory." >&2
230688b3aecSmrg      exit 1
231688b3aecSmrg    fi
232688b3aecSmrg  fi
233688b3aecSmrgfi
234688b3aecSmrg
235e9554658Smrgif test -z "$dir_arg"; then
2365dd5f640Smrg  do_exit='(exit $ret); exit $ret'
2375dd5f640Smrg  trap "ret=129; $do_exit" 1
2385dd5f640Smrg  trap "ret=130; $do_exit" 2
2395dd5f640Smrg  trap "ret=141; $do_exit" 13
2405dd5f640Smrg  trap "ret=143; $do_exit" 15
241e9554658Smrg
242e9554658Smrg  # Set umask so as not to create temps with too-generous modes.
243e9554658Smrg  # However, 'strip' requires both read and write access to temps.
244e9554658Smrg  case $mode in
245e9554658Smrg    # Optimize common cases.
246e9554658Smrg    *644) cp_umask=133;;
247e9554658Smrg    *755) cp_umask=22;;
248e9554658Smrg
249e9554658Smrg    *[0-7])
250e9554658Smrg      if test -z "$stripcmd"; then
251688b3aecSmrg        u_plus_rw=
252e9554658Smrg      else
253688b3aecSmrg        u_plus_rw='% 200'
254e9554658Smrg      fi
255e9554658Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
256e9554658Smrg    *)
257e9554658Smrg      if test -z "$stripcmd"; then
258688b3aecSmrg        u_plus_rw=
259e9554658Smrg      else
260688b3aecSmrg        u_plus_rw=,u+rw
261e9554658Smrg      fi
262e9554658Smrg      cp_umask=$mode$u_plus_rw;;
263e9554658Smrg  esac
264e9554658Smrgfi
265e9554658Smrg
266e9554658Smrgfor src
267e9554658Smrgdo
2685dd5f640Smrg  # Protect names problematic for 'test' and other utilities.
269e9554658Smrg  case $src in
2705dd5f640Smrg    -* | [=\(\)!]) src=./$src;;
271e9554658Smrg  esac
272e9554658Smrg
273e9554658Smrg  if test -n "$dir_arg"; then
274e9554658Smrg    dst=$src
275e9554658Smrg    dstdir=$dst
276e9554658Smrg    test -d "$dstdir"
277e9554658Smrg    dstdir_status=$?
27814bc0770Smrg    # Don't chown directories that already exist.
27914bc0770Smrg    if test $dstdir_status = 0; then
28014bc0770Smrg      chowncmd=""
28114bc0770Smrg    fi
282e9554658Smrg  else
283e9554658Smrg
284e9554658Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
285e9554658Smrg    # might cause directories to be created, which would be especially bad
286e9554658Smrg    # if $src (and thus $dsttmp) contains '*'.
287e9554658Smrg    if test ! -f "$src" && test ! -d "$src"; then
288e9554658Smrg      echo "$0: $src does not exist." >&2
289e9554658Smrg      exit 1
290e9554658Smrg    fi
291e9554658Smrg
292c2b339b4Smrg    if test -z "$dst_arg"; then
293e9554658Smrg      echo "$0: no destination specified." >&2
294e9554658Smrg      exit 1
295e9554658Smrg    fi
296c2b339b4Smrg    dst=$dst_arg
297e9554658Smrg
29814bc0770Smrg    # If destination is a directory, append the input filename.
299e9554658Smrg    if test -d "$dst"; then
300688b3aecSmrg      if test "$is_target_a_directory" = never; then
301688b3aecSmrg        echo "$0: $dst_arg: Is a directory" >&2
302688b3aecSmrg        exit 1
303e9554658Smrg      fi
304e9554658Smrg      dstdir=$dst
30514bc0770Smrg      dstbase=`basename "$src"`
30614bc0770Smrg      case $dst in
30714bc0770Smrg	*/) dst=$dst$dstbase;;
30814bc0770Smrg	*)  dst=$dst/$dstbase;;
30914bc0770Smrg      esac
310e9554658Smrg      dstdir_status=0
311e9554658Smrg    else
312688b3aecSmrg      dstdir=`dirname "$dst"`
313e9554658Smrg      test -d "$dstdir"
314e9554658Smrg      dstdir_status=$?
315e9554658Smrg    fi
316e9554658Smrg  fi
317e9554658Smrg
31814bc0770Smrg  case $dstdir in
31914bc0770Smrg    */) dstdirslash=$dstdir;;
32014bc0770Smrg    *)  dstdirslash=$dstdir/;;
32114bc0770Smrg  esac
32214bc0770Smrg
323e9554658Smrg  obsolete_mkdir_used=false
324e9554658Smrg
325e9554658Smrg  if test $dstdir_status != 0; then
326e9554658Smrg    case $posix_mkdir in
327e9554658Smrg      '')
328688b3aecSmrg        # With -d, create the new directory with the user-specified mode.
329688b3aecSmrg        # Otherwise, rely on $mkdir_umask.
330688b3aecSmrg        if test -n "$dir_arg"; then
331688b3aecSmrg          mkdir_mode=-m$mode
332688b3aecSmrg        else
333688b3aecSmrg          mkdir_mode=
334688b3aecSmrg        fi
335688b3aecSmrg
336688b3aecSmrg        posix_mkdir=false
33714bc0770Smrg	# The $RANDOM variable is not portable (e.g., dash).  Use it
33814bc0770Smrg	# here however when possible just to lower collision chance.
33914bc0770Smrg	tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
34014bc0770Smrg
34114bc0770Smrg	trap '
34214bc0770Smrg	  ret=$?
34314bc0770Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null
34414bc0770Smrg	  exit $ret
34514bc0770Smrg	' 0
34614bc0770Smrg
34714bc0770Smrg	# Because "mkdir -p" follows existing symlinks and we likely work
34814bc0770Smrg	# directly in world-writeable /tmp, make sure that the '$tmpdir'
34914bc0770Smrg	# directory is successfully created first before we actually test
35014bc0770Smrg	# 'mkdir -p'.
35114bc0770Smrg	if (umask $mkdir_umask &&
35214bc0770Smrg	    $mkdirprog $mkdir_mode "$tmpdir" &&
35314bc0770Smrg	    exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
35414bc0770Smrg	then
35514bc0770Smrg	  if test -z "$dir_arg" || {
35614bc0770Smrg	       # Check for POSIX incompatibilities with -m.
35714bc0770Smrg	       # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
35814bc0770Smrg	       # other-writable bit of parent directory when it shouldn't.
35914bc0770Smrg	       # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
36014bc0770Smrg	       test_tmpdir="$tmpdir/a"
36114bc0770Smrg	       ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
36214bc0770Smrg	       case $ls_ld_tmpdir in
36314bc0770Smrg		 d????-?r-*) different_mode=700;;
36414bc0770Smrg		 d????-?--*) different_mode=755;;
36514bc0770Smrg		 *) false;;
36614bc0770Smrg	       esac &&
36714bc0770Smrg	       $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
36814bc0770Smrg		 ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
36914bc0770Smrg		 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
37014bc0770Smrg	       }
37114bc0770Smrg	     }
37214bc0770Smrg	  then posix_mkdir=:
37314bc0770Smrg	  fi
37414bc0770Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
37514bc0770Smrg	else
37614bc0770Smrg	  # Remove any dirs left behind by ancient mkdir implementations.
37714bc0770Smrg	  rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
37814bc0770Smrg	fi
37914bc0770Smrg	trap '' 0;;
380e9554658Smrg    esac
381e9554658Smrg
382e9554658Smrg    if
383e9554658Smrg      $posix_mkdir && (
384688b3aecSmrg        umask $mkdir_umask &&
385688b3aecSmrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
386e9554658Smrg      )
387e9554658Smrg    then :
388e9554658Smrg    else
389e9554658Smrg
39014bc0770Smrg      # mkdir does not conform to POSIX,
391e9554658Smrg      # or it failed possibly due to a race condition.  Create the
392e9554658Smrg      # directory the slow way, step by step, checking for races as we go.
393e9554658Smrg
394e9554658Smrg      case $dstdir in
395688b3aecSmrg        /*) prefix='/';;
396688b3aecSmrg        [-=\(\)!]*) prefix='./';;
397688b3aecSmrg        *)  prefix='';;
398e9554658Smrg      esac
399e9554658Smrg
400e9554658Smrg      oIFS=$IFS
401e9554658Smrg      IFS=/
402688b3aecSmrg      set -f
403e9554658Smrg      set fnord $dstdir
404e9554658Smrg      shift
405688b3aecSmrg      set +f
406e9554658Smrg      IFS=$oIFS
407e9554658Smrg
408e9554658Smrg      prefixes=
409e9554658Smrg
410e9554658Smrg      for d
411e9554658Smrg      do
412688b3aecSmrg        test X"$d" = X && continue
413688b3aecSmrg
414688b3aecSmrg        prefix=$prefix$d
415688b3aecSmrg        if test -d "$prefix"; then
416688b3aecSmrg          prefixes=
417688b3aecSmrg        else
418688b3aecSmrg          if $posix_mkdir; then
41914bc0770Smrg            (umask $mkdir_umask &&
420688b3aecSmrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
421688b3aecSmrg            # Don't fail if two instances are running concurrently.
422688b3aecSmrg            test -d "$prefix" || exit 1
423688b3aecSmrg          else
424688b3aecSmrg            case $prefix in
425688b3aecSmrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
426688b3aecSmrg              *) qprefix=$prefix;;
427688b3aecSmrg            esac
428688b3aecSmrg            prefixes="$prefixes '$qprefix'"
429688b3aecSmrg          fi
430688b3aecSmrg        fi
431688b3aecSmrg        prefix=$prefix/
432e9554658Smrg      done
433e9554658Smrg
434e9554658Smrg      if test -n "$prefixes"; then
435688b3aecSmrg        # Don't fail if two instances are running concurrently.
436688b3aecSmrg        (umask $mkdir_umask &&
437688b3aecSmrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
438688b3aecSmrg          test -d "$dstdir" || exit 1
439688b3aecSmrg        obsolete_mkdir_used=true
440e9554658Smrg      fi
441e9554658Smrg    fi
442e9554658Smrg  fi
443e9554658Smrg
444e9554658Smrg  if test -n "$dir_arg"; then
445e9554658Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
446e9554658Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
447e9554658Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
448e9554658Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
449e9554658Smrg  else
450e9554658Smrg
451e9554658Smrg    # Make a couple of temp file names in the proper directory.
45214bc0770Smrg    dsttmp=${dstdirslash}_inst.$$_
45314bc0770Smrg    rmtmp=${dstdirslash}_rm.$$_
454e9554658Smrg
455e9554658Smrg    # Trap to clean up those temp files at exit.
456e9554658Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
457e9554658Smrg
458e9554658Smrg    # Copy the file name to the temp name.
45914bc0770Smrg    (umask $cp_umask &&
46014bc0770Smrg     { test -z "$stripcmd" || {
46114bc0770Smrg	 # Create $dsttmp read-write so that cp doesn't create it read-only,
46214bc0770Smrg	 # which would cause strip to fail.
46314bc0770Smrg	 if test -z "$doit"; then
46414bc0770Smrg	   : >"$dsttmp" # No need to fork-exec 'touch'.
46514bc0770Smrg	 else
46614bc0770Smrg	   $doit touch "$dsttmp"
46714bc0770Smrg	 fi
46814bc0770Smrg       }
46914bc0770Smrg     } &&
47014bc0770Smrg     $doit_exec $cpprog "$src" "$dsttmp") &&
471e9554658Smrg
472e9554658Smrg    # and set any options; do chmod last to preserve setuid bits.
473e9554658Smrg    #
474e9554658Smrg    # If any of these fail, we abort the whole thing.  If we want to
475e9554658Smrg    # ignore errors from any of these, just make sure not to ignore
476e9554658Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
477e9554658Smrg    #
478c2b339b4Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
479c2b339b4Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
480c2b339b4Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
481c2b339b4Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
482c2b339b4Smrg
483c2b339b4Smrg    # If -C, don't bother to copy if it wouldn't change the file.
484c2b339b4Smrg    if $copy_on_change &&
485688b3aecSmrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
486688b3aecSmrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
487688b3aecSmrg       set -f &&
488c2b339b4Smrg       set X $old && old=:$2:$4:$5:$6 &&
489c2b339b4Smrg       set X $new && new=:$2:$4:$5:$6 &&
490688b3aecSmrg       set +f &&
491c2b339b4Smrg       test "$old" = "$new" &&
492c2b339b4Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
493c2b339b4Smrg    then
494c2b339b4Smrg      rm -f "$dsttmp"
495c2b339b4Smrg    else
49614bc0770Smrg      # If $backupsuffix is set, and the file being installed
49714bc0770Smrg      # already exists, attempt a backup.  Don't worry if it fails,
49814bc0770Smrg      # e.g., if mv doesn't support -f.
49914bc0770Smrg      if test -n "$backupsuffix" && test -f "$dst"; then
50014bc0770Smrg        $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null
50114bc0770Smrg      fi
50214bc0770Smrg
503c2b339b4Smrg      # Rename the file to the real destination.
504c2b339b4Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
505c2b339b4Smrg
506c2b339b4Smrg      # The rename failed, perhaps because mv can't rename something else
507c2b339b4Smrg      # to itself, or perhaps because mv is so ancient that it does not
508c2b339b4Smrg      # support -f.
509c2b339b4Smrg      {
510688b3aecSmrg        # Now remove or move aside any old file at destination location.
511688b3aecSmrg        # We try this two ways since rm can't unlink itself on some
512688b3aecSmrg        # systems and the destination file might be busy for other
513688b3aecSmrg        # reasons.  In this case, the final cleanup might fail but the new
514688b3aecSmrg        # file should still install successfully.
515688b3aecSmrg        {
516688b3aecSmrg          test ! -f "$dst" ||
51714bc0770Smrg          $doit $rmcmd "$dst" 2>/dev/null ||
518688b3aecSmrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
51914bc0770Smrg            { $doit $rmcmd "$rmtmp" 2>/dev/null; :; }
520688b3aecSmrg          } ||
521688b3aecSmrg          { echo "$0: cannot unlink or rename $dst" >&2
522688b3aecSmrg            (exit 1); exit 1
523688b3aecSmrg          }
524688b3aecSmrg        } &&
525688b3aecSmrg
526688b3aecSmrg        # Now rename the file to the real destination.
527688b3aecSmrg        $doit $mvcmd "$dsttmp" "$dst"
528c2b339b4Smrg      }
529c2b339b4Smrg    fi || exit 1
530e9554658Smrg
531e9554658Smrg    trap '' 0
532e9554658Smrg  fi
533e9554658Smrgdone
534e9554658Smrg
535e9554658Smrg# Local variables:
53614bc0770Smrg# eval: (add-hook 'before-save-hook 'time-stamp)
537e9554658Smrg# time-stamp-start: "scriptversion="
538e9554658Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
53914bc0770Smrg# time-stamp-time-zone: "UTC0"
540c2b339b4Smrg# time-stamp-end: "; # UTC"
541e9554658Smrg# End:
542