install-sh revision 09924814
108f3b10eSmrg#!/bin/sh
208f3b10eSmrg# install - install a program, script, or datafile
3516e9f99Smrg
409924814Smrgscriptversion=2020-11-14.01; # UTC
5516e9f99Smrg
608f3b10eSmrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
708f3b10eSmrg# later released in X11R6 (xc/config/util/install.sh) with the
808f3b10eSmrg# following copyright and license.
908f3b10eSmrg#
1008f3b10eSmrg# Copyright (C) 1994 X Consortium
1108f3b10eSmrg#
1208f3b10eSmrg# Permission is hereby granted, free of charge, to any person obtaining a copy
1308f3b10eSmrg# of this software and associated documentation files (the "Software"), to
1408f3b10eSmrg# deal in the Software without restriction, including without limitation the
1508f3b10eSmrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
1608f3b10eSmrg# sell copies of the Software, and to permit persons to whom the Software is
1708f3b10eSmrg# furnished to do so, subject to the following conditions:
1808f3b10eSmrg#
1908f3b10eSmrg# The above copyright notice and this permission notice shall be included in
2008f3b10eSmrg# all copies or substantial portions of the Software.
2108f3b10eSmrg#
2208f3b10eSmrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2308f3b10eSmrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2408f3b10eSmrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
2508f3b10eSmrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
2608f3b10eSmrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
2708f3b10eSmrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2808f3b10eSmrg#
2908f3b10eSmrg# Except as contained in this notice, the name of the X Consortium shall not
3008f3b10eSmrg# be used in advertising or otherwise to promote the sale, use or other deal-
3108f3b10eSmrg# ings in this Software without prior written authorization from the X Consor-
3208f3b10eSmrg# tium.
3308f3b10eSmrg#
3408f3b10eSmrg#
3508f3b10eSmrg# FSF changes to this file are in the public domain.
3608f3b10eSmrg#
3708f3b10eSmrg# Calling this script install-sh is preferred over install.sh, to prevent
3809924814Smrg# 'make' implicit rules from creating a file called install from it
3908f3b10eSmrg# when there is no Makefile.
4008f3b10eSmrg#
4108f3b10eSmrg# This script is compatible with the BSD install script, but was written
42516e9f99Smrg# from scratch.
4308f3b10eSmrg
4409924814Smrgtab='	'
45516e9f99Smrgnl='
46516e9f99Smrg'
4709924814SmrgIFS=" $tab$nl"
4808f3b10eSmrg
4909924814Smrg# Set DOITPROG to "echo" to test this script.
5008f3b10eSmrg
51516e9f99Smrgdoit=${DOITPROG-}
5209924814Smrgdoit_exec=${doit:-exec}
5308f3b10eSmrg
54516e9f99Smrg# Put in absolute file names if you don't have them in your path;
55516e9f99Smrg# or use environment vars.
56516e9f99Smrg
57516e9f99Smrgchgrpprog=${CHGRPPROG-chgrp}
58516e9f99Smrgchmodprog=${CHMODPROG-chmod}
59516e9f99Smrgchownprog=${CHOWNPROG-chown}
60516e9f99Smrgcmpprog=${CMPPROG-cmp}
61516e9f99Smrgcpprog=${CPPROG-cp}
62516e9f99Smrgmkdirprog=${MKDIRPROG-mkdir}
63516e9f99Smrgmvprog=${MVPROG-mv}
64516e9f99Smrgrmprog=${RMPROG-rm}
65516e9f99Smrgstripprog=${STRIPPROG-strip}
66516e9f99Smrg
67516e9f99Smrgposix_mkdir=
68516e9f99Smrg
69516e9f99Smrg# Desired mode of installed file.
70516e9f99Smrgmode=0755
71516e9f99Smrg
7209924814Smrg# Create dirs (including intermediate dirs) using mode 755.
7309924814Smrg# This is like GNU 'install' as of coreutils 8.32 (2020).
7409924814Smrgmkdir_umask=22
7509924814Smrg
7609924814Smrgbackupsuffix=
77516e9f99Smrgchgrpcmd=
78516e9f99Smrgchmodcmd=$chmodprog
79516e9f99Smrgchowncmd=
80516e9f99Smrgmvcmd=$mvprog
81516e9f99Smrgrmcmd="$rmprog -f"
82516e9f99Smrgstripcmd=
8308f3b10eSmrg
84516e9f99Smrgsrc=
85516e9f99Smrgdst=
86516e9f99Smrgdir_arg=
87516e9f99Smrgdst_arg=
8808f3b10eSmrg
89516e9f99Smrgcopy_on_change=false
9009924814Smrgis_target_a_directory=possibly
9108f3b10eSmrg
92516e9f99Smrgusage="\
93516e9f99SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
94516e9f99Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
95516e9f99Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
96516e9f99Smrg   or: $0 [OPTION]... -d DIRECTORIES...
9708f3b10eSmrg
98516e9f99SmrgIn the 1st form, copy SRCFILE to DSTFILE.
99516e9f99SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
100516e9f99SmrgIn the 4th, create DIRECTORIES.
10108f3b10eSmrg
102516e9f99SmrgOptions:
103516e9f99Smrg     --help     display this help and exit.
104516e9f99Smrg     --version  display version info and exit.
10508f3b10eSmrg
106516e9f99Smrg  -c            (ignored)
10709924814Smrg  -C            install only if different (preserve data modification time)
108516e9f99Smrg  -d            create directories instead of installing files.
109516e9f99Smrg  -g GROUP      $chgrpprog installed files to GROUP.
110516e9f99Smrg  -m MODE       $chmodprog installed files to MODE.
111516e9f99Smrg  -o USER       $chownprog installed files to USER.
11209924814Smrg  -p            pass -p to $cpprog.
113516e9f99Smrg  -s            $stripprog installed files.
11409924814Smrg  -S SUFFIX     attempt to back up existing files, with suffix SUFFIX.
115516e9f99Smrg  -t DIRECTORY  install into DIRECTORY.
116516e9f99Smrg  -T            report an error if DSTFILE is a directory.
11708f3b10eSmrg
118516e9f99SmrgEnvironment variables override the default commands:
119516e9f99Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
120516e9f99Smrg  RMPROG STRIPPROG
12109924814Smrg
12209924814SmrgBy default, rm is invoked with -f; when overridden with RMPROG,
12309924814Smrgit's up to you to specify -f if you want it.
12409924814Smrg
12509924814SmrgIf -S is not specified, no backups are attempted.
12609924814Smrg
12709924814SmrgEmail bug reports to bug-automake@gnu.org.
12809924814SmrgAutomake home page: https://www.gnu.org/software/automake/
129516e9f99Smrg"
13008f3b10eSmrg
131516e9f99Smrgwhile test $# -ne 0; do
132516e9f99Smrg  case $1 in
133516e9f99Smrg    -c) ;;
13408f3b10eSmrg
135516e9f99Smrg    -C) copy_on_change=true;;
13608f3b10eSmrg
137516e9f99Smrg    -d) dir_arg=true;;
13808f3b10eSmrg
139516e9f99Smrg    -g) chgrpcmd="$chgrpprog $2"
14009924814Smrg        shift;;
14108f3b10eSmrg
142516e9f99Smrg    --help) echo "$usage"; exit $?;;
14308f3b10eSmrg
144516e9f99Smrg    -m) mode=$2
14509924814Smrg        case $mode in
14609924814Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
14709924814Smrg            echo "$0: invalid mode: $mode" >&2
14809924814Smrg            exit 1;;
14909924814Smrg        esac
15009924814Smrg        shift;;
15108f3b10eSmrg
152516e9f99Smrg    -o) chowncmd="$chownprog $2"
15309924814Smrg        shift;;
15409924814Smrg
15509924814Smrg    -p) cpprog="$cpprog -p";;
15608f3b10eSmrg
157516e9f99Smrg    -s) stripcmd=$stripprog;;
15808f3b10eSmrg
15909924814Smrg    -S) backupsuffix="$2"
16009924814Smrg        shift;;
16109924814Smrg
16209924814Smrg    -t)
16309924814Smrg        is_target_a_directory=always
16409924814Smrg        dst_arg=$2
16509924814Smrg        # Protect names problematic for 'test' and other utilities.
16609924814Smrg        case $dst_arg in
16709924814Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
16809924814Smrg        esac
16909924814Smrg        shift;;
17008f3b10eSmrg
17109924814Smrg    -T) is_target_a_directory=never;;
17208f3b10eSmrg
173516e9f99Smrg    --version) echo "$0 $scriptversion"; exit $?;;
17408f3b10eSmrg
17509924814Smrg    --) shift
17609924814Smrg        break;;
17708f3b10eSmrg
17809924814Smrg    -*) echo "$0: invalid option: $1" >&2
17909924814Smrg        exit 1;;
18008f3b10eSmrg
181516e9f99Smrg    *)  break;;
182516e9f99Smrg  esac
183516e9f99Smrg  shift
184516e9f99Smrgdone
18508f3b10eSmrg
18609924814Smrg# We allow the use of options -d and -T together, by making -d
18709924814Smrg# take the precedence; this is for compatibility with GNU install.
18809924814Smrg
18909924814Smrgif test -n "$dir_arg"; then
19009924814Smrg  if test -n "$dst_arg"; then
19109924814Smrg    echo "$0: target directory not allowed when installing a directory." >&2
19209924814Smrg    exit 1
19309924814Smrg  fi
19409924814Smrgfi
19509924814Smrg
196516e9f99Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
197516e9f99Smrg  # When -d is used, all remaining arguments are directories to create.
198516e9f99Smrg  # When -t is used, the destination is already specified.
199516e9f99Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
200516e9f99Smrg  for arg
201516e9f99Smrg  do
202516e9f99Smrg    if test -n "$dst_arg"; then
203516e9f99Smrg      # $@ is not empty: it contains at least $arg.
204516e9f99Smrg      set fnord "$@" "$dst_arg"
205516e9f99Smrg      shift # fnord
206516e9f99Smrg    fi
207516e9f99Smrg    shift # arg
208516e9f99Smrg    dst_arg=$arg
20909924814Smrg    # Protect names problematic for 'test' and other utilities.
21009924814Smrg    case $dst_arg in
21109924814Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
21209924814Smrg    esac
213516e9f99Smrg  done
214516e9f99Smrgfi
21508f3b10eSmrg
216516e9f99Smrgif test $# -eq 0; then
217516e9f99Smrg  if test -z "$dir_arg"; then
218516e9f99Smrg    echo "$0: no input file specified." >&2
219516e9f99Smrg    exit 1
220516e9f99Smrg  fi
22109924814Smrg  # It's OK to call 'install-sh -d' without argument.
222516e9f99Smrg  # This can happen when creating conditional directories.
223516e9f99Smrg  exit 0
224516e9f99Smrgfi
22508f3b10eSmrg
226516e9f99Smrgif test -z "$dir_arg"; then
22709924814Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
22809924814Smrg    if test ! -d "$dst_arg"; then
22909924814Smrg      echo "$0: $dst_arg: Is not a directory." >&2
23009924814Smrg      exit 1
23109924814Smrg    fi
23209924814Smrg  fi
23309924814Smrgfi
23409924814Smrg
23509924814Smrgif test -z "$dir_arg"; then
23609924814Smrg  do_exit='(exit $ret); exit $ret'
23709924814Smrg  trap "ret=129; $do_exit" 1
23809924814Smrg  trap "ret=130; $do_exit" 2
23909924814Smrg  trap "ret=141; $do_exit" 13
24009924814Smrg  trap "ret=143; $do_exit" 15
241516e9f99Smrg
242516e9f99Smrg  # Set umask so as not to create temps with too-generous modes.
243516e9f99Smrg  # However, 'strip' requires both read and write access to temps.
244516e9f99Smrg  case $mode in
245516e9f99Smrg    # Optimize common cases.
246516e9f99Smrg    *644) cp_umask=133;;
247516e9f99Smrg    *755) cp_umask=22;;
248516e9f99Smrg
249516e9f99Smrg    *[0-7])
250516e9f99Smrg      if test -z "$stripcmd"; then
25109924814Smrg        u_plus_rw=
252516e9f99Smrg      else
25309924814Smrg        u_plus_rw='% 200'
254516e9f99Smrg      fi
255516e9f99Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
256516e9f99Smrg    *)
257516e9f99Smrg      if test -z "$stripcmd"; then
25809924814Smrg        u_plus_rw=
259516e9f99Smrg      else
26009924814Smrg        u_plus_rw=,u+rw
261516e9f99Smrg      fi
262516e9f99Smrg      cp_umask=$mode$u_plus_rw;;
263516e9f99Smrg  esac
264516e9f99Smrgfi
26508f3b10eSmrg
266516e9f99Smrgfor src
267516e9f99Smrgdo
26809924814Smrg  # Protect names problematic for 'test' and other utilities.
269516e9f99Smrg  case $src in
27009924814Smrg    -* | [=\(\)!]) src=./$src;;
271516e9f99Smrg  esac
272516e9f99Smrg
273516e9f99Smrg  if test -n "$dir_arg"; then
274516e9f99Smrg    dst=$src
275516e9f99Smrg    dstdir=$dst
276516e9f99Smrg    test -d "$dstdir"
277516e9f99Smrg    dstdir_status=$?
27809924814Smrg    # Don't chown directories that already exist.
27909924814Smrg    if test $dstdir_status = 0; then
28009924814Smrg      chowncmd=""
28109924814Smrg    fi
282516e9f99Smrg  else
283516e9f99Smrg
284516e9f99Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
285516e9f99Smrg    # might cause directories to be created, which would be especially bad
286516e9f99Smrg    # if $src (and thus $dsttmp) contains '*'.
287516e9f99Smrg    if test ! -f "$src" && test ! -d "$src"; then
288516e9f99Smrg      echo "$0: $src does not exist." >&2
289516e9f99Smrg      exit 1
290516e9f99Smrg    fi
291516e9f99Smrg
292516e9f99Smrg    if test -z "$dst_arg"; then
293516e9f99Smrg      echo "$0: no destination specified." >&2
294516e9f99Smrg      exit 1
295516e9f99Smrg    fi
296516e9f99Smrg    dst=$dst_arg
29708f3b10eSmrg
29809924814Smrg    # If destination is a directory, append the input filename.
299516e9f99Smrg    if test -d "$dst"; then
30009924814Smrg      if test "$is_target_a_directory" = never; then
30109924814Smrg        echo "$0: $dst_arg: Is a directory" >&2
30209924814Smrg        exit 1
303516e9f99Smrg      fi
304516e9f99Smrg      dstdir=$dst
30509924814Smrg      dstbase=`basename "$src"`
30609924814Smrg      case $dst in
30709924814Smrg	*/) dst=$dst$dstbase;;
30809924814Smrg	*)  dst=$dst/$dstbase;;
30909924814Smrg      esac
310516e9f99Smrg      dstdir_status=0
311516e9f99Smrg    else
31209924814Smrg      dstdir=`dirname "$dst"`
313516e9f99Smrg      test -d "$dstdir"
314516e9f99Smrg      dstdir_status=$?
315516e9f99Smrg    fi
316516e9f99Smrg  fi
317516e9f99Smrg
31809924814Smrg  case $dstdir in
31909924814Smrg    */) dstdirslash=$dstdir;;
32009924814Smrg    *)  dstdirslash=$dstdir/;;
32109924814Smrg  esac
32209924814Smrg
323516e9f99Smrg  obsolete_mkdir_used=false
324516e9f99Smrg
325516e9f99Smrg  if test $dstdir_status != 0; then
326516e9f99Smrg    case $posix_mkdir in
327516e9f99Smrg      '')
32809924814Smrg        # With -d, create the new directory with the user-specified mode.
32909924814Smrg        # Otherwise, rely on $mkdir_umask.
33009924814Smrg        if test -n "$dir_arg"; then
33109924814Smrg          mkdir_mode=-m$mode
33209924814Smrg        else
33309924814Smrg          mkdir_mode=
33409924814Smrg        fi
33509924814Smrg
33609924814Smrg        posix_mkdir=false
33709924814Smrg	# The $RANDOM variable is not portable (e.g., dash).  Use it
33809924814Smrg	# here however when possible just to lower collision chance.
33909924814Smrg	tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
34009924814Smrg
34109924814Smrg	trap '
34209924814Smrg	  ret=$?
34309924814Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null
34409924814Smrg	  exit $ret
34509924814Smrg	' 0
34609924814Smrg
34709924814Smrg	# Because "mkdir -p" follows existing symlinks and we likely work
34809924814Smrg	# directly in world-writeable /tmp, make sure that the '$tmpdir'
34909924814Smrg	# directory is successfully created first before we actually test
35009924814Smrg	# 'mkdir -p'.
35109924814Smrg	if (umask $mkdir_umask &&
35209924814Smrg	    $mkdirprog $mkdir_mode "$tmpdir" &&
35309924814Smrg	    exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
35409924814Smrg	then
35509924814Smrg	  if test -z "$dir_arg" || {
35609924814Smrg	       # Check for POSIX incompatibilities with -m.
35709924814Smrg	       # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
35809924814Smrg	       # other-writable bit of parent directory when it shouldn't.
35909924814Smrg	       # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
36009924814Smrg	       test_tmpdir="$tmpdir/a"
36109924814Smrg	       ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
36209924814Smrg	       case $ls_ld_tmpdir in
36309924814Smrg		 d????-?r-*) different_mode=700;;
36409924814Smrg		 d????-?--*) different_mode=755;;
36509924814Smrg		 *) false;;
36609924814Smrg	       esac &&
36709924814Smrg	       $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
36809924814Smrg		 ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
36909924814Smrg		 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
37009924814Smrg	       }
37109924814Smrg	     }
37209924814Smrg	  then posix_mkdir=:
37309924814Smrg	  fi
37409924814Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
37508f3b10eSmrg	else
37609924814Smrg	  # Remove any dirs left behind by ancient mkdir implementations.
37709924814Smrg	  rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
37808f3b10eSmrg	fi
37909924814Smrg	trap '' 0;;
380516e9f99Smrg    esac
38108f3b10eSmrg
382516e9f99Smrg    if
383516e9f99Smrg      $posix_mkdir && (
38409924814Smrg        umask $mkdir_umask &&
38509924814Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
386516e9f99Smrg      )
387516e9f99Smrg    then :
388516e9f99Smrg    else
389516e9f99Smrg
39009924814Smrg      # mkdir does not conform to POSIX,
391516e9f99Smrg      # or it failed possibly due to a race condition.  Create the
392516e9f99Smrg      # directory the slow way, step by step, checking for races as we go.
393516e9f99Smrg
394516e9f99Smrg      case $dstdir in
39509924814Smrg        /*) prefix='/';;
39609924814Smrg        [-=\(\)!]*) prefix='./';;
39709924814Smrg        *)  prefix='';;
398516e9f99Smrg      esac
399516e9f99Smrg
400516e9f99Smrg      oIFS=$IFS
401516e9f99Smrg      IFS=/
40209924814Smrg      set -f
403516e9f99Smrg      set fnord $dstdir
404516e9f99Smrg      shift
40509924814Smrg      set +f
406516e9f99Smrg      IFS=$oIFS
407516e9f99Smrg
408516e9f99Smrg      prefixes=
409516e9f99Smrg
410516e9f99Smrg      for d
411516e9f99Smrg      do
41209924814Smrg        test X"$d" = X && continue
41309924814Smrg
41409924814Smrg        prefix=$prefix$d
41509924814Smrg        if test -d "$prefix"; then
41609924814Smrg          prefixes=
41709924814Smrg        else
41809924814Smrg          if $posix_mkdir; then
41909924814Smrg            (umask $mkdir_umask &&
42009924814Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
42109924814Smrg            # Don't fail if two instances are running concurrently.
42209924814Smrg            test -d "$prefix" || exit 1
42309924814Smrg          else
42409924814Smrg            case $prefix in
42509924814Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
42609924814Smrg              *) qprefix=$prefix;;
42709924814Smrg            esac
42809924814Smrg            prefixes="$prefixes '$qprefix'"
42909924814Smrg          fi
43009924814Smrg        fi
43109924814Smrg        prefix=$prefix/
432516e9f99Smrg      done
433516e9f99Smrg
434516e9f99Smrg      if test -n "$prefixes"; then
43509924814Smrg        # Don't fail if two instances are running concurrently.
43609924814Smrg        (umask $mkdir_umask &&
43709924814Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
43809924814Smrg          test -d "$dstdir" || exit 1
43909924814Smrg        obsolete_mkdir_used=true
440516e9f99Smrg      fi
441516e9f99Smrg    fi
442516e9f99Smrg  fi
443516e9f99Smrg
444516e9f99Smrg  if test -n "$dir_arg"; then
445516e9f99Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
446516e9f99Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
447516e9f99Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
448516e9f99Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
449516e9f99Smrg  else
450516e9f99Smrg
451516e9f99Smrg    # Make a couple of temp file names in the proper directory.
45209924814Smrg    dsttmp=${dstdirslash}_inst.$$_
45309924814Smrg    rmtmp=${dstdirslash}_rm.$$_
454516e9f99Smrg
455516e9f99Smrg    # Trap to clean up those temp files at exit.
456516e9f99Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
457516e9f99Smrg
458516e9f99Smrg    # Copy the file name to the temp name.
45909924814Smrg    (umask $cp_umask &&
46009924814Smrg     { test -z "$stripcmd" || {
46109924814Smrg	 # Create $dsttmp read-write so that cp doesn't create it read-only,
46209924814Smrg	 # which would cause strip to fail.
46309924814Smrg	 if test -z "$doit"; then
46409924814Smrg	   : >"$dsttmp" # No need to fork-exec 'touch'.
46509924814Smrg	 else
46609924814Smrg	   $doit touch "$dsttmp"
46709924814Smrg	 fi
46809924814Smrg       }
46909924814Smrg     } &&
47009924814Smrg     $doit_exec $cpprog "$src" "$dsttmp") &&
471516e9f99Smrg
472516e9f99Smrg    # and set any options; do chmod last to preserve setuid bits.
473516e9f99Smrg    #
474516e9f99Smrg    # If any of these fail, we abort the whole thing.  If we want to
475516e9f99Smrg    # ignore errors from any of these, just make sure not to ignore
476516e9f99Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
477516e9f99Smrg    #
478516e9f99Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
479516e9f99Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
480516e9f99Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
481516e9f99Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
482516e9f99Smrg
483516e9f99Smrg    # If -C, don't bother to copy if it wouldn't change the file.
484516e9f99Smrg    if $copy_on_change &&
48509924814Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
48609924814Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
48709924814Smrg       set -f &&
488516e9f99Smrg       set X $old && old=:$2:$4:$5:$6 &&
489516e9f99Smrg       set X $new && new=:$2:$4:$5:$6 &&
49009924814Smrg       set +f &&
491516e9f99Smrg       test "$old" = "$new" &&
492516e9f99Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
493516e9f99Smrg    then
494516e9f99Smrg      rm -f "$dsttmp"
495516e9f99Smrg    else
49609924814Smrg      # If $backupsuffix is set, and the file being installed
49709924814Smrg      # already exists, attempt a backup.  Don't worry if it fails,
49809924814Smrg      # e.g., if mv doesn't support -f.
49909924814Smrg      if test -n "$backupsuffix" && test -f "$dst"; then
50009924814Smrg        $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null
50109924814Smrg      fi
50209924814Smrg
503516e9f99Smrg      # Rename the file to the real destination.
504516e9f99Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
505516e9f99Smrg
506516e9f99Smrg      # The rename failed, perhaps because mv can't rename something else
507516e9f99Smrg      # to itself, or perhaps because mv is so ancient that it does not
508516e9f99Smrg      # support -f.
509516e9f99Smrg      {
51009924814Smrg        # Now remove or move aside any old file at destination location.
51109924814Smrg        # We try this two ways since rm can't unlink itself on some
51209924814Smrg        # systems and the destination file might be busy for other
51309924814Smrg        # reasons.  In this case, the final cleanup might fail but the new
51409924814Smrg        # file should still install successfully.
51509924814Smrg        {
51609924814Smrg          test ! -f "$dst" ||
51709924814Smrg          $doit $rmcmd "$dst" 2>/dev/null ||
51809924814Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
51909924814Smrg            { $doit $rmcmd "$rmtmp" 2>/dev/null; :; }
52009924814Smrg          } ||
52109924814Smrg          { echo "$0: cannot unlink or rename $dst" >&2
52209924814Smrg            (exit 1); exit 1
52309924814Smrg          }
52409924814Smrg        } &&
52509924814Smrg
52609924814Smrg        # Now rename the file to the real destination.
52709924814Smrg        $doit $mvcmd "$dsttmp" "$dst"
528516e9f99Smrg      }
529516e9f99Smrg    fi || exit 1
530516e9f99Smrg
531516e9f99Smrg    trap '' 0
532516e9f99Smrg  fi
533516e9f99Smrgdone
53408f3b10eSmrg
535516e9f99Smrg# Local variables:
53609924814Smrg# eval: (add-hook 'before-save-hook 'time-stamp)
537516e9f99Smrg# time-stamp-start: "scriptversion="
538516e9f99Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
53909924814Smrg# time-stamp-time-zone: "UTC0"
540516e9f99Smrg# time-stamp-end: "; # UTC"
541516e9f99Smrg# End:
542