1e07dc26bSmrg#!/bin/sh
2e07dc26bSmrg# install - install a program, script, or datafile
3e07dc26bSmrg
4636c353eSmrgscriptversion=2020-11-14.01; # UTC
5e07dc26bSmrg
6e07dc26bSmrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7e07dc26bSmrg# later released in X11R6 (xc/config/util/install.sh) with the
8e07dc26bSmrg# following copyright and license.
9e07dc26bSmrg#
10e07dc26bSmrg# Copyright (C) 1994 X Consortium
11e07dc26bSmrg#
12e07dc26bSmrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13e07dc26bSmrg# of this software and associated documentation files (the "Software"), to
14e07dc26bSmrg# deal in the Software without restriction, including without limitation the
15e07dc26bSmrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16e07dc26bSmrg# sell copies of the Software, and to permit persons to whom the Software is
17e07dc26bSmrg# furnished to do so, subject to the following conditions:
18e07dc26bSmrg#
19e07dc26bSmrg# The above copyright notice and this permission notice shall be included in
20e07dc26bSmrg# all copies or substantial portions of the Software.
21e07dc26bSmrg#
22e07dc26bSmrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23e07dc26bSmrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24e07dc26bSmrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25e07dc26bSmrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26e07dc26bSmrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27e07dc26bSmrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28e07dc26bSmrg#
29e07dc26bSmrg# Except as contained in this notice, the name of the X Consortium shall not
30e07dc26bSmrg# be used in advertising or otherwise to promote the sale, use or other deal-
31e07dc26bSmrg# ings in this Software without prior written authorization from the X Consor-
32e07dc26bSmrg# tium.
33e07dc26bSmrg#
34e07dc26bSmrg#
35e07dc26bSmrg# FSF changes to this file are in the public domain.
36e07dc26bSmrg#
37e07dc26bSmrg# Calling this script install-sh is preferred over install.sh, to prevent
38e07dc26bSmrg# 'make' implicit rules from creating a file called install from it
39e07dc26bSmrg# when there is no Makefile.
40e07dc26bSmrg#
41e07dc26bSmrg# This script is compatible with the BSD install script, but was written
42e07dc26bSmrg# from scratch.
43e07dc26bSmrg
44e07dc26bSmrgtab='	'
45e07dc26bSmrgnl='
46e07dc26bSmrg'
47e07dc26bSmrgIFS=" $tab$nl"
48e07dc26bSmrg
49e07dc26bSmrg# Set DOITPROG to "echo" to test this script.
50e07dc26bSmrg
51e07dc26bSmrgdoit=${DOITPROG-}
52e07dc26bSmrgdoit_exec=${doit:-exec}
53e07dc26bSmrg
54e07dc26bSmrg# Put in absolute file names if you don't have them in your path;
55e07dc26bSmrg# or use environment vars.
56e07dc26bSmrg
57e07dc26bSmrgchgrpprog=${CHGRPPROG-chgrp}
58e07dc26bSmrgchmodprog=${CHMODPROG-chmod}
59e07dc26bSmrgchownprog=${CHOWNPROG-chown}
60e07dc26bSmrgcmpprog=${CMPPROG-cmp}
61e07dc26bSmrgcpprog=${CPPROG-cp}
62e07dc26bSmrgmkdirprog=${MKDIRPROG-mkdir}
63e07dc26bSmrgmvprog=${MVPROG-mv}
64e07dc26bSmrgrmprog=${RMPROG-rm}
65e07dc26bSmrgstripprog=${STRIPPROG-strip}
66e07dc26bSmrg
67e07dc26bSmrgposix_mkdir=
68e07dc26bSmrg
69e07dc26bSmrg# Desired mode of installed file.
70e07dc26bSmrgmode=0755
71e07dc26bSmrg
72636c353eSmrg# Create dirs (including intermediate dirs) using mode 755.
73636c353eSmrg# This is like GNU 'install' as of coreutils 8.32 (2020).
74636c353eSmrgmkdir_umask=22
75636c353eSmrg
76636c353eSmrgbackupsuffix=
77e07dc26bSmrgchgrpcmd=
78e07dc26bSmrgchmodcmd=$chmodprog
79e07dc26bSmrgchowncmd=
80e07dc26bSmrgmvcmd=$mvprog
81e07dc26bSmrgrmcmd="$rmprog -f"
82e07dc26bSmrgstripcmd=
83e07dc26bSmrg
84e07dc26bSmrgsrc=
85e07dc26bSmrgdst=
86e07dc26bSmrgdir_arg=
87e07dc26bSmrgdst_arg=
88e07dc26bSmrg
89e07dc26bSmrgcopy_on_change=false
90e07dc26bSmrgis_target_a_directory=possibly
91e07dc26bSmrg
92e07dc26bSmrgusage="\
93e07dc26bSmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
94e07dc26bSmrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
95e07dc26bSmrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
96e07dc26bSmrg   or: $0 [OPTION]... -d DIRECTORIES...
97e07dc26bSmrg
98e07dc26bSmrgIn the 1st form, copy SRCFILE to DSTFILE.
99e07dc26bSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
100e07dc26bSmrgIn the 4th, create DIRECTORIES.
101e07dc26bSmrg
102e07dc26bSmrgOptions:
103e07dc26bSmrg     --help     display this help and exit.
104e07dc26bSmrg     --version  display version info and exit.
105e07dc26bSmrg
106e07dc26bSmrg  -c            (ignored)
107636c353eSmrg  -C            install only if different (preserve data modification time)
108e07dc26bSmrg  -d            create directories instead of installing files.
109e07dc26bSmrg  -g GROUP      $chgrpprog installed files to GROUP.
110e07dc26bSmrg  -m MODE       $chmodprog installed files to MODE.
111e07dc26bSmrg  -o USER       $chownprog installed files to USER.
112636c353eSmrg  -p            pass -p to $cpprog.
113e07dc26bSmrg  -s            $stripprog installed files.
114636c353eSmrg  -S SUFFIX     attempt to back up existing files, with suffix SUFFIX.
115e07dc26bSmrg  -t DIRECTORY  install into DIRECTORY.
116e07dc26bSmrg  -T            report an error if DSTFILE is a directory.
117e07dc26bSmrg
118e07dc26bSmrgEnvironment variables override the default commands:
119e07dc26bSmrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
120e07dc26bSmrg  RMPROG STRIPPROG
121636c353eSmrg
122636c353eSmrgBy default, rm is invoked with -f; when overridden with RMPROG,
123636c353eSmrgit's up to you to specify -f if you want it.
124636c353eSmrg
125636c353eSmrgIf -S is not specified, no backups are attempted.
126636c353eSmrg
127636c353eSmrgEmail bug reports to bug-automake@gnu.org.
128636c353eSmrgAutomake home page: https://www.gnu.org/software/automake/
129e07dc26bSmrg"
130e07dc26bSmrg
131e07dc26bSmrgwhile test $# -ne 0; do
132e07dc26bSmrg  case $1 in
133e07dc26bSmrg    -c) ;;
134e07dc26bSmrg
135e07dc26bSmrg    -C) copy_on_change=true;;
136e07dc26bSmrg
137e07dc26bSmrg    -d) dir_arg=true;;
138e07dc26bSmrg
139e07dc26bSmrg    -g) chgrpcmd="$chgrpprog $2"
140e07dc26bSmrg        shift;;
141e07dc26bSmrg
142e07dc26bSmrg    --help) echo "$usage"; exit $?;;
143e07dc26bSmrg
144e07dc26bSmrg    -m) mode=$2
145e07dc26bSmrg        case $mode in
146e07dc26bSmrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
147e07dc26bSmrg            echo "$0: invalid mode: $mode" >&2
148e07dc26bSmrg            exit 1;;
149e07dc26bSmrg        esac
150e07dc26bSmrg        shift;;
151e07dc26bSmrg
152e07dc26bSmrg    -o) chowncmd="$chownprog $2"
153e07dc26bSmrg        shift;;
154e07dc26bSmrg
155636c353eSmrg    -p) cpprog="$cpprog -p";;
156636c353eSmrg
157e07dc26bSmrg    -s) stripcmd=$stripprog;;
158e07dc26bSmrg
159636c353eSmrg    -S) backupsuffix="$2"
160636c353eSmrg        shift;;
161636c353eSmrg
162e07dc26bSmrg    -t)
163e07dc26bSmrg        is_target_a_directory=always
164e07dc26bSmrg        dst_arg=$2
165e07dc26bSmrg        # Protect names problematic for 'test' and other utilities.
166e07dc26bSmrg        case $dst_arg in
167e07dc26bSmrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
168e07dc26bSmrg        esac
169e07dc26bSmrg        shift;;
170e07dc26bSmrg
171e07dc26bSmrg    -T) is_target_a_directory=never;;
172e07dc26bSmrg
173e07dc26bSmrg    --version) echo "$0 $scriptversion"; exit $?;;
174e07dc26bSmrg
175e07dc26bSmrg    --) shift
176e07dc26bSmrg        break;;
177e07dc26bSmrg
178e07dc26bSmrg    -*) echo "$0: invalid option: $1" >&2
179e07dc26bSmrg        exit 1;;
180e07dc26bSmrg
181e07dc26bSmrg    *)  break;;
182e07dc26bSmrg  esac
183e07dc26bSmrg  shift
184e07dc26bSmrgdone
185e07dc26bSmrg
186e07dc26bSmrg# We allow the use of options -d and -T together, by making -d
187e07dc26bSmrg# take the precedence; this is for compatibility with GNU install.
188e07dc26bSmrg
189e07dc26bSmrgif test -n "$dir_arg"; then
190e07dc26bSmrg  if test -n "$dst_arg"; then
191e07dc26bSmrg    echo "$0: target directory not allowed when installing a directory." >&2
192e07dc26bSmrg    exit 1
193e07dc26bSmrg  fi
194e07dc26bSmrgfi
195e07dc26bSmrg
196e07dc26bSmrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
197e07dc26bSmrg  # When -d is used, all remaining arguments are directories to create.
198e07dc26bSmrg  # When -t is used, the destination is already specified.
199e07dc26bSmrg  # Otherwise, the last argument is the destination.  Remove it from $@.
200e07dc26bSmrg  for arg
201e07dc26bSmrg  do
202e07dc26bSmrg    if test -n "$dst_arg"; then
203e07dc26bSmrg      # $@ is not empty: it contains at least $arg.
204e07dc26bSmrg      set fnord "$@" "$dst_arg"
205e07dc26bSmrg      shift # fnord
206e07dc26bSmrg    fi
207e07dc26bSmrg    shift # arg
208e07dc26bSmrg    dst_arg=$arg
209e07dc26bSmrg    # Protect names problematic for 'test' and other utilities.
210e07dc26bSmrg    case $dst_arg in
211e07dc26bSmrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
212e07dc26bSmrg    esac
213e07dc26bSmrg  done
214e07dc26bSmrgfi
215e07dc26bSmrg
216e07dc26bSmrgif test $# -eq 0; then
217e07dc26bSmrg  if test -z "$dir_arg"; then
218e07dc26bSmrg    echo "$0: no input file specified." >&2
219e07dc26bSmrg    exit 1
220e07dc26bSmrg  fi
221e07dc26bSmrg  # It's OK to call 'install-sh -d' without argument.
222e07dc26bSmrg  # This can happen when creating conditional directories.
223e07dc26bSmrg  exit 0
224e07dc26bSmrgfi
225e07dc26bSmrg
226e07dc26bSmrgif test -z "$dir_arg"; then
227e07dc26bSmrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
228e07dc26bSmrg    if test ! -d "$dst_arg"; then
229e07dc26bSmrg      echo "$0: $dst_arg: Is not a directory." >&2
230e07dc26bSmrg      exit 1
231e07dc26bSmrg    fi
232e07dc26bSmrg  fi
233e07dc26bSmrgfi
234e07dc26bSmrg
235e07dc26bSmrgif test -z "$dir_arg"; then
236e07dc26bSmrg  do_exit='(exit $ret); exit $ret'
237e07dc26bSmrg  trap "ret=129; $do_exit" 1
238e07dc26bSmrg  trap "ret=130; $do_exit" 2
239e07dc26bSmrg  trap "ret=141; $do_exit" 13
240e07dc26bSmrg  trap "ret=143; $do_exit" 15
241e07dc26bSmrg
242e07dc26bSmrg  # Set umask so as not to create temps with too-generous modes.
243e07dc26bSmrg  # However, 'strip' requires both read and write access to temps.
244e07dc26bSmrg  case $mode in
245e07dc26bSmrg    # Optimize common cases.
246e07dc26bSmrg    *644) cp_umask=133;;
247e07dc26bSmrg    *755) cp_umask=22;;
248e07dc26bSmrg
249e07dc26bSmrg    *[0-7])
250e07dc26bSmrg      if test -z "$stripcmd"; then
251e07dc26bSmrg        u_plus_rw=
252e07dc26bSmrg      else
253e07dc26bSmrg        u_plus_rw='% 200'
254e07dc26bSmrg      fi
255e07dc26bSmrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
256e07dc26bSmrg    *)
257e07dc26bSmrg      if test -z "$stripcmd"; then
258e07dc26bSmrg        u_plus_rw=
259e07dc26bSmrg      else
260e07dc26bSmrg        u_plus_rw=,u+rw
261e07dc26bSmrg      fi
262e07dc26bSmrg      cp_umask=$mode$u_plus_rw;;
263e07dc26bSmrg  esac
264e07dc26bSmrgfi
265e07dc26bSmrg
266e07dc26bSmrgfor src
267e07dc26bSmrgdo
268e07dc26bSmrg  # Protect names problematic for 'test' and other utilities.
269e07dc26bSmrg  case $src in
270e07dc26bSmrg    -* | [=\(\)!]) src=./$src;;
271e07dc26bSmrg  esac
272e07dc26bSmrg
273e07dc26bSmrg  if test -n "$dir_arg"; then
274e07dc26bSmrg    dst=$src
275e07dc26bSmrg    dstdir=$dst
276e07dc26bSmrg    test -d "$dstdir"
277e07dc26bSmrg    dstdir_status=$?
278636c353eSmrg    # Don't chown directories that already exist.
279636c353eSmrg    if test $dstdir_status = 0; then
280636c353eSmrg      chowncmd=""
281636c353eSmrg    fi
282e07dc26bSmrg  else
283e07dc26bSmrg
284e07dc26bSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
285e07dc26bSmrg    # might cause directories to be created, which would be especially bad
286e07dc26bSmrg    # if $src (and thus $dsttmp) contains '*'.
287e07dc26bSmrg    if test ! -f "$src" && test ! -d "$src"; then
288e07dc26bSmrg      echo "$0: $src does not exist." >&2
289e07dc26bSmrg      exit 1
290e07dc26bSmrg    fi
291e07dc26bSmrg
292e07dc26bSmrg    if test -z "$dst_arg"; then
293e07dc26bSmrg      echo "$0: no destination specified." >&2
294e07dc26bSmrg      exit 1
295e07dc26bSmrg    fi
296e07dc26bSmrg    dst=$dst_arg
297e07dc26bSmrg
298636c353eSmrg    # If destination is a directory, append the input filename.
299e07dc26bSmrg    if test -d "$dst"; then
300e07dc26bSmrg      if test "$is_target_a_directory" = never; then
301e07dc26bSmrg        echo "$0: $dst_arg: Is a directory" >&2
302e07dc26bSmrg        exit 1
303e07dc26bSmrg      fi
304e07dc26bSmrg      dstdir=$dst
305636c353eSmrg      dstbase=`basename "$src"`
306636c353eSmrg      case $dst in
307636c353eSmrg	*/) dst=$dst$dstbase;;
308636c353eSmrg	*)  dst=$dst/$dstbase;;
309636c353eSmrg      esac
310e07dc26bSmrg      dstdir_status=0
311e07dc26bSmrg    else
312e07dc26bSmrg      dstdir=`dirname "$dst"`
313e07dc26bSmrg      test -d "$dstdir"
314e07dc26bSmrg      dstdir_status=$?
315e07dc26bSmrg    fi
316e07dc26bSmrg  fi
317e07dc26bSmrg
318636c353eSmrg  case $dstdir in
319636c353eSmrg    */) dstdirslash=$dstdir;;
320636c353eSmrg    *)  dstdirslash=$dstdir/;;
321636c353eSmrg  esac
322636c353eSmrg
323e07dc26bSmrg  obsolete_mkdir_used=false
324e07dc26bSmrg
325e07dc26bSmrg  if test $dstdir_status != 0; then
326e07dc26bSmrg    case $posix_mkdir in
327e07dc26bSmrg      '')
328e07dc26bSmrg        # With -d, create the new directory with the user-specified mode.
329e07dc26bSmrg        # Otherwise, rely on $mkdir_umask.
330e07dc26bSmrg        if test -n "$dir_arg"; then
331e07dc26bSmrg          mkdir_mode=-m$mode
332e07dc26bSmrg        else
333e07dc26bSmrg          mkdir_mode=
334e07dc26bSmrg        fi
335e07dc26bSmrg
336e07dc26bSmrg        posix_mkdir=false
337636c353eSmrg	# The $RANDOM variable is not portable (e.g., dash).  Use it
338636c353eSmrg	# here however when possible just to lower collision chance.
339636c353eSmrg	tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
340636c353eSmrg
341636c353eSmrg	trap '
342636c353eSmrg	  ret=$?
343636c353eSmrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null
344636c353eSmrg	  exit $ret
345636c353eSmrg	' 0
346636c353eSmrg
347636c353eSmrg	# Because "mkdir -p" follows existing symlinks and we likely work
348636c353eSmrg	# directly in world-writeable /tmp, make sure that the '$tmpdir'
349636c353eSmrg	# directory is successfully created first before we actually test
350636c353eSmrg	# 'mkdir -p'.
351636c353eSmrg	if (umask $mkdir_umask &&
352636c353eSmrg	    $mkdirprog $mkdir_mode "$tmpdir" &&
353636c353eSmrg	    exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
354636c353eSmrg	then
355636c353eSmrg	  if test -z "$dir_arg" || {
356636c353eSmrg	       # Check for POSIX incompatibilities with -m.
357636c353eSmrg	       # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
358636c353eSmrg	       # other-writable bit of parent directory when it shouldn't.
359636c353eSmrg	       # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
360636c353eSmrg	       test_tmpdir="$tmpdir/a"
361636c353eSmrg	       ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
362636c353eSmrg	       case $ls_ld_tmpdir in
363636c353eSmrg		 d????-?r-*) different_mode=700;;
364636c353eSmrg		 d????-?--*) different_mode=755;;
365636c353eSmrg		 *) false;;
366636c353eSmrg	       esac &&
367636c353eSmrg	       $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
368636c353eSmrg		 ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
369636c353eSmrg		 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
370636c353eSmrg	       }
371636c353eSmrg	     }
372636c353eSmrg	  then posix_mkdir=:
373636c353eSmrg	  fi
374636c353eSmrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
375636c353eSmrg	else
376636c353eSmrg	  # Remove any dirs left behind by ancient mkdir implementations.
377636c353eSmrg	  rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
378636c353eSmrg	fi
379636c353eSmrg	trap '' 0;;
380e07dc26bSmrg    esac
381e07dc26bSmrg
382e07dc26bSmrg    if
383e07dc26bSmrg      $posix_mkdir && (
384e07dc26bSmrg        umask $mkdir_umask &&
385e07dc26bSmrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
386e07dc26bSmrg      )
387e07dc26bSmrg    then :
388e07dc26bSmrg    else
389e07dc26bSmrg
390636c353eSmrg      # mkdir does not conform to POSIX,
391e07dc26bSmrg      # or it failed possibly due to a race condition.  Create the
392e07dc26bSmrg      # directory the slow way, step by step, checking for races as we go.
393e07dc26bSmrg
394e07dc26bSmrg      case $dstdir in
395e07dc26bSmrg        /*) prefix='/';;
396e07dc26bSmrg        [-=\(\)!]*) prefix='./';;
397e07dc26bSmrg        *)  prefix='';;
398e07dc26bSmrg      esac
399e07dc26bSmrg
400e07dc26bSmrg      oIFS=$IFS
401e07dc26bSmrg      IFS=/
402e07dc26bSmrg      set -f
403e07dc26bSmrg      set fnord $dstdir
404e07dc26bSmrg      shift
405e07dc26bSmrg      set +f
406e07dc26bSmrg      IFS=$oIFS
407e07dc26bSmrg
408e07dc26bSmrg      prefixes=
409e07dc26bSmrg
410e07dc26bSmrg      for d
411e07dc26bSmrg      do
412e07dc26bSmrg        test X"$d" = X && continue
413e07dc26bSmrg
414e07dc26bSmrg        prefix=$prefix$d
415e07dc26bSmrg        if test -d "$prefix"; then
416e07dc26bSmrg          prefixes=
417e07dc26bSmrg        else
418e07dc26bSmrg          if $posix_mkdir; then
419636c353eSmrg            (umask $mkdir_umask &&
420e07dc26bSmrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
421e07dc26bSmrg            # Don't fail if two instances are running concurrently.
422e07dc26bSmrg            test -d "$prefix" || exit 1
423e07dc26bSmrg          else
424e07dc26bSmrg            case $prefix in
425e07dc26bSmrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
426e07dc26bSmrg              *) qprefix=$prefix;;
427e07dc26bSmrg            esac
428e07dc26bSmrg            prefixes="$prefixes '$qprefix'"
429e07dc26bSmrg          fi
430e07dc26bSmrg        fi
431e07dc26bSmrg        prefix=$prefix/
432e07dc26bSmrg      done
433e07dc26bSmrg
434e07dc26bSmrg      if test -n "$prefixes"; then
435e07dc26bSmrg        # Don't fail if two instances are running concurrently.
436e07dc26bSmrg        (umask $mkdir_umask &&
437e07dc26bSmrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
438e07dc26bSmrg          test -d "$dstdir" || exit 1
439e07dc26bSmrg        obsolete_mkdir_used=true
440e07dc26bSmrg      fi
441e07dc26bSmrg    fi
442e07dc26bSmrg  fi
443e07dc26bSmrg
444e07dc26bSmrg  if test -n "$dir_arg"; then
445e07dc26bSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
446e07dc26bSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
447e07dc26bSmrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
448e07dc26bSmrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
449e07dc26bSmrg  else
450e07dc26bSmrg
451e07dc26bSmrg    # Make a couple of temp file names in the proper directory.
452636c353eSmrg    dsttmp=${dstdirslash}_inst.$$_
453636c353eSmrg    rmtmp=${dstdirslash}_rm.$$_
454e07dc26bSmrg
455e07dc26bSmrg    # Trap to clean up those temp files at exit.
456e07dc26bSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
457e07dc26bSmrg
458e07dc26bSmrg    # Copy the file name to the temp name.
459636c353eSmrg    (umask $cp_umask &&
460636c353eSmrg     { test -z "$stripcmd" || {
461636c353eSmrg	 # Create $dsttmp read-write so that cp doesn't create it read-only,
462636c353eSmrg	 # which would cause strip to fail.
463636c353eSmrg	 if test -z "$doit"; then
464636c353eSmrg	   : >"$dsttmp" # No need to fork-exec 'touch'.
465636c353eSmrg	 else
466636c353eSmrg	   $doit touch "$dsttmp"
467636c353eSmrg	 fi
468636c353eSmrg       }
469636c353eSmrg     } &&
470636c353eSmrg     $doit_exec $cpprog "$src" "$dsttmp") &&
471e07dc26bSmrg
472e07dc26bSmrg    # and set any options; do chmod last to preserve setuid bits.
473e07dc26bSmrg    #
474e07dc26bSmrg    # If any of these fail, we abort the whole thing.  If we want to
475e07dc26bSmrg    # ignore errors from any of these, just make sure not to ignore
476e07dc26bSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
477e07dc26bSmrg    #
478e07dc26bSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
479e07dc26bSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
480e07dc26bSmrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
481e07dc26bSmrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
482e07dc26bSmrg
483e07dc26bSmrg    # If -C, don't bother to copy if it wouldn't change the file.
484e07dc26bSmrg    if $copy_on_change &&
485e07dc26bSmrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
486e07dc26bSmrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
487e07dc26bSmrg       set -f &&
488e07dc26bSmrg       set X $old && old=:$2:$4:$5:$6 &&
489e07dc26bSmrg       set X $new && new=:$2:$4:$5:$6 &&
490e07dc26bSmrg       set +f &&
491e07dc26bSmrg       test "$old" = "$new" &&
492e07dc26bSmrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
493e07dc26bSmrg    then
494e07dc26bSmrg      rm -f "$dsttmp"
495e07dc26bSmrg    else
496636c353eSmrg      # If $backupsuffix is set, and the file being installed
497636c353eSmrg      # already exists, attempt a backup.  Don't worry if it fails,
498636c353eSmrg      # e.g., if mv doesn't support -f.
499636c353eSmrg      if test -n "$backupsuffix" && test -f "$dst"; then
500636c353eSmrg        $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null
501636c353eSmrg      fi
502636c353eSmrg
503e07dc26bSmrg      # Rename the file to the real destination.
504e07dc26bSmrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
505e07dc26bSmrg
506e07dc26bSmrg      # The rename failed, perhaps because mv can't rename something else
507e07dc26bSmrg      # to itself, or perhaps because mv is so ancient that it does not
508e07dc26bSmrg      # support -f.
509e07dc26bSmrg      {
510e07dc26bSmrg        # Now remove or move aside any old file at destination location.
511e07dc26bSmrg        # We try this two ways since rm can't unlink itself on some
512e07dc26bSmrg        # systems and the destination file might be busy for other
513e07dc26bSmrg        # reasons.  In this case, the final cleanup might fail but the new
514e07dc26bSmrg        # file should still install successfully.
515e07dc26bSmrg        {
516e07dc26bSmrg          test ! -f "$dst" ||
517636c353eSmrg          $doit $rmcmd "$dst" 2>/dev/null ||
518e07dc26bSmrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
519636c353eSmrg            { $doit $rmcmd "$rmtmp" 2>/dev/null; :; }
520e07dc26bSmrg          } ||
521e07dc26bSmrg          { echo "$0: cannot unlink or rename $dst" >&2
522e07dc26bSmrg            (exit 1); exit 1
523e07dc26bSmrg          }
524e07dc26bSmrg        } &&
525e07dc26bSmrg
526e07dc26bSmrg        # Now rename the file to the real destination.
527e07dc26bSmrg        $doit $mvcmd "$dsttmp" "$dst"
528e07dc26bSmrg      }
529e07dc26bSmrg    fi || exit 1
530e07dc26bSmrg
531e07dc26bSmrg    trap '' 0
532e07dc26bSmrg  fi
533e07dc26bSmrgdone
534e07dc26bSmrg
535e07dc26bSmrg# Local variables:
536636c353eSmrg# eval: (add-hook 'before-save-hook 'time-stamp)
537e07dc26bSmrg# time-stamp-start: "scriptversion="
538e07dc26bSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
539636c353eSmrg# time-stamp-time-zone: "UTC0"
540e07dc26bSmrg# time-stamp-end: "; # UTC"
541e07dc26bSmrg# End:
542