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