1caade7ccSmrg#!/bin/sh
2caade7ccSmrg# install - install a program, script, or datafile
3caade7ccSmrg
40760f5d2Smrgscriptversion=2020-11-14.01; # UTC
5caade7ccSmrg
6caade7ccSmrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7caade7ccSmrg# later released in X11R6 (xc/config/util/install.sh) with the
8caade7ccSmrg# following copyright and license.
9caade7ccSmrg#
10caade7ccSmrg# Copyright (C) 1994 X Consortium
11caade7ccSmrg#
12caade7ccSmrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13caade7ccSmrg# of this software and associated documentation files (the "Software"), to
14caade7ccSmrg# deal in the Software without restriction, including without limitation the
15caade7ccSmrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16caade7ccSmrg# sell copies of the Software, and to permit persons to whom the Software is
17caade7ccSmrg# furnished to do so, subject to the following conditions:
18caade7ccSmrg#
19caade7ccSmrg# The above copyright notice and this permission notice shall be included in
20caade7ccSmrg# all copies or substantial portions of the Software.
21caade7ccSmrg#
22caade7ccSmrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23caade7ccSmrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24caade7ccSmrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25caade7ccSmrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26caade7ccSmrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27caade7ccSmrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28caade7ccSmrg#
29caade7ccSmrg# Except as contained in this notice, the name of the X Consortium shall not
30caade7ccSmrg# be used in advertising or otherwise to promote the sale, use or other deal-
31caade7ccSmrg# ings in this Software without prior written authorization from the X Consor-
32caade7ccSmrg# tium.
33caade7ccSmrg#
34caade7ccSmrg#
35caade7ccSmrg# FSF changes to this file are in the public domain.
36caade7ccSmrg#
37caade7ccSmrg# Calling this script install-sh is preferred over install.sh, to prevent
38af9a7ee5Smrg# 'make' implicit rules from creating a file called install from it
39caade7ccSmrg# when there is no Makefile.
40caade7ccSmrg#
41caade7ccSmrg# This script is compatible with the BSD install script, but was written
42caade7ccSmrg# from scratch.
43caade7ccSmrg
44cc4920e5Smrgtab='	'
45caade7ccSmrgnl='
46caade7ccSmrg'
47cc4920e5SmrgIFS=" $tab$nl"
48caade7ccSmrg
49cc4920e5Smrg# Set DOITPROG to "echo" to test this script.
50caade7ccSmrg
51485f0483Smrgdoit=${DOITPROG-}
52cc4920e5Smrgdoit_exec=${doit:-exec}
53caade7ccSmrg
54caade7ccSmrg# Put in absolute file names if you don't have them in your path;
55caade7ccSmrg# or use environment vars.
56caade7ccSmrg
57485f0483Smrgchgrpprog=${CHGRPPROG-chgrp}
58485f0483Smrgchmodprog=${CHMODPROG-chmod}
59485f0483Smrgchownprog=${CHOWNPROG-chown}
60485f0483Smrgcmpprog=${CMPPROG-cmp}
61485f0483Smrgcpprog=${CPPROG-cp}
62485f0483Smrgmkdirprog=${MKDIRPROG-mkdir}
63485f0483Smrgmvprog=${MVPROG-mv}
64485f0483Smrgrmprog=${RMPROG-rm}
65485f0483Smrgstripprog=${STRIPPROG-strip}
66485f0483Smrg
67caade7ccSmrgposix_mkdir=
68caade7ccSmrg
69caade7ccSmrg# Desired mode of installed file.
70caade7ccSmrgmode=0755
71caade7ccSmrg
720760f5d2Smrg# Create dirs (including intermediate dirs) using mode 755.
730760f5d2Smrg# This is like GNU 'install' as of coreutils 8.32 (2020).
740760f5d2Smrgmkdir_umask=22
750760f5d2Smrg
760760f5d2Smrgbackupsuffix=
77485f0483Smrgchgrpcmd=
78caade7ccSmrgchmodcmd=$chmodprog
79caade7ccSmrgchowncmd=
80485f0483Smrgmvcmd=$mvprog
81caade7ccSmrgrmcmd="$rmprog -f"
82485f0483Smrgstripcmd=
83485f0483Smrg
84caade7ccSmrgsrc=
85caade7ccSmrgdst=
86caade7ccSmrgdir_arg=
87485f0483Smrgdst_arg=
88485f0483Smrg
89485f0483Smrgcopy_on_change=false
90cc4920e5Smrgis_target_a_directory=possibly
91caade7ccSmrg
92485f0483Smrgusage="\
93485f0483SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
94caade7ccSmrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
95caade7ccSmrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
96caade7ccSmrg   or: $0 [OPTION]... -d DIRECTORIES...
97caade7ccSmrg
98caade7ccSmrgIn the 1st form, copy SRCFILE to DSTFILE.
99caade7ccSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
100caade7ccSmrgIn the 4th, create DIRECTORIES.
101caade7ccSmrg
102caade7ccSmrgOptions:
103485f0483Smrg     --help     display this help and exit.
104485f0483Smrg     --version  display version info and exit.
105485f0483Smrg
106485f0483Smrg  -c            (ignored)
1070760f5d2Smrg  -C            install only if different (preserve data modification time)
108485f0483Smrg  -d            create directories instead of installing files.
109485f0483Smrg  -g GROUP      $chgrpprog installed files to GROUP.
110485f0483Smrg  -m MODE       $chmodprog installed files to MODE.
111485f0483Smrg  -o USER       $chownprog installed files to USER.
1120760f5d2Smrg  -p            pass -p to $cpprog.
113485f0483Smrg  -s            $stripprog installed files.
1140760f5d2Smrg  -S SUFFIX     attempt to back up existing files, with suffix SUFFIX.
115485f0483Smrg  -t DIRECTORY  install into DIRECTORY.
116485f0483Smrg  -T            report an error if DSTFILE is a directory.
117caade7ccSmrg
118caade7ccSmrgEnvironment variables override the default commands:
119485f0483Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
120485f0483Smrg  RMPROG STRIPPROG
1210760f5d2Smrg
1220760f5d2SmrgBy default, rm is invoked with -f; when overridden with RMPROG,
1230760f5d2Smrgit's up to you to specify -f if you want it.
1240760f5d2Smrg
1250760f5d2SmrgIf -S is not specified, no backups are attempted.
1260760f5d2Smrg
1270760f5d2SmrgEmail bug reports to bug-automake@gnu.org.
1280760f5d2SmrgAutomake home page: https://www.gnu.org/software/automake/
129caade7ccSmrg"
130caade7ccSmrg
131caade7ccSmrgwhile test $# -ne 0; do
132caade7ccSmrg  case $1 in
133485f0483Smrg    -c) ;;
134485f0483Smrg
135485f0483Smrg    -C) copy_on_change=true;;
136caade7ccSmrg
137485f0483Smrg    -d) dir_arg=true;;
138caade7ccSmrg
139caade7ccSmrg    -g) chgrpcmd="$chgrpprog $2"
140cc4920e5Smrg        shift;;
141caade7ccSmrg
142caade7ccSmrg    --help) echo "$usage"; exit $?;;
143caade7ccSmrg
144caade7ccSmrg    -m) mode=$2
145cc4920e5Smrg        case $mode in
146cc4920e5Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
147cc4920e5Smrg            echo "$0: invalid mode: $mode" >&2
148cc4920e5Smrg            exit 1;;
149cc4920e5Smrg        esac
150cc4920e5Smrg        shift;;
151caade7ccSmrg
152caade7ccSmrg    -o) chowncmd="$chownprog $2"
153cc4920e5Smrg        shift;;
154caade7ccSmrg
1550760f5d2Smrg    -p) cpprog="$cpprog -p";;
1560760f5d2Smrg
157485f0483Smrg    -s) stripcmd=$stripprog;;
158caade7ccSmrg
1590760f5d2Smrg    -S) backupsuffix="$2"
1600760f5d2Smrg        shift;;
1610760f5d2Smrg
162cc4920e5Smrg    -t)
163cc4920e5Smrg        is_target_a_directory=always
164cc4920e5Smrg        dst_arg=$2
165cc4920e5Smrg        # Protect names problematic for 'test' and other utilities.
166cc4920e5Smrg        case $dst_arg in
167cc4920e5Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
168cc4920e5Smrg        esac
169cc4920e5Smrg        shift;;
170caade7ccSmrg
171cc4920e5Smrg    -T) is_target_a_directory=never;;
172caade7ccSmrg
173caade7ccSmrg    --version) echo "$0 $scriptversion"; exit $?;;
174caade7ccSmrg
175cc4920e5Smrg    --) shift
176cc4920e5Smrg        break;;
177caade7ccSmrg
178cc4920e5Smrg    -*) echo "$0: invalid option: $1" >&2
179cc4920e5Smrg        exit 1;;
180caade7ccSmrg
181caade7ccSmrg    *)  break;;
182caade7ccSmrg  esac
183485f0483Smrg  shift
184caade7ccSmrgdone
185caade7ccSmrg
186cc4920e5Smrg# We allow the use of options -d and -T together, by making -d
187cc4920e5Smrg# take the precedence; this is for compatibility with GNU install.
188cc4920e5Smrg
189cc4920e5Smrgif test -n "$dir_arg"; then
190cc4920e5Smrg  if test -n "$dst_arg"; then
191cc4920e5Smrg    echo "$0: target directory not allowed when installing a directory." >&2
192cc4920e5Smrg    exit 1
193cc4920e5Smrg  fi
194cc4920e5Smrgfi
195cc4920e5Smrg
196485f0483Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
197caade7ccSmrg  # When -d is used, all remaining arguments are directories to create.
198caade7ccSmrg  # When -t is used, the destination is already specified.
199caade7ccSmrg  # Otherwise, the last argument is the destination.  Remove it from $@.
200caade7ccSmrg  for arg
201caade7ccSmrg  do
202485f0483Smrg    if test -n "$dst_arg"; then
203caade7ccSmrg      # $@ is not empty: it contains at least $arg.
204485f0483Smrg      set fnord "$@" "$dst_arg"
205caade7ccSmrg      shift # fnord
206caade7ccSmrg    fi
207caade7ccSmrg    shift # arg
208485f0483Smrg    dst_arg=$arg
209af9a7ee5Smrg    # Protect names problematic for 'test' and other utilities.
210af9a7ee5Smrg    case $dst_arg in
211af9a7ee5Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
212af9a7ee5Smrg    esac
213caade7ccSmrg  done
214caade7ccSmrgfi
215caade7ccSmrg
216caade7ccSmrgif test $# -eq 0; then
217caade7ccSmrg  if test -z "$dir_arg"; then
218caade7ccSmrg    echo "$0: no input file specified." >&2
219caade7ccSmrg    exit 1
220caade7ccSmrg  fi
221af9a7ee5Smrg  # It's OK to call 'install-sh -d' without argument.
222caade7ccSmrg  # This can happen when creating conditional directories.
223caade7ccSmrg  exit 0
224caade7ccSmrgfi
225caade7ccSmrg
226cc4920e5Smrgif test -z "$dir_arg"; then
227cc4920e5Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
228cc4920e5Smrg    if test ! -d "$dst_arg"; then
229cc4920e5Smrg      echo "$0: $dst_arg: Is not a directory." >&2
230cc4920e5Smrg      exit 1
231cc4920e5Smrg    fi
232cc4920e5Smrg  fi
233cc4920e5Smrgfi
234cc4920e5Smrg
235caade7ccSmrgif test -z "$dir_arg"; then
236af9a7ee5Smrg  do_exit='(exit $ret); exit $ret'
237af9a7ee5Smrg  trap "ret=129; $do_exit" 1
238af9a7ee5Smrg  trap "ret=130; $do_exit" 2
239af9a7ee5Smrg  trap "ret=141; $do_exit" 13
240af9a7ee5Smrg  trap "ret=143; $do_exit" 15
241caade7ccSmrg
242caade7ccSmrg  # Set umask so as not to create temps with too-generous modes.
243caade7ccSmrg  # However, 'strip' requires both read and write access to temps.
244caade7ccSmrg  case $mode in
245caade7ccSmrg    # Optimize common cases.
246caade7ccSmrg    *644) cp_umask=133;;
247caade7ccSmrg    *755) cp_umask=22;;
248caade7ccSmrg
249caade7ccSmrg    *[0-7])
250caade7ccSmrg      if test -z "$stripcmd"; then
251cc4920e5Smrg        u_plus_rw=
252caade7ccSmrg      else
253cc4920e5Smrg        u_plus_rw='% 200'
254caade7ccSmrg      fi
255caade7ccSmrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
256caade7ccSmrg    *)
257caade7ccSmrg      if test -z "$stripcmd"; then
258cc4920e5Smrg        u_plus_rw=
259caade7ccSmrg      else
260cc4920e5Smrg        u_plus_rw=,u+rw
261caade7ccSmrg      fi
262caade7ccSmrg      cp_umask=$mode$u_plus_rw;;
263caade7ccSmrg  esac
264caade7ccSmrgfi
265caade7ccSmrg
266caade7ccSmrgfor src
267caade7ccSmrgdo
268af9a7ee5Smrg  # Protect names problematic for 'test' and other utilities.
269caade7ccSmrg  case $src in
270af9a7ee5Smrg    -* | [=\(\)!]) src=./$src;;
271caade7ccSmrg  esac
272caade7ccSmrg
273caade7ccSmrg  if test -n "$dir_arg"; then
274caade7ccSmrg    dst=$src
275caade7ccSmrg    dstdir=$dst
276caade7ccSmrg    test -d "$dstdir"
277caade7ccSmrg    dstdir_status=$?
2780760f5d2Smrg    # Don't chown directories that already exist.
2790760f5d2Smrg    if test $dstdir_status = 0; then
2800760f5d2Smrg      chowncmd=""
2810760f5d2Smrg    fi
282caade7ccSmrg  else
283caade7ccSmrg
284caade7ccSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
285caade7ccSmrg    # might cause directories to be created, which would be especially bad
286caade7ccSmrg    # if $src (and thus $dsttmp) contains '*'.
287caade7ccSmrg    if test ! -f "$src" && test ! -d "$src"; then
288caade7ccSmrg      echo "$0: $src does not exist." >&2
289caade7ccSmrg      exit 1
290caade7ccSmrg    fi
291caade7ccSmrg
292485f0483Smrg    if test -z "$dst_arg"; then
293caade7ccSmrg      echo "$0: no destination specified." >&2
294caade7ccSmrg      exit 1
295caade7ccSmrg    fi
296485f0483Smrg    dst=$dst_arg
297caade7ccSmrg
2980760f5d2Smrg    # If destination is a directory, append the input filename.
299caade7ccSmrg    if test -d "$dst"; then
300cc4920e5Smrg      if test "$is_target_a_directory" = never; then
301cc4920e5Smrg        echo "$0: $dst_arg: Is a directory" >&2
302cc4920e5Smrg        exit 1
303caade7ccSmrg      fi
304caade7ccSmrg      dstdir=$dst
3050760f5d2Smrg      dstbase=`basename "$src"`
3060760f5d2Smrg      case $dst in
3070760f5d2Smrg	*/) dst=$dst$dstbase;;
3080760f5d2Smrg	*)  dst=$dst/$dstbase;;
3090760f5d2Smrg      esac
310caade7ccSmrg      dstdir_status=0
311caade7ccSmrg    else
312cc4920e5Smrg      dstdir=`dirname "$dst"`
313caade7ccSmrg      test -d "$dstdir"
314caade7ccSmrg      dstdir_status=$?
315caade7ccSmrg    fi
316caade7ccSmrg  fi
317caade7ccSmrg
3180760f5d2Smrg  case $dstdir in
3190760f5d2Smrg    */) dstdirslash=$dstdir;;
3200760f5d2Smrg    *)  dstdirslash=$dstdir/;;
3210760f5d2Smrg  esac
3220760f5d2Smrg
323caade7ccSmrg  obsolete_mkdir_used=false
324caade7ccSmrg
325caade7ccSmrg  if test $dstdir_status != 0; then
326caade7ccSmrg    case $posix_mkdir in
327caade7ccSmrg      '')
328cc4920e5Smrg        # With -d, create the new directory with the user-specified mode.
329cc4920e5Smrg        # Otherwise, rely on $mkdir_umask.
330cc4920e5Smrg        if test -n "$dir_arg"; then
331cc4920e5Smrg          mkdir_mode=-m$mode
332cc4920e5Smrg        else
333cc4920e5Smrg          mkdir_mode=
334cc4920e5Smrg        fi
335cc4920e5Smrg
336cc4920e5Smrg        posix_mkdir=false
3370760f5d2Smrg	# The $RANDOM variable is not portable (e.g., dash).  Use it
3380760f5d2Smrg	# here however when possible just to lower collision chance.
3390760f5d2Smrg	tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
3400760f5d2Smrg
3410760f5d2Smrg	trap '
3420760f5d2Smrg	  ret=$?
3430760f5d2Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null
3440760f5d2Smrg	  exit $ret
3450760f5d2Smrg	' 0
3460760f5d2Smrg
3470760f5d2Smrg	# Because "mkdir -p" follows existing symlinks and we likely work
3480760f5d2Smrg	# directly in world-writeable /tmp, make sure that the '$tmpdir'
3490760f5d2Smrg	# directory is successfully created first before we actually test
3500760f5d2Smrg	# 'mkdir -p'.
3510760f5d2Smrg	if (umask $mkdir_umask &&
3520760f5d2Smrg	    $mkdirprog $mkdir_mode "$tmpdir" &&
3530760f5d2Smrg	    exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
3540760f5d2Smrg	then
3550760f5d2Smrg	  if test -z "$dir_arg" || {
3560760f5d2Smrg	       # Check for POSIX incompatibilities with -m.
3570760f5d2Smrg	       # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
3580760f5d2Smrg	       # other-writable bit of parent directory when it shouldn't.
3590760f5d2Smrg	       # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
3600760f5d2Smrg	       test_tmpdir="$tmpdir/a"
3610760f5d2Smrg	       ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
3620760f5d2Smrg	       case $ls_ld_tmpdir in
3630760f5d2Smrg		 d????-?r-*) different_mode=700;;
3640760f5d2Smrg		 d????-?--*) different_mode=755;;
3650760f5d2Smrg		 *) false;;
3660760f5d2Smrg	       esac &&
3670760f5d2Smrg	       $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
3680760f5d2Smrg		 ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
3690760f5d2Smrg		 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
3700760f5d2Smrg	       }
3710760f5d2Smrg	     }
3720760f5d2Smrg	  then posix_mkdir=:
3730760f5d2Smrg	  fi
3740760f5d2Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
3750760f5d2Smrg	else
3760760f5d2Smrg	  # Remove any dirs left behind by ancient mkdir implementations.
3770760f5d2Smrg	  rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
3780760f5d2Smrg	fi
3790760f5d2Smrg	trap '' 0;;
380caade7ccSmrg    esac
381caade7ccSmrg
382caade7ccSmrg    if
383caade7ccSmrg      $posix_mkdir && (
384cc4920e5Smrg        umask $mkdir_umask &&
385cc4920e5Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
386caade7ccSmrg      )
387caade7ccSmrg    then :
388caade7ccSmrg    else
389caade7ccSmrg
3900760f5d2Smrg      # mkdir does not conform to POSIX,
391caade7ccSmrg      # or it failed possibly due to a race condition.  Create the
392caade7ccSmrg      # directory the slow way, step by step, checking for races as we go.
393caade7ccSmrg
394caade7ccSmrg      case $dstdir in
395cc4920e5Smrg        /*) prefix='/';;
396cc4920e5Smrg        [-=\(\)!]*) prefix='./';;
397cc4920e5Smrg        *)  prefix='';;
398caade7ccSmrg      esac
399caade7ccSmrg
400caade7ccSmrg      oIFS=$IFS
401caade7ccSmrg      IFS=/
402cc4920e5Smrg      set -f
403caade7ccSmrg      set fnord $dstdir
404caade7ccSmrg      shift
405cc4920e5Smrg      set +f
406caade7ccSmrg      IFS=$oIFS
407caade7ccSmrg
408caade7ccSmrg      prefixes=
409caade7ccSmrg
410caade7ccSmrg      for d
411caade7ccSmrg      do
412cc4920e5Smrg        test X"$d" = X && continue
413cc4920e5Smrg
414cc4920e5Smrg        prefix=$prefix$d
415cc4920e5Smrg        if test -d "$prefix"; then
416cc4920e5Smrg          prefixes=
417cc4920e5Smrg        else
418cc4920e5Smrg          if $posix_mkdir; then
4190760f5d2Smrg            (umask $mkdir_umask &&
420cc4920e5Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
421cc4920e5Smrg            # Don't fail if two instances are running concurrently.
422cc4920e5Smrg            test -d "$prefix" || exit 1
423cc4920e5Smrg          else
424cc4920e5Smrg            case $prefix in
425cc4920e5Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
426cc4920e5Smrg              *) qprefix=$prefix;;
427cc4920e5Smrg            esac
428cc4920e5Smrg            prefixes="$prefixes '$qprefix'"
429cc4920e5Smrg          fi
430cc4920e5Smrg        fi
431cc4920e5Smrg        prefix=$prefix/
432caade7ccSmrg      done
433caade7ccSmrg
434caade7ccSmrg      if test -n "$prefixes"; then
435cc4920e5Smrg        # Don't fail if two instances are running concurrently.
436cc4920e5Smrg        (umask $mkdir_umask &&
437cc4920e5Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
438cc4920e5Smrg          test -d "$dstdir" || exit 1
439cc4920e5Smrg        obsolete_mkdir_used=true
440caade7ccSmrg      fi
441caade7ccSmrg    fi
442caade7ccSmrg  fi
443caade7ccSmrg
444caade7ccSmrg  if test -n "$dir_arg"; then
445caade7ccSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
446caade7ccSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
447caade7ccSmrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
448caade7ccSmrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
449caade7ccSmrg  else
450caade7ccSmrg
451caade7ccSmrg    # Make a couple of temp file names in the proper directory.
4520760f5d2Smrg    dsttmp=${dstdirslash}_inst.$$_
4530760f5d2Smrg    rmtmp=${dstdirslash}_rm.$$_
454caade7ccSmrg
455caade7ccSmrg    # Trap to clean up those temp files at exit.
456caade7ccSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
457caade7ccSmrg
458caade7ccSmrg    # Copy the file name to the temp name.
4590760f5d2Smrg    (umask $cp_umask &&
4600760f5d2Smrg     { test -z "$stripcmd" || {
4610760f5d2Smrg	 # Create $dsttmp read-write so that cp doesn't create it read-only,
4620760f5d2Smrg	 # which would cause strip to fail.
4630760f5d2Smrg	 if test -z "$doit"; then
4640760f5d2Smrg	   : >"$dsttmp" # No need to fork-exec 'touch'.
4650760f5d2Smrg	 else
4660760f5d2Smrg	   $doit touch "$dsttmp"
4670760f5d2Smrg	 fi
4680760f5d2Smrg       }
4690760f5d2Smrg     } &&
4700760f5d2Smrg     $doit_exec $cpprog "$src" "$dsttmp") &&
471caade7ccSmrg
472caade7ccSmrg    # and set any options; do chmod last to preserve setuid bits.
473caade7ccSmrg    #
474caade7ccSmrg    # If any of these fail, we abort the whole thing.  If we want to
475caade7ccSmrg    # ignore errors from any of these, just make sure not to ignore
476caade7ccSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
477caade7ccSmrg    #
478485f0483Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
479485f0483Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
480485f0483Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
481485f0483Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
482485f0483Smrg
483485f0483Smrg    # If -C, don't bother to copy if it wouldn't change the file.
484485f0483Smrg    if $copy_on_change &&
485cc4920e5Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
486cc4920e5Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
487cc4920e5Smrg       set -f &&
488485f0483Smrg       set X $old && old=:$2:$4:$5:$6 &&
489485f0483Smrg       set X $new && new=:$2:$4:$5:$6 &&
490cc4920e5Smrg       set +f &&
491485f0483Smrg       test "$old" = "$new" &&
492485f0483Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
493485f0483Smrg    then
494485f0483Smrg      rm -f "$dsttmp"
495485f0483Smrg    else
4960760f5d2Smrg      # If $backupsuffix is set, and the file being installed
4970760f5d2Smrg      # already exists, attempt a backup.  Don't worry if it fails,
4980760f5d2Smrg      # e.g., if mv doesn't support -f.
4990760f5d2Smrg      if test -n "$backupsuffix" && test -f "$dst"; then
5000760f5d2Smrg        $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null
5010760f5d2Smrg      fi
5020760f5d2Smrg
503485f0483Smrg      # Rename the file to the real destination.
504485f0483Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
505485f0483Smrg
506485f0483Smrg      # The rename failed, perhaps because mv can't rename something else
507485f0483Smrg      # to itself, or perhaps because mv is so ancient that it does not
508485f0483Smrg      # support -f.
509485f0483Smrg      {
510cc4920e5Smrg        # Now remove or move aside any old file at destination location.
511cc4920e5Smrg        # We try this two ways since rm can't unlink itself on some
512cc4920e5Smrg        # systems and the destination file might be busy for other
513cc4920e5Smrg        # reasons.  In this case, the final cleanup might fail but the new
514cc4920e5Smrg        # file should still install successfully.
515cc4920e5Smrg        {
516cc4920e5Smrg          test ! -f "$dst" ||
5170760f5d2Smrg          $doit $rmcmd "$dst" 2>/dev/null ||
518cc4920e5Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
5190760f5d2Smrg            { $doit $rmcmd "$rmtmp" 2>/dev/null; :; }
520cc4920e5Smrg          } ||
521cc4920e5Smrg          { echo "$0: cannot unlink or rename $dst" >&2
522cc4920e5Smrg            (exit 1); exit 1
523cc4920e5Smrg          }
524cc4920e5Smrg        } &&
525cc4920e5Smrg
526cc4920e5Smrg        # Now rename the file to the real destination.
527cc4920e5Smrg        $doit $mvcmd "$dsttmp" "$dst"
528485f0483Smrg      }
529485f0483Smrg    fi || exit 1
530caade7ccSmrg
531caade7ccSmrg    trap '' 0
532caade7ccSmrg  fi
533caade7ccSmrgdone
534caade7ccSmrg
535caade7ccSmrg# Local variables:
5360760f5d2Smrg# eval: (add-hook 'before-save-hook 'time-stamp)
537caade7ccSmrg# time-stamp-start: "scriptversion="
538caade7ccSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
5390760f5d2Smrg# time-stamp-time-zone: "UTC0"
540485f0483Smrg# time-stamp-end: "; # UTC"
541caade7ccSmrg# End:
542