10eb10989Smrg#!/bin/sh
20eb10989Smrg# install - install a program, script, or datafile
30eb10989Smrg
474901992Smrgscriptversion=2020-11-14.01; # UTC
50eb10989Smrg
60eb10989Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
70eb10989Smrg# later released in X11R6 (xc/config/util/install.sh) with the
80eb10989Smrg# following copyright and license.
90eb10989Smrg#
100eb10989Smrg# Copyright (C) 1994 X Consortium
110eb10989Smrg#
120eb10989Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
130eb10989Smrg# of this software and associated documentation files (the "Software"), to
140eb10989Smrg# deal in the Software without restriction, including without limitation the
150eb10989Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
160eb10989Smrg# sell copies of the Software, and to permit persons to whom the Software is
170eb10989Smrg# furnished to do so, subject to the following conditions:
180eb10989Smrg#
190eb10989Smrg# The above copyright notice and this permission notice shall be included in
200eb10989Smrg# all copies or substantial portions of the Software.
210eb10989Smrg#
220eb10989Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
230eb10989Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
240eb10989Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
250eb10989Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
260eb10989Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
270eb10989Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
280eb10989Smrg#
290eb10989Smrg# Except as contained in this notice, the name of the X Consortium shall not
300eb10989Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
310eb10989Smrg# ings in this Software without prior written authorization from the X Consor-
320eb10989Smrg# tium.
330eb10989Smrg#
340eb10989Smrg#
350eb10989Smrg# FSF changes to this file are in the public domain.
360eb10989Smrg#
370eb10989Smrg# Calling this script install-sh is preferred over install.sh, to prevent
38079e7944Smrg# 'make' implicit rules from creating a file called install from it
390eb10989Smrg# when there is no Makefile.
400eb10989Smrg#
410eb10989Smrg# This script is compatible with the BSD install script, but was written
4263165362Smrg# from scratch.
4363165362Smrg
44b2f5b1dbSmrgtab='	'
4563165362Smrgnl='
4663165362Smrg'
47b2f5b1dbSmrgIFS=" $tab$nl"
480eb10989Smrg
49b2f5b1dbSmrg# Set DOITPROG to "echo" to test this script.
500eb10989Smrg
5163165362Smrgdoit=${DOITPROG-}
52b2f5b1dbSmrgdoit_exec=${doit:-exec}
530eb10989Smrg
5463165362Smrg# Put in absolute file names if you don't have them in your path;
5563165362Smrg# or use environment vars.
5663165362Smrg
5763165362Smrgchgrpprog=${CHGRPPROG-chgrp}
5863165362Smrgchmodprog=${CHMODPROG-chmod}
5963165362Smrgchownprog=${CHOWNPROG-chown}
6063165362Smrgcmpprog=${CMPPROG-cmp}
6163165362Smrgcpprog=${CPPROG-cp}
6263165362Smrgmkdirprog=${MKDIRPROG-mkdir}
6363165362Smrgmvprog=${MVPROG-mv}
6463165362Smrgrmprog=${RMPROG-rm}
6563165362Smrgstripprog=${STRIPPROG-strip}
6663165362Smrg
6763165362Smrgposix_mkdir=
6863165362Smrg
6963165362Smrg# Desired mode of installed file.
7063165362Smrgmode=0755
710eb10989Smrg
7274901992Smrg# Create dirs (including intermediate dirs) using mode 755.
7374901992Smrg# This is like GNU 'install' as of coreutils 8.32 (2020).
7474901992Smrgmkdir_umask=22
7574901992Smrg
7674901992Smrgbackupsuffix=
770eb10989Smrgchgrpcmd=
7863165362Smrgchmodcmd=$chmodprog
7963165362Smrgchowncmd=
8063165362Smrgmvcmd=$mvprog
810eb10989Smrgrmcmd="$rmprog -f"
8263165362Smrgstripcmd=
8363165362Smrg
840eb10989Smrgsrc=
850eb10989Smrgdst=
860eb10989Smrgdir_arg=
8763165362Smrgdst_arg=
8863165362Smrg
8963165362Smrgcopy_on_change=false
90b2f5b1dbSmrgis_target_a_directory=possibly
910eb10989Smrg
9263165362Smrgusage="\
9363165362SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
940eb10989Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
950eb10989Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
960eb10989Smrg   or: $0 [OPTION]... -d DIRECTORIES...
970eb10989Smrg
980eb10989SmrgIn the 1st form, copy SRCFILE to DSTFILE.
990eb10989SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
1000eb10989SmrgIn the 4th, create DIRECTORIES.
1010eb10989Smrg
1020eb10989SmrgOptions:
10363165362Smrg     --help     display this help and exit.
10463165362Smrg     --version  display version info and exit.
10563165362Smrg
10663165362Smrg  -c            (ignored)
10774901992Smrg  -C            install only if different (preserve data modification time)
10863165362Smrg  -d            create directories instead of installing files.
10963165362Smrg  -g GROUP      $chgrpprog installed files to GROUP.
11063165362Smrg  -m MODE       $chmodprog installed files to MODE.
11163165362Smrg  -o USER       $chownprog installed files to USER.
11274901992Smrg  -p            pass -p to $cpprog.
11363165362Smrg  -s            $stripprog installed files.
11474901992Smrg  -S SUFFIX     attempt to back up existing files, with suffix SUFFIX.
11563165362Smrg  -t DIRECTORY  install into DIRECTORY.
11663165362Smrg  -T            report an error if DSTFILE is a directory.
1170eb10989Smrg
1180eb10989SmrgEnvironment variables override the default commands:
11963165362Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
12063165362Smrg  RMPROG STRIPPROG
12174901992Smrg
12274901992SmrgBy default, rm is invoked with -f; when overridden with RMPROG,
12374901992Smrgit's up to you to specify -f if you want it.
12474901992Smrg
12574901992SmrgIf -S is not specified, no backups are attempted.
12674901992Smrg
12774901992SmrgEmail bug reports to bug-automake@gnu.org.
12874901992SmrgAutomake home page: https://www.gnu.org/software/automake/
1290eb10989Smrg"
1300eb10989Smrg
13163165362Smrgwhile test $# -ne 0; do
1320eb10989Smrg  case $1 in
13363165362Smrg    -c) ;;
13463165362Smrg
13563165362Smrg    -C) copy_on_change=true;;
1360eb10989Smrg
13763165362Smrg    -d) dir_arg=true;;
1380eb10989Smrg
1390eb10989Smrg    -g) chgrpcmd="$chgrpprog $2"
140b2f5b1dbSmrg        shift;;
1410eb10989Smrg
1420eb10989Smrg    --help) echo "$usage"; exit $?;;
1430eb10989Smrg
14463165362Smrg    -m) mode=$2
145b2f5b1dbSmrg        case $mode in
146b2f5b1dbSmrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
147b2f5b1dbSmrg            echo "$0: invalid mode: $mode" >&2
148b2f5b1dbSmrg            exit 1;;
149b2f5b1dbSmrg        esac
150b2f5b1dbSmrg        shift;;
1510eb10989Smrg
1520eb10989Smrg    -o) chowncmd="$chownprog $2"
153b2f5b1dbSmrg        shift;;
1540eb10989Smrg
15574901992Smrg    -p) cpprog="$cpprog -p";;
15674901992Smrg
15763165362Smrg    -s) stripcmd=$stripprog;;
1580eb10989Smrg
15974901992Smrg    -S) backupsuffix="$2"
16074901992Smrg        shift;;
16174901992Smrg
162b2f5b1dbSmrg    -t)
163b2f5b1dbSmrg        is_target_a_directory=always
164b2f5b1dbSmrg        dst_arg=$2
165b2f5b1dbSmrg        # Protect names problematic for 'test' and other utilities.
166b2f5b1dbSmrg        case $dst_arg in
167b2f5b1dbSmrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
168b2f5b1dbSmrg        esac
169b2f5b1dbSmrg        shift;;
1700eb10989Smrg
171b2f5b1dbSmrg    -T) is_target_a_directory=never;;
1720eb10989Smrg
1730eb10989Smrg    --version) echo "$0 $scriptversion"; exit $?;;
1740eb10989Smrg
175b2f5b1dbSmrg    --) shift
176b2f5b1dbSmrg        break;;
17763165362Smrg
178b2f5b1dbSmrg    -*) echo "$0: invalid option: $1" >&2
179b2f5b1dbSmrg        exit 1;;
18063165362Smrg
18163165362Smrg    *)  break;;
1820eb10989Smrg  esac
18363165362Smrg  shift
1840eb10989Smrgdone
1850eb10989Smrg
186b2f5b1dbSmrg# We allow the use of options -d and -T together, by making -d
187b2f5b1dbSmrg# take the precedence; this is for compatibility with GNU install.
188b2f5b1dbSmrg
189b2f5b1dbSmrgif test -n "$dir_arg"; then
190b2f5b1dbSmrg  if test -n "$dst_arg"; then
191b2f5b1dbSmrg    echo "$0: target directory not allowed when installing a directory." >&2
192b2f5b1dbSmrg    exit 1
193b2f5b1dbSmrg  fi
194b2f5b1dbSmrgfi
195b2f5b1dbSmrg
19663165362Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
19763165362Smrg  # When -d is used, all remaining arguments are directories to create.
19863165362Smrg  # When -t is used, the destination is already specified.
19963165362Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
20063165362Smrg  for arg
20163165362Smrg  do
20263165362Smrg    if test -n "$dst_arg"; then
20363165362Smrg      # $@ is not empty: it contains at least $arg.
20463165362Smrg      set fnord "$@" "$dst_arg"
20563165362Smrg      shift # fnord
20663165362Smrg    fi
20763165362Smrg    shift # arg
20863165362Smrg    dst_arg=$arg
209079e7944Smrg    # Protect names problematic for 'test' and other utilities.
210079e7944Smrg    case $dst_arg in
211079e7944Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
212079e7944Smrg    esac
21363165362Smrg  done
21463165362Smrgfi
21563165362Smrg
21663165362Smrgif test $# -eq 0; then
2170eb10989Smrg  if test -z "$dir_arg"; then
2180eb10989Smrg    echo "$0: no input file specified." >&2
2190eb10989Smrg    exit 1
2200eb10989Smrg  fi
221079e7944Smrg  # It's OK to call 'install-sh -d' without argument.
2220eb10989Smrg  # This can happen when creating conditional directories.
2230eb10989Smrg  exit 0
2240eb10989Smrgfi
2250eb10989Smrg
226b2f5b1dbSmrgif test -z "$dir_arg"; then
227b2f5b1dbSmrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
228b2f5b1dbSmrg    if test ! -d "$dst_arg"; then
229b2f5b1dbSmrg      echo "$0: $dst_arg: Is not a directory." >&2
230b2f5b1dbSmrg      exit 1
231b2f5b1dbSmrg    fi
232b2f5b1dbSmrg  fi
233b2f5b1dbSmrgfi
234b2f5b1dbSmrg
23563165362Smrgif test -z "$dir_arg"; then
236079e7944Smrg  do_exit='(exit $ret); exit $ret'
237079e7944Smrg  trap "ret=129; $do_exit" 1
238079e7944Smrg  trap "ret=130; $do_exit" 2
239079e7944Smrg  trap "ret=141; $do_exit" 13
240079e7944Smrg  trap "ret=143; $do_exit" 15
24163165362Smrg
24263165362Smrg  # Set umask so as not to create temps with too-generous modes.
24363165362Smrg  # However, 'strip' requires both read and write access to temps.
24463165362Smrg  case $mode in
24563165362Smrg    # Optimize common cases.
24663165362Smrg    *644) cp_umask=133;;
24763165362Smrg    *755) cp_umask=22;;
24863165362Smrg
24963165362Smrg    *[0-7])
25063165362Smrg      if test -z "$stripcmd"; then
251b2f5b1dbSmrg        u_plus_rw=
25263165362Smrg      else
253b2f5b1dbSmrg        u_plus_rw='% 200'
25463165362Smrg      fi
25563165362Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
25663165362Smrg    *)
25763165362Smrg      if test -z "$stripcmd"; then
258b2f5b1dbSmrg        u_plus_rw=
25963165362Smrg      else
260b2f5b1dbSmrg        u_plus_rw=,u+rw
26163165362Smrg      fi
26263165362Smrg      cp_umask=$mode$u_plus_rw;;
26363165362Smrg  esac
26463165362Smrgfi
26563165362Smrg
2660eb10989Smrgfor src
2670eb10989Smrgdo
268079e7944Smrg  # Protect names problematic for 'test' and other utilities.
2690eb10989Smrg  case $src in
270079e7944Smrg    -* | [=\(\)!]) src=./$src;;
2710eb10989Smrg  esac
2720eb10989Smrg
2730eb10989Smrg  if test -n "$dir_arg"; then
2740eb10989Smrg    dst=$src
27563165362Smrg    dstdir=$dst
27663165362Smrg    test -d "$dstdir"
27763165362Smrg    dstdir_status=$?
27874901992Smrg    # Don't chown directories that already exist.
27974901992Smrg    if test $dstdir_status = 0; then
28074901992Smrg      chowncmd=""
28174901992Smrg    fi
2820eb10989Smrg  else
28363165362Smrg
2840eb10989Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
2850eb10989Smrg    # might cause directories to be created, which would be especially bad
2860eb10989Smrg    # if $src (and thus $dsttmp) contains '*'.
2870eb10989Smrg    if test ! -f "$src" && test ! -d "$src"; then
2880eb10989Smrg      echo "$0: $src does not exist." >&2
2890eb10989Smrg      exit 1
2900eb10989Smrg    fi
2910eb10989Smrg
29263165362Smrg    if test -z "$dst_arg"; then
2930eb10989Smrg      echo "$0: no destination specified." >&2
2940eb10989Smrg      exit 1
2950eb10989Smrg    fi
29663165362Smrg    dst=$dst_arg
2970eb10989Smrg
29874901992Smrg    # If destination is a directory, append the input filename.
2990eb10989Smrg    if test -d "$dst"; then
300b2f5b1dbSmrg      if test "$is_target_a_directory" = never; then
301b2f5b1dbSmrg        echo "$0: $dst_arg: Is a directory" >&2
302b2f5b1dbSmrg        exit 1
3030eb10989Smrg      fi
30463165362Smrg      dstdir=$dst
30574901992Smrg      dstbase=`basename "$src"`
30674901992Smrg      case $dst in
30774901992Smrg	*/) dst=$dst$dstbase;;
30874901992Smrg	*)  dst=$dst/$dstbase;;
30974901992Smrg      esac
31063165362Smrg      dstdir_status=0
31163165362Smrg    else
312b2f5b1dbSmrg      dstdir=`dirname "$dst"`
31363165362Smrg      test -d "$dstdir"
31463165362Smrg      dstdir_status=$?
3150eb10989Smrg    fi
3160eb10989Smrg  fi
3170eb10989Smrg
31874901992Smrg  case $dstdir in
31974901992Smrg    */) dstdirslash=$dstdir;;
32074901992Smrg    *)  dstdirslash=$dstdir/;;
32174901992Smrg  esac
32274901992Smrg
32363165362Smrg  obsolete_mkdir_used=false
32463165362Smrg
32563165362Smrg  if test $dstdir_status != 0; then
32663165362Smrg    case $posix_mkdir in
32763165362Smrg      '')
328b2f5b1dbSmrg        # With -d, create the new directory with the user-specified mode.
329b2f5b1dbSmrg        # Otherwise, rely on $mkdir_umask.
330b2f5b1dbSmrg        if test -n "$dir_arg"; then
331b2f5b1dbSmrg          mkdir_mode=-m$mode
332b2f5b1dbSmrg        else
333b2f5b1dbSmrg          mkdir_mode=
334b2f5b1dbSmrg        fi
335b2f5b1dbSmrg
336b2f5b1dbSmrg        posix_mkdir=false
33774901992Smrg	# The $RANDOM variable is not portable (e.g., dash).  Use it
33874901992Smrg	# here however when possible just to lower collision chance.
33974901992Smrg	tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
34074901992Smrg
34174901992Smrg	trap '
34274901992Smrg	  ret=$?
34374901992Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null
34474901992Smrg	  exit $ret
34574901992Smrg	' 0
34674901992Smrg
34774901992Smrg	# Because "mkdir -p" follows existing symlinks and we likely work
34874901992Smrg	# directly in world-writeable /tmp, make sure that the '$tmpdir'
34974901992Smrg	# directory is successfully created first before we actually test
35074901992Smrg	# 'mkdir -p'.
35174901992Smrg	if (umask $mkdir_umask &&
35274901992Smrg	    $mkdirprog $mkdir_mode "$tmpdir" &&
35374901992Smrg	    exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
35474901992Smrg	then
35574901992Smrg	  if test -z "$dir_arg" || {
35674901992Smrg	       # Check for POSIX incompatibilities with -m.
35774901992Smrg	       # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
35874901992Smrg	       # other-writable bit of parent directory when it shouldn't.
35974901992Smrg	       # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
36074901992Smrg	       test_tmpdir="$tmpdir/a"
36174901992Smrg	       ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
36274901992Smrg	       case $ls_ld_tmpdir in
36374901992Smrg		 d????-?r-*) different_mode=700;;
36474901992Smrg		 d????-?--*) different_mode=755;;
36574901992Smrg		 *) false;;
36674901992Smrg	       esac &&
36774901992Smrg	       $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
36874901992Smrg		 ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
36974901992Smrg		 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
37074901992Smrg	       }
37174901992Smrg	     }
37274901992Smrg	  then posix_mkdir=:
37374901992Smrg	  fi
37474901992Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
37574901992Smrg	else
37674901992Smrg	  # Remove any dirs left behind by ancient mkdir implementations.
37774901992Smrg	  rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
37874901992Smrg	fi
37974901992Smrg	trap '' 0;;
38063165362Smrg    esac
3810eb10989Smrg
38263165362Smrg    if
38363165362Smrg      $posix_mkdir && (
384b2f5b1dbSmrg        umask $mkdir_umask &&
385b2f5b1dbSmrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
38663165362Smrg      )
38763165362Smrg    then :
38863165362Smrg    else
3890eb10989Smrg
39074901992Smrg      # mkdir does not conform to POSIX,
39163165362Smrg      # or it failed possibly due to a race condition.  Create the
39263165362Smrg      # directory the slow way, step by step, checking for races as we go.
3930eb10989Smrg
39463165362Smrg      case $dstdir in
395b2f5b1dbSmrg        /*) prefix='/';;
396b2f5b1dbSmrg        [-=\(\)!]*) prefix='./';;
397b2f5b1dbSmrg        *)  prefix='';;
39863165362Smrg      esac
3990eb10989Smrg
40063165362Smrg      oIFS=$IFS
40163165362Smrg      IFS=/
402b2f5b1dbSmrg      set -f
40363165362Smrg      set fnord $dstdir
4040eb10989Smrg      shift
405b2f5b1dbSmrg      set +f
40663165362Smrg      IFS=$oIFS
40763165362Smrg
40863165362Smrg      prefixes=
40963165362Smrg
41063165362Smrg      for d
41163165362Smrg      do
412b2f5b1dbSmrg        test X"$d" = X && continue
413b2f5b1dbSmrg
414b2f5b1dbSmrg        prefix=$prefix$d
415b2f5b1dbSmrg        if test -d "$prefix"; then
416b2f5b1dbSmrg          prefixes=
417b2f5b1dbSmrg        else
418b2f5b1dbSmrg          if $posix_mkdir; then
41974901992Smrg            (umask $mkdir_umask &&
420b2f5b1dbSmrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
421b2f5b1dbSmrg            # Don't fail if two instances are running concurrently.
422b2f5b1dbSmrg            test -d "$prefix" || exit 1
423b2f5b1dbSmrg          else
424b2f5b1dbSmrg            case $prefix in
425b2f5b1dbSmrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
426b2f5b1dbSmrg              *) qprefix=$prefix;;
427b2f5b1dbSmrg            esac
428b2f5b1dbSmrg            prefixes="$prefixes '$qprefix'"
429b2f5b1dbSmrg          fi
430b2f5b1dbSmrg        fi
431b2f5b1dbSmrg        prefix=$prefix/
43263165362Smrg      done
43363165362Smrg
43463165362Smrg      if test -n "$prefixes"; then
435b2f5b1dbSmrg        # Don't fail if two instances are running concurrently.
436b2f5b1dbSmrg        (umask $mkdir_umask &&
437b2f5b1dbSmrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
438b2f5b1dbSmrg          test -d "$dstdir" || exit 1
439b2f5b1dbSmrg        obsolete_mkdir_used=true
4400eb10989Smrg      fi
44163165362Smrg    fi
4420eb10989Smrg  fi
4430eb10989Smrg
4440eb10989Smrg  if test -n "$dir_arg"; then
44563165362Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
44663165362Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
44763165362Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
44863165362Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
4490eb10989Smrg  else
4500eb10989Smrg
4510eb10989Smrg    # Make a couple of temp file names in the proper directory.
45274901992Smrg    dsttmp=${dstdirslash}_inst.$$_
45374901992Smrg    rmtmp=${dstdirslash}_rm.$$_
4540eb10989Smrg
4550eb10989Smrg    # Trap to clean up those temp files at exit.
4560eb10989Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
4570eb10989Smrg
4580eb10989Smrg    # Copy the file name to the temp name.
45974901992Smrg    (umask $cp_umask &&
46074901992Smrg     { test -z "$stripcmd" || {
46174901992Smrg	 # Create $dsttmp read-write so that cp doesn't create it read-only,
46274901992Smrg	 # which would cause strip to fail.
46374901992Smrg	 if test -z "$doit"; then
46474901992Smrg	   : >"$dsttmp" # No need to fork-exec 'touch'.
46574901992Smrg	 else
46674901992Smrg	   $doit touch "$dsttmp"
46774901992Smrg	 fi
46874901992Smrg       }
46974901992Smrg     } &&
47074901992Smrg     $doit_exec $cpprog "$src" "$dsttmp") &&
4710eb10989Smrg
4720eb10989Smrg    # and set any options; do chmod last to preserve setuid bits.
4730eb10989Smrg    #
4740eb10989Smrg    # If any of these fail, we abort the whole thing.  If we want to
4750eb10989Smrg    # ignore errors from any of these, just make sure not to ignore
4760eb10989Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
4770eb10989Smrg    #
47863165362Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
47963165362Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
48063165362Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
48163165362Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
48263165362Smrg
48363165362Smrg    # If -C, don't bother to copy if it wouldn't change the file.
48463165362Smrg    if $copy_on_change &&
485b2f5b1dbSmrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
486b2f5b1dbSmrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
487b2f5b1dbSmrg       set -f &&
48863165362Smrg       set X $old && old=:$2:$4:$5:$6 &&
48963165362Smrg       set X $new && new=:$2:$4:$5:$6 &&
490b2f5b1dbSmrg       set +f &&
49163165362Smrg       test "$old" = "$new" &&
49263165362Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
49363165362Smrg    then
49463165362Smrg      rm -f "$dsttmp"
49563165362Smrg    else
49674901992Smrg      # If $backupsuffix is set, and the file being installed
49774901992Smrg      # already exists, attempt a backup.  Don't worry if it fails,
49874901992Smrg      # e.g., if mv doesn't support -f.
49974901992Smrg      if test -n "$backupsuffix" && test -f "$dst"; then
50074901992Smrg        $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null
50174901992Smrg      fi
50274901992Smrg
50363165362Smrg      # Rename the file to the real destination.
50463165362Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
50563165362Smrg
50663165362Smrg      # The rename failed, perhaps because mv can't rename something else
50763165362Smrg      # to itself, or perhaps because mv is so ancient that it does not
50863165362Smrg      # support -f.
50963165362Smrg      {
510b2f5b1dbSmrg        # Now remove or move aside any old file at destination location.
511b2f5b1dbSmrg        # We try this two ways since rm can't unlink itself on some
512b2f5b1dbSmrg        # systems and the destination file might be busy for other
513b2f5b1dbSmrg        # reasons.  In this case, the final cleanup might fail but the new
514b2f5b1dbSmrg        # file should still install successfully.
515b2f5b1dbSmrg        {
516b2f5b1dbSmrg          test ! -f "$dst" ||
51774901992Smrg          $doit $rmcmd "$dst" 2>/dev/null ||
518b2f5b1dbSmrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
51974901992Smrg            { $doit $rmcmd "$rmtmp" 2>/dev/null; :; }
520b2f5b1dbSmrg          } ||
521b2f5b1dbSmrg          { echo "$0: cannot unlink or rename $dst" >&2
522b2f5b1dbSmrg            (exit 1); exit 1
523b2f5b1dbSmrg          }
524b2f5b1dbSmrg        } &&
525b2f5b1dbSmrg
526b2f5b1dbSmrg        # Now rename the file to the real destination.
527b2f5b1dbSmrg        $doit $mvcmd "$dsttmp" "$dst"
52863165362Smrg      }
52963165362Smrg    fi || exit 1
53063165362Smrg
53163165362Smrg    trap '' 0
53263165362Smrg  fi
5330eb10989Smrgdone
5340eb10989Smrg
5350eb10989Smrg# Local variables:
53674901992Smrg# eval: (add-hook 'before-save-hook 'time-stamp)
5370eb10989Smrg# time-stamp-start: "scriptversion="
5380eb10989Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
53974901992Smrg# time-stamp-time-zone: "UTC0"
54063165362Smrg# time-stamp-end: "; # UTC"
5410eb10989Smrg# End:
542