1a966c04fSmrg#!/bin/sh
2a966c04fSmrg# install - install a program, script, or datafile
3a966c04fSmrg
474835918Smrgscriptversion=2020-11-14.01; # UTC
5a966c04fSmrg
6a966c04fSmrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7a966c04fSmrg# later released in X11R6 (xc/config/util/install.sh) with the
8a966c04fSmrg# following copyright and license.
9a966c04fSmrg#
10a966c04fSmrg# Copyright (C) 1994 X Consortium
11a966c04fSmrg#
12a966c04fSmrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13a966c04fSmrg# of this software and associated documentation files (the "Software"), to
14a966c04fSmrg# deal in the Software without restriction, including without limitation the
15a966c04fSmrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16a966c04fSmrg# sell copies of the Software, and to permit persons to whom the Software is
17a966c04fSmrg# furnished to do so, subject to the following conditions:
18a966c04fSmrg#
19a966c04fSmrg# The above copyright notice and this permission notice shall be included in
20a966c04fSmrg# all copies or substantial portions of the Software.
21a966c04fSmrg#
22a966c04fSmrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23a966c04fSmrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24a966c04fSmrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25a966c04fSmrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26a966c04fSmrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27a966c04fSmrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28a966c04fSmrg#
29a966c04fSmrg# Except as contained in this notice, the name of the X Consortium shall not
30a966c04fSmrg# be used in advertising or otherwise to promote the sale, use or other deal-
31a966c04fSmrg# ings in this Software without prior written authorization from the X Consor-
32a966c04fSmrg# tium.
33a966c04fSmrg#
34a966c04fSmrg#
35a966c04fSmrg# FSF changes to this file are in the public domain.
36a966c04fSmrg#
37a966c04fSmrg# Calling this script install-sh is preferred over install.sh, to prevent
38ac92798bSmrg# 'make' implicit rules from creating a file called install from it
39a966c04fSmrg# when there is no Makefile.
40a966c04fSmrg#
41a966c04fSmrg# This script is compatible with the BSD install script, but was written
42a966c04fSmrg# from scratch.
43a966c04fSmrg
44edce3322Smrgtab='	'
45a966c04fSmrgnl='
46a966c04fSmrg'
47edce3322SmrgIFS=" $tab$nl"
48a966c04fSmrg
49edce3322Smrg# Set DOITPROG to "echo" to test this script.
50a966c04fSmrg
512e2dd055Smrgdoit=${DOITPROG-}
52edce3322Smrgdoit_exec=${doit:-exec}
53a966c04fSmrg
54a966c04fSmrg# Put in absolute file names if you don't have them in your path;
55a966c04fSmrg# or use environment vars.
56a966c04fSmrg
572e2dd055Smrgchgrpprog=${CHGRPPROG-chgrp}
582e2dd055Smrgchmodprog=${CHMODPROG-chmod}
592e2dd055Smrgchownprog=${CHOWNPROG-chown}
602e2dd055Smrgcmpprog=${CMPPROG-cmp}
612e2dd055Smrgcpprog=${CPPROG-cp}
622e2dd055Smrgmkdirprog=${MKDIRPROG-mkdir}
632e2dd055Smrgmvprog=${MVPROG-mv}
642e2dd055Smrgrmprog=${RMPROG-rm}
652e2dd055Smrgstripprog=${STRIPPROG-strip}
662e2dd055Smrg
67a966c04fSmrgposix_mkdir=
68a966c04fSmrg
69a966c04fSmrg# Desired mode of installed file.
70a966c04fSmrgmode=0755
71a966c04fSmrg
7274835918Smrg# Create dirs (including intermediate dirs) using mode 755.
7374835918Smrg# This is like GNU 'install' as of coreutils 8.32 (2020).
7474835918Smrgmkdir_umask=22
7574835918Smrg
7674835918Smrgbackupsuffix=
772e2dd055Smrgchgrpcmd=
78a966c04fSmrgchmodcmd=$chmodprog
79a966c04fSmrgchowncmd=
802e2dd055Smrgmvcmd=$mvprog
81a966c04fSmrgrmcmd="$rmprog -f"
822e2dd055Smrgstripcmd=
832e2dd055Smrg
84a966c04fSmrgsrc=
85a966c04fSmrgdst=
86a966c04fSmrgdir_arg=
872e2dd055Smrgdst_arg=
882e2dd055Smrg
892e2dd055Smrgcopy_on_change=false
90edce3322Smrgis_target_a_directory=possibly
91a966c04fSmrg
922e2dd055Smrgusage="\
932e2dd055SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
94a966c04fSmrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
95a966c04fSmrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
96a966c04fSmrg   or: $0 [OPTION]... -d DIRECTORIES...
97a966c04fSmrg
98a966c04fSmrgIn the 1st form, copy SRCFILE to DSTFILE.
99a966c04fSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
100a966c04fSmrgIn the 4th, create DIRECTORIES.
101a966c04fSmrg
102a966c04fSmrgOptions:
1032e2dd055Smrg     --help     display this help and exit.
1042e2dd055Smrg     --version  display version info and exit.
1052e2dd055Smrg
1062e2dd055Smrg  -c            (ignored)
10774835918Smrg  -C            install only if different (preserve data modification time)
1082e2dd055Smrg  -d            create directories instead of installing files.
1092e2dd055Smrg  -g GROUP      $chgrpprog installed files to GROUP.
1102e2dd055Smrg  -m MODE       $chmodprog installed files to MODE.
1112e2dd055Smrg  -o USER       $chownprog installed files to USER.
11274835918Smrg  -p            pass -p to $cpprog.
1132e2dd055Smrg  -s            $stripprog installed files.
11474835918Smrg  -S SUFFIX     attempt to back up existing files, with suffix SUFFIX.
1152e2dd055Smrg  -t DIRECTORY  install into DIRECTORY.
1162e2dd055Smrg  -T            report an error if DSTFILE is a directory.
117a966c04fSmrg
118a966c04fSmrgEnvironment variables override the default commands:
1192e2dd055Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
1202e2dd055Smrg  RMPROG STRIPPROG
12174835918Smrg
12274835918SmrgBy default, rm is invoked with -f; when overridden with RMPROG,
12374835918Smrgit's up to you to specify -f if you want it.
12474835918Smrg
12574835918SmrgIf -S is not specified, no backups are attempted.
12674835918Smrg
12774835918SmrgEmail bug reports to bug-automake@gnu.org.
12874835918SmrgAutomake home page: https://www.gnu.org/software/automake/
129a966c04fSmrg"
130a966c04fSmrg
131a966c04fSmrgwhile test $# -ne 0; do
132a966c04fSmrg  case $1 in
1332e2dd055Smrg    -c) ;;
1342e2dd055Smrg
1352e2dd055Smrg    -C) copy_on_change=true;;
136a966c04fSmrg
1372e2dd055Smrg    -d) dir_arg=true;;
138a966c04fSmrg
139a966c04fSmrg    -g) chgrpcmd="$chgrpprog $2"
140edce3322Smrg        shift;;
141a966c04fSmrg
142a966c04fSmrg    --help) echo "$usage"; exit $?;;
143a966c04fSmrg
144a966c04fSmrg    -m) mode=$2
145edce3322Smrg        case $mode in
146edce3322Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
147edce3322Smrg            echo "$0: invalid mode: $mode" >&2
148edce3322Smrg            exit 1;;
149edce3322Smrg        esac
150edce3322Smrg        shift;;
151a966c04fSmrg
152a966c04fSmrg    -o) chowncmd="$chownprog $2"
153edce3322Smrg        shift;;
154a966c04fSmrg
15574835918Smrg    -p) cpprog="$cpprog -p";;
15674835918Smrg
1572e2dd055Smrg    -s) stripcmd=$stripprog;;
158a966c04fSmrg
15974835918Smrg    -S) backupsuffix="$2"
16074835918Smrg        shift;;
16174835918Smrg
162edce3322Smrg    -t)
163edce3322Smrg        is_target_a_directory=always
164edce3322Smrg        dst_arg=$2
165edce3322Smrg        # Protect names problematic for 'test' and other utilities.
166edce3322Smrg        case $dst_arg in
167edce3322Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
168edce3322Smrg        esac
169edce3322Smrg        shift;;
170a966c04fSmrg
171edce3322Smrg    -T) is_target_a_directory=never;;
172a966c04fSmrg
173a966c04fSmrg    --version) echo "$0 $scriptversion"; exit $?;;
174a966c04fSmrg
175edce3322Smrg    --) shift
176edce3322Smrg        break;;
177a966c04fSmrg
178edce3322Smrg    -*) echo "$0: invalid option: $1" >&2
179edce3322Smrg        exit 1;;
180a966c04fSmrg
181a966c04fSmrg    *)  break;;
182a966c04fSmrg  esac
1832e2dd055Smrg  shift
184a966c04fSmrgdone
185a966c04fSmrg
186edce3322Smrg# We allow the use of options -d and -T together, by making -d
187edce3322Smrg# take the precedence; this is for compatibility with GNU install.
188edce3322Smrg
189edce3322Smrgif test -n "$dir_arg"; then
190edce3322Smrg  if test -n "$dst_arg"; then
191edce3322Smrg    echo "$0: target directory not allowed when installing a directory." >&2
192edce3322Smrg    exit 1
193edce3322Smrg  fi
194edce3322Smrgfi
195edce3322Smrg
1962e2dd055Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
197a966c04fSmrg  # When -d is used, all remaining arguments are directories to create.
198a966c04fSmrg  # When -t is used, the destination is already specified.
199a966c04fSmrg  # Otherwise, the last argument is the destination.  Remove it from $@.
200a966c04fSmrg  for arg
201a966c04fSmrg  do
2022e2dd055Smrg    if test -n "$dst_arg"; then
203a966c04fSmrg      # $@ is not empty: it contains at least $arg.
2042e2dd055Smrg      set fnord "$@" "$dst_arg"
205a966c04fSmrg      shift # fnord
206a966c04fSmrg    fi
207a966c04fSmrg    shift # arg
2082e2dd055Smrg    dst_arg=$arg
209ac92798bSmrg    # Protect names problematic for 'test' and other utilities.
210ac92798bSmrg    case $dst_arg in
211ac92798bSmrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
212ac92798bSmrg    esac
213a966c04fSmrg  done
214a966c04fSmrgfi
215a966c04fSmrg
216a966c04fSmrgif test $# -eq 0; then
217a966c04fSmrg  if test -z "$dir_arg"; then
218a966c04fSmrg    echo "$0: no input file specified." >&2
219a966c04fSmrg    exit 1
220a966c04fSmrg  fi
221ac92798bSmrg  # It's OK to call 'install-sh -d' without argument.
222a966c04fSmrg  # This can happen when creating conditional directories.
223a966c04fSmrg  exit 0
224a966c04fSmrgfi
225a966c04fSmrg
226edce3322Smrgif test -z "$dir_arg"; then
227edce3322Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
228edce3322Smrg    if test ! -d "$dst_arg"; then
229edce3322Smrg      echo "$0: $dst_arg: Is not a directory." >&2
230edce3322Smrg      exit 1
231edce3322Smrg    fi
232edce3322Smrg  fi
233edce3322Smrgfi
234edce3322Smrg
235a966c04fSmrgif test -z "$dir_arg"; then
236ac92798bSmrg  do_exit='(exit $ret); exit $ret'
237ac92798bSmrg  trap "ret=129; $do_exit" 1
238ac92798bSmrg  trap "ret=130; $do_exit" 2
239ac92798bSmrg  trap "ret=141; $do_exit" 13
240ac92798bSmrg  trap "ret=143; $do_exit" 15
241a966c04fSmrg
242a966c04fSmrg  # Set umask so as not to create temps with too-generous modes.
243a966c04fSmrg  # However, 'strip' requires both read and write access to temps.
244a966c04fSmrg  case $mode in
245a966c04fSmrg    # Optimize common cases.
246a966c04fSmrg    *644) cp_umask=133;;
247a966c04fSmrg    *755) cp_umask=22;;
248a966c04fSmrg
249a966c04fSmrg    *[0-7])
250a966c04fSmrg      if test -z "$stripcmd"; then
251edce3322Smrg        u_plus_rw=
252a966c04fSmrg      else
253edce3322Smrg        u_plus_rw='% 200'
254a966c04fSmrg      fi
255a966c04fSmrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
256a966c04fSmrg    *)
257a966c04fSmrg      if test -z "$stripcmd"; then
258edce3322Smrg        u_plus_rw=
259a966c04fSmrg      else
260edce3322Smrg        u_plus_rw=,u+rw
261a966c04fSmrg      fi
262a966c04fSmrg      cp_umask=$mode$u_plus_rw;;
263a966c04fSmrg  esac
264a966c04fSmrgfi
265a966c04fSmrg
266a966c04fSmrgfor src
267a966c04fSmrgdo
268ac92798bSmrg  # Protect names problematic for 'test' and other utilities.
269a966c04fSmrg  case $src in
270ac92798bSmrg    -* | [=\(\)!]) src=./$src;;
271a966c04fSmrg  esac
272a966c04fSmrg
273a966c04fSmrg  if test -n "$dir_arg"; then
274a966c04fSmrg    dst=$src
275a966c04fSmrg    dstdir=$dst
276a966c04fSmrg    test -d "$dstdir"
277a966c04fSmrg    dstdir_status=$?
27874835918Smrg    # Don't chown directories that already exist.
27974835918Smrg    if test $dstdir_status = 0; then
28074835918Smrg      chowncmd=""
28174835918Smrg    fi
282a966c04fSmrg  else
283a966c04fSmrg
284a966c04fSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
285a966c04fSmrg    # might cause directories to be created, which would be especially bad
286a966c04fSmrg    # if $src (and thus $dsttmp) contains '*'.
287a966c04fSmrg    if test ! -f "$src" && test ! -d "$src"; then
288a966c04fSmrg      echo "$0: $src does not exist." >&2
289a966c04fSmrg      exit 1
290a966c04fSmrg    fi
291a966c04fSmrg
2922e2dd055Smrg    if test -z "$dst_arg"; then
293a966c04fSmrg      echo "$0: no destination specified." >&2
294a966c04fSmrg      exit 1
295a966c04fSmrg    fi
2962e2dd055Smrg    dst=$dst_arg
297a966c04fSmrg
29874835918Smrg    # If destination is a directory, append the input filename.
299a966c04fSmrg    if test -d "$dst"; then
300edce3322Smrg      if test "$is_target_a_directory" = never; then
301edce3322Smrg        echo "$0: $dst_arg: Is a directory" >&2
302edce3322Smrg        exit 1
303a966c04fSmrg      fi
304a966c04fSmrg      dstdir=$dst
30574835918Smrg      dstbase=`basename "$src"`
30674835918Smrg      case $dst in
30774835918Smrg	*/) dst=$dst$dstbase;;
30874835918Smrg	*)  dst=$dst/$dstbase;;
30974835918Smrg      esac
310a966c04fSmrg      dstdir_status=0
311a966c04fSmrg    else
312edce3322Smrg      dstdir=`dirname "$dst"`
313a966c04fSmrg      test -d "$dstdir"
314a966c04fSmrg      dstdir_status=$?
315a966c04fSmrg    fi
316a966c04fSmrg  fi
317a966c04fSmrg
31874835918Smrg  case $dstdir in
31974835918Smrg    */) dstdirslash=$dstdir;;
32074835918Smrg    *)  dstdirslash=$dstdir/;;
32174835918Smrg  esac
32274835918Smrg
323a966c04fSmrg  obsolete_mkdir_used=false
324a966c04fSmrg
325a966c04fSmrg  if test $dstdir_status != 0; then
326a966c04fSmrg    case $posix_mkdir in
327a966c04fSmrg      '')
328edce3322Smrg        # With -d, create the new directory with the user-specified mode.
329edce3322Smrg        # Otherwise, rely on $mkdir_umask.
330edce3322Smrg        if test -n "$dir_arg"; then
331edce3322Smrg          mkdir_mode=-m$mode
332edce3322Smrg        else
333edce3322Smrg          mkdir_mode=
334edce3322Smrg        fi
335edce3322Smrg
336edce3322Smrg        posix_mkdir=false
33774835918Smrg	# The $RANDOM variable is not portable (e.g., dash).  Use it
33874835918Smrg	# here however when possible just to lower collision chance.
33974835918Smrg	tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
34074835918Smrg
34174835918Smrg	trap '
34274835918Smrg	  ret=$?
34374835918Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null
34474835918Smrg	  exit $ret
34574835918Smrg	' 0
34674835918Smrg
34774835918Smrg	# Because "mkdir -p" follows existing symlinks and we likely work
34874835918Smrg	# directly in world-writeable /tmp, make sure that the '$tmpdir'
34974835918Smrg	# directory is successfully created first before we actually test
35074835918Smrg	# 'mkdir -p'.
35174835918Smrg	if (umask $mkdir_umask &&
35274835918Smrg	    $mkdirprog $mkdir_mode "$tmpdir" &&
35374835918Smrg	    exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
35474835918Smrg	then
35574835918Smrg	  if test -z "$dir_arg" || {
35674835918Smrg	       # Check for POSIX incompatibilities with -m.
35774835918Smrg	       # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
35874835918Smrg	       # other-writable bit of parent directory when it shouldn't.
35974835918Smrg	       # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
36074835918Smrg	       test_tmpdir="$tmpdir/a"
36174835918Smrg	       ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
36274835918Smrg	       case $ls_ld_tmpdir in
36374835918Smrg		 d????-?r-*) different_mode=700;;
36474835918Smrg		 d????-?--*) different_mode=755;;
36574835918Smrg		 *) false;;
36674835918Smrg	       esac &&
36774835918Smrg	       $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
36874835918Smrg		 ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
36974835918Smrg		 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
37074835918Smrg	       }
37174835918Smrg	     }
37274835918Smrg	  then posix_mkdir=:
37374835918Smrg	  fi
37474835918Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
37574835918Smrg	else
37674835918Smrg	  # Remove any dirs left behind by ancient mkdir implementations.
37774835918Smrg	  rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
37874835918Smrg	fi
37974835918Smrg	trap '' 0;;
380a966c04fSmrg    esac
381a966c04fSmrg
382a966c04fSmrg    if
383a966c04fSmrg      $posix_mkdir && (
384edce3322Smrg        umask $mkdir_umask &&
385edce3322Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
386a966c04fSmrg      )
387a966c04fSmrg    then :
388a966c04fSmrg    else
389a966c04fSmrg
39074835918Smrg      # mkdir does not conform to POSIX,
391a966c04fSmrg      # or it failed possibly due to a race condition.  Create the
392a966c04fSmrg      # directory the slow way, step by step, checking for races as we go.
393a966c04fSmrg
394a966c04fSmrg      case $dstdir in
395edce3322Smrg        /*) prefix='/';;
396edce3322Smrg        [-=\(\)!]*) prefix='./';;
397edce3322Smrg        *)  prefix='';;
398a966c04fSmrg      esac
399a966c04fSmrg
400a966c04fSmrg      oIFS=$IFS
401a966c04fSmrg      IFS=/
402edce3322Smrg      set -f
403a966c04fSmrg      set fnord $dstdir
404a966c04fSmrg      shift
405edce3322Smrg      set +f
406a966c04fSmrg      IFS=$oIFS
407a966c04fSmrg
408a966c04fSmrg      prefixes=
409a966c04fSmrg
410a966c04fSmrg      for d
411a966c04fSmrg      do
412edce3322Smrg        test X"$d" = X && continue
413edce3322Smrg
414edce3322Smrg        prefix=$prefix$d
415edce3322Smrg        if test -d "$prefix"; then
416edce3322Smrg          prefixes=
417edce3322Smrg        else
418edce3322Smrg          if $posix_mkdir; then
41974835918Smrg            (umask $mkdir_umask &&
420edce3322Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
421edce3322Smrg            # Don't fail if two instances are running concurrently.
422edce3322Smrg            test -d "$prefix" || exit 1
423edce3322Smrg          else
424edce3322Smrg            case $prefix in
425edce3322Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
426edce3322Smrg              *) qprefix=$prefix;;
427edce3322Smrg            esac
428edce3322Smrg            prefixes="$prefixes '$qprefix'"
429edce3322Smrg          fi
430edce3322Smrg        fi
431edce3322Smrg        prefix=$prefix/
432a966c04fSmrg      done
433a966c04fSmrg
434a966c04fSmrg      if test -n "$prefixes"; then
435edce3322Smrg        # Don't fail if two instances are running concurrently.
436edce3322Smrg        (umask $mkdir_umask &&
437edce3322Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
438edce3322Smrg          test -d "$dstdir" || exit 1
439edce3322Smrg        obsolete_mkdir_used=true
440a966c04fSmrg      fi
441a966c04fSmrg    fi
442a966c04fSmrg  fi
443a966c04fSmrg
444a966c04fSmrg  if test -n "$dir_arg"; then
445a966c04fSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
446a966c04fSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
447a966c04fSmrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
448a966c04fSmrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
449a966c04fSmrg  else
450a966c04fSmrg
451a966c04fSmrg    # Make a couple of temp file names in the proper directory.
45274835918Smrg    dsttmp=${dstdirslash}_inst.$$_
45374835918Smrg    rmtmp=${dstdirslash}_rm.$$_
454a966c04fSmrg
455a966c04fSmrg    # Trap to clean up those temp files at exit.
456a966c04fSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
457a966c04fSmrg
458a966c04fSmrg    # Copy the file name to the temp name.
45974835918Smrg    (umask $cp_umask &&
46074835918Smrg     { test -z "$stripcmd" || {
46174835918Smrg	 # Create $dsttmp read-write so that cp doesn't create it read-only,
46274835918Smrg	 # which would cause strip to fail.
46374835918Smrg	 if test -z "$doit"; then
46474835918Smrg	   : >"$dsttmp" # No need to fork-exec 'touch'.
46574835918Smrg	 else
46674835918Smrg	   $doit touch "$dsttmp"
46774835918Smrg	 fi
46874835918Smrg       }
46974835918Smrg     } &&
47074835918Smrg     $doit_exec $cpprog "$src" "$dsttmp") &&
471a966c04fSmrg
472a966c04fSmrg    # and set any options; do chmod last to preserve setuid bits.
473a966c04fSmrg    #
474a966c04fSmrg    # If any of these fail, we abort the whole thing.  If we want to
475a966c04fSmrg    # ignore errors from any of these, just make sure not to ignore
476a966c04fSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
477a966c04fSmrg    #
4782e2dd055Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
4792e2dd055Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
4802e2dd055Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
4812e2dd055Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4822e2dd055Smrg
4832e2dd055Smrg    # If -C, don't bother to copy if it wouldn't change the file.
4842e2dd055Smrg    if $copy_on_change &&
485edce3322Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
486edce3322Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
487edce3322Smrg       set -f &&
4882e2dd055Smrg       set X $old && old=:$2:$4:$5:$6 &&
4892e2dd055Smrg       set X $new && new=:$2:$4:$5:$6 &&
490edce3322Smrg       set +f &&
4912e2dd055Smrg       test "$old" = "$new" &&
4922e2dd055Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
4932e2dd055Smrg    then
4942e2dd055Smrg      rm -f "$dsttmp"
4952e2dd055Smrg    else
49674835918Smrg      # If $backupsuffix is set, and the file being installed
49774835918Smrg      # already exists, attempt a backup.  Don't worry if it fails,
49874835918Smrg      # e.g., if mv doesn't support -f.
49974835918Smrg      if test -n "$backupsuffix" && test -f "$dst"; then
50074835918Smrg        $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null
50174835918Smrg      fi
50274835918Smrg
5032e2dd055Smrg      # Rename the file to the real destination.
5042e2dd055Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
5052e2dd055Smrg
5062e2dd055Smrg      # The rename failed, perhaps because mv can't rename something else
5072e2dd055Smrg      # to itself, or perhaps because mv is so ancient that it does not
5082e2dd055Smrg      # support -f.
5092e2dd055Smrg      {
510edce3322Smrg        # Now remove or move aside any old file at destination location.
511edce3322Smrg        # We try this two ways since rm can't unlink itself on some
512edce3322Smrg        # systems and the destination file might be busy for other
513edce3322Smrg        # reasons.  In this case, the final cleanup might fail but the new
514edce3322Smrg        # file should still install successfully.
515edce3322Smrg        {
516edce3322Smrg          test ! -f "$dst" ||
51774835918Smrg          $doit $rmcmd "$dst" 2>/dev/null ||
518edce3322Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
51974835918Smrg            { $doit $rmcmd "$rmtmp" 2>/dev/null; :; }
520edce3322Smrg          } ||
521edce3322Smrg          { echo "$0: cannot unlink or rename $dst" >&2
522edce3322Smrg            (exit 1); exit 1
523edce3322Smrg          }
524edce3322Smrg        } &&
525edce3322Smrg
526edce3322Smrg        # Now rename the file to the real destination.
527edce3322Smrg        $doit $mvcmd "$dsttmp" "$dst"
5282e2dd055Smrg      }
5292e2dd055Smrg    fi || exit 1
530a966c04fSmrg
531a966c04fSmrg    trap '' 0
532a966c04fSmrg  fi
533a966c04fSmrgdone
534a966c04fSmrg
535a966c04fSmrg# Local variables:
53674835918Smrg# eval: (add-hook 'before-save-hook 'time-stamp)
537a966c04fSmrg# time-stamp-start: "scriptversion="
538a966c04fSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
53974835918Smrg# time-stamp-time-zone: "UTC0"
5402e2dd055Smrg# time-stamp-end: "; # UTC"
541a966c04fSmrg# End:
542