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