1067610f1Smrg#!/bin/sh
2067610f1Smrg# install - install a program, script, or datafile
3067610f1Smrg
458105723Smrgscriptversion=2020-11-14.01; # UTC
5067610f1Smrg
6067610f1Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7067610f1Smrg# later released in X11R6 (xc/config/util/install.sh) with the
8067610f1Smrg# following copyright and license.
9067610f1Smrg#
10067610f1Smrg# Copyright (C) 1994 X Consortium
11067610f1Smrg#
12067610f1Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13067610f1Smrg# of this software and associated documentation files (the "Software"), to
14067610f1Smrg# deal in the Software without restriction, including without limitation the
15067610f1Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16067610f1Smrg# sell copies of the Software, and to permit persons to whom the Software is
17067610f1Smrg# furnished to do so, subject to the following conditions:
18067610f1Smrg#
19067610f1Smrg# The above copyright notice and this permission notice shall be included in
20067610f1Smrg# all copies or substantial portions of the Software.
21067610f1Smrg#
22067610f1Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23067610f1Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24067610f1Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25067610f1Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26067610f1Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27067610f1Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28067610f1Smrg#
29067610f1Smrg# Except as contained in this notice, the name of the X Consortium shall not
30067610f1Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
31067610f1Smrg# ings in this Software without prior written authorization from the X Consor-
32067610f1Smrg# tium.
33067610f1Smrg#
34067610f1Smrg#
35067610f1Smrg# FSF changes to this file are in the public domain.
36067610f1Smrg#
37067610f1Smrg# Calling this script install-sh is preferred over install.sh, to prevent
38e7fdea5fSmrg# 'make' implicit rules from creating a file called install from it
39067610f1Smrg# when there is no Makefile.
40067610f1Smrg#
41067610f1Smrg# This script is compatible with the BSD install script, but was written
42067610f1Smrg# from scratch.
43067610f1Smrg
444d9306dbSmrgtab='	'
45067610f1Smrgnl='
46067610f1Smrg'
474d9306dbSmrgIFS=" $tab$nl"
48067610f1Smrg
494d9306dbSmrg# Set DOITPROG to "echo" to test this script.
50067610f1Smrg
517e6533d5Smrgdoit=${DOITPROG-}
524d9306dbSmrgdoit_exec=${doit:-exec}
53067610f1Smrg
54067610f1Smrg# Put in absolute file names if you don't have them in your path;
55067610f1Smrg# or use environment vars.
56067610f1Smrg
577e6533d5Smrgchgrpprog=${CHGRPPROG-chgrp}
587e6533d5Smrgchmodprog=${CHMODPROG-chmod}
597e6533d5Smrgchownprog=${CHOWNPROG-chown}
607e6533d5Smrgcmpprog=${CMPPROG-cmp}
617e6533d5Smrgcpprog=${CPPROG-cp}
627e6533d5Smrgmkdirprog=${MKDIRPROG-mkdir}
637e6533d5Smrgmvprog=${MVPROG-mv}
647e6533d5Smrgrmprog=${RMPROG-rm}
657e6533d5Smrgstripprog=${STRIPPROG-strip}
667e6533d5Smrg
67067610f1Smrgposix_mkdir=
68067610f1Smrg
69067610f1Smrg# Desired mode of installed file.
70067610f1Smrgmode=0755
71067610f1Smrg
7258105723Smrg# Create dirs (including intermediate dirs) using mode 755.
7358105723Smrg# This is like GNU 'install' as of coreutils 8.32 (2020).
7458105723Smrgmkdir_umask=22
7558105723Smrg
7658105723Smrgbackupsuffix=
777e6533d5Smrgchgrpcmd=
78067610f1Smrgchmodcmd=$chmodprog
79067610f1Smrgchowncmd=
807e6533d5Smrgmvcmd=$mvprog
81067610f1Smrgrmcmd="$rmprog -f"
827e6533d5Smrgstripcmd=
837e6533d5Smrg
84067610f1Smrgsrc=
85067610f1Smrgdst=
86067610f1Smrgdir_arg=
877e6533d5Smrgdst_arg=
887e6533d5Smrg
897e6533d5Smrgcopy_on_change=false
904d9306dbSmrgis_target_a_directory=possibly
91067610f1Smrg
927e6533d5Smrgusage="\
937e6533d5SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
94067610f1Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
95067610f1Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
96067610f1Smrg   or: $0 [OPTION]... -d DIRECTORIES...
97067610f1Smrg
98067610f1SmrgIn the 1st form, copy SRCFILE to DSTFILE.
99067610f1SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
100067610f1SmrgIn the 4th, create DIRECTORIES.
101067610f1Smrg
102067610f1SmrgOptions:
1037e6533d5Smrg     --help     display this help and exit.
1047e6533d5Smrg     --version  display version info and exit.
1057e6533d5Smrg
1067e6533d5Smrg  -c            (ignored)
10758105723Smrg  -C            install only if different (preserve data modification time)
1087e6533d5Smrg  -d            create directories instead of installing files.
1097e6533d5Smrg  -g GROUP      $chgrpprog installed files to GROUP.
1107e6533d5Smrg  -m MODE       $chmodprog installed files to MODE.
1117e6533d5Smrg  -o USER       $chownprog installed files to USER.
11258105723Smrg  -p            pass -p to $cpprog.
1137e6533d5Smrg  -s            $stripprog installed files.
11458105723Smrg  -S SUFFIX     attempt to back up existing files, with suffix SUFFIX.
1157e6533d5Smrg  -t DIRECTORY  install into DIRECTORY.
1167e6533d5Smrg  -T            report an error if DSTFILE is a directory.
117067610f1Smrg
118067610f1SmrgEnvironment variables override the default commands:
1197e6533d5Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
1207e6533d5Smrg  RMPROG STRIPPROG
12158105723Smrg
12258105723SmrgBy default, rm is invoked with -f; when overridden with RMPROG,
12358105723Smrgit's up to you to specify -f if you want it.
12458105723Smrg
12558105723SmrgIf -S is not specified, no backups are attempted.
12658105723Smrg
12758105723SmrgEmail bug reports to bug-automake@gnu.org.
12858105723SmrgAutomake home page: https://www.gnu.org/software/automake/
129067610f1Smrg"
130067610f1Smrg
131067610f1Smrgwhile test $# -ne 0; do
132067610f1Smrg  case $1 in
1337e6533d5Smrg    -c) ;;
1347e6533d5Smrg
1357e6533d5Smrg    -C) copy_on_change=true;;
136067610f1Smrg
1377e6533d5Smrg    -d) dir_arg=true;;
138067610f1Smrg
139067610f1Smrg    -g) chgrpcmd="$chgrpprog $2"
1404d9306dbSmrg        shift;;
141067610f1Smrg
142067610f1Smrg    --help) echo "$usage"; exit $?;;
143067610f1Smrg
144067610f1Smrg    -m) mode=$2
1454d9306dbSmrg        case $mode in
1464d9306dbSmrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
1474d9306dbSmrg            echo "$0: invalid mode: $mode" >&2
1484d9306dbSmrg            exit 1;;
1494d9306dbSmrg        esac
1504d9306dbSmrg        shift;;
151067610f1Smrg
152067610f1Smrg    -o) chowncmd="$chownprog $2"
1534d9306dbSmrg        shift;;
154067610f1Smrg
15558105723Smrg    -p) cpprog="$cpprog -p";;
15658105723Smrg
1577e6533d5Smrg    -s) stripcmd=$stripprog;;
158067610f1Smrg
15958105723Smrg    -S) backupsuffix="$2"
16058105723Smrg        shift;;
16158105723Smrg
1624d9306dbSmrg    -t)
1634d9306dbSmrg        is_target_a_directory=always
1644d9306dbSmrg        dst_arg=$2
1654d9306dbSmrg        # Protect names problematic for 'test' and other utilities.
1664d9306dbSmrg        case $dst_arg in
1674d9306dbSmrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
1684d9306dbSmrg        esac
1694d9306dbSmrg        shift;;
170067610f1Smrg
1714d9306dbSmrg    -T) is_target_a_directory=never;;
172067610f1Smrg
173067610f1Smrg    --version) echo "$0 $scriptversion"; exit $?;;
174067610f1Smrg
1754d9306dbSmrg    --) shift
1764d9306dbSmrg        break;;
177067610f1Smrg
1784d9306dbSmrg    -*) echo "$0: invalid option: $1" >&2
1794d9306dbSmrg        exit 1;;
180067610f1Smrg
181067610f1Smrg    *)  break;;
182067610f1Smrg  esac
1837e6533d5Smrg  shift
184067610f1Smrgdone
185067610f1Smrg
1864d9306dbSmrg# We allow the use of options -d and -T together, by making -d
1874d9306dbSmrg# take the precedence; this is for compatibility with GNU install.
1884d9306dbSmrg
1894d9306dbSmrgif test -n "$dir_arg"; then
1904d9306dbSmrg  if test -n "$dst_arg"; then
1914d9306dbSmrg    echo "$0: target directory not allowed when installing a directory." >&2
1924d9306dbSmrg    exit 1
1934d9306dbSmrg  fi
1944d9306dbSmrgfi
1954d9306dbSmrg
1967e6533d5Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
197067610f1Smrg  # When -d is used, all remaining arguments are directories to create.
198067610f1Smrg  # When -t is used, the destination is already specified.
199067610f1Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
200067610f1Smrg  for arg
201067610f1Smrg  do
2027e6533d5Smrg    if test -n "$dst_arg"; then
203067610f1Smrg      # $@ is not empty: it contains at least $arg.
2047e6533d5Smrg      set fnord "$@" "$dst_arg"
205067610f1Smrg      shift # fnord
206067610f1Smrg    fi
207067610f1Smrg    shift # arg
2087e6533d5Smrg    dst_arg=$arg
209e7fdea5fSmrg    # Protect names problematic for 'test' and other utilities.
210aea1d7ffSmrg    case $dst_arg in
211aea1d7ffSmrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
212aea1d7ffSmrg    esac
213067610f1Smrg  done
214067610f1Smrgfi
215067610f1Smrg
216067610f1Smrgif test $# -eq 0; then
217067610f1Smrg  if test -z "$dir_arg"; then
218067610f1Smrg    echo "$0: no input file specified." >&2
219067610f1Smrg    exit 1
220067610f1Smrg  fi
221e7fdea5fSmrg  # It's OK to call 'install-sh -d' without argument.
222067610f1Smrg  # This can happen when creating conditional directories.
223067610f1Smrg  exit 0
224067610f1Smrgfi
225067610f1Smrg
2264d9306dbSmrgif test -z "$dir_arg"; then
2274d9306dbSmrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
2284d9306dbSmrg    if test ! -d "$dst_arg"; then
2294d9306dbSmrg      echo "$0: $dst_arg: Is not a directory." >&2
2304d9306dbSmrg      exit 1
2314d9306dbSmrg    fi
2324d9306dbSmrg  fi
2334d9306dbSmrgfi
2344d9306dbSmrg
235067610f1Smrgif test -z "$dir_arg"; then
236aea1d7ffSmrg  do_exit='(exit $ret); exit $ret'
237aea1d7ffSmrg  trap "ret=129; $do_exit" 1
238aea1d7ffSmrg  trap "ret=130; $do_exit" 2
239aea1d7ffSmrg  trap "ret=141; $do_exit" 13
240aea1d7ffSmrg  trap "ret=143; $do_exit" 15
241067610f1Smrg
242067610f1Smrg  # Set umask so as not to create temps with too-generous modes.
243067610f1Smrg  # However, 'strip' requires both read and write access to temps.
244067610f1Smrg  case $mode in
245067610f1Smrg    # Optimize common cases.
246067610f1Smrg    *644) cp_umask=133;;
247067610f1Smrg    *755) cp_umask=22;;
248067610f1Smrg
249067610f1Smrg    *[0-7])
250067610f1Smrg      if test -z "$stripcmd"; then
2514d9306dbSmrg        u_plus_rw=
252067610f1Smrg      else
2534d9306dbSmrg        u_plus_rw='% 200'
254067610f1Smrg      fi
255067610f1Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
256067610f1Smrg    *)
257067610f1Smrg      if test -z "$stripcmd"; then
2584d9306dbSmrg        u_plus_rw=
259067610f1Smrg      else
2604d9306dbSmrg        u_plus_rw=,u+rw
261067610f1Smrg      fi
262067610f1Smrg      cp_umask=$mode$u_plus_rw;;
263067610f1Smrg  esac
264067610f1Smrgfi
265067610f1Smrg
266067610f1Smrgfor src
267067610f1Smrgdo
268e7fdea5fSmrg  # Protect names problematic for 'test' and other utilities.
269067610f1Smrg  case $src in
270aea1d7ffSmrg    -* | [=\(\)!]) src=./$src;;
271067610f1Smrg  esac
272067610f1Smrg
273067610f1Smrg  if test -n "$dir_arg"; then
274067610f1Smrg    dst=$src
275067610f1Smrg    dstdir=$dst
276067610f1Smrg    test -d "$dstdir"
277067610f1Smrg    dstdir_status=$?
27858105723Smrg    # Don't chown directories that already exist.
27958105723Smrg    if test $dstdir_status = 0; then
28058105723Smrg      chowncmd=""
28158105723Smrg    fi
282067610f1Smrg  else
283067610f1Smrg
284067610f1Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
285067610f1Smrg    # might cause directories to be created, which would be especially bad
286067610f1Smrg    # if $src (and thus $dsttmp) contains '*'.
287067610f1Smrg    if test ! -f "$src" && test ! -d "$src"; then
288067610f1Smrg      echo "$0: $src does not exist." >&2
289067610f1Smrg      exit 1
290067610f1Smrg    fi
291067610f1Smrg
2927e6533d5Smrg    if test -z "$dst_arg"; then
293067610f1Smrg      echo "$0: no destination specified." >&2
294067610f1Smrg      exit 1
295067610f1Smrg    fi
2967e6533d5Smrg    dst=$dst_arg
297067610f1Smrg
29858105723Smrg    # If destination is a directory, append the input filename.
299067610f1Smrg    if test -d "$dst"; then
3004d9306dbSmrg      if test "$is_target_a_directory" = never; then
3014d9306dbSmrg        echo "$0: $dst_arg: Is a directory" >&2
3024d9306dbSmrg        exit 1
303067610f1Smrg      fi
304067610f1Smrg      dstdir=$dst
30558105723Smrg      dstbase=`basename "$src"`
30658105723Smrg      case $dst in
30758105723Smrg	*/) dst=$dst$dstbase;;
30858105723Smrg	*)  dst=$dst/$dstbase;;
30958105723Smrg      esac
310067610f1Smrg      dstdir_status=0
311067610f1Smrg    else
3124d9306dbSmrg      dstdir=`dirname "$dst"`
313067610f1Smrg      test -d "$dstdir"
314067610f1Smrg      dstdir_status=$?
315067610f1Smrg    fi
316067610f1Smrg  fi
317067610f1Smrg
31858105723Smrg  case $dstdir in
31958105723Smrg    */) dstdirslash=$dstdir;;
32058105723Smrg    *)  dstdirslash=$dstdir/;;
32158105723Smrg  esac
32258105723Smrg
323067610f1Smrg  obsolete_mkdir_used=false
324067610f1Smrg
325067610f1Smrg  if test $dstdir_status != 0; then
326067610f1Smrg    case $posix_mkdir in
327067610f1Smrg      '')
3284d9306dbSmrg        # With -d, create the new directory with the user-specified mode.
3294d9306dbSmrg        # Otherwise, rely on $mkdir_umask.
3304d9306dbSmrg        if test -n "$dir_arg"; then
3314d9306dbSmrg          mkdir_mode=-m$mode
3324d9306dbSmrg        else
3334d9306dbSmrg          mkdir_mode=
3344d9306dbSmrg        fi
3354d9306dbSmrg
3364d9306dbSmrg        posix_mkdir=false
33758105723Smrg	# The $RANDOM variable is not portable (e.g., dash).  Use it
33858105723Smrg	# here however when possible just to lower collision chance.
33958105723Smrg	tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
34058105723Smrg
34158105723Smrg	trap '
34258105723Smrg	  ret=$?
34358105723Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null
34458105723Smrg	  exit $ret
34558105723Smrg	' 0
34658105723Smrg
34758105723Smrg	# Because "mkdir -p" follows existing symlinks and we likely work
34858105723Smrg	# directly in world-writeable /tmp, make sure that the '$tmpdir'
34958105723Smrg	# directory is successfully created first before we actually test
35058105723Smrg	# 'mkdir -p'.
35158105723Smrg	if (umask $mkdir_umask &&
35258105723Smrg	    $mkdirprog $mkdir_mode "$tmpdir" &&
35358105723Smrg	    exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
35458105723Smrg	then
35558105723Smrg	  if test -z "$dir_arg" || {
35658105723Smrg	       # Check for POSIX incompatibilities with -m.
35758105723Smrg	       # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
35858105723Smrg	       # other-writable bit of parent directory when it shouldn't.
35958105723Smrg	       # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
36058105723Smrg	       test_tmpdir="$tmpdir/a"
36158105723Smrg	       ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
36258105723Smrg	       case $ls_ld_tmpdir in
36358105723Smrg		 d????-?r-*) different_mode=700;;
36458105723Smrg		 d????-?--*) different_mode=755;;
36558105723Smrg		 *) false;;
36658105723Smrg	       esac &&
36758105723Smrg	       $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
36858105723Smrg		 ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
36958105723Smrg		 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
37058105723Smrg	       }
37158105723Smrg	     }
37258105723Smrg	  then posix_mkdir=:
37358105723Smrg	  fi
37458105723Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
37558105723Smrg	else
37658105723Smrg	  # Remove any dirs left behind by ancient mkdir implementations.
37758105723Smrg	  rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
37858105723Smrg	fi
37958105723Smrg	trap '' 0;;
380067610f1Smrg    esac
381067610f1Smrg
382067610f1Smrg    if
383067610f1Smrg      $posix_mkdir && (
3844d9306dbSmrg        umask $mkdir_umask &&
3854d9306dbSmrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
386067610f1Smrg      )
387067610f1Smrg    then :
388067610f1Smrg    else
389067610f1Smrg
39058105723Smrg      # mkdir does not conform to POSIX,
391067610f1Smrg      # or it failed possibly due to a race condition.  Create the
392067610f1Smrg      # directory the slow way, step by step, checking for races as we go.
393067610f1Smrg
394067610f1Smrg      case $dstdir in
3954d9306dbSmrg        /*) prefix='/';;
3964d9306dbSmrg        [-=\(\)!]*) prefix='./';;
3974d9306dbSmrg        *)  prefix='';;
398067610f1Smrg      esac
399067610f1Smrg
400067610f1Smrg      oIFS=$IFS
401067610f1Smrg      IFS=/
4024d9306dbSmrg      set -f
403067610f1Smrg      set fnord $dstdir
404067610f1Smrg      shift
4054d9306dbSmrg      set +f
406067610f1Smrg      IFS=$oIFS
407067610f1Smrg
408067610f1Smrg      prefixes=
409067610f1Smrg
410067610f1Smrg      for d
411067610f1Smrg      do
4124d9306dbSmrg        test X"$d" = X && continue
4134d9306dbSmrg
4144d9306dbSmrg        prefix=$prefix$d
4154d9306dbSmrg        if test -d "$prefix"; then
4164d9306dbSmrg          prefixes=
4174d9306dbSmrg        else
4184d9306dbSmrg          if $posix_mkdir; then
41958105723Smrg            (umask $mkdir_umask &&
4204d9306dbSmrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
4214d9306dbSmrg            # Don't fail if two instances are running concurrently.
4224d9306dbSmrg            test -d "$prefix" || exit 1
4234d9306dbSmrg          else
4244d9306dbSmrg            case $prefix in
4254d9306dbSmrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
4264d9306dbSmrg              *) qprefix=$prefix;;
4274d9306dbSmrg            esac
4284d9306dbSmrg            prefixes="$prefixes '$qprefix'"
4294d9306dbSmrg          fi
4304d9306dbSmrg        fi
4314d9306dbSmrg        prefix=$prefix/
432067610f1Smrg      done
433067610f1Smrg
434067610f1Smrg      if test -n "$prefixes"; then
4354d9306dbSmrg        # Don't fail if two instances are running concurrently.
4364d9306dbSmrg        (umask $mkdir_umask &&
4374d9306dbSmrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
4384d9306dbSmrg          test -d "$dstdir" || exit 1
4394d9306dbSmrg        obsolete_mkdir_used=true
440067610f1Smrg      fi
441067610f1Smrg    fi
442067610f1Smrg  fi
443067610f1Smrg
444067610f1Smrg  if test -n "$dir_arg"; then
445067610f1Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
446067610f1Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
447067610f1Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
448067610f1Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
449067610f1Smrg  else
450067610f1Smrg
451067610f1Smrg    # Make a couple of temp file names in the proper directory.
45258105723Smrg    dsttmp=${dstdirslash}_inst.$$_
45358105723Smrg    rmtmp=${dstdirslash}_rm.$$_
454067610f1Smrg
455067610f1Smrg    # Trap to clean up those temp files at exit.
456067610f1Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
457067610f1Smrg
458067610f1Smrg    # Copy the file name to the temp name.
45958105723Smrg    (umask $cp_umask &&
46058105723Smrg     { test -z "$stripcmd" || {
46158105723Smrg	 # Create $dsttmp read-write so that cp doesn't create it read-only,
46258105723Smrg	 # which would cause strip to fail.
46358105723Smrg	 if test -z "$doit"; then
46458105723Smrg	   : >"$dsttmp" # No need to fork-exec 'touch'.
46558105723Smrg	 else
46658105723Smrg	   $doit touch "$dsttmp"
46758105723Smrg	 fi
46858105723Smrg       }
46958105723Smrg     } &&
47058105723Smrg     $doit_exec $cpprog "$src" "$dsttmp") &&
471067610f1Smrg
472067610f1Smrg    # and set any options; do chmod last to preserve setuid bits.
473067610f1Smrg    #
474067610f1Smrg    # If any of these fail, we abort the whole thing.  If we want to
475067610f1Smrg    # ignore errors from any of these, just make sure not to ignore
476067610f1Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
477067610f1Smrg    #
4787e6533d5Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
4797e6533d5Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
4807e6533d5Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
4817e6533d5Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4827e6533d5Smrg
4837e6533d5Smrg    # If -C, don't bother to copy if it wouldn't change the file.
4847e6533d5Smrg    if $copy_on_change &&
4854d9306dbSmrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
4864d9306dbSmrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
4874d9306dbSmrg       set -f &&
4887e6533d5Smrg       set X $old && old=:$2:$4:$5:$6 &&
4897e6533d5Smrg       set X $new && new=:$2:$4:$5:$6 &&
4904d9306dbSmrg       set +f &&
4917e6533d5Smrg       test "$old" = "$new" &&
4927e6533d5Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
4937e6533d5Smrg    then
4947e6533d5Smrg      rm -f "$dsttmp"
4957e6533d5Smrg    else
49658105723Smrg      # If $backupsuffix is set, and the file being installed
49758105723Smrg      # already exists, attempt a backup.  Don't worry if it fails,
49858105723Smrg      # e.g., if mv doesn't support -f.
49958105723Smrg      if test -n "$backupsuffix" && test -f "$dst"; then
50058105723Smrg        $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null
50158105723Smrg      fi
50258105723Smrg
5037e6533d5Smrg      # Rename the file to the real destination.
5047e6533d5Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
5057e6533d5Smrg
5067e6533d5Smrg      # The rename failed, perhaps because mv can't rename something else
5077e6533d5Smrg      # to itself, or perhaps because mv is so ancient that it does not
5087e6533d5Smrg      # support -f.
5097e6533d5Smrg      {
5104d9306dbSmrg        # Now remove or move aside any old file at destination location.
5114d9306dbSmrg        # We try this two ways since rm can't unlink itself on some
5124d9306dbSmrg        # systems and the destination file might be busy for other
5134d9306dbSmrg        # reasons.  In this case, the final cleanup might fail but the new
5144d9306dbSmrg        # file should still install successfully.
5154d9306dbSmrg        {
5164d9306dbSmrg          test ! -f "$dst" ||
51758105723Smrg          $doit $rmcmd "$dst" 2>/dev/null ||
5184d9306dbSmrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
51958105723Smrg            { $doit $rmcmd "$rmtmp" 2>/dev/null; :; }
5204d9306dbSmrg          } ||
5214d9306dbSmrg          { echo "$0: cannot unlink or rename $dst" >&2
5224d9306dbSmrg            (exit 1); exit 1
5234d9306dbSmrg          }
5244d9306dbSmrg        } &&
5254d9306dbSmrg
5264d9306dbSmrg        # Now rename the file to the real destination.
5274d9306dbSmrg        $doit $mvcmd "$dsttmp" "$dst"
5287e6533d5Smrg      }
5297e6533d5Smrg    fi || exit 1
530067610f1Smrg
531067610f1Smrg    trap '' 0
532067610f1Smrg  fi
533067610f1Smrgdone
534067610f1Smrg
535067610f1Smrg# Local variables:
53658105723Smrg# eval: (add-hook 'before-save-hook 'time-stamp)
537067610f1Smrg# time-stamp-start: "scriptversion="
538067610f1Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
53958105723Smrg# time-stamp-time-zone: "UTC0"
5407e6533d5Smrg# time-stamp-end: "; # UTC"
541067610f1Smrg# End:
542