109885543Smrg#!/bin/sh
209885543Smrg# install - install a program, script, or datafile
309885543Smrg
4beef1b22Smrgscriptversion=2020-11-14.01; # UTC
509885543Smrg
609885543Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
709885543Smrg# later released in X11R6 (xc/config/util/install.sh) with the
809885543Smrg# following copyright and license.
909885543Smrg#
1009885543Smrg# Copyright (C) 1994 X Consortium
1109885543Smrg#
1209885543Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
1309885543Smrg# of this software and associated documentation files (the "Software"), to
1409885543Smrg# deal in the Software without restriction, including without limitation the
1509885543Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
1609885543Smrg# sell copies of the Software, and to permit persons to whom the Software is
1709885543Smrg# furnished to do so, subject to the following conditions:
1809885543Smrg#
1909885543Smrg# The above copyright notice and this permission notice shall be included in
2009885543Smrg# all copies or substantial portions of the Software.
2109885543Smrg#
2209885543Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2309885543Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2409885543Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
2509885543Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
2609885543Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
2709885543Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2809885543Smrg#
2909885543Smrg# Except as contained in this notice, the name of the X Consortium shall not
3009885543Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
3109885543Smrg# ings in this Software without prior written authorization from the X Consor-
3209885543Smrg# tium.
3309885543Smrg#
3409885543Smrg#
3509885543Smrg# FSF changes to this file are in the public domain.
3609885543Smrg#
3709885543Smrg# Calling this script install-sh is preferred over install.sh, to prevent
38df9ffc95Smrg# 'make' implicit rules from creating a file called install from it
3909885543Smrg# when there is no Makefile.
4009885543Smrg#
4109885543Smrg# This script is compatible with the BSD install script, but was written
427104f784Smrg# from scratch.
437104f784Smrg
44f395c03eSmrgtab='	'
457104f784Smrgnl='
467104f784Smrg'
47f395c03eSmrgIFS=" $tab$nl"
4809885543Smrg
49f395c03eSmrg# Set DOITPROG to "echo" to test this script.
5009885543Smrg
517104f784Smrgdoit=${DOITPROG-}
52f395c03eSmrgdoit_exec=${doit:-exec}
5309885543Smrg
547104f784Smrg# Put in absolute file names if you don't have them in your path;
557104f784Smrg# or use environment vars.
567104f784Smrg
577104f784Smrgchgrpprog=${CHGRPPROG-chgrp}
587104f784Smrgchmodprog=${CHMODPROG-chmod}
597104f784Smrgchownprog=${CHOWNPROG-chown}
607104f784Smrgcmpprog=${CMPPROG-cmp}
617104f784Smrgcpprog=${CPPROG-cp}
627104f784Smrgmkdirprog=${MKDIRPROG-mkdir}
637104f784Smrgmvprog=${MVPROG-mv}
647104f784Smrgrmprog=${RMPROG-rm}
657104f784Smrgstripprog=${STRIPPROG-strip}
667104f784Smrg
677104f784Smrgposix_mkdir=
687104f784Smrg
697104f784Smrg# Desired mode of installed file.
707104f784Smrgmode=0755
7109885543Smrg
72beef1b22Smrg# Create dirs (including intermediate dirs) using mode 755.
73beef1b22Smrg# This is like GNU 'install' as of coreutils 8.32 (2020).
74beef1b22Smrgmkdir_umask=22
75beef1b22Smrg
76beef1b22Smrgbackupsuffix=
7709885543Smrgchgrpcmd=
787104f784Smrgchmodcmd=$chmodprog
797104f784Smrgchowncmd=
807104f784Smrgmvcmd=$mvprog
8109885543Smrgrmcmd="$rmprog -f"
827104f784Smrgstripcmd=
837104f784Smrg
8409885543Smrgsrc=
8509885543Smrgdst=
8609885543Smrgdir_arg=
877104f784Smrgdst_arg=
887104f784Smrg
897104f784Smrgcopy_on_change=false
90f395c03eSmrgis_target_a_directory=possibly
9109885543Smrg
927104f784Smrgusage="\
937104f784SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
9409885543Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
9509885543Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
9609885543Smrg   or: $0 [OPTION]... -d DIRECTORIES...
9709885543Smrg
9809885543SmrgIn the 1st form, copy SRCFILE to DSTFILE.
9909885543SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
10009885543SmrgIn the 4th, create DIRECTORIES.
10109885543Smrg
10209885543SmrgOptions:
1037104f784Smrg     --help     display this help and exit.
1047104f784Smrg     --version  display version info and exit.
1057104f784Smrg
1067104f784Smrg  -c            (ignored)
107beef1b22Smrg  -C            install only if different (preserve data modification time)
1087104f784Smrg  -d            create directories instead of installing files.
1097104f784Smrg  -g GROUP      $chgrpprog installed files to GROUP.
1107104f784Smrg  -m MODE       $chmodprog installed files to MODE.
1117104f784Smrg  -o USER       $chownprog installed files to USER.
112beef1b22Smrg  -p            pass -p to $cpprog.
1137104f784Smrg  -s            $stripprog installed files.
114beef1b22Smrg  -S SUFFIX     attempt to back up existing files, with suffix SUFFIX.
1157104f784Smrg  -t DIRECTORY  install into DIRECTORY.
1167104f784Smrg  -T            report an error if DSTFILE is a directory.
11709885543Smrg
11809885543SmrgEnvironment variables override the default commands:
1197104f784Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
1207104f784Smrg  RMPROG STRIPPROG
121beef1b22Smrg
122beef1b22SmrgBy default, rm is invoked with -f; when overridden with RMPROG,
123beef1b22Smrgit's up to you to specify -f if you want it.
124beef1b22Smrg
125beef1b22SmrgIf -S is not specified, no backups are attempted.
126beef1b22Smrg
127beef1b22SmrgEmail bug reports to bug-automake@gnu.org.
128beef1b22SmrgAutomake home page: https://www.gnu.org/software/automake/
12909885543Smrg"
13009885543Smrg
1317104f784Smrgwhile test $# -ne 0; do
13209885543Smrg  case $1 in
1337104f784Smrg    -c) ;;
1347104f784Smrg
1357104f784Smrg    -C) copy_on_change=true;;
13609885543Smrg
1377104f784Smrg    -d) dir_arg=true;;
13809885543Smrg
13909885543Smrg    -g) chgrpcmd="$chgrpprog $2"
140f395c03eSmrg        shift;;
14109885543Smrg
14209885543Smrg    --help) echo "$usage"; exit $?;;
14309885543Smrg
1447104f784Smrg    -m) mode=$2
145f395c03eSmrg        case $mode in
146f395c03eSmrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
147f395c03eSmrg            echo "$0: invalid mode: $mode" >&2
148f395c03eSmrg            exit 1;;
149f395c03eSmrg        esac
150f395c03eSmrg        shift;;
15109885543Smrg
15209885543Smrg    -o) chowncmd="$chownprog $2"
153f395c03eSmrg        shift;;
15409885543Smrg
155beef1b22Smrg    -p) cpprog="$cpprog -p";;
156beef1b22Smrg
1577104f784Smrg    -s) stripcmd=$stripprog;;
15809885543Smrg
159beef1b22Smrg    -S) backupsuffix="$2"
160beef1b22Smrg        shift;;
161beef1b22Smrg
162f395c03eSmrg    -t)
163f395c03eSmrg        is_target_a_directory=always
164f395c03eSmrg        dst_arg=$2
165f395c03eSmrg        # Protect names problematic for 'test' and other utilities.
166f395c03eSmrg        case $dst_arg in
167f395c03eSmrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
168f395c03eSmrg        esac
169f395c03eSmrg        shift;;
17009885543Smrg
171f395c03eSmrg    -T) is_target_a_directory=never;;
17209885543Smrg
17309885543Smrg    --version) echo "$0 $scriptversion"; exit $?;;
17409885543Smrg
175f395c03eSmrg    --) shift
176f395c03eSmrg        break;;
1777104f784Smrg
178f395c03eSmrg    -*) echo "$0: invalid option: $1" >&2
179f395c03eSmrg        exit 1;;
1807104f784Smrg
1817104f784Smrg    *)  break;;
18209885543Smrg  esac
1837104f784Smrg  shift
18409885543Smrgdone
18509885543Smrg
186f395c03eSmrg# We allow the use of options -d and -T together, by making -d
187f395c03eSmrg# take the precedence; this is for compatibility with GNU install.
188f395c03eSmrg
189f395c03eSmrgif test -n "$dir_arg"; then
190f395c03eSmrg  if test -n "$dst_arg"; then
191f395c03eSmrg    echo "$0: target directory not allowed when installing a directory." >&2
192f395c03eSmrg    exit 1
193f395c03eSmrg  fi
194f395c03eSmrgfi
195f395c03eSmrg
1967104f784Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1977104f784Smrg  # When -d is used, all remaining arguments are directories to create.
1987104f784Smrg  # When -t is used, the destination is already specified.
1997104f784Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
2007104f784Smrg  for arg
2017104f784Smrg  do
2027104f784Smrg    if test -n "$dst_arg"; then
2037104f784Smrg      # $@ is not empty: it contains at least $arg.
2047104f784Smrg      set fnord "$@" "$dst_arg"
2057104f784Smrg      shift # fnord
2067104f784Smrg    fi
2077104f784Smrg    shift # arg
2087104f784Smrg    dst_arg=$arg
209df9ffc95Smrg    # Protect names problematic for 'test' and other utilities.
210b12e5c03Smrg    case $dst_arg in
211b12e5c03Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
212b12e5c03Smrg    esac
2137104f784Smrg  done
2147104f784Smrgfi
2157104f784Smrg
2167104f784Smrgif test $# -eq 0; then
21709885543Smrg  if test -z "$dir_arg"; then
21809885543Smrg    echo "$0: no input file specified." >&2
21909885543Smrg    exit 1
22009885543Smrg  fi
221df9ffc95Smrg  # It's OK to call 'install-sh -d' without argument.
22209885543Smrg  # This can happen when creating conditional directories.
22309885543Smrg  exit 0
22409885543Smrgfi
22509885543Smrg
226f395c03eSmrgif test -z "$dir_arg"; then
227f395c03eSmrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
228f395c03eSmrg    if test ! -d "$dst_arg"; then
229f395c03eSmrg      echo "$0: $dst_arg: Is not a directory." >&2
230f395c03eSmrg      exit 1
231f395c03eSmrg    fi
232f395c03eSmrg  fi
233f395c03eSmrgfi
234f395c03eSmrg
2357104f784Smrgif test -z "$dir_arg"; then
236b12e5c03Smrg  do_exit='(exit $ret); exit $ret'
237b12e5c03Smrg  trap "ret=129; $do_exit" 1
238b12e5c03Smrg  trap "ret=130; $do_exit" 2
239b12e5c03Smrg  trap "ret=141; $do_exit" 13
240b12e5c03Smrg  trap "ret=143; $do_exit" 15
2417104f784Smrg
2427104f784Smrg  # Set umask so as not to create temps with too-generous modes.
2437104f784Smrg  # However, 'strip' requires both read and write access to temps.
2447104f784Smrg  case $mode in
2457104f784Smrg    # Optimize common cases.
2467104f784Smrg    *644) cp_umask=133;;
2477104f784Smrg    *755) cp_umask=22;;
2487104f784Smrg
2497104f784Smrg    *[0-7])
2507104f784Smrg      if test -z "$stripcmd"; then
251f395c03eSmrg        u_plus_rw=
2527104f784Smrg      else
253f395c03eSmrg        u_plus_rw='% 200'
2547104f784Smrg      fi
2557104f784Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2567104f784Smrg    *)
2577104f784Smrg      if test -z "$stripcmd"; then
258f395c03eSmrg        u_plus_rw=
2597104f784Smrg      else
260f395c03eSmrg        u_plus_rw=,u+rw
2617104f784Smrg      fi
2627104f784Smrg      cp_umask=$mode$u_plus_rw;;
2637104f784Smrg  esac
2647104f784Smrgfi
2657104f784Smrg
26609885543Smrgfor src
26709885543Smrgdo
268df9ffc95Smrg  # Protect names problematic for 'test' and other utilities.
26909885543Smrg  case $src in
270b12e5c03Smrg    -* | [=\(\)!]) src=./$src;;
27109885543Smrg  esac
27209885543Smrg
27309885543Smrg  if test -n "$dir_arg"; then
27409885543Smrg    dst=$src
2757104f784Smrg    dstdir=$dst
2767104f784Smrg    test -d "$dstdir"
2777104f784Smrg    dstdir_status=$?
278beef1b22Smrg    # Don't chown directories that already exist.
279beef1b22Smrg    if test $dstdir_status = 0; then
280beef1b22Smrg      chowncmd=""
281beef1b22Smrg    fi
28209885543Smrg  else
2837104f784Smrg
28409885543Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
28509885543Smrg    # might cause directories to be created, which would be especially bad
28609885543Smrg    # if $src (and thus $dsttmp) contains '*'.
28709885543Smrg    if test ! -f "$src" && test ! -d "$src"; then
28809885543Smrg      echo "$0: $src does not exist." >&2
28909885543Smrg      exit 1
29009885543Smrg    fi
29109885543Smrg
2927104f784Smrg    if test -z "$dst_arg"; then
29309885543Smrg      echo "$0: no destination specified." >&2
29409885543Smrg      exit 1
29509885543Smrg    fi
2967104f784Smrg    dst=$dst_arg
29709885543Smrg
298beef1b22Smrg    # If destination is a directory, append the input filename.
29909885543Smrg    if test -d "$dst"; then
300f395c03eSmrg      if test "$is_target_a_directory" = never; then
301f395c03eSmrg        echo "$0: $dst_arg: Is a directory" >&2
302f395c03eSmrg        exit 1
30309885543Smrg      fi
3047104f784Smrg      dstdir=$dst
305beef1b22Smrg      dstbase=`basename "$src"`
306beef1b22Smrg      case $dst in
307beef1b22Smrg	*/) dst=$dst$dstbase;;
308beef1b22Smrg	*)  dst=$dst/$dstbase;;
309beef1b22Smrg      esac
3107104f784Smrg      dstdir_status=0
3117104f784Smrg    else
312f395c03eSmrg      dstdir=`dirname "$dst"`
3137104f784Smrg      test -d "$dstdir"
3147104f784Smrg      dstdir_status=$?
31509885543Smrg    fi
31609885543Smrg  fi
31709885543Smrg
318beef1b22Smrg  case $dstdir in
319beef1b22Smrg    */) dstdirslash=$dstdir;;
320beef1b22Smrg    *)  dstdirslash=$dstdir/;;
321beef1b22Smrg  esac
322beef1b22Smrg
3237104f784Smrg  obsolete_mkdir_used=false
3247104f784Smrg
3257104f784Smrg  if test $dstdir_status != 0; then
3267104f784Smrg    case $posix_mkdir in
3277104f784Smrg      '')
328f395c03eSmrg        # With -d, create the new directory with the user-specified mode.
329f395c03eSmrg        # Otherwise, rely on $mkdir_umask.
330f395c03eSmrg        if test -n "$dir_arg"; then
331f395c03eSmrg          mkdir_mode=-m$mode
332f395c03eSmrg        else
333f395c03eSmrg          mkdir_mode=
334f395c03eSmrg        fi
335f395c03eSmrg
336f395c03eSmrg        posix_mkdir=false
337beef1b22Smrg	# The $RANDOM variable is not portable (e.g., dash).  Use it
338beef1b22Smrg	# here however when possible just to lower collision chance.
339beef1b22Smrg	tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
340beef1b22Smrg
341beef1b22Smrg	trap '
342beef1b22Smrg	  ret=$?
343beef1b22Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null
344beef1b22Smrg	  exit $ret
345beef1b22Smrg	' 0
346beef1b22Smrg
347beef1b22Smrg	# Because "mkdir -p" follows existing symlinks and we likely work
348beef1b22Smrg	# directly in world-writeable /tmp, make sure that the '$tmpdir'
349beef1b22Smrg	# directory is successfully created first before we actually test
350beef1b22Smrg	# 'mkdir -p'.
351beef1b22Smrg	if (umask $mkdir_umask &&
352beef1b22Smrg	    $mkdirprog $mkdir_mode "$tmpdir" &&
353beef1b22Smrg	    exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
354beef1b22Smrg	then
355beef1b22Smrg	  if test -z "$dir_arg" || {
356beef1b22Smrg	       # Check for POSIX incompatibilities with -m.
357beef1b22Smrg	       # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
358beef1b22Smrg	       # other-writable bit of parent directory when it shouldn't.
359beef1b22Smrg	       # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
360beef1b22Smrg	       test_tmpdir="$tmpdir/a"
361beef1b22Smrg	       ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
362beef1b22Smrg	       case $ls_ld_tmpdir in
363beef1b22Smrg		 d????-?r-*) different_mode=700;;
364beef1b22Smrg		 d????-?--*) different_mode=755;;
365beef1b22Smrg		 *) false;;
366beef1b22Smrg	       esac &&
367beef1b22Smrg	       $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
368beef1b22Smrg		 ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
369beef1b22Smrg		 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
370beef1b22Smrg	       }
371beef1b22Smrg	     }
372beef1b22Smrg	  then posix_mkdir=:
373beef1b22Smrg	  fi
374beef1b22Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
375beef1b22Smrg	else
376beef1b22Smrg	  # Remove any dirs left behind by ancient mkdir implementations.
377beef1b22Smrg	  rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
378beef1b22Smrg	fi
379beef1b22Smrg	trap '' 0;;
3807104f784Smrg    esac
38109885543Smrg
3827104f784Smrg    if
3837104f784Smrg      $posix_mkdir && (
384f395c03eSmrg        umask $mkdir_umask &&
385f395c03eSmrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3867104f784Smrg      )
3877104f784Smrg    then :
3887104f784Smrg    else
38909885543Smrg
390beef1b22Smrg      # mkdir does not conform to POSIX,
3917104f784Smrg      # or it failed possibly due to a race condition.  Create the
3927104f784Smrg      # directory the slow way, step by step, checking for races as we go.
39309885543Smrg
3947104f784Smrg      case $dstdir in
395f395c03eSmrg        /*) prefix='/';;
396f395c03eSmrg        [-=\(\)!]*) prefix='./';;
397f395c03eSmrg        *)  prefix='';;
3987104f784Smrg      esac
39909885543Smrg
4007104f784Smrg      oIFS=$IFS
4017104f784Smrg      IFS=/
402f395c03eSmrg      set -f
4037104f784Smrg      set fnord $dstdir
40409885543Smrg      shift
405f395c03eSmrg      set +f
4067104f784Smrg      IFS=$oIFS
4077104f784Smrg
4087104f784Smrg      prefixes=
4097104f784Smrg
4107104f784Smrg      for d
4117104f784Smrg      do
412f395c03eSmrg        test X"$d" = X && continue
413f395c03eSmrg
414f395c03eSmrg        prefix=$prefix$d
415f395c03eSmrg        if test -d "$prefix"; then
416f395c03eSmrg          prefixes=
417f395c03eSmrg        else
418f395c03eSmrg          if $posix_mkdir; then
419beef1b22Smrg            (umask $mkdir_umask &&
420f395c03eSmrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
421f395c03eSmrg            # Don't fail if two instances are running concurrently.
422f395c03eSmrg            test -d "$prefix" || exit 1
423f395c03eSmrg          else
424f395c03eSmrg            case $prefix in
425f395c03eSmrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
426f395c03eSmrg              *) qprefix=$prefix;;
427f395c03eSmrg            esac
428f395c03eSmrg            prefixes="$prefixes '$qprefix'"
429f395c03eSmrg          fi
430f395c03eSmrg        fi
431f395c03eSmrg        prefix=$prefix/
4327104f784Smrg      done
4337104f784Smrg
4347104f784Smrg      if test -n "$prefixes"; then
435f395c03eSmrg        # Don't fail if two instances are running concurrently.
436f395c03eSmrg        (umask $mkdir_umask &&
437f395c03eSmrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
438f395c03eSmrg          test -d "$dstdir" || exit 1
439f395c03eSmrg        obsolete_mkdir_used=true
44009885543Smrg      fi
4417104f784Smrg    fi
44209885543Smrg  fi
44309885543Smrg
44409885543Smrg  if test -n "$dir_arg"; then
4457104f784Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4467104f784Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4477104f784Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4487104f784Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
44909885543Smrg  else
45009885543Smrg
45109885543Smrg    # Make a couple of temp file names in the proper directory.
452beef1b22Smrg    dsttmp=${dstdirslash}_inst.$$_
453beef1b22Smrg    rmtmp=${dstdirslash}_rm.$$_
45409885543Smrg
45509885543Smrg    # Trap to clean up those temp files at exit.
45609885543Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
45709885543Smrg
45809885543Smrg    # Copy the file name to the temp name.
459beef1b22Smrg    (umask $cp_umask &&
460beef1b22Smrg     { test -z "$stripcmd" || {
461beef1b22Smrg	 # Create $dsttmp read-write so that cp doesn't create it read-only,
462beef1b22Smrg	 # which would cause strip to fail.
463beef1b22Smrg	 if test -z "$doit"; then
464beef1b22Smrg	   : >"$dsttmp" # No need to fork-exec 'touch'.
465beef1b22Smrg	 else
466beef1b22Smrg	   $doit touch "$dsttmp"
467beef1b22Smrg	 fi
468beef1b22Smrg       }
469beef1b22Smrg     } &&
470beef1b22Smrg     $doit_exec $cpprog "$src" "$dsttmp") &&
47109885543Smrg
47209885543Smrg    # and set any options; do chmod last to preserve setuid bits.
47309885543Smrg    #
47409885543Smrg    # If any of these fail, we abort the whole thing.  If we want to
47509885543Smrg    # ignore errors from any of these, just make sure not to ignore
47609885543Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
47709885543Smrg    #
4787104f784Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
4797104f784Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
4807104f784Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
4817104f784Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4827104f784Smrg
4837104f784Smrg    # If -C, don't bother to copy if it wouldn't change the file.
4847104f784Smrg    if $copy_on_change &&
485f395c03eSmrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
486f395c03eSmrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
487f395c03eSmrg       set -f &&
4887104f784Smrg       set X $old && old=:$2:$4:$5:$6 &&
4897104f784Smrg       set X $new && new=:$2:$4:$5:$6 &&
490f395c03eSmrg       set +f &&
4917104f784Smrg       test "$old" = "$new" &&
4927104f784Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
4937104f784Smrg    then
4947104f784Smrg      rm -f "$dsttmp"
4957104f784Smrg    else
496beef1b22Smrg      # If $backupsuffix is set, and the file being installed
497beef1b22Smrg      # already exists, attempt a backup.  Don't worry if it fails,
498beef1b22Smrg      # e.g., if mv doesn't support -f.
499beef1b22Smrg      if test -n "$backupsuffix" && test -f "$dst"; then
500beef1b22Smrg        $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null
501beef1b22Smrg      fi
502beef1b22Smrg
5037104f784Smrg      # Rename the file to the real destination.
5047104f784Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
5057104f784Smrg
5067104f784Smrg      # The rename failed, perhaps because mv can't rename something else
5077104f784Smrg      # to itself, or perhaps because mv is so ancient that it does not
5087104f784Smrg      # support -f.
5097104f784Smrg      {
510f395c03eSmrg        # Now remove or move aside any old file at destination location.
511f395c03eSmrg        # We try this two ways since rm can't unlink itself on some
512f395c03eSmrg        # systems and the destination file might be busy for other
513f395c03eSmrg        # reasons.  In this case, the final cleanup might fail but the new
514f395c03eSmrg        # file should still install successfully.
515f395c03eSmrg        {
516f395c03eSmrg          test ! -f "$dst" ||
517beef1b22Smrg          $doit $rmcmd "$dst" 2>/dev/null ||
518f395c03eSmrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
519beef1b22Smrg            { $doit $rmcmd "$rmtmp" 2>/dev/null; :; }
520f395c03eSmrg          } ||
521f395c03eSmrg          { echo "$0: cannot unlink or rename $dst" >&2
522f395c03eSmrg            (exit 1); exit 1
523f395c03eSmrg          }
524f395c03eSmrg        } &&
525f395c03eSmrg
526f395c03eSmrg        # Now rename the file to the real destination.
527f395c03eSmrg        $doit $mvcmd "$dsttmp" "$dst"
5287104f784Smrg      }
5297104f784Smrg    fi || exit 1
5307104f784Smrg
5317104f784Smrg    trap '' 0
5327104f784Smrg  fi
53309885543Smrgdone
53409885543Smrg
53509885543Smrg# Local variables:
536beef1b22Smrg# eval: (add-hook 'before-save-hook 'time-stamp)
53709885543Smrg# time-stamp-start: "scriptversion="
53809885543Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
539beef1b22Smrg# time-stamp-time-zone: "UTC0"
540b698ba48Smrg# time-stamp-end: "; # UTC"
54109885543Smrg# End:
542