121c2f794Smrg#!/bin/sh
221c2f794Smrg# install - install a program, script, or datafile
321c2f794Smrg
4889a2364Smrgscriptversion=2020-11-14.01; # UTC
521c2f794Smrg
621c2f794Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
721c2f794Smrg# later released in X11R6 (xc/config/util/install.sh) with the
821c2f794Smrg# following copyright and license.
921c2f794Smrg#
1021c2f794Smrg# Copyright (C) 1994 X Consortium
1121c2f794Smrg#
1221c2f794Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
1321c2f794Smrg# of this software and associated documentation files (the "Software"), to
1421c2f794Smrg# deal in the Software without restriction, including without limitation the
1521c2f794Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
1621c2f794Smrg# sell copies of the Software, and to permit persons to whom the Software is
1721c2f794Smrg# furnished to do so, subject to the following conditions:
1821c2f794Smrg#
1921c2f794Smrg# The above copyright notice and this permission notice shall be included in
2021c2f794Smrg# all copies or substantial portions of the Software.
2121c2f794Smrg#
2221c2f794Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2321c2f794Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2421c2f794Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
2521c2f794Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
2621c2f794Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
2721c2f794Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2821c2f794Smrg#
2921c2f794Smrg# Except as contained in this notice, the name of the X Consortium shall not
3021c2f794Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
3121c2f794Smrg# ings in this Software without prior written authorization from the X Consor-
3221c2f794Smrg# tium.
3321c2f794Smrg#
3421c2f794Smrg#
3521c2f794Smrg# FSF changes to this file are in the public domain.
3621c2f794Smrg#
3721c2f794Smrg# Calling this script install-sh is preferred over install.sh, to prevent
381b2353dbSmrg# 'make' implicit rules from creating a file called install from it
3921c2f794Smrg# when there is no Makefile.
4021c2f794Smrg#
4121c2f794Smrg# This script is compatible with the BSD install script, but was written
4221c2f794Smrg# from scratch.
4321c2f794Smrg
44889a2364Smrgtab='	'
4521c2f794Smrgnl='
4621c2f794Smrg'
47889a2364SmrgIFS=" $tab$nl"
4821c2f794Smrg
49889a2364Smrg# Set DOITPROG to "echo" to test this script.
5021c2f794Smrg
5112391620Smrgdoit=${DOITPROG-}
52889a2364Smrgdoit_exec=${doit:-exec}
5321c2f794Smrg
5421c2f794Smrg# Put in absolute file names if you don't have them in your path;
5521c2f794Smrg# or use environment vars.
5621c2f794Smrg
5712391620Smrgchgrpprog=${CHGRPPROG-chgrp}
5812391620Smrgchmodprog=${CHMODPROG-chmod}
5912391620Smrgchownprog=${CHOWNPROG-chown}
6012391620Smrgcmpprog=${CMPPROG-cmp}
6112391620Smrgcpprog=${CPPROG-cp}
6212391620Smrgmkdirprog=${MKDIRPROG-mkdir}
6312391620Smrgmvprog=${MVPROG-mv}
6412391620Smrgrmprog=${RMPROG-rm}
6512391620Smrgstripprog=${STRIPPROG-strip}
6612391620Smrg
6721c2f794Smrgposix_mkdir=
6821c2f794Smrg
6921c2f794Smrg# Desired mode of installed file.
7021c2f794Smrgmode=0755
7121c2f794Smrg
72889a2364Smrg# Create dirs (including intermediate dirs) using mode 755.
73889a2364Smrg# This is like GNU 'install' as of coreutils 8.32 (2020).
74889a2364Smrgmkdir_umask=22
75889a2364Smrg
76889a2364Smrgbackupsuffix=
7712391620Smrgchgrpcmd=
7821c2f794Smrgchmodcmd=$chmodprog
7921c2f794Smrgchowncmd=
8012391620Smrgmvcmd=$mvprog
8121c2f794Smrgrmcmd="$rmprog -f"
8212391620Smrgstripcmd=
8312391620Smrg
8421c2f794Smrgsrc=
8521c2f794Smrgdst=
8621c2f794Smrgdir_arg=
8712391620Smrgdst_arg=
8812391620Smrg
8912391620Smrgcopy_on_change=false
90889a2364Smrgis_target_a_directory=possibly
9121c2f794Smrg
9212391620Smrgusage="\
9312391620SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
9421c2f794Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
9521c2f794Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
9621c2f794Smrg   or: $0 [OPTION]... -d DIRECTORIES...
9721c2f794Smrg
9821c2f794SmrgIn the 1st form, copy SRCFILE to DSTFILE.
9921c2f794SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
10021c2f794SmrgIn the 4th, create DIRECTORIES.
10121c2f794Smrg
10221c2f794SmrgOptions:
10312391620Smrg     --help     display this help and exit.
10412391620Smrg     --version  display version info and exit.
10512391620Smrg
10612391620Smrg  -c            (ignored)
107889a2364Smrg  -C            install only if different (preserve data modification time)
10812391620Smrg  -d            create directories instead of installing files.
10912391620Smrg  -g GROUP      $chgrpprog installed files to GROUP.
11012391620Smrg  -m MODE       $chmodprog installed files to MODE.
11112391620Smrg  -o USER       $chownprog installed files to USER.
112889a2364Smrg  -p            pass -p to $cpprog.
11312391620Smrg  -s            $stripprog installed files.
114889a2364Smrg  -S SUFFIX     attempt to back up existing files, with suffix SUFFIX.
11512391620Smrg  -t DIRECTORY  install into DIRECTORY.
11612391620Smrg  -T            report an error if DSTFILE is a directory.
11721c2f794Smrg
11821c2f794SmrgEnvironment variables override the default commands:
11912391620Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
12012391620Smrg  RMPROG STRIPPROG
121889a2364Smrg
122889a2364SmrgBy default, rm is invoked with -f; when overridden with RMPROG,
123889a2364Smrgit's up to you to specify -f if you want it.
124889a2364Smrg
125889a2364SmrgIf -S is not specified, no backups are attempted.
126889a2364Smrg
127889a2364SmrgEmail bug reports to bug-automake@gnu.org.
128889a2364SmrgAutomake home page: https://www.gnu.org/software/automake/
12921c2f794Smrg"
13021c2f794Smrg
13121c2f794Smrgwhile test $# -ne 0; do
13221c2f794Smrg  case $1 in
13312391620Smrg    -c) ;;
13412391620Smrg
13512391620Smrg    -C) copy_on_change=true;;
13621c2f794Smrg
13712391620Smrg    -d) dir_arg=true;;
13821c2f794Smrg
13921c2f794Smrg    -g) chgrpcmd="$chgrpprog $2"
140889a2364Smrg        shift;;
14121c2f794Smrg
14221c2f794Smrg    --help) echo "$usage"; exit $?;;
14321c2f794Smrg
14421c2f794Smrg    -m) mode=$2
145889a2364Smrg        case $mode in
146889a2364Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
147889a2364Smrg            echo "$0: invalid mode: $mode" >&2
148889a2364Smrg            exit 1;;
149889a2364Smrg        esac
150889a2364Smrg        shift;;
15121c2f794Smrg
15221c2f794Smrg    -o) chowncmd="$chownprog $2"
153889a2364Smrg        shift;;
154889a2364Smrg
155889a2364Smrg    -p) cpprog="$cpprog -p";;
15621c2f794Smrg
15712391620Smrg    -s) stripcmd=$stripprog;;
15821c2f794Smrg
159889a2364Smrg    -S) backupsuffix="$2"
160889a2364Smrg        shift;;
16121c2f794Smrg
162889a2364Smrg    -t)
163889a2364Smrg        is_target_a_directory=always
164889a2364Smrg        dst_arg=$2
165889a2364Smrg        # Protect names problematic for 'test' and other utilities.
166889a2364Smrg        case $dst_arg in
167889a2364Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
168889a2364Smrg        esac
169889a2364Smrg        shift;;
170889a2364Smrg
171889a2364Smrg    -T) is_target_a_directory=never;;
17221c2f794Smrg
17321c2f794Smrg    --version) echo "$0 $scriptversion"; exit $?;;
17421c2f794Smrg
175889a2364Smrg    --) shift
176889a2364Smrg        break;;
17721c2f794Smrg
178889a2364Smrg    -*) echo "$0: invalid option: $1" >&2
179889a2364Smrg        exit 1;;
18021c2f794Smrg
18121c2f794Smrg    *)  break;;
18221c2f794Smrg  esac
18312391620Smrg  shift
18421c2f794Smrgdone
18521c2f794Smrg
186889a2364Smrg# We allow the use of options -d and -T together, by making -d
187889a2364Smrg# take the precedence; this is for compatibility with GNU install.
188889a2364Smrg
189889a2364Smrgif test -n "$dir_arg"; then
190889a2364Smrg  if test -n "$dst_arg"; then
191889a2364Smrg    echo "$0: target directory not allowed when installing a directory." >&2
192889a2364Smrg    exit 1
193889a2364Smrg  fi
194889a2364Smrgfi
195889a2364Smrg
19612391620Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
19721c2f794Smrg  # When -d is used, all remaining arguments are directories to create.
19821c2f794Smrg  # When -t is used, the destination is already specified.
19921c2f794Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
20021c2f794Smrg  for arg
20121c2f794Smrg  do
20212391620Smrg    if test -n "$dst_arg"; then
20321c2f794Smrg      # $@ is not empty: it contains at least $arg.
20412391620Smrg      set fnord "$@" "$dst_arg"
20521c2f794Smrg      shift # fnord
20621c2f794Smrg    fi
20721c2f794Smrg    shift # arg
20812391620Smrg    dst_arg=$arg
2091b2353dbSmrg    # Protect names problematic for 'test' and other utilities.
21082e12b1bSmrg    case $dst_arg in
21182e12b1bSmrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
21282e12b1bSmrg    esac
21321c2f794Smrg  done
21421c2f794Smrgfi
21521c2f794Smrg
21621c2f794Smrgif test $# -eq 0; then
21721c2f794Smrg  if test -z "$dir_arg"; then
21821c2f794Smrg    echo "$0: no input file specified." >&2
21921c2f794Smrg    exit 1
22021c2f794Smrg  fi
2211b2353dbSmrg  # It's OK to call 'install-sh -d' without argument.
22221c2f794Smrg  # This can happen when creating conditional directories.
22321c2f794Smrg  exit 0
22421c2f794Smrgfi
22521c2f794Smrg
226889a2364Smrgif test -z "$dir_arg"; then
227889a2364Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
228889a2364Smrg    if test ! -d "$dst_arg"; then
229889a2364Smrg      echo "$0: $dst_arg: Is not a directory." >&2
230889a2364Smrg      exit 1
231889a2364Smrg    fi
232889a2364Smrg  fi
233889a2364Smrgfi
234889a2364Smrg
23521c2f794Smrgif test -z "$dir_arg"; then
23682e12b1bSmrg  do_exit='(exit $ret); exit $ret'
23782e12b1bSmrg  trap "ret=129; $do_exit" 1
23882e12b1bSmrg  trap "ret=130; $do_exit" 2
23982e12b1bSmrg  trap "ret=141; $do_exit" 13
24082e12b1bSmrg  trap "ret=143; $do_exit" 15
24121c2f794Smrg
24221c2f794Smrg  # Set umask so as not to create temps with too-generous modes.
24321c2f794Smrg  # However, 'strip' requires both read and write access to temps.
24421c2f794Smrg  case $mode in
24521c2f794Smrg    # Optimize common cases.
24621c2f794Smrg    *644) cp_umask=133;;
24721c2f794Smrg    *755) cp_umask=22;;
24821c2f794Smrg
24921c2f794Smrg    *[0-7])
25021c2f794Smrg      if test -z "$stripcmd"; then
251889a2364Smrg        u_plus_rw=
25221c2f794Smrg      else
253889a2364Smrg        u_plus_rw='% 200'
25421c2f794Smrg      fi
25521c2f794Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
25621c2f794Smrg    *)
25721c2f794Smrg      if test -z "$stripcmd"; then
258889a2364Smrg        u_plus_rw=
25921c2f794Smrg      else
260889a2364Smrg        u_plus_rw=,u+rw
26121c2f794Smrg      fi
26221c2f794Smrg      cp_umask=$mode$u_plus_rw;;
26321c2f794Smrg  esac
26421c2f794Smrgfi
26521c2f794Smrg
26621c2f794Smrgfor src
26721c2f794Smrgdo
2681b2353dbSmrg  # Protect names problematic for 'test' and other utilities.
26921c2f794Smrg  case $src in
27082e12b1bSmrg    -* | [=\(\)!]) src=./$src;;
27121c2f794Smrg  esac
27221c2f794Smrg
27321c2f794Smrg  if test -n "$dir_arg"; then
27421c2f794Smrg    dst=$src
27521c2f794Smrg    dstdir=$dst
27621c2f794Smrg    test -d "$dstdir"
27721c2f794Smrg    dstdir_status=$?
278889a2364Smrg    # Don't chown directories that already exist.
279889a2364Smrg    if test $dstdir_status = 0; then
280889a2364Smrg      chowncmd=""
281889a2364Smrg    fi
28221c2f794Smrg  else
28321c2f794Smrg
28421c2f794Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
28521c2f794Smrg    # might cause directories to be created, which would be especially bad
28621c2f794Smrg    # if $src (and thus $dsttmp) contains '*'.
28721c2f794Smrg    if test ! -f "$src" && test ! -d "$src"; then
28821c2f794Smrg      echo "$0: $src does not exist." >&2
28921c2f794Smrg      exit 1
29021c2f794Smrg    fi
29121c2f794Smrg
29212391620Smrg    if test -z "$dst_arg"; then
29321c2f794Smrg      echo "$0: no destination specified." >&2
29421c2f794Smrg      exit 1
29521c2f794Smrg    fi
29612391620Smrg    dst=$dst_arg
29721c2f794Smrg
298889a2364Smrg    # If destination is a directory, append the input filename.
29921c2f794Smrg    if test -d "$dst"; then
300889a2364Smrg      if test "$is_target_a_directory" = never; then
301889a2364Smrg        echo "$0: $dst_arg: Is a directory" >&2
302889a2364Smrg        exit 1
30321c2f794Smrg      fi
30421c2f794Smrg      dstdir=$dst
305889a2364Smrg      dstbase=`basename "$src"`
306889a2364Smrg      case $dst in
307889a2364Smrg	*/) dst=$dst$dstbase;;
308889a2364Smrg	*)  dst=$dst/$dstbase;;
309889a2364Smrg      esac
31021c2f794Smrg      dstdir_status=0
31121c2f794Smrg    else
312889a2364Smrg      dstdir=`dirname "$dst"`
31321c2f794Smrg      test -d "$dstdir"
31421c2f794Smrg      dstdir_status=$?
31521c2f794Smrg    fi
31621c2f794Smrg  fi
31721c2f794Smrg
318889a2364Smrg  case $dstdir in
319889a2364Smrg    */) dstdirslash=$dstdir;;
320889a2364Smrg    *)  dstdirslash=$dstdir/;;
321889a2364Smrg  esac
322889a2364Smrg
32321c2f794Smrg  obsolete_mkdir_used=false
32421c2f794Smrg
32521c2f794Smrg  if test $dstdir_status != 0; then
32621c2f794Smrg    case $posix_mkdir in
32721c2f794Smrg      '')
328889a2364Smrg        # With -d, create the new directory with the user-specified mode.
329889a2364Smrg        # Otherwise, rely on $mkdir_umask.
330889a2364Smrg        if test -n "$dir_arg"; then
331889a2364Smrg          mkdir_mode=-m$mode
332889a2364Smrg        else
333889a2364Smrg          mkdir_mode=
334889a2364Smrg        fi
335889a2364Smrg
336889a2364Smrg        posix_mkdir=false
337889a2364Smrg	# The $RANDOM variable is not portable (e.g., dash).  Use it
338889a2364Smrg	# here however when possible just to lower collision chance.
339889a2364Smrg	tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
340889a2364Smrg
341889a2364Smrg	trap '
342889a2364Smrg	  ret=$?
343889a2364Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null
344889a2364Smrg	  exit $ret
345889a2364Smrg	' 0
346889a2364Smrg
347889a2364Smrg	# Because "mkdir -p" follows existing symlinks and we likely work
348889a2364Smrg	# directly in world-writeable /tmp, make sure that the '$tmpdir'
349889a2364Smrg	# directory is successfully created first before we actually test
350889a2364Smrg	# 'mkdir -p'.
351889a2364Smrg	if (umask $mkdir_umask &&
352889a2364Smrg	    $mkdirprog $mkdir_mode "$tmpdir" &&
353889a2364Smrg	    exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
354889a2364Smrg	then
355889a2364Smrg	  if test -z "$dir_arg" || {
356889a2364Smrg	       # Check for POSIX incompatibilities with -m.
357889a2364Smrg	       # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
358889a2364Smrg	       # other-writable bit of parent directory when it shouldn't.
359889a2364Smrg	       # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
360889a2364Smrg	       test_tmpdir="$tmpdir/a"
361889a2364Smrg	       ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
362889a2364Smrg	       case $ls_ld_tmpdir in
363889a2364Smrg		 d????-?r-*) different_mode=700;;
364889a2364Smrg		 d????-?--*) different_mode=755;;
365889a2364Smrg		 *) false;;
366889a2364Smrg	       esac &&
367889a2364Smrg	       $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
368889a2364Smrg		 ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
369889a2364Smrg		 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
370889a2364Smrg	       }
371889a2364Smrg	     }
372889a2364Smrg	  then posix_mkdir=:
373889a2364Smrg	  fi
374889a2364Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
37521c2f794Smrg	else
376889a2364Smrg	  # Remove any dirs left behind by ancient mkdir implementations.
377889a2364Smrg	  rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
37821c2f794Smrg	fi
379889a2364Smrg	trap '' 0;;
38021c2f794Smrg    esac
38121c2f794Smrg
38221c2f794Smrg    if
38321c2f794Smrg      $posix_mkdir && (
384889a2364Smrg        umask $mkdir_umask &&
385889a2364Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
38621c2f794Smrg      )
38721c2f794Smrg    then :
38821c2f794Smrg    else
38921c2f794Smrg
390889a2364Smrg      # mkdir does not conform to POSIX,
39121c2f794Smrg      # or it failed possibly due to a race condition.  Create the
39221c2f794Smrg      # directory the slow way, step by step, checking for races as we go.
39321c2f794Smrg
39421c2f794Smrg      case $dstdir in
395889a2364Smrg        /*) prefix='/';;
396889a2364Smrg        [-=\(\)!]*) prefix='./';;
397889a2364Smrg        *)  prefix='';;
39821c2f794Smrg      esac
39921c2f794Smrg
40021c2f794Smrg      oIFS=$IFS
40121c2f794Smrg      IFS=/
402889a2364Smrg      set -f
40321c2f794Smrg      set fnord $dstdir
40421c2f794Smrg      shift
405889a2364Smrg      set +f
40621c2f794Smrg      IFS=$oIFS
40721c2f794Smrg
40821c2f794Smrg      prefixes=
40921c2f794Smrg
41021c2f794Smrg      for d
41121c2f794Smrg      do
412889a2364Smrg        test X"$d" = X && continue
413889a2364Smrg
414889a2364Smrg        prefix=$prefix$d
415889a2364Smrg        if test -d "$prefix"; then
416889a2364Smrg          prefixes=
417889a2364Smrg        else
418889a2364Smrg          if $posix_mkdir; then
419889a2364Smrg            (umask $mkdir_umask &&
420889a2364Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
421889a2364Smrg            # Don't fail if two instances are running concurrently.
422889a2364Smrg            test -d "$prefix" || exit 1
423889a2364Smrg          else
424889a2364Smrg            case $prefix in
425889a2364Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
426889a2364Smrg              *) qprefix=$prefix;;
427889a2364Smrg            esac
428889a2364Smrg            prefixes="$prefixes '$qprefix'"
429889a2364Smrg          fi
430889a2364Smrg        fi
431889a2364Smrg        prefix=$prefix/
43221c2f794Smrg      done
43321c2f794Smrg
43421c2f794Smrg      if test -n "$prefixes"; then
435889a2364Smrg        # Don't fail if two instances are running concurrently.
436889a2364Smrg        (umask $mkdir_umask &&
437889a2364Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
438889a2364Smrg          test -d "$dstdir" || exit 1
439889a2364Smrg        obsolete_mkdir_used=true
44021c2f794Smrg      fi
44121c2f794Smrg    fi
44221c2f794Smrg  fi
44321c2f794Smrg
44421c2f794Smrg  if test -n "$dir_arg"; then
44521c2f794Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
44621c2f794Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
44721c2f794Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
44821c2f794Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
44921c2f794Smrg  else
45021c2f794Smrg
45121c2f794Smrg    # Make a couple of temp file names in the proper directory.
452889a2364Smrg    dsttmp=${dstdirslash}_inst.$$_
453889a2364Smrg    rmtmp=${dstdirslash}_rm.$$_
45421c2f794Smrg
45521c2f794Smrg    # Trap to clean up those temp files at exit.
45621c2f794Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
45721c2f794Smrg
45821c2f794Smrg    # Copy the file name to the temp name.
459889a2364Smrg    (umask $cp_umask &&
460889a2364Smrg     { test -z "$stripcmd" || {
461889a2364Smrg	 # Create $dsttmp read-write so that cp doesn't create it read-only,
462889a2364Smrg	 # which would cause strip to fail.
463889a2364Smrg	 if test -z "$doit"; then
464889a2364Smrg	   : >"$dsttmp" # No need to fork-exec 'touch'.
465889a2364Smrg	 else
466889a2364Smrg	   $doit touch "$dsttmp"
467889a2364Smrg	 fi
468889a2364Smrg       }
469889a2364Smrg     } &&
470889a2364Smrg     $doit_exec $cpprog "$src" "$dsttmp") &&
47121c2f794Smrg
47221c2f794Smrg    # and set any options; do chmod last to preserve setuid bits.
47321c2f794Smrg    #
47421c2f794Smrg    # If any of these fail, we abort the whole thing.  If we want to
47521c2f794Smrg    # ignore errors from any of these, just make sure not to ignore
47621c2f794Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
47721c2f794Smrg    #
47812391620Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
47912391620Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
48012391620Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
48112391620Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
48212391620Smrg
48312391620Smrg    # If -C, don't bother to copy if it wouldn't change the file.
48412391620Smrg    if $copy_on_change &&
485889a2364Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
486889a2364Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
487889a2364Smrg       set -f &&
48812391620Smrg       set X $old && old=:$2:$4:$5:$6 &&
48912391620Smrg       set X $new && new=:$2:$4:$5:$6 &&
490889a2364Smrg       set +f &&
49112391620Smrg       test "$old" = "$new" &&
49212391620Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
49312391620Smrg    then
49412391620Smrg      rm -f "$dsttmp"
49512391620Smrg    else
496889a2364Smrg      # If $backupsuffix is set, and the file being installed
497889a2364Smrg      # already exists, attempt a backup.  Don't worry if it fails,
498889a2364Smrg      # e.g., if mv doesn't support -f.
499889a2364Smrg      if test -n "$backupsuffix" && test -f "$dst"; then
500889a2364Smrg        $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null
501889a2364Smrg      fi
502889a2364Smrg
50312391620Smrg      # Rename the file to the real destination.
50412391620Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
50512391620Smrg
50612391620Smrg      # The rename failed, perhaps because mv can't rename something else
50712391620Smrg      # to itself, or perhaps because mv is so ancient that it does not
50812391620Smrg      # support -f.
50912391620Smrg      {
510889a2364Smrg        # Now remove or move aside any old file at destination location.
511889a2364Smrg        # We try this two ways since rm can't unlink itself on some
512889a2364Smrg        # systems and the destination file might be busy for other
513889a2364Smrg        # reasons.  In this case, the final cleanup might fail but the new
514889a2364Smrg        # file should still install successfully.
515889a2364Smrg        {
516889a2364Smrg          test ! -f "$dst" ||
517889a2364Smrg          $doit $rmcmd "$dst" 2>/dev/null ||
518889a2364Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
519889a2364Smrg            { $doit $rmcmd "$rmtmp" 2>/dev/null; :; }
520889a2364Smrg          } ||
521889a2364Smrg          { echo "$0: cannot unlink or rename $dst" >&2
522889a2364Smrg            (exit 1); exit 1
523889a2364Smrg          }
524889a2364Smrg        } &&
525889a2364Smrg
526889a2364Smrg        # Now rename the file to the real destination.
527889a2364Smrg        $doit $mvcmd "$dsttmp" "$dst"
52812391620Smrg      }
52912391620Smrg    fi || exit 1
53021c2f794Smrg
53121c2f794Smrg    trap '' 0
53221c2f794Smrg  fi
53321c2f794Smrgdone
53421c2f794Smrg
53521c2f794Smrg# Local variables:
536889a2364Smrg# eval: (add-hook 'before-save-hook 'time-stamp)
53721c2f794Smrg# time-stamp-start: "scriptversion="
53821c2f794Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
539889a2364Smrg# time-stamp-time-zone: "UTC0"
54012391620Smrg# time-stamp-end: "; # UTC"
54121c2f794Smrg# End:
542