install-sh revision efb46889
1692f60a7Smrg#!/bin/sh
2692f60a7Smrg# install - install a program, script, or datafile
3692f60a7Smrg
4efb46889Smrgscriptversion=2020-11-14.01; # UTC
5692f60a7Smrg
6692f60a7Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7692f60a7Smrg# later released in X11R6 (xc/config/util/install.sh) with the
8692f60a7Smrg# following copyright and license.
9692f60a7Smrg#
10692f60a7Smrg# Copyright (C) 1994 X Consortium
11692f60a7Smrg#
12692f60a7Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13692f60a7Smrg# of this software and associated documentation files (the "Software"), to
14692f60a7Smrg# deal in the Software without restriction, including without limitation the
15692f60a7Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16692f60a7Smrg# sell copies of the Software, and to permit persons to whom the Software is
17692f60a7Smrg# furnished to do so, subject to the following conditions:
18692f60a7Smrg#
19692f60a7Smrg# The above copyright notice and this permission notice shall be included in
20692f60a7Smrg# all copies or substantial portions of the Software.
21692f60a7Smrg#
22692f60a7Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23692f60a7Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24692f60a7Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25692f60a7Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26692f60a7Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27692f60a7Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28692f60a7Smrg#
29692f60a7Smrg# Except as contained in this notice, the name of the X Consortium shall not
30692f60a7Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
31692f60a7Smrg# ings in this Software without prior written authorization from the X Consor-
32692f60a7Smrg# tium.
33692f60a7Smrg#
34692f60a7Smrg#
35692f60a7Smrg# FSF changes to this file are in the public domain.
36692f60a7Smrg#
37692f60a7Smrg# Calling this script install-sh is preferred over install.sh, to prevent
38eaa3dbe0Smrg# 'make' implicit rules from creating a file called install from it
39692f60a7Smrg# when there is no Makefile.
40692f60a7Smrg#
41692f60a7Smrg# This script is compatible with the BSD install script, but was written
4254569438Smrg# from scratch.
4354569438Smrg
442f60eb46Smrgtab='	'
4554569438Smrgnl='
4654569438Smrg'
472f60eb46SmrgIFS=" $tab$nl"
48692f60a7Smrg
492f60eb46Smrg# Set DOITPROG to "echo" to test this script.
50692f60a7Smrg
5154569438Smrgdoit=${DOITPROG-}
522f60eb46Smrgdoit_exec=${doit:-exec}
53692f60a7Smrg
5454569438Smrg# Put in absolute file names if you don't have them in your path;
5554569438Smrg# or use environment vars.
5654569438Smrg
5754569438Smrgchgrpprog=${CHGRPPROG-chgrp}
5854569438Smrgchmodprog=${CHMODPROG-chmod}
5954569438Smrgchownprog=${CHOWNPROG-chown}
6054569438Smrgcmpprog=${CMPPROG-cmp}
6154569438Smrgcpprog=${CPPROG-cp}
6254569438Smrgmkdirprog=${MKDIRPROG-mkdir}
6354569438Smrgmvprog=${MVPROG-mv}
6454569438Smrgrmprog=${RMPROG-rm}
6554569438Smrgstripprog=${STRIPPROG-strip}
6654569438Smrg
6754569438Smrgposix_mkdir=
6854569438Smrg
6954569438Smrg# Desired mode of installed file.
7054569438Smrgmode=0755
71692f60a7Smrg
72efb46889Smrg# Create dirs (including intermediate dirs) using mode 755.
73efb46889Smrg# This is like GNU 'install' as of coreutils 8.32 (2020).
74efb46889Smrgmkdir_umask=22
75efb46889Smrg
76efb46889Smrgbackupsuffix=
77692f60a7Smrgchgrpcmd=
7854569438Smrgchmodcmd=$chmodprog
7954569438Smrgchowncmd=
8054569438Smrgmvcmd=$mvprog
81692f60a7Smrgrmcmd="$rmprog -f"
8254569438Smrgstripcmd=
8354569438Smrg
84692f60a7Smrgsrc=
85692f60a7Smrgdst=
86692f60a7Smrgdir_arg=
8754569438Smrgdst_arg=
8854569438Smrg
8954569438Smrgcopy_on_change=false
902f60eb46Smrgis_target_a_directory=possibly
91692f60a7Smrg
9254569438Smrgusage="\
9354569438SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
94692f60a7Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
95692f60a7Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
96692f60a7Smrg   or: $0 [OPTION]... -d DIRECTORIES...
97692f60a7Smrg
98692f60a7SmrgIn the 1st form, copy SRCFILE to DSTFILE.
99692f60a7SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
100692f60a7SmrgIn the 4th, create DIRECTORIES.
101692f60a7Smrg
102692f60a7SmrgOptions:
10354569438Smrg     --help     display this help and exit.
10454569438Smrg     --version  display version info and exit.
10554569438Smrg
10654569438Smrg  -c            (ignored)
107efb46889Smrg  -C            install only if different (preserve data modification time)
10854569438Smrg  -d            create directories instead of installing files.
10954569438Smrg  -g GROUP      $chgrpprog installed files to GROUP.
11054569438Smrg  -m MODE       $chmodprog installed files to MODE.
11154569438Smrg  -o USER       $chownprog installed files to USER.
112efb46889Smrg  -p            pass -p to $cpprog.
11354569438Smrg  -s            $stripprog installed files.
114efb46889Smrg  -S SUFFIX     attempt to back up existing files, with suffix SUFFIX.
11554569438Smrg  -t DIRECTORY  install into DIRECTORY.
11654569438Smrg  -T            report an error if DSTFILE is a directory.
117692f60a7Smrg
118692f60a7SmrgEnvironment variables override the default commands:
11954569438Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
12054569438Smrg  RMPROG STRIPPROG
121efb46889Smrg
122efb46889SmrgBy default, rm is invoked with -f; when overridden with RMPROG,
123efb46889Smrgit's up to you to specify -f if you want it.
124efb46889Smrg
125efb46889SmrgIf -S is not specified, no backups are attempted.
126efb46889Smrg
127efb46889SmrgEmail bug reports to bug-automake@gnu.org.
128efb46889SmrgAutomake home page: https://www.gnu.org/software/automake/
129692f60a7Smrg"
130692f60a7Smrg
13154569438Smrgwhile test $# -ne 0; do
132692f60a7Smrg  case $1 in
13354569438Smrg    -c) ;;
13454569438Smrg
13554569438Smrg    -C) copy_on_change=true;;
136692f60a7Smrg
13754569438Smrg    -d) dir_arg=true;;
138692f60a7Smrg
139692f60a7Smrg    -g) chgrpcmd="$chgrpprog $2"
1402f60eb46Smrg        shift;;
141692f60a7Smrg
142692f60a7Smrg    --help) echo "$usage"; exit $?;;
143692f60a7Smrg
14454569438Smrg    -m) mode=$2
1452f60eb46Smrg        case $mode in
1462f60eb46Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
1472f60eb46Smrg            echo "$0: invalid mode: $mode" >&2
1482f60eb46Smrg            exit 1;;
1492f60eb46Smrg        esac
1502f60eb46Smrg        shift;;
151692f60a7Smrg
152692f60a7Smrg    -o) chowncmd="$chownprog $2"
1532f60eb46Smrg        shift;;
154692f60a7Smrg
155efb46889Smrg    -p) cpprog="$cpprog -p";;
156efb46889Smrg
15754569438Smrg    -s) stripcmd=$stripprog;;
158692f60a7Smrg
159efb46889Smrg    -S) backupsuffix="$2"
160efb46889Smrg        shift;;
161efb46889Smrg
1622f60eb46Smrg    -t)
1632f60eb46Smrg        is_target_a_directory=always
1642f60eb46Smrg        dst_arg=$2
1652f60eb46Smrg        # Protect names problematic for 'test' and other utilities.
1662f60eb46Smrg        case $dst_arg in
1672f60eb46Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
1682f60eb46Smrg        esac
1692f60eb46Smrg        shift;;
170692f60a7Smrg
1712f60eb46Smrg    -T) is_target_a_directory=never;;
172692f60a7Smrg
173692f60a7Smrg    --version) echo "$0 $scriptversion"; exit $?;;
174692f60a7Smrg
1752f60eb46Smrg    --) shift
1762f60eb46Smrg        break;;
17754569438Smrg
1782f60eb46Smrg    -*) echo "$0: invalid option: $1" >&2
1792f60eb46Smrg        exit 1;;
18054569438Smrg
18154569438Smrg    *)  break;;
182692f60a7Smrg  esac
18354569438Smrg  shift
184692f60a7Smrgdone
185692f60a7Smrg
1862f60eb46Smrg# We allow the use of options -d and -T together, by making -d
1872f60eb46Smrg# take the precedence; this is for compatibility with GNU install.
1882f60eb46Smrg
1892f60eb46Smrgif test -n "$dir_arg"; then
1902f60eb46Smrg  if test -n "$dst_arg"; then
1912f60eb46Smrg    echo "$0: target directory not allowed when installing a directory." >&2
1922f60eb46Smrg    exit 1
1932f60eb46Smrg  fi
1942f60eb46Smrgfi
1952f60eb46Smrg
19654569438Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
19754569438Smrg  # When -d is used, all remaining arguments are directories to create.
19854569438Smrg  # When -t is used, the destination is already specified.
19954569438Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
20054569438Smrg  for arg
20154569438Smrg  do
20254569438Smrg    if test -n "$dst_arg"; then
20354569438Smrg      # $@ is not empty: it contains at least $arg.
20454569438Smrg      set fnord "$@" "$dst_arg"
20554569438Smrg      shift # fnord
20654569438Smrg    fi
20754569438Smrg    shift # arg
20854569438Smrg    dst_arg=$arg
209eaa3dbe0Smrg    # Protect names problematic for 'test' and other utilities.
2103f6d0e1dSmrg    case $dst_arg in
2113f6d0e1dSmrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
2123f6d0e1dSmrg    esac
21354569438Smrg  done
21454569438Smrgfi
21554569438Smrg
21654569438Smrgif test $# -eq 0; then
217692f60a7Smrg  if test -z "$dir_arg"; then
218692f60a7Smrg    echo "$0: no input file specified." >&2
219692f60a7Smrg    exit 1
220692f60a7Smrg  fi
221eaa3dbe0Smrg  # It's OK to call 'install-sh -d' without argument.
222692f60a7Smrg  # This can happen when creating conditional directories.
223692f60a7Smrg  exit 0
224692f60a7Smrgfi
225692f60a7Smrg
2262f60eb46Smrgif test -z "$dir_arg"; then
2272f60eb46Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
2282f60eb46Smrg    if test ! -d "$dst_arg"; then
2292f60eb46Smrg      echo "$0: $dst_arg: Is not a directory." >&2
2302f60eb46Smrg      exit 1
2312f60eb46Smrg    fi
2322f60eb46Smrg  fi
2332f60eb46Smrgfi
2342f60eb46Smrg
23554569438Smrgif test -z "$dir_arg"; then
2363f6d0e1dSmrg  do_exit='(exit $ret); exit $ret'
2373f6d0e1dSmrg  trap "ret=129; $do_exit" 1
2383f6d0e1dSmrg  trap "ret=130; $do_exit" 2
2393f6d0e1dSmrg  trap "ret=141; $do_exit" 13
2403f6d0e1dSmrg  trap "ret=143; $do_exit" 15
24154569438Smrg
24254569438Smrg  # Set umask so as not to create temps with too-generous modes.
24354569438Smrg  # However, 'strip' requires both read and write access to temps.
24454569438Smrg  case $mode in
24554569438Smrg    # Optimize common cases.
24654569438Smrg    *644) cp_umask=133;;
24754569438Smrg    *755) cp_umask=22;;
24854569438Smrg
24954569438Smrg    *[0-7])
25054569438Smrg      if test -z "$stripcmd"; then
2512f60eb46Smrg        u_plus_rw=
25254569438Smrg      else
2532f60eb46Smrg        u_plus_rw='% 200'
25454569438Smrg      fi
25554569438Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
25654569438Smrg    *)
25754569438Smrg      if test -z "$stripcmd"; then
2582f60eb46Smrg        u_plus_rw=
25954569438Smrg      else
2602f60eb46Smrg        u_plus_rw=,u+rw
26154569438Smrg      fi
26254569438Smrg      cp_umask=$mode$u_plus_rw;;
26354569438Smrg  esac
26454569438Smrgfi
26554569438Smrg
266692f60a7Smrgfor src
267692f60a7Smrgdo
268eaa3dbe0Smrg  # Protect names problematic for 'test' and other utilities.
269692f60a7Smrg  case $src in
2703f6d0e1dSmrg    -* | [=\(\)!]) src=./$src;;
271692f60a7Smrg  esac
272692f60a7Smrg
273692f60a7Smrg  if test -n "$dir_arg"; then
274692f60a7Smrg    dst=$src
27554569438Smrg    dstdir=$dst
27654569438Smrg    test -d "$dstdir"
27754569438Smrg    dstdir_status=$?
278efb46889Smrg    # Don't chown directories that already exist.
279efb46889Smrg    if test $dstdir_status = 0; then
280efb46889Smrg      chowncmd=""
281efb46889Smrg    fi
282692f60a7Smrg  else
28354569438Smrg
284692f60a7Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
285692f60a7Smrg    # might cause directories to be created, which would be especially bad
286692f60a7Smrg    # if $src (and thus $dsttmp) contains '*'.
287692f60a7Smrg    if test ! -f "$src" && test ! -d "$src"; then
288692f60a7Smrg      echo "$0: $src does not exist." >&2
289692f60a7Smrg      exit 1
290692f60a7Smrg    fi
291692f60a7Smrg
29254569438Smrg    if test -z "$dst_arg"; then
293692f60a7Smrg      echo "$0: no destination specified." >&2
294692f60a7Smrg      exit 1
295692f60a7Smrg    fi
29654569438Smrg    dst=$dst_arg
297692f60a7Smrg
298efb46889Smrg    # If destination is a directory, append the input filename.
299692f60a7Smrg    if test -d "$dst"; then
3002f60eb46Smrg      if test "$is_target_a_directory" = never; then
3012f60eb46Smrg        echo "$0: $dst_arg: Is a directory" >&2
3022f60eb46Smrg        exit 1
303692f60a7Smrg      fi
30454569438Smrg      dstdir=$dst
305efb46889Smrg      dstbase=`basename "$src"`
306efb46889Smrg      case $dst in
307efb46889Smrg	*/) dst=$dst$dstbase;;
308efb46889Smrg	*)  dst=$dst/$dstbase;;
309efb46889Smrg      esac
31054569438Smrg      dstdir_status=0
31154569438Smrg    else
3122f60eb46Smrg      dstdir=`dirname "$dst"`
31354569438Smrg      test -d "$dstdir"
31454569438Smrg      dstdir_status=$?
315692f60a7Smrg    fi
316692f60a7Smrg  fi
317692f60a7Smrg
318efb46889Smrg  case $dstdir in
319efb46889Smrg    */) dstdirslash=$dstdir;;
320efb46889Smrg    *)  dstdirslash=$dstdir/;;
321efb46889Smrg  esac
322efb46889Smrg
32354569438Smrg  obsolete_mkdir_used=false
32454569438Smrg
32554569438Smrg  if test $dstdir_status != 0; then
32654569438Smrg    case $posix_mkdir in
32754569438Smrg      '')
3282f60eb46Smrg        # With -d, create the new directory with the user-specified mode.
3292f60eb46Smrg        # Otherwise, rely on $mkdir_umask.
3302f60eb46Smrg        if test -n "$dir_arg"; then
3312f60eb46Smrg          mkdir_mode=-m$mode
3322f60eb46Smrg        else
3332f60eb46Smrg          mkdir_mode=
3342f60eb46Smrg        fi
3352f60eb46Smrg
3362f60eb46Smrg        posix_mkdir=false
337efb46889Smrg	# The $RANDOM variable is not portable (e.g., dash).  Use it
338efb46889Smrg	# here however when possible just to lower collision chance.
339efb46889Smrg	tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
340efb46889Smrg
341efb46889Smrg	trap '
342efb46889Smrg	  ret=$?
343efb46889Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null
344efb46889Smrg	  exit $ret
345efb46889Smrg	' 0
346efb46889Smrg
347efb46889Smrg	# Because "mkdir -p" follows existing symlinks and we likely work
348efb46889Smrg	# directly in world-writeable /tmp, make sure that the '$tmpdir'
349efb46889Smrg	# directory is successfully created first before we actually test
350efb46889Smrg	# 'mkdir -p'.
351efb46889Smrg	if (umask $mkdir_umask &&
352efb46889Smrg	    $mkdirprog $mkdir_mode "$tmpdir" &&
353efb46889Smrg	    exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
354efb46889Smrg	then
355efb46889Smrg	  if test -z "$dir_arg" || {
356efb46889Smrg	       # Check for POSIX incompatibilities with -m.
357efb46889Smrg	       # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
358efb46889Smrg	       # other-writable bit of parent directory when it shouldn't.
359efb46889Smrg	       # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
360efb46889Smrg	       test_tmpdir="$tmpdir/a"
361efb46889Smrg	       ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
362efb46889Smrg	       case $ls_ld_tmpdir in
363efb46889Smrg		 d????-?r-*) different_mode=700;;
364efb46889Smrg		 d????-?--*) different_mode=755;;
365efb46889Smrg		 *) false;;
366efb46889Smrg	       esac &&
367efb46889Smrg	       $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
368efb46889Smrg		 ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
369efb46889Smrg		 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
370efb46889Smrg	       }
371efb46889Smrg	     }
372efb46889Smrg	  then posix_mkdir=:
373efb46889Smrg	  fi
374efb46889Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
375efb46889Smrg	else
376efb46889Smrg	  # Remove any dirs left behind by ancient mkdir implementations.
377efb46889Smrg	  rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
378efb46889Smrg	fi
379efb46889Smrg	trap '' 0;;
38054569438Smrg    esac
381692f60a7Smrg
38254569438Smrg    if
38354569438Smrg      $posix_mkdir && (
3842f60eb46Smrg        umask $mkdir_umask &&
3852f60eb46Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
38654569438Smrg      )
38754569438Smrg    then :
38854569438Smrg    else
389692f60a7Smrg
390efb46889Smrg      # mkdir does not conform to POSIX,
39154569438Smrg      # or it failed possibly due to a race condition.  Create the
39254569438Smrg      # directory the slow way, step by step, checking for races as we go.
393692f60a7Smrg
39454569438Smrg      case $dstdir in
3952f60eb46Smrg        /*) prefix='/';;
3962f60eb46Smrg        [-=\(\)!]*) prefix='./';;
3972f60eb46Smrg        *)  prefix='';;
39854569438Smrg      esac
399692f60a7Smrg
40054569438Smrg      oIFS=$IFS
40154569438Smrg      IFS=/
4022f60eb46Smrg      set -f
40354569438Smrg      set fnord $dstdir
404692f60a7Smrg      shift
4052f60eb46Smrg      set +f
40654569438Smrg      IFS=$oIFS
40754569438Smrg
40854569438Smrg      prefixes=
40954569438Smrg
41054569438Smrg      for d
41154569438Smrg      do
4122f60eb46Smrg        test X"$d" = X && continue
4132f60eb46Smrg
4142f60eb46Smrg        prefix=$prefix$d
4152f60eb46Smrg        if test -d "$prefix"; then
4162f60eb46Smrg          prefixes=
4172f60eb46Smrg        else
4182f60eb46Smrg          if $posix_mkdir; then
419efb46889Smrg            (umask $mkdir_umask &&
4202f60eb46Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
4212f60eb46Smrg            # Don't fail if two instances are running concurrently.
4222f60eb46Smrg            test -d "$prefix" || exit 1
4232f60eb46Smrg          else
4242f60eb46Smrg            case $prefix in
4252f60eb46Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
4262f60eb46Smrg              *) qprefix=$prefix;;
4272f60eb46Smrg            esac
4282f60eb46Smrg            prefixes="$prefixes '$qprefix'"
4292f60eb46Smrg          fi
4302f60eb46Smrg        fi
4312f60eb46Smrg        prefix=$prefix/
43254569438Smrg      done
43354569438Smrg
43454569438Smrg      if test -n "$prefixes"; then
4352f60eb46Smrg        # Don't fail if two instances are running concurrently.
4362f60eb46Smrg        (umask $mkdir_umask &&
4372f60eb46Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
4382f60eb46Smrg          test -d "$dstdir" || exit 1
4392f60eb46Smrg        obsolete_mkdir_used=true
440692f60a7Smrg      fi
44154569438Smrg    fi
442692f60a7Smrg  fi
443692f60a7Smrg
444692f60a7Smrg  if test -n "$dir_arg"; then
44554569438Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
44654569438Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
44754569438Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
44854569438Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
449692f60a7Smrg  else
450692f60a7Smrg
451692f60a7Smrg    # Make a couple of temp file names in the proper directory.
452efb46889Smrg    dsttmp=${dstdirslash}_inst.$$_
453efb46889Smrg    rmtmp=${dstdirslash}_rm.$$_
454692f60a7Smrg
455692f60a7Smrg    # Trap to clean up those temp files at exit.
456692f60a7Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
457692f60a7Smrg
458692f60a7Smrg    # Copy the file name to the temp name.
459efb46889Smrg    (umask $cp_umask &&
460efb46889Smrg     { test -z "$stripcmd" || {
461efb46889Smrg	 # Create $dsttmp read-write so that cp doesn't create it read-only,
462efb46889Smrg	 # which would cause strip to fail.
463efb46889Smrg	 if test -z "$doit"; then
464efb46889Smrg	   : >"$dsttmp" # No need to fork-exec 'touch'.
465efb46889Smrg	 else
466efb46889Smrg	   $doit touch "$dsttmp"
467efb46889Smrg	 fi
468efb46889Smrg       }
469efb46889Smrg     } &&
470efb46889Smrg     $doit_exec $cpprog "$src" "$dsttmp") &&
471692f60a7Smrg
472692f60a7Smrg    # and set any options; do chmod last to preserve setuid bits.
473692f60a7Smrg    #
474692f60a7Smrg    # If any of these fail, we abort the whole thing.  If we want to
475692f60a7Smrg    # ignore errors from any of these, just make sure not to ignore
476692f60a7Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
477692f60a7Smrg    #
47854569438Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
47954569438Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
48054569438Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
48154569438Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
48254569438Smrg
48354569438Smrg    # If -C, don't bother to copy if it wouldn't change the file.
48454569438Smrg    if $copy_on_change &&
4852f60eb46Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
4862f60eb46Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
4872f60eb46Smrg       set -f &&
48854569438Smrg       set X $old && old=:$2:$4:$5:$6 &&
48954569438Smrg       set X $new && new=:$2:$4:$5:$6 &&
4902f60eb46Smrg       set +f &&
49154569438Smrg       test "$old" = "$new" &&
49254569438Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
49354569438Smrg    then
49454569438Smrg      rm -f "$dsttmp"
49554569438Smrg    else
496efb46889Smrg      # If $backupsuffix is set, and the file being installed
497efb46889Smrg      # already exists, attempt a backup.  Don't worry if it fails,
498efb46889Smrg      # e.g., if mv doesn't support -f.
499efb46889Smrg      if test -n "$backupsuffix" && test -f "$dst"; then
500efb46889Smrg        $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null
501efb46889Smrg      fi
502efb46889Smrg
50354569438Smrg      # Rename the file to the real destination.
50454569438Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
50554569438Smrg
50654569438Smrg      # The rename failed, perhaps because mv can't rename something else
50754569438Smrg      # to itself, or perhaps because mv is so ancient that it does not
50854569438Smrg      # support -f.
50954569438Smrg      {
5102f60eb46Smrg        # Now remove or move aside any old file at destination location.
5112f60eb46Smrg        # We try this two ways since rm can't unlink itself on some
5122f60eb46Smrg        # systems and the destination file might be busy for other
5132f60eb46Smrg        # reasons.  In this case, the final cleanup might fail but the new
5142f60eb46Smrg        # file should still install successfully.
5152f60eb46Smrg        {
5162f60eb46Smrg          test ! -f "$dst" ||
517efb46889Smrg          $doit $rmcmd "$dst" 2>/dev/null ||
5182f60eb46Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
519efb46889Smrg            { $doit $rmcmd "$rmtmp" 2>/dev/null; :; }
5202f60eb46Smrg          } ||
5212f60eb46Smrg          { echo "$0: cannot unlink or rename $dst" >&2
5222f60eb46Smrg            (exit 1); exit 1
5232f60eb46Smrg          }
5242f60eb46Smrg        } &&
5252f60eb46Smrg
5262f60eb46Smrg        # Now rename the file to the real destination.
5272f60eb46Smrg        $doit $mvcmd "$dsttmp" "$dst"
52854569438Smrg      }
52954569438Smrg    fi || exit 1
53054569438Smrg
53154569438Smrg    trap '' 0
53254569438Smrg  fi
533692f60a7Smrgdone
534692f60a7Smrg
535692f60a7Smrg# Local variables:
536efb46889Smrg# eval: (add-hook 'before-save-hook 'time-stamp)
537692f60a7Smrg# time-stamp-start: "scriptversion="
538692f60a7Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
539efb46889Smrg# time-stamp-time-zone: "UTC0"
5403f6d0e1dSmrg# time-stamp-end: "; # UTC"
541692f60a7Smrg# End:
542