1a96d7823Smrg#!/bin/sh
2a96d7823Smrg# install - install a program, script, or datafile
3a96d7823Smrg
46a46240fSmrgscriptversion=2024-06-19.01; # UTC
5a96d7823Smrg
6a96d7823Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7a96d7823Smrg# later released in X11R6 (xc/config/util/install.sh) with the
8a96d7823Smrg# following copyright and license.
9a96d7823Smrg#
10a96d7823Smrg# Copyright (C) 1994 X Consortium
11a96d7823Smrg#
12a96d7823Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13a96d7823Smrg# of this software and associated documentation files (the "Software"), to
14a96d7823Smrg# deal in the Software without restriction, including without limitation the
15a96d7823Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16a96d7823Smrg# sell copies of the Software, and to permit persons to whom the Software is
17a96d7823Smrg# furnished to do so, subject to the following conditions:
18a96d7823Smrg#
19a96d7823Smrg# The above copyright notice and this permission notice shall be included in
20a96d7823Smrg# all copies or substantial portions of the Software.
21a96d7823Smrg#
22a96d7823Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23a96d7823Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24a96d7823Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25a96d7823Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26a96d7823Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27a96d7823Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28a96d7823Smrg#
29a96d7823Smrg# Except as contained in this notice, the name of the X Consortium shall not
30a96d7823Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
31a96d7823Smrg# ings in this Software without prior written authorization from the X Consor-
32a96d7823Smrg# tium.
33a96d7823Smrg#
34a96d7823Smrg#
35a96d7823Smrg# FSF changes to this file are in the public domain.
36a96d7823Smrg#
37a96d7823Smrg# Calling this script install-sh is preferred over install.sh, to prevent
38a96d7823Smrg# 'make' implicit rules from creating a file called install from it
39a96d7823Smrg# when there is no Makefile.
40a96d7823Smrg#
41a96d7823Smrg# This script is compatible with the BSD install script, but was written
42a96d7823Smrg# from scratch.
43a96d7823Smrg
44a96d7823Smrgtab='	'
45a96d7823Smrgnl='
46a96d7823Smrg'
47a96d7823SmrgIFS=" $tab$nl"
48a96d7823Smrg
49a96d7823Smrg# Set DOITPROG to "echo" to test this script.
50a96d7823Smrg
51a96d7823Smrgdoit=${DOITPROG-}
52a96d7823Smrgdoit_exec=${doit:-exec}
53a96d7823Smrg
54a96d7823Smrg# Put in absolute file names if you don't have them in your path;
55a96d7823Smrg# or use environment vars.
56a96d7823Smrg
57a96d7823Smrgchgrpprog=${CHGRPPROG-chgrp}
58a96d7823Smrgchmodprog=${CHMODPROG-chmod}
59a96d7823Smrgchownprog=${CHOWNPROG-chown}
60a96d7823Smrgcmpprog=${CMPPROG-cmp}
61a96d7823Smrgcpprog=${CPPROG-cp}
62a96d7823Smrgmkdirprog=${MKDIRPROG-mkdir}
63a96d7823Smrgmvprog=${MVPROG-mv}
64a96d7823Smrgrmprog=${RMPROG-rm}
65a96d7823Smrgstripprog=${STRIPPROG-strip}
66a96d7823Smrg
67a96d7823Smrgposix_mkdir=
68a96d7823Smrg
69a96d7823Smrg# Desired mode of installed file.
70a96d7823Smrgmode=0755
71a96d7823Smrg
7260da515cSmrg# Create dirs (including intermediate dirs) using mode 755.
7360da515cSmrg# This is like GNU 'install' as of coreutils 8.32 (2020).
7460da515cSmrgmkdir_umask=22
7560da515cSmrg
7660da515cSmrgbackupsuffix=
77a96d7823Smrgchgrpcmd=
78a96d7823Smrgchmodcmd=$chmodprog
79a96d7823Smrgchowncmd=
80a96d7823Smrgmvcmd=$mvprog
81a96d7823Smrgrmcmd="$rmprog -f"
82a96d7823Smrgstripcmd=
83a96d7823Smrg
84a96d7823Smrgsrc=
85a96d7823Smrgdst=
86a96d7823Smrgdir_arg=
87a96d7823Smrgdst_arg=
88a96d7823Smrg
89a96d7823Smrgcopy_on_change=false
90a96d7823Smrgis_target_a_directory=possibly
91a96d7823Smrg
92a96d7823Smrgusage="\
93a96d7823SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
94a96d7823Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
95a96d7823Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
96a96d7823Smrg   or: $0 [OPTION]... -d DIRECTORIES...
97a96d7823Smrg
98a96d7823SmrgIn the 1st form, copy SRCFILE to DSTFILE.
99a96d7823SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
100a96d7823SmrgIn the 4th, create DIRECTORIES.
101a96d7823Smrg
102a96d7823SmrgOptions:
103a96d7823Smrg     --help     display this help and exit.
104a96d7823Smrg     --version  display version info and exit.
105a96d7823Smrg
106a96d7823Smrg  -c            (ignored)
10760da515cSmrg  -C            install only if different (preserve data modification time)
108a96d7823Smrg  -d            create directories instead of installing files.
109a96d7823Smrg  -g GROUP      $chgrpprog installed files to GROUP.
110a96d7823Smrg  -m MODE       $chmodprog installed files to MODE.
111a96d7823Smrg  -o USER       $chownprog installed files to USER.
11260da515cSmrg  -p            pass -p to $cpprog.
113a96d7823Smrg  -s            $stripprog installed files.
11460da515cSmrg  -S SUFFIX     attempt to back up existing files, with suffix SUFFIX.
115a96d7823Smrg  -t DIRECTORY  install into DIRECTORY.
116a96d7823Smrg  -T            report an error if DSTFILE is a directory.
117a96d7823Smrg
118a96d7823SmrgEnvironment variables override the default commands:
119a96d7823Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
120a96d7823Smrg  RMPROG STRIPPROG
12160da515cSmrg
12260da515cSmrgBy default, rm is invoked with -f; when overridden with RMPROG,
12360da515cSmrgit's up to you to specify -f if you want it.
12460da515cSmrg
12560da515cSmrgIf -S is not specified, no backups are attempted.
12660da515cSmrg
1276a46240fSmrgReport bugs to <bug-automake@gnu.org>.
1286a46240fSmrgGNU Automake home page: <https://www.gnu.org/software/automake/>.
1296a46240fSmrgGeneral help using GNU software: <https://www.gnu.org/gethelp/>."
130a96d7823Smrg
131a96d7823Smrgwhile test $# -ne 0; do
132a96d7823Smrg  case $1 in
133a96d7823Smrg    -c) ;;
134a96d7823Smrg
135a96d7823Smrg    -C) copy_on_change=true;;
136a96d7823Smrg
137a96d7823Smrg    -d) dir_arg=true;;
138a96d7823Smrg
139a96d7823Smrg    -g) chgrpcmd="$chgrpprog $2"
140a96d7823Smrg        shift;;
141a96d7823Smrg
142a96d7823Smrg    --help) echo "$usage"; exit $?;;
143a96d7823Smrg
144a96d7823Smrg    -m) mode=$2
145a96d7823Smrg        case $mode in
146a96d7823Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
147a96d7823Smrg            echo "$0: invalid mode: $mode" >&2
148a96d7823Smrg            exit 1;;
149a96d7823Smrg        esac
150a96d7823Smrg        shift;;
151a96d7823Smrg
152a96d7823Smrg    -o) chowncmd="$chownprog $2"
153a96d7823Smrg        shift;;
154a96d7823Smrg
15560da515cSmrg    -p) cpprog="$cpprog -p";;
15660da515cSmrg
157a96d7823Smrg    -s) stripcmd=$stripprog;;
158a96d7823Smrg
15960da515cSmrg    -S) backupsuffix="$2"
16060da515cSmrg        shift;;
16160da515cSmrg
162a96d7823Smrg    -t)
163a96d7823Smrg        is_target_a_directory=always
164a96d7823Smrg        dst_arg=$2
165a96d7823Smrg        # Protect names problematic for 'test' and other utilities.
166a96d7823Smrg        case $dst_arg in
167a96d7823Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
168a96d7823Smrg        esac
169a96d7823Smrg        shift;;
170a96d7823Smrg
171a96d7823Smrg    -T) is_target_a_directory=never;;
172a96d7823Smrg
1736a46240fSmrg    --version) echo "$0 (GNU Automake) $scriptversion"; exit $?;;
174a96d7823Smrg
175a96d7823Smrg    --) shift
176a96d7823Smrg        break;;
177a96d7823Smrg
178a96d7823Smrg    -*) echo "$0: invalid option: $1" >&2
179a96d7823Smrg        exit 1;;
180a96d7823Smrg
181a96d7823Smrg    *)  break;;
182a96d7823Smrg  esac
183a96d7823Smrg  shift
184a96d7823Smrgdone
185a96d7823Smrg
186a96d7823Smrg# We allow the use of options -d and -T together, by making -d
187a96d7823Smrg# take the precedence; this is for compatibility with GNU install.
188a96d7823Smrg
189a96d7823Smrgif test -n "$dir_arg"; then
190a96d7823Smrg  if test -n "$dst_arg"; then
191a96d7823Smrg    echo "$0: target directory not allowed when installing a directory." >&2
192a96d7823Smrg    exit 1
193a96d7823Smrg  fi
194a96d7823Smrgfi
195a96d7823Smrg
196a96d7823Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
197a96d7823Smrg  # When -d is used, all remaining arguments are directories to create.
198a96d7823Smrg  # When -t is used, the destination is already specified.
199a96d7823Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
200a96d7823Smrg  for arg
201a96d7823Smrg  do
202a96d7823Smrg    if test -n "$dst_arg"; then
203a96d7823Smrg      # $@ is not empty: it contains at least $arg.
204a96d7823Smrg      set fnord "$@" "$dst_arg"
205a96d7823Smrg      shift # fnord
206a96d7823Smrg    fi
207a96d7823Smrg    shift # arg
208a96d7823Smrg    dst_arg=$arg
209a96d7823Smrg    # Protect names problematic for 'test' and other utilities.
210a96d7823Smrg    case $dst_arg in
211a96d7823Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
212a96d7823Smrg    esac
213a96d7823Smrg  done
214a96d7823Smrgfi
215a96d7823Smrg
216a96d7823Smrgif test $# -eq 0; then
217a96d7823Smrg  if test -z "$dir_arg"; then
218a96d7823Smrg    echo "$0: no input file specified." >&2
219a96d7823Smrg    exit 1
220a96d7823Smrg  fi
221a96d7823Smrg  # It's OK to call 'install-sh -d' without argument.
222a96d7823Smrg  # This can happen when creating conditional directories.
223a96d7823Smrg  exit 0
224a96d7823Smrgfi
225a96d7823Smrg
226a96d7823Smrgif test -z "$dir_arg"; then
227a96d7823Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
228a96d7823Smrg    if test ! -d "$dst_arg"; then
229a96d7823Smrg      echo "$0: $dst_arg: Is not a directory." >&2
230a96d7823Smrg      exit 1
231a96d7823Smrg    fi
232a96d7823Smrg  fi
233a96d7823Smrgfi
234a96d7823Smrg
235a96d7823Smrgif test -z "$dir_arg"; then
236a96d7823Smrg  do_exit='(exit $ret); exit $ret'
237a96d7823Smrg  trap "ret=129; $do_exit" 1
238a96d7823Smrg  trap "ret=130; $do_exit" 2
239a96d7823Smrg  trap "ret=141; $do_exit" 13
240a96d7823Smrg  trap "ret=143; $do_exit" 15
241a96d7823Smrg
242a96d7823Smrg  # Set umask so as not to create temps with too-generous modes.
243a96d7823Smrg  # However, 'strip' requires both read and write access to temps.
244a96d7823Smrg  case $mode in
245a96d7823Smrg    # Optimize common cases.
246a96d7823Smrg    *644) cp_umask=133;;
247a96d7823Smrg    *755) cp_umask=22;;
248a96d7823Smrg
249a96d7823Smrg    *[0-7])
250a96d7823Smrg      if test -z "$stripcmd"; then
251a96d7823Smrg        u_plus_rw=
252a96d7823Smrg      else
253a96d7823Smrg        u_plus_rw='% 200'
254a96d7823Smrg      fi
255a96d7823Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
256a96d7823Smrg    *)
257a96d7823Smrg      if test -z "$stripcmd"; then
258a96d7823Smrg        u_plus_rw=
259a96d7823Smrg      else
260a96d7823Smrg        u_plus_rw=,u+rw
261a96d7823Smrg      fi
262a96d7823Smrg      cp_umask=$mode$u_plus_rw;;
263a96d7823Smrg  esac
264a96d7823Smrgfi
265a96d7823Smrg
266a96d7823Smrgfor src
267a96d7823Smrgdo
268a96d7823Smrg  # Protect names problematic for 'test' and other utilities.
269a96d7823Smrg  case $src in
270a96d7823Smrg    -* | [=\(\)!]) src=./$src;;
271a96d7823Smrg  esac
272a96d7823Smrg
273a96d7823Smrg  if test -n "$dir_arg"; then
274a96d7823Smrg    dst=$src
275a96d7823Smrg    dstdir=$dst
276a96d7823Smrg    test -d "$dstdir"
277a96d7823Smrg    dstdir_status=$?
27860da515cSmrg    # Don't chown directories that already exist.
27960da515cSmrg    if test $dstdir_status = 0; then
28060da515cSmrg      chowncmd=""
28160da515cSmrg    fi
282a96d7823Smrg  else
283a96d7823Smrg
284a96d7823Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
285a96d7823Smrg    # might cause directories to be created, which would be especially bad
286a96d7823Smrg    # if $src (and thus $dsttmp) contains '*'.
287a96d7823Smrg    if test ! -f "$src" && test ! -d "$src"; then
288a96d7823Smrg      echo "$0: $src does not exist." >&2
289a96d7823Smrg      exit 1
290a96d7823Smrg    fi
291a96d7823Smrg
292a96d7823Smrg    if test -z "$dst_arg"; then
293a96d7823Smrg      echo "$0: no destination specified." >&2
294a96d7823Smrg      exit 1
295a96d7823Smrg    fi
296a96d7823Smrg    dst=$dst_arg
297a96d7823Smrg
29860da515cSmrg    # If destination is a directory, append the input filename.
299a96d7823Smrg    if test -d "$dst"; then
300a96d7823Smrg      if test "$is_target_a_directory" = never; then
301a96d7823Smrg        echo "$0: $dst_arg: Is a directory" >&2
302a96d7823Smrg        exit 1
303a96d7823Smrg      fi
304a96d7823Smrg      dstdir=$dst
30560da515cSmrg      dstbase=`basename "$src"`
30660da515cSmrg      case $dst in
30760da515cSmrg	*/) dst=$dst$dstbase;;
30860da515cSmrg	*)  dst=$dst/$dstbase;;
30960da515cSmrg      esac
310a96d7823Smrg      dstdir_status=0
311a96d7823Smrg    else
312a96d7823Smrg      dstdir=`dirname "$dst"`
313a96d7823Smrg      test -d "$dstdir"
314a96d7823Smrg      dstdir_status=$?
315a96d7823Smrg    fi
316a96d7823Smrg  fi
317a96d7823Smrg
31860da515cSmrg  case $dstdir in
31960da515cSmrg    */) dstdirslash=$dstdir;;
32060da515cSmrg    *)  dstdirslash=$dstdir/;;
32160da515cSmrg  esac
32260da515cSmrg
323a96d7823Smrg  obsolete_mkdir_used=false
324a96d7823Smrg
325a96d7823Smrg  if test $dstdir_status != 0; then
326a96d7823Smrg    case $posix_mkdir in
327a96d7823Smrg      '')
328a96d7823Smrg        # With -d, create the new directory with the user-specified mode.
329a96d7823Smrg        # Otherwise, rely on $mkdir_umask.
330a96d7823Smrg        if test -n "$dir_arg"; then
331a96d7823Smrg          mkdir_mode=-m$mode
332a96d7823Smrg        else
333a96d7823Smrg          mkdir_mode=
334a96d7823Smrg        fi
335a96d7823Smrg
336a96d7823Smrg        posix_mkdir=false
33760da515cSmrg	# The $RANDOM variable is not portable (e.g., dash).  Use it
33860da515cSmrg	# here however when possible just to lower collision chance.
33960da515cSmrg	tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
34060da515cSmrg
34160da515cSmrg	trap '
34260da515cSmrg	  ret=$?
34360da515cSmrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null
34460da515cSmrg	  exit $ret
34560da515cSmrg	' 0
34660da515cSmrg
34760da515cSmrg	# Because "mkdir -p" follows existing symlinks and we likely work
3486a46240fSmrg	# directly in world-writable /tmp, make sure that the '$tmpdir'
34960da515cSmrg	# directory is successfully created first before we actually test
35060da515cSmrg	# 'mkdir -p'.
35160da515cSmrg	if (umask $mkdir_umask &&
35260da515cSmrg	    $mkdirprog $mkdir_mode "$tmpdir" &&
35360da515cSmrg	    exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
35460da515cSmrg	then
35560da515cSmrg	  if test -z "$dir_arg" || {
3566a46240fSmrg	       # Check for POSIX incompatibility with -m.
35760da515cSmrg	       # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
35860da515cSmrg	       # other-writable bit of parent directory when it shouldn't.
35960da515cSmrg	       # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
36060da515cSmrg	       test_tmpdir="$tmpdir/a"
36160da515cSmrg	       ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
36260da515cSmrg	       case $ls_ld_tmpdir in
36360da515cSmrg		 d????-?r-*) different_mode=700;;
36460da515cSmrg		 d????-?--*) different_mode=755;;
36560da515cSmrg		 *) false;;
36660da515cSmrg	       esac &&
36760da515cSmrg	       $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
36860da515cSmrg		 ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
36960da515cSmrg		 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
37060da515cSmrg	       }
37160da515cSmrg	     }
37260da515cSmrg	  then posix_mkdir=:
37360da515cSmrg	  fi
37460da515cSmrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
37560da515cSmrg	else
37660da515cSmrg	  # Remove any dirs left behind by ancient mkdir implementations.
37760da515cSmrg	  rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
37860da515cSmrg	fi
37960da515cSmrg	trap '' 0;;
380a96d7823Smrg    esac
381a96d7823Smrg
382a96d7823Smrg    if
383a96d7823Smrg      $posix_mkdir && (
384a96d7823Smrg        umask $mkdir_umask &&
385a96d7823Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
386a96d7823Smrg      )
387a96d7823Smrg    then :
388a96d7823Smrg    else
389a96d7823Smrg
39060da515cSmrg      # mkdir does not conform to POSIX,
391a96d7823Smrg      # or it failed possibly due to a race condition.  Create the
392a96d7823Smrg      # directory the slow way, step by step, checking for races as we go.
393a96d7823Smrg
394a96d7823Smrg      case $dstdir in
395a96d7823Smrg        /*) prefix='/';;
396a96d7823Smrg        [-=\(\)!]*) prefix='./';;
397a96d7823Smrg        *)  prefix='';;
398a96d7823Smrg      esac
399a96d7823Smrg
400a96d7823Smrg      oIFS=$IFS
401a96d7823Smrg      IFS=/
402a96d7823Smrg      set -f
403a96d7823Smrg      set fnord $dstdir
404a96d7823Smrg      shift
405a96d7823Smrg      set +f
406a96d7823Smrg      IFS=$oIFS
407a96d7823Smrg
408a96d7823Smrg      prefixes=
409a96d7823Smrg
410a96d7823Smrg      for d
411a96d7823Smrg      do
412a96d7823Smrg        test X"$d" = X && continue
413a96d7823Smrg
414a96d7823Smrg        prefix=$prefix$d
415a96d7823Smrg        if test -d "$prefix"; then
416a96d7823Smrg          prefixes=
417a96d7823Smrg        else
418a96d7823Smrg          if $posix_mkdir; then
41960da515cSmrg            (umask $mkdir_umask &&
420a96d7823Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
421a96d7823Smrg            # Don't fail if two instances are running concurrently.
422a96d7823Smrg            test -d "$prefix" || exit 1
423a96d7823Smrg          else
424a96d7823Smrg            case $prefix in
425a96d7823Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
426a96d7823Smrg              *) qprefix=$prefix;;
427a96d7823Smrg            esac
428a96d7823Smrg            prefixes="$prefixes '$qprefix'"
429a96d7823Smrg          fi
430a96d7823Smrg        fi
431a96d7823Smrg        prefix=$prefix/
432a96d7823Smrg      done
433a96d7823Smrg
434a96d7823Smrg      if test -n "$prefixes"; then
435a96d7823Smrg        # Don't fail if two instances are running concurrently.
436a96d7823Smrg        (umask $mkdir_umask &&
437a96d7823Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
438a96d7823Smrg          test -d "$dstdir" || exit 1
439a96d7823Smrg        obsolete_mkdir_used=true
440a96d7823Smrg      fi
441a96d7823Smrg    fi
442a96d7823Smrg  fi
443a96d7823Smrg
444a96d7823Smrg  if test -n "$dir_arg"; then
445a96d7823Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
446a96d7823Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
447a96d7823Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
448a96d7823Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
449a96d7823Smrg  else
450a96d7823Smrg
451a96d7823Smrg    # Make a couple of temp file names in the proper directory.
45260da515cSmrg    dsttmp=${dstdirslash}_inst.$$_
45360da515cSmrg    rmtmp=${dstdirslash}_rm.$$_
454a96d7823Smrg
455a96d7823Smrg    # Trap to clean up those temp files at exit.
456a96d7823Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
457a96d7823Smrg
458a96d7823Smrg    # Copy the file name to the temp name.
45960da515cSmrg    (umask $cp_umask &&
46060da515cSmrg     { test -z "$stripcmd" || {
46160da515cSmrg	 # Create $dsttmp read-write so that cp doesn't create it read-only,
46260da515cSmrg	 # which would cause strip to fail.
46360da515cSmrg	 if test -z "$doit"; then
46460da515cSmrg	   : >"$dsttmp" # No need to fork-exec 'touch'.
46560da515cSmrg	 else
46660da515cSmrg	   $doit touch "$dsttmp"
46760da515cSmrg	 fi
46860da515cSmrg       }
46960da515cSmrg     } &&
47060da515cSmrg     $doit_exec $cpprog "$src" "$dsttmp") &&
471a96d7823Smrg
472a96d7823Smrg    # and set any options; do chmod last to preserve setuid bits.
473a96d7823Smrg    #
474a96d7823Smrg    # If any of these fail, we abort the whole thing.  If we want to
475a96d7823Smrg    # ignore errors from any of these, just make sure not to ignore
476a96d7823Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
477a96d7823Smrg    #
478a96d7823Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
479a96d7823Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
480a96d7823Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
481a96d7823Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
482a96d7823Smrg
483a96d7823Smrg    # If -C, don't bother to copy if it wouldn't change the file.
484a96d7823Smrg    if $copy_on_change &&
485a96d7823Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
486a96d7823Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
487a96d7823Smrg       set -f &&
488a96d7823Smrg       set X $old && old=:$2:$4:$5:$6 &&
489a96d7823Smrg       set X $new && new=:$2:$4:$5:$6 &&
490a96d7823Smrg       set +f &&
491a96d7823Smrg       test "$old" = "$new" &&
492a96d7823Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
493a96d7823Smrg    then
494a96d7823Smrg      rm -f "$dsttmp"
495a96d7823Smrg    else
49660da515cSmrg      # If $backupsuffix is set, and the file being installed
49760da515cSmrg      # already exists, attempt a backup.  Don't worry if it fails,
49860da515cSmrg      # e.g., if mv doesn't support -f.
49960da515cSmrg      if test -n "$backupsuffix" && test -f "$dst"; then
50060da515cSmrg        $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null
50160da515cSmrg      fi
50260da515cSmrg
503a96d7823Smrg      # Rename the file to the real destination.
504a96d7823Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
505a96d7823Smrg
506a96d7823Smrg      # The rename failed, perhaps because mv can't rename something else
507a96d7823Smrg      # to itself, or perhaps because mv is so ancient that it does not
508a96d7823Smrg      # support -f.
509a96d7823Smrg      {
510a96d7823Smrg        # Now remove or move aside any old file at destination location.
511a96d7823Smrg        # We try this two ways since rm can't unlink itself on some
512a96d7823Smrg        # systems and the destination file might be busy for other
513a96d7823Smrg        # reasons.  In this case, the final cleanup might fail but the new
514a96d7823Smrg        # file should still install successfully.
515a96d7823Smrg        {
516a96d7823Smrg          test ! -f "$dst" ||
51760da515cSmrg          $doit $rmcmd "$dst" 2>/dev/null ||
518a96d7823Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
51960da515cSmrg            { $doit $rmcmd "$rmtmp" 2>/dev/null; :; }
520a96d7823Smrg          } ||
521a96d7823Smrg          { echo "$0: cannot unlink or rename $dst" >&2
522a96d7823Smrg            (exit 1); exit 1
523a96d7823Smrg          }
524a96d7823Smrg        } &&
525a96d7823Smrg
526a96d7823Smrg        # Now rename the file to the real destination.
527a96d7823Smrg        $doit $mvcmd "$dsttmp" "$dst"
528a96d7823Smrg      }
529a96d7823Smrg    fi || exit 1
530a96d7823Smrg
531a96d7823Smrg    trap '' 0
532a96d7823Smrg  fi
533a96d7823Smrgdone
534a96d7823Smrg
535a96d7823Smrg# Local variables:
53660da515cSmrg# eval: (add-hook 'before-save-hook 'time-stamp)
537a96d7823Smrg# time-stamp-start: "scriptversion="
538a96d7823Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
53960da515cSmrg# time-stamp-time-zone: "UTC0"
540a96d7823Smrg# time-stamp-end: "; # UTC"
541a96d7823Smrg# End:
542