1b1297603Smrg#!/bin/sh
2b1297603Smrg# install - install a program, script, or datafile
3b1297603Smrg
420f5670eSmrgscriptversion=2020-11-14.01; # UTC
5b1297603Smrg
6b1297603Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7b1297603Smrg# later released in X11R6 (xc/config/util/install.sh) with the
8b1297603Smrg# following copyright and license.
9b1297603Smrg#
10b1297603Smrg# Copyright (C) 1994 X Consortium
11b1297603Smrg#
12b1297603Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13b1297603Smrg# of this software and associated documentation files (the "Software"), to
14b1297603Smrg# deal in the Software without restriction, including without limitation the
15b1297603Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16b1297603Smrg# sell copies of the Software, and to permit persons to whom the Software is
17b1297603Smrg# furnished to do so, subject to the following conditions:
18b1297603Smrg#
19b1297603Smrg# The above copyright notice and this permission notice shall be included in
20b1297603Smrg# all copies or substantial portions of the Software.
21b1297603Smrg#
22b1297603Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23b1297603Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24b1297603Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25b1297603Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26b1297603Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27b1297603Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28b1297603Smrg#
29b1297603Smrg# Except as contained in this notice, the name of the X Consortium shall not
30b1297603Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
31b1297603Smrg# ings in this Software without prior written authorization from the X Consor-
32b1297603Smrg# tium.
33b1297603Smrg#
34b1297603Smrg#
35b1297603Smrg# FSF changes to this file are in the public domain.
36b1297603Smrg#
37b1297603Smrg# Calling this script install-sh is preferred over install.sh, to prevent
3833734831Smrg# 'make' implicit rules from creating a file called install from it
39b1297603Smrg# when there is no Makefile.
40b1297603Smrg#
41b1297603Smrg# This script is compatible with the BSD install script, but was written
425b944e2aSmrg# from scratch.
435b944e2aSmrg
44a570218aSmrgtab='	'
455b944e2aSmrgnl='
465b944e2aSmrg'
47a570218aSmrgIFS=" $tab$nl"
48b1297603Smrg
49a570218aSmrg# Set DOITPROG to "echo" to test this script.
50b1297603Smrg
5133734831Smrgdoit=${DOITPROG-}
52a570218aSmrgdoit_exec=${doit:-exec}
53b1297603Smrg
545b944e2aSmrg# Put in absolute file names if you don't have them in your path;
555b944e2aSmrg# or use environment vars.
56b1297603Smrg
5733734831Smrgchgrpprog=${CHGRPPROG-chgrp}
5833734831Smrgchmodprog=${CHMODPROG-chmod}
5933734831Smrgchownprog=${CHOWNPROG-chown}
6033734831Smrgcmpprog=${CMPPROG-cmp}
6133734831Smrgcpprog=${CPPROG-cp}
6233734831Smrgmkdirprog=${MKDIRPROG-mkdir}
6333734831Smrgmvprog=${MVPROG-mv}
6433734831Smrgrmprog=${RMPROG-rm}
6533734831Smrgstripprog=${STRIPPROG-strip}
6633734831Smrg
675b944e2aSmrgposix_mkdir=
685b944e2aSmrg
695b944e2aSmrg# Desired mode of installed file.
705b944e2aSmrgmode=0755
715b944e2aSmrg
7220f5670eSmrg# Create dirs (including intermediate dirs) using mode 755.
7320f5670eSmrg# This is like GNU 'install' as of coreutils 8.32 (2020).
7420f5670eSmrgmkdir_umask=22
7520f5670eSmrg
7620f5670eSmrgbackupsuffix=
7733734831Smrgchgrpcmd=
785b944e2aSmrgchmodcmd=$chmodprog
79b1297603Smrgchowncmd=
8033734831Smrgmvcmd=$mvprog
81b1297603Smrgrmcmd="$rmprog -f"
8233734831Smrgstripcmd=
8333734831Smrg
84b1297603Smrgsrc=
85b1297603Smrgdst=
86b1297603Smrgdir_arg=
8733734831Smrgdst_arg=
8833734831Smrg
8933734831Smrgcopy_on_change=false
90a570218aSmrgis_target_a_directory=possibly
91b1297603Smrg
9233734831Smrgusage="\
9333734831SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
94b1297603Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
95b1297603Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
96b1297603Smrg   or: $0 [OPTION]... -d DIRECTORIES...
97b1297603Smrg
98b1297603SmrgIn the 1st form, copy SRCFILE to DSTFILE.
99b1297603SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
100b1297603SmrgIn the 4th, create DIRECTORIES.
101b1297603Smrg
102b1297603SmrgOptions:
10333734831Smrg     --help     display this help and exit.
10433734831Smrg     --version  display version info and exit.
10533734831Smrg
10633734831Smrg  -c            (ignored)
10720f5670eSmrg  -C            install only if different (preserve data modification time)
10833734831Smrg  -d            create directories instead of installing files.
10933734831Smrg  -g GROUP      $chgrpprog installed files to GROUP.
11033734831Smrg  -m MODE       $chmodprog installed files to MODE.
11133734831Smrg  -o USER       $chownprog installed files to USER.
11220f5670eSmrg  -p            pass -p to $cpprog.
11333734831Smrg  -s            $stripprog installed files.
11420f5670eSmrg  -S SUFFIX     attempt to back up existing files, with suffix SUFFIX.
11533734831Smrg  -t DIRECTORY  install into DIRECTORY.
11633734831Smrg  -T            report an error if DSTFILE is a directory.
117b1297603Smrg
118b1297603SmrgEnvironment variables override the default commands:
11933734831Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
12033734831Smrg  RMPROG STRIPPROG
12120f5670eSmrg
12220f5670eSmrgBy default, rm is invoked with -f; when overridden with RMPROG,
12320f5670eSmrgit's up to you to specify -f if you want it.
12420f5670eSmrg
12520f5670eSmrgIf -S is not specified, no backups are attempted.
12620f5670eSmrg
12720f5670eSmrgEmail bug reports to bug-automake@gnu.org.
12820f5670eSmrgAutomake home page: https://www.gnu.org/software/automake/
129b1297603Smrg"
130b1297603Smrg
1315b944e2aSmrgwhile test $# -ne 0; do
132b1297603Smrg  case $1 in
13333734831Smrg    -c) ;;
134b1297603Smrg
13533734831Smrg    -C) copy_on_change=true;;
13633734831Smrg
13733734831Smrg    -d) dir_arg=true;;
138b1297603Smrg
139b1297603Smrg    -g) chgrpcmd="$chgrpprog $2"
140a570218aSmrg        shift;;
141b1297603Smrg
142b1297603Smrg    --help) echo "$usage"; exit $?;;
143b1297603Smrg
1445b944e2aSmrg    -m) mode=$2
145a570218aSmrg        case $mode in
146a570218aSmrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
147a570218aSmrg            echo "$0: invalid mode: $mode" >&2
148a570218aSmrg            exit 1;;
149a570218aSmrg        esac
150a570218aSmrg        shift;;
151b1297603Smrg
152b1297603Smrg    -o) chowncmd="$chownprog $2"
153a570218aSmrg        shift;;
154b1297603Smrg
15520f5670eSmrg    -p) cpprog="$cpprog -p";;
15620f5670eSmrg
15733734831Smrg    -s) stripcmd=$stripprog;;
158b1297603Smrg
15920f5670eSmrg    -S) backupsuffix="$2"
16020f5670eSmrg        shift;;
16120f5670eSmrg
162a570218aSmrg    -t)
163a570218aSmrg        is_target_a_directory=always
164a570218aSmrg        dst_arg=$2
165a570218aSmrg        # Protect names problematic for 'test' and other utilities.
166a570218aSmrg        case $dst_arg in
167a570218aSmrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
168a570218aSmrg        esac
169a570218aSmrg        shift;;
170b1297603Smrg
171a570218aSmrg    -T) is_target_a_directory=never;;
172b1297603Smrg
173b1297603Smrg    --version) echo "$0 $scriptversion"; exit $?;;
174b1297603Smrg
175a570218aSmrg    --) shift
176a570218aSmrg        break;;
1775b944e2aSmrg
178a570218aSmrg    -*) echo "$0: invalid option: $1" >&2
179a570218aSmrg        exit 1;;
1805b944e2aSmrg
1815b944e2aSmrg    *)  break;;
182b1297603Smrg  esac
18333734831Smrg  shift
184b1297603Smrgdone
185b1297603Smrg
186a570218aSmrg# We allow the use of options -d and -T together, by making -d
187a570218aSmrg# take the precedence; this is for compatibility with GNU install.
188a570218aSmrg
189a570218aSmrgif test -n "$dir_arg"; then
190a570218aSmrg  if test -n "$dst_arg"; then
191a570218aSmrg    echo "$0: target directory not allowed when installing a directory." >&2
192a570218aSmrg    exit 1
193a570218aSmrg  fi
194a570218aSmrgfi
195a570218aSmrg
19633734831Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1975b944e2aSmrg  # When -d is used, all remaining arguments are directories to create.
1985b944e2aSmrg  # When -t is used, the destination is already specified.
1995b944e2aSmrg  # Otherwise, the last argument is the destination.  Remove it from $@.
2005b944e2aSmrg  for arg
2015b944e2aSmrg  do
20233734831Smrg    if test -n "$dst_arg"; then
2035b944e2aSmrg      # $@ is not empty: it contains at least $arg.
20433734831Smrg      set fnord "$@" "$dst_arg"
2055b944e2aSmrg      shift # fnord
2065b944e2aSmrg    fi
2075b944e2aSmrg    shift # arg
20833734831Smrg    dst_arg=$arg
20933734831Smrg    # Protect names problematic for 'test' and other utilities.
21033734831Smrg    case $dst_arg in
21133734831Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
21233734831Smrg    esac
2135b944e2aSmrg  done
2145b944e2aSmrgfi
2155b944e2aSmrg
2165b944e2aSmrgif test $# -eq 0; then
217b1297603Smrg  if test -z "$dir_arg"; then
218b1297603Smrg    echo "$0: no input file specified." >&2
219b1297603Smrg    exit 1
220b1297603Smrg  fi
22133734831Smrg  # It's OK to call 'install-sh -d' without argument.
222b1297603Smrg  # This can happen when creating conditional directories.
223b1297603Smrg  exit 0
224b1297603Smrgfi
225b1297603Smrg
226a570218aSmrgif test -z "$dir_arg"; then
227a570218aSmrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
228a570218aSmrg    if test ! -d "$dst_arg"; then
229a570218aSmrg      echo "$0: $dst_arg: Is not a directory." >&2
230a570218aSmrg      exit 1
231a570218aSmrg    fi
232a570218aSmrg  fi
233a570218aSmrgfi
234a570218aSmrg
2355b944e2aSmrgif test -z "$dir_arg"; then
23633734831Smrg  do_exit='(exit $ret); exit $ret'
23733734831Smrg  trap "ret=129; $do_exit" 1
23833734831Smrg  trap "ret=130; $do_exit" 2
23933734831Smrg  trap "ret=141; $do_exit" 13
24033734831Smrg  trap "ret=143; $do_exit" 15
2415b944e2aSmrg
2425b944e2aSmrg  # Set umask so as not to create temps with too-generous modes.
2435b944e2aSmrg  # However, 'strip' requires both read and write access to temps.
2445b944e2aSmrg  case $mode in
2455b944e2aSmrg    # Optimize common cases.
2465b944e2aSmrg    *644) cp_umask=133;;
2475b944e2aSmrg    *755) cp_umask=22;;
2485b944e2aSmrg
2495b944e2aSmrg    *[0-7])
2505b944e2aSmrg      if test -z "$stripcmd"; then
251a570218aSmrg        u_plus_rw=
2525b944e2aSmrg      else
253a570218aSmrg        u_plus_rw='% 200'
2545b944e2aSmrg      fi
2555b944e2aSmrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2565b944e2aSmrg    *)
2575b944e2aSmrg      if test -z "$stripcmd"; then
258a570218aSmrg        u_plus_rw=
2595b944e2aSmrg      else
260a570218aSmrg        u_plus_rw=,u+rw
2615b944e2aSmrg      fi
2625b944e2aSmrg      cp_umask=$mode$u_plus_rw;;
2635b944e2aSmrg  esac
2645b944e2aSmrgfi
2655b944e2aSmrg
266b1297603Smrgfor src
267b1297603Smrgdo
26833734831Smrg  # Protect names problematic for 'test' and other utilities.
269b1297603Smrg  case $src in
27033734831Smrg    -* | [=\(\)!]) src=./$src;;
271b1297603Smrg  esac
272b1297603Smrg
273b1297603Smrg  if test -n "$dir_arg"; then
274b1297603Smrg    dst=$src
2755b944e2aSmrg    dstdir=$dst
2765b944e2aSmrg    test -d "$dstdir"
2775b944e2aSmrg    dstdir_status=$?
27820f5670eSmrg    # Don't chown directories that already exist.
27920f5670eSmrg    if test $dstdir_status = 0; then
28020f5670eSmrg      chowncmd=""
28120f5670eSmrg    fi
282b1297603Smrg  else
2835b944e2aSmrg
284b1297603Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
285b1297603Smrg    # might cause directories to be created, which would be especially bad
286b1297603Smrg    # if $src (and thus $dsttmp) contains '*'.
287b1297603Smrg    if test ! -f "$src" && test ! -d "$src"; then
288b1297603Smrg      echo "$0: $src does not exist." >&2
289b1297603Smrg      exit 1
290b1297603Smrg    fi
291b1297603Smrg
29233734831Smrg    if test -z "$dst_arg"; then
293b1297603Smrg      echo "$0: no destination specified." >&2
294b1297603Smrg      exit 1
295b1297603Smrg    fi
29633734831Smrg    dst=$dst_arg
297b1297603Smrg
298a570218aSmrg    # If destination is a directory, append the input filename.
299b1297603Smrg    if test -d "$dst"; then
300a570218aSmrg      if test "$is_target_a_directory" = never; then
301a570218aSmrg        echo "$0: $dst_arg: Is a directory" >&2
302a570218aSmrg        exit 1
303b1297603Smrg      fi
3045b944e2aSmrg      dstdir=$dst
305a570218aSmrg      dstbase=`basename "$src"`
306a570218aSmrg      case $dst in
307a570218aSmrg	*/) dst=$dst$dstbase;;
308a570218aSmrg	*)  dst=$dst/$dstbase;;
309a570218aSmrg      esac
3105b944e2aSmrg      dstdir_status=0
3115b944e2aSmrg    else
312a570218aSmrg      dstdir=`dirname "$dst"`
3135b944e2aSmrg      test -d "$dstdir"
3145b944e2aSmrg      dstdir_status=$?
315b1297603Smrg    fi
316b1297603Smrg  fi
317b1297603Smrg
318a570218aSmrg  case $dstdir in
319a570218aSmrg    */) dstdirslash=$dstdir;;
320a570218aSmrg    *)  dstdirslash=$dstdir/;;
321a570218aSmrg  esac
322a570218aSmrg
3235b944e2aSmrg  obsolete_mkdir_used=false
3245b944e2aSmrg
3255b944e2aSmrg  if test $dstdir_status != 0; then
3265b944e2aSmrg    case $posix_mkdir in
3275b944e2aSmrg      '')
328a570218aSmrg        # With -d, create the new directory with the user-specified mode.
329a570218aSmrg        # Otherwise, rely on $mkdir_umask.
330a570218aSmrg        if test -n "$dir_arg"; then
331a570218aSmrg          mkdir_mode=-m$mode
332a570218aSmrg        else
333a570218aSmrg          mkdir_mode=
334a570218aSmrg        fi
335a570218aSmrg
336a570218aSmrg        posix_mkdir=false
33720f5670eSmrg	# The $RANDOM variable is not portable (e.g., dash).  Use it
33820f5670eSmrg	# here however when possible just to lower collision chance.
33920f5670eSmrg	tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
34020f5670eSmrg
34120f5670eSmrg	trap '
34220f5670eSmrg	  ret=$?
34320f5670eSmrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null
34420f5670eSmrg	  exit $ret
34520f5670eSmrg	' 0
34620f5670eSmrg
34720f5670eSmrg	# Because "mkdir -p" follows existing symlinks and we likely work
34820f5670eSmrg	# directly in world-writeable /tmp, make sure that the '$tmpdir'
34920f5670eSmrg	# directory is successfully created first before we actually test
35020f5670eSmrg	# 'mkdir -p'.
35120f5670eSmrg	if (umask $mkdir_umask &&
35220f5670eSmrg	    $mkdirprog $mkdir_mode "$tmpdir" &&
35320f5670eSmrg	    exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
35420f5670eSmrg	then
35520f5670eSmrg	  if test -z "$dir_arg" || {
35620f5670eSmrg	       # Check for POSIX incompatibilities with -m.
35720f5670eSmrg	       # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
35820f5670eSmrg	       # other-writable bit of parent directory when it shouldn't.
35920f5670eSmrg	       # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
36020f5670eSmrg	       test_tmpdir="$tmpdir/a"
36120f5670eSmrg	       ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
36220f5670eSmrg	       case $ls_ld_tmpdir in
36320f5670eSmrg		 d????-?r-*) different_mode=700;;
36420f5670eSmrg		 d????-?--*) different_mode=755;;
36520f5670eSmrg		 *) false;;
36620f5670eSmrg	       esac &&
36720f5670eSmrg	       $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
36820f5670eSmrg		 ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
36920f5670eSmrg		 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
37020f5670eSmrg	       }
37120f5670eSmrg	     }
37220f5670eSmrg	  then posix_mkdir=:
37320f5670eSmrg	  fi
37420f5670eSmrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
37520f5670eSmrg	else
37620f5670eSmrg	  # Remove any dirs left behind by ancient mkdir implementations.
37720f5670eSmrg	  rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
37820f5670eSmrg	fi
37920f5670eSmrg	trap '' 0;;
3805b944e2aSmrg    esac
381b1297603Smrg
3825b944e2aSmrg    if
3835b944e2aSmrg      $posix_mkdir && (
384a570218aSmrg        umask $mkdir_umask &&
385a570218aSmrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3865b944e2aSmrg      )
3875b944e2aSmrg    then :
3885b944e2aSmrg    else
389b1297603Smrg
39020f5670eSmrg      # mkdir does not conform to POSIX,
3915b944e2aSmrg      # or it failed possibly due to a race condition.  Create the
3925b944e2aSmrg      # directory the slow way, step by step, checking for races as we go.
3935b944e2aSmrg
3945b944e2aSmrg      case $dstdir in
395a570218aSmrg        /*) prefix='/';;
396a570218aSmrg        [-=\(\)!]*) prefix='./';;
397a570218aSmrg        *)  prefix='';;
3985b944e2aSmrg      esac
3995b944e2aSmrg
4005b944e2aSmrg      oIFS=$IFS
4015b944e2aSmrg      IFS=/
402a570218aSmrg      set -f
4035b944e2aSmrg      set fnord $dstdir
404b1297603Smrg      shift
405a570218aSmrg      set +f
4065b944e2aSmrg      IFS=$oIFS
4075b944e2aSmrg
4085b944e2aSmrg      prefixes=
4095b944e2aSmrg
4105b944e2aSmrg      for d
4115b944e2aSmrg      do
412a570218aSmrg        test X"$d" = X && continue
413a570218aSmrg
414a570218aSmrg        prefix=$prefix$d
415a570218aSmrg        if test -d "$prefix"; then
416a570218aSmrg          prefixes=
417a570218aSmrg        else
418a570218aSmrg          if $posix_mkdir; then
41920f5670eSmrg            (umask $mkdir_umask &&
420a570218aSmrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
421a570218aSmrg            # Don't fail if two instances are running concurrently.
422a570218aSmrg            test -d "$prefix" || exit 1
423a570218aSmrg          else
424a570218aSmrg            case $prefix in
425a570218aSmrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
426a570218aSmrg              *) qprefix=$prefix;;
427a570218aSmrg            esac
428a570218aSmrg            prefixes="$prefixes '$qprefix'"
429a570218aSmrg          fi
430a570218aSmrg        fi
431a570218aSmrg        prefix=$prefix/
4325b944e2aSmrg      done
4335b944e2aSmrg
4345b944e2aSmrg      if test -n "$prefixes"; then
435a570218aSmrg        # Don't fail if two instances are running concurrently.
436a570218aSmrg        (umask $mkdir_umask &&
437a570218aSmrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
438a570218aSmrg          test -d "$dstdir" || exit 1
439a570218aSmrg        obsolete_mkdir_used=true
440b1297603Smrg      fi
4415b944e2aSmrg    fi
442b1297603Smrg  fi
443b1297603Smrg
444b1297603Smrg  if test -n "$dir_arg"; then
4455b944e2aSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4465b944e2aSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4475b944e2aSmrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4485b944e2aSmrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
449b1297603Smrg  else
450b1297603Smrg
451b1297603Smrg    # Make a couple of temp file names in the proper directory.
452a570218aSmrg    dsttmp=${dstdirslash}_inst.$$_
453a570218aSmrg    rmtmp=${dstdirslash}_rm.$$_
454b1297603Smrg
455b1297603Smrg    # Trap to clean up those temp files at exit.
456b1297603Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
457b1297603Smrg
458b1297603Smrg    # Copy the file name to the temp name.
45920f5670eSmrg    (umask $cp_umask &&
46020f5670eSmrg     { test -z "$stripcmd" || {
46120f5670eSmrg	 # Create $dsttmp read-write so that cp doesn't create it read-only,
46220f5670eSmrg	 # which would cause strip to fail.
46320f5670eSmrg	 if test -z "$doit"; then
46420f5670eSmrg	   : >"$dsttmp" # No need to fork-exec 'touch'.
46520f5670eSmrg	 else
46620f5670eSmrg	   $doit touch "$dsttmp"
46720f5670eSmrg	 fi
46820f5670eSmrg       }
46920f5670eSmrg     } &&
47020f5670eSmrg     $doit_exec $cpprog "$src" "$dsttmp") &&
471b1297603Smrg
472b1297603Smrg    # and set any options; do chmod last to preserve setuid bits.
473b1297603Smrg    #
474b1297603Smrg    # If any of these fail, we abort the whole thing.  If we want to
475b1297603Smrg    # ignore errors from any of these, just make sure not to ignore
476b1297603Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
477b1297603Smrg    #
47833734831Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
47933734831Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
48033734831Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
48133734831Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
48233734831Smrg
48333734831Smrg    # If -C, don't bother to copy if it wouldn't change the file.
48433734831Smrg    if $copy_on_change &&
485a570218aSmrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
486a570218aSmrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
487a570218aSmrg       set -f &&
48833734831Smrg       set X $old && old=:$2:$4:$5:$6 &&
48933734831Smrg       set X $new && new=:$2:$4:$5:$6 &&
490a570218aSmrg       set +f &&
49133734831Smrg       test "$old" = "$new" &&
49233734831Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
49333734831Smrg    then
49433734831Smrg      rm -f "$dsttmp"
49533734831Smrg    else
49620f5670eSmrg      # If $backupsuffix is set, and the file being installed
49720f5670eSmrg      # already exists, attempt a backup.  Don't worry if it fails,
49820f5670eSmrg      # e.g., if mv doesn't support -f.
49920f5670eSmrg      if test -n "$backupsuffix" && test -f "$dst"; then
50020f5670eSmrg        $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null
50120f5670eSmrg      fi
50220f5670eSmrg
50333734831Smrg      # Rename the file to the real destination.
50433734831Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
50533734831Smrg
50633734831Smrg      # The rename failed, perhaps because mv can't rename something else
50733734831Smrg      # to itself, or perhaps because mv is so ancient that it does not
50833734831Smrg      # support -f.
50933734831Smrg      {
510a570218aSmrg        # Now remove or move aside any old file at destination location.
511a570218aSmrg        # We try this two ways since rm can't unlink itself on some
512a570218aSmrg        # systems and the destination file might be busy for other
513a570218aSmrg        # reasons.  In this case, the final cleanup might fail but the new
514a570218aSmrg        # file should still install successfully.
515a570218aSmrg        {
516a570218aSmrg          test ! -f "$dst" ||
51720f5670eSmrg          $doit $rmcmd "$dst" 2>/dev/null ||
518a570218aSmrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
51920f5670eSmrg            { $doit $rmcmd "$rmtmp" 2>/dev/null; :; }
520a570218aSmrg          } ||
521a570218aSmrg          { echo "$0: cannot unlink or rename $dst" >&2
522a570218aSmrg            (exit 1); exit 1
523a570218aSmrg          }
524a570218aSmrg        } &&
525a570218aSmrg
526a570218aSmrg        # Now rename the file to the real destination.
527a570218aSmrg        $doit $mvcmd "$dsttmp" "$dst"
52833734831Smrg      }
52933734831Smrg    fi || exit 1
530b1297603Smrg
5315b944e2aSmrg    trap '' 0
5325b944e2aSmrg  fi
5335b944e2aSmrgdone
534b1297603Smrg
535b1297603Smrg# Local variables:
536a570218aSmrg# eval: (add-hook 'before-save-hook 'time-stamp)
537b1297603Smrg# time-stamp-start: "scriptversion="
538b1297603Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
539a570218aSmrg# time-stamp-time-zone: "UTC0"
54033734831Smrg# time-stamp-end: "; # UTC"
541b1297603Smrg# End:
542