1d1333322Smrg#!/bin/sh
2d1333322Smrg# install - install a program, script, or datafile
3d1333322Smrg
4742aa962Smrgscriptversion=2024-06-19.01; # UTC
5d1333322Smrg
6d1333322Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7d1333322Smrg# later released in X11R6 (xc/config/util/install.sh) with the
8d1333322Smrg# following copyright and license.
9d1333322Smrg#
10d1333322Smrg# Copyright (C) 1994 X Consortium
11d1333322Smrg#
12d1333322Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13d1333322Smrg# of this software and associated documentation files (the "Software"), to
14d1333322Smrg# deal in the Software without restriction, including without limitation the
15d1333322Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16d1333322Smrg# sell copies of the Software, and to permit persons to whom the Software is
17d1333322Smrg# furnished to do so, subject to the following conditions:
18d1333322Smrg#
19d1333322Smrg# The above copyright notice and this permission notice shall be included in
20d1333322Smrg# all copies or substantial portions of the Software.
21d1333322Smrg#
22d1333322Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23d1333322Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24d1333322Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25d1333322Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26d1333322Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27d1333322Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28d1333322Smrg#
29d1333322Smrg# Except as contained in this notice, the name of the X Consortium shall not
30d1333322Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
31d1333322Smrg# ings in this Software without prior written authorization from the X Consor-
32d1333322Smrg# tium.
33d1333322Smrg#
34d1333322Smrg#
35d1333322Smrg# FSF changes to this file are in the public domain.
36d1333322Smrg#
37d1333322Smrg# Calling this script install-sh is preferred over install.sh, to prevent
3869ef5f27Smrg# 'make' implicit rules from creating a file called install from it
39d1333322Smrg# when there is no Makefile.
40d1333322Smrg#
41d1333322Smrg# This script is compatible with the BSD install script, but was written
42d1333322Smrg# from scratch.
43d1333322Smrg
44d04472e1Smrgtab='	'
45d1333322Smrgnl='
46d1333322Smrg'
47d04472e1SmrgIFS=" $tab$nl"
48d1333322Smrg
49d04472e1Smrg# Set DOITPROG to "echo" to test this script.
50d1333322Smrg
514f9ac78aSmrgdoit=${DOITPROG-}
52d04472e1Smrgdoit_exec=${doit:-exec}
53d1333322Smrg
54d1333322Smrg# Put in absolute file names if you don't have them in your path;
55d1333322Smrg# or use environment vars.
56d1333322Smrg
574f9ac78aSmrgchgrpprog=${CHGRPPROG-chgrp}
584f9ac78aSmrgchmodprog=${CHMODPROG-chmod}
594f9ac78aSmrgchownprog=${CHOWNPROG-chown}
604f9ac78aSmrgcmpprog=${CMPPROG-cmp}
614f9ac78aSmrgcpprog=${CPPROG-cp}
624f9ac78aSmrgmkdirprog=${MKDIRPROG-mkdir}
634f9ac78aSmrgmvprog=${MVPROG-mv}
644f9ac78aSmrgrmprog=${RMPROG-rm}
654f9ac78aSmrgstripprog=${STRIPPROG-strip}
664f9ac78aSmrg
67d1333322Smrgposix_mkdir=
68d1333322Smrg
69d1333322Smrg# Desired mode of installed file.
70d1333322Smrgmode=0755
71d1333322Smrg
72d04472e1Smrg# Create dirs (including intermediate dirs) using mode 755.
73d04472e1Smrg# This is like GNU 'install' as of coreutils 8.32 (2020).
74d04472e1Smrgmkdir_umask=22
75d04472e1Smrg
76d04472e1Smrgbackupsuffix=
774f9ac78aSmrgchgrpcmd=
78d1333322Smrgchmodcmd=$chmodprog
79d1333322Smrgchowncmd=
804f9ac78aSmrgmvcmd=$mvprog
81d1333322Smrgrmcmd="$rmprog -f"
824f9ac78aSmrgstripcmd=
834f9ac78aSmrg
84d1333322Smrgsrc=
85d1333322Smrgdst=
86d1333322Smrgdir_arg=
874f9ac78aSmrgdst_arg=
884f9ac78aSmrg
894f9ac78aSmrgcopy_on_change=false
90d04472e1Smrgis_target_a_directory=possibly
91d1333322Smrg
924f9ac78aSmrgusage="\
934f9ac78aSmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
94d1333322Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
95d1333322Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
96d1333322Smrg   or: $0 [OPTION]... -d DIRECTORIES...
97d1333322Smrg
98d1333322SmrgIn the 1st form, copy SRCFILE to DSTFILE.
99d1333322SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
100d1333322SmrgIn the 4th, create DIRECTORIES.
101d1333322Smrg
102d1333322SmrgOptions:
1034f9ac78aSmrg     --help     display this help and exit.
1044f9ac78aSmrg     --version  display version info and exit.
1054f9ac78aSmrg
1064f9ac78aSmrg  -c            (ignored)
107d04472e1Smrg  -C            install only if different (preserve data modification time)
1084f9ac78aSmrg  -d            create directories instead of installing files.
1094f9ac78aSmrg  -g GROUP      $chgrpprog installed files to GROUP.
1104f9ac78aSmrg  -m MODE       $chmodprog installed files to MODE.
1114f9ac78aSmrg  -o USER       $chownprog installed files to USER.
112d04472e1Smrg  -p            pass -p to $cpprog.
1134f9ac78aSmrg  -s            $stripprog installed files.
114d04472e1Smrg  -S SUFFIX     attempt to back up existing files, with suffix SUFFIX.
1154f9ac78aSmrg  -t DIRECTORY  install into DIRECTORY.
1164f9ac78aSmrg  -T            report an error if DSTFILE is a directory.
117d1333322Smrg
118d1333322SmrgEnvironment variables override the default commands:
1194f9ac78aSmrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
1204f9ac78aSmrg  RMPROG STRIPPROG
121d04472e1Smrg
122d04472e1SmrgBy default, rm is invoked with -f; when overridden with RMPROG,
123d04472e1Smrgit's up to you to specify -f if you want it.
124d04472e1Smrg
125d04472e1SmrgIf -S is not specified, no backups are attempted.
126d04472e1Smrg
127742aa962SmrgReport bugs to <bug-automake@gnu.org>.
128742aa962SmrgGNU Automake home page: <https://www.gnu.org/software/automake/>.
129742aa962SmrgGeneral help using GNU software: <https://www.gnu.org/gethelp/>."
130d1333322Smrg
131d1333322Smrgwhile test $# -ne 0; do
132d1333322Smrg  case $1 in
1334f9ac78aSmrg    -c) ;;
1344f9ac78aSmrg
1354f9ac78aSmrg    -C) copy_on_change=true;;
136d1333322Smrg
1374f9ac78aSmrg    -d) dir_arg=true;;
138d1333322Smrg
139d1333322Smrg    -g) chgrpcmd="$chgrpprog $2"
140d04472e1Smrg        shift;;
141d1333322Smrg
142d1333322Smrg    --help) echo "$usage"; exit $?;;
143d1333322Smrg
144d1333322Smrg    -m) mode=$2
145d04472e1Smrg        case $mode in
146d04472e1Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
147d04472e1Smrg            echo "$0: invalid mode: $mode" >&2
148d04472e1Smrg            exit 1;;
149d04472e1Smrg        esac
150d04472e1Smrg        shift;;
151d1333322Smrg
152d1333322Smrg    -o) chowncmd="$chownprog $2"
153d04472e1Smrg        shift;;
154d04472e1Smrg
155d04472e1Smrg    -p) cpprog="$cpprog -p";;
156d1333322Smrg
1574f9ac78aSmrg    -s) stripcmd=$stripprog;;
158d1333322Smrg
159d04472e1Smrg    -S) backupsuffix="$2"
160d04472e1Smrg        shift;;
161d1333322Smrg
162d04472e1Smrg    -t)
163d04472e1Smrg        is_target_a_directory=always
164d04472e1Smrg        dst_arg=$2
165d04472e1Smrg        # Protect names problematic for 'test' and other utilities.
166d04472e1Smrg        case $dst_arg in
167d04472e1Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
168d04472e1Smrg        esac
169d04472e1Smrg        shift;;
170d04472e1Smrg
171d04472e1Smrg    -T) is_target_a_directory=never;;
172d1333322Smrg
173742aa962Smrg    --version) echo "$0 (GNU Automake) $scriptversion"; exit $?;;
174d1333322Smrg
175d04472e1Smrg    --) shift
176d04472e1Smrg        break;;
177d1333322Smrg
178d04472e1Smrg    -*) echo "$0: invalid option: $1" >&2
179d04472e1Smrg        exit 1;;
180d1333322Smrg
181d1333322Smrg    *)  break;;
182d1333322Smrg  esac
1834f9ac78aSmrg  shift
184d1333322Smrgdone
185d1333322Smrg
186d04472e1Smrg# We allow the use of options -d and -T together, by making -d
187d04472e1Smrg# take the precedence; this is for compatibility with GNU install.
188d04472e1Smrg
189d04472e1Smrgif test -n "$dir_arg"; then
190d04472e1Smrg  if test -n "$dst_arg"; then
191d04472e1Smrg    echo "$0: target directory not allowed when installing a directory." >&2
192d04472e1Smrg    exit 1
193d04472e1Smrg  fi
194d04472e1Smrgfi
195d04472e1Smrg
1964f9ac78aSmrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
197d1333322Smrg  # When -d is used, all remaining arguments are directories to create.
198d1333322Smrg  # When -t is used, the destination is already specified.
199d1333322Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
200d1333322Smrg  for arg
201d1333322Smrg  do
2024f9ac78aSmrg    if test -n "$dst_arg"; then
203d1333322Smrg      # $@ is not empty: it contains at least $arg.
2044f9ac78aSmrg      set fnord "$@" "$dst_arg"
205d1333322Smrg      shift # fnord
206d1333322Smrg    fi
207d1333322Smrg    shift # arg
2084f9ac78aSmrg    dst_arg=$arg
20969ef5f27Smrg    # Protect names problematic for 'test' and other utilities.
210c27291ebSmrg    case $dst_arg in
211c27291ebSmrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
212c27291ebSmrg    esac
213d1333322Smrg  done
214d1333322Smrgfi
215d1333322Smrg
216d1333322Smrgif test $# -eq 0; then
217d1333322Smrg  if test -z "$dir_arg"; then
218d1333322Smrg    echo "$0: no input file specified." >&2
219d1333322Smrg    exit 1
220d1333322Smrg  fi
22169ef5f27Smrg  # It's OK to call 'install-sh -d' without argument.
222d1333322Smrg  # This can happen when creating conditional directories.
223d1333322Smrg  exit 0
224d1333322Smrgfi
225d1333322Smrg
226d04472e1Smrgif test -z "$dir_arg"; then
227d04472e1Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
228d04472e1Smrg    if test ! -d "$dst_arg"; then
229d04472e1Smrg      echo "$0: $dst_arg: Is not a directory." >&2
230d04472e1Smrg      exit 1
231d04472e1Smrg    fi
232d04472e1Smrg  fi
233d04472e1Smrgfi
234d04472e1Smrg
235d1333322Smrgif test -z "$dir_arg"; then
236c27291ebSmrg  do_exit='(exit $ret); exit $ret'
237c27291ebSmrg  trap "ret=129; $do_exit" 1
238c27291ebSmrg  trap "ret=130; $do_exit" 2
239c27291ebSmrg  trap "ret=141; $do_exit" 13
240c27291ebSmrg  trap "ret=143; $do_exit" 15
241d1333322Smrg
242d1333322Smrg  # Set umask so as not to create temps with too-generous modes.
243d1333322Smrg  # However, 'strip' requires both read and write access to temps.
244d1333322Smrg  case $mode in
245d1333322Smrg    # Optimize common cases.
246d1333322Smrg    *644) cp_umask=133;;
247d1333322Smrg    *755) cp_umask=22;;
248d1333322Smrg
249d1333322Smrg    *[0-7])
250d1333322Smrg      if test -z "$stripcmd"; then
251d04472e1Smrg        u_plus_rw=
252d1333322Smrg      else
253d04472e1Smrg        u_plus_rw='% 200'
254d1333322Smrg      fi
255d1333322Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
256d1333322Smrg    *)
257d1333322Smrg      if test -z "$stripcmd"; then
258d04472e1Smrg        u_plus_rw=
259d1333322Smrg      else
260d04472e1Smrg        u_plus_rw=,u+rw
261d1333322Smrg      fi
262d1333322Smrg      cp_umask=$mode$u_plus_rw;;
263d1333322Smrg  esac
264d1333322Smrgfi
265d1333322Smrg
266d1333322Smrgfor src
267d1333322Smrgdo
26869ef5f27Smrg  # Protect names problematic for 'test' and other utilities.
269d1333322Smrg  case $src in
270c27291ebSmrg    -* | [=\(\)!]) src=./$src;;
271d1333322Smrg  esac
272d1333322Smrg
273d1333322Smrg  if test -n "$dir_arg"; then
274d1333322Smrg    dst=$src
275d1333322Smrg    dstdir=$dst
276d1333322Smrg    test -d "$dstdir"
277d1333322Smrg    dstdir_status=$?
278d04472e1Smrg    # Don't chown directories that already exist.
279d04472e1Smrg    if test $dstdir_status = 0; then
280d04472e1Smrg      chowncmd=""
281d04472e1Smrg    fi
282d1333322Smrg  else
283d1333322Smrg
284d1333322Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
285d1333322Smrg    # might cause directories to be created, which would be especially bad
286d1333322Smrg    # if $src (and thus $dsttmp) contains '*'.
287d1333322Smrg    if test ! -f "$src" && test ! -d "$src"; then
288d1333322Smrg      echo "$0: $src does not exist." >&2
289d1333322Smrg      exit 1
290d1333322Smrg    fi
291d1333322Smrg
2924f9ac78aSmrg    if test -z "$dst_arg"; then
293d1333322Smrg      echo "$0: no destination specified." >&2
294d1333322Smrg      exit 1
295d1333322Smrg    fi
2964f9ac78aSmrg    dst=$dst_arg
297d1333322Smrg
298d04472e1Smrg    # If destination is a directory, append the input filename.
299d1333322Smrg    if test -d "$dst"; then
300d04472e1Smrg      if test "$is_target_a_directory" = never; then
301d04472e1Smrg        echo "$0: $dst_arg: Is a directory" >&2
302d04472e1Smrg        exit 1
303d1333322Smrg      fi
304d1333322Smrg      dstdir=$dst
305d04472e1Smrg      dstbase=`basename "$src"`
306d04472e1Smrg      case $dst in
307d04472e1Smrg	*/) dst=$dst$dstbase;;
308d04472e1Smrg	*)  dst=$dst/$dstbase;;
309d04472e1Smrg      esac
310d1333322Smrg      dstdir_status=0
311d1333322Smrg    else
312d04472e1Smrg      dstdir=`dirname "$dst"`
313d1333322Smrg      test -d "$dstdir"
314d1333322Smrg      dstdir_status=$?
315d1333322Smrg    fi
316d1333322Smrg  fi
317d1333322Smrg
318d04472e1Smrg  case $dstdir in
319d04472e1Smrg    */) dstdirslash=$dstdir;;
320d04472e1Smrg    *)  dstdirslash=$dstdir/;;
321d04472e1Smrg  esac
322d04472e1Smrg
323d1333322Smrg  obsolete_mkdir_used=false
324d1333322Smrg
325d1333322Smrg  if test $dstdir_status != 0; then
326d1333322Smrg    case $posix_mkdir in
327d1333322Smrg      '')
328d04472e1Smrg        # With -d, create the new directory with the user-specified mode.
329d04472e1Smrg        # Otherwise, rely on $mkdir_umask.
330d04472e1Smrg        if test -n "$dir_arg"; then
331d04472e1Smrg          mkdir_mode=-m$mode
332d04472e1Smrg        else
333d04472e1Smrg          mkdir_mode=
334d04472e1Smrg        fi
335d04472e1Smrg
336d04472e1Smrg        posix_mkdir=false
337d04472e1Smrg	# The $RANDOM variable is not portable (e.g., dash).  Use it
338d04472e1Smrg	# here however when possible just to lower collision chance.
339d04472e1Smrg	tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
340d04472e1Smrg
341d04472e1Smrg	trap '
342d04472e1Smrg	  ret=$?
343d04472e1Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null
344d04472e1Smrg	  exit $ret
345d04472e1Smrg	' 0
346d04472e1Smrg
347d04472e1Smrg	# Because "mkdir -p" follows existing symlinks and we likely work
348742aa962Smrg	# directly in world-writable /tmp, make sure that the '$tmpdir'
349d04472e1Smrg	# directory is successfully created first before we actually test
350d04472e1Smrg	# 'mkdir -p'.
351d04472e1Smrg	if (umask $mkdir_umask &&
352d04472e1Smrg	    $mkdirprog $mkdir_mode "$tmpdir" &&
353d04472e1Smrg	    exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
354d04472e1Smrg	then
355d04472e1Smrg	  if test -z "$dir_arg" || {
356742aa962Smrg	       # Check for POSIX incompatibility with -m.
357d04472e1Smrg	       # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
358d04472e1Smrg	       # other-writable bit of parent directory when it shouldn't.
359d04472e1Smrg	       # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
360d04472e1Smrg	       test_tmpdir="$tmpdir/a"
361d04472e1Smrg	       ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
362d04472e1Smrg	       case $ls_ld_tmpdir in
363d04472e1Smrg		 d????-?r-*) different_mode=700;;
364d04472e1Smrg		 d????-?--*) different_mode=755;;
365d04472e1Smrg		 *) false;;
366d04472e1Smrg	       esac &&
367d04472e1Smrg	       $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
368d04472e1Smrg		 ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
369d04472e1Smrg		 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
370d04472e1Smrg	       }
371d04472e1Smrg	     }
372d04472e1Smrg	  then posix_mkdir=:
373d04472e1Smrg	  fi
374d04472e1Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
375d1333322Smrg	else
376d04472e1Smrg	  # Remove any dirs left behind by ancient mkdir implementations.
377d04472e1Smrg	  rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
378d1333322Smrg	fi
379d04472e1Smrg	trap '' 0;;
380d1333322Smrg    esac
381d1333322Smrg
382d1333322Smrg    if
383d1333322Smrg      $posix_mkdir && (
384d04472e1Smrg        umask $mkdir_umask &&
385d04472e1Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
386d1333322Smrg      )
387d1333322Smrg    then :
388d1333322Smrg    else
389d1333322Smrg
390d04472e1Smrg      # mkdir does not conform to POSIX,
391d1333322Smrg      # or it failed possibly due to a race condition.  Create the
392d1333322Smrg      # directory the slow way, step by step, checking for races as we go.
393d1333322Smrg
394d1333322Smrg      case $dstdir in
395d04472e1Smrg        /*) prefix='/';;
396d04472e1Smrg        [-=\(\)!]*) prefix='./';;
397d04472e1Smrg        *)  prefix='';;
398d1333322Smrg      esac
399d1333322Smrg
400d1333322Smrg      oIFS=$IFS
401d1333322Smrg      IFS=/
402d04472e1Smrg      set -f
403d1333322Smrg      set fnord $dstdir
404d1333322Smrg      shift
405d04472e1Smrg      set +f
406d1333322Smrg      IFS=$oIFS
407d1333322Smrg
408d1333322Smrg      prefixes=
409d1333322Smrg
410d1333322Smrg      for d
411d1333322Smrg      do
412d04472e1Smrg        test X"$d" = X && continue
413d04472e1Smrg
414d04472e1Smrg        prefix=$prefix$d
415d04472e1Smrg        if test -d "$prefix"; then
416d04472e1Smrg          prefixes=
417d04472e1Smrg        else
418d04472e1Smrg          if $posix_mkdir; then
419d04472e1Smrg            (umask $mkdir_umask &&
420d04472e1Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
421d04472e1Smrg            # Don't fail if two instances are running concurrently.
422d04472e1Smrg            test -d "$prefix" || exit 1
423d04472e1Smrg          else
424d04472e1Smrg            case $prefix in
425d04472e1Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
426d04472e1Smrg              *) qprefix=$prefix;;
427d04472e1Smrg            esac
428d04472e1Smrg            prefixes="$prefixes '$qprefix'"
429d04472e1Smrg          fi
430d04472e1Smrg        fi
431d04472e1Smrg        prefix=$prefix/
432d1333322Smrg      done
433d1333322Smrg
434d1333322Smrg      if test -n "$prefixes"; then
435d04472e1Smrg        # Don't fail if two instances are running concurrently.
436d04472e1Smrg        (umask $mkdir_umask &&
437d04472e1Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
438d04472e1Smrg          test -d "$dstdir" || exit 1
439d04472e1Smrg        obsolete_mkdir_used=true
440d1333322Smrg      fi
441d1333322Smrg    fi
442d1333322Smrg  fi
443d1333322Smrg
444d1333322Smrg  if test -n "$dir_arg"; then
445d1333322Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
446d1333322Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
447d1333322Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
448d1333322Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
449d1333322Smrg  else
450d1333322Smrg
451d1333322Smrg    # Make a couple of temp file names in the proper directory.
452d04472e1Smrg    dsttmp=${dstdirslash}_inst.$$_
453d04472e1Smrg    rmtmp=${dstdirslash}_rm.$$_
454d1333322Smrg
455d1333322Smrg    # Trap to clean up those temp files at exit.
456d1333322Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
457d1333322Smrg
458d1333322Smrg    # Copy the file name to the temp name.
459d04472e1Smrg    (umask $cp_umask &&
460d04472e1Smrg     { test -z "$stripcmd" || {
461d04472e1Smrg	 # Create $dsttmp read-write so that cp doesn't create it read-only,
462d04472e1Smrg	 # which would cause strip to fail.
463d04472e1Smrg	 if test -z "$doit"; then
464d04472e1Smrg	   : >"$dsttmp" # No need to fork-exec 'touch'.
465d04472e1Smrg	 else
466d04472e1Smrg	   $doit touch "$dsttmp"
467d04472e1Smrg	 fi
468d04472e1Smrg       }
469d04472e1Smrg     } &&
470d04472e1Smrg     $doit_exec $cpprog "$src" "$dsttmp") &&
471d1333322Smrg
472d1333322Smrg    # and set any options; do chmod last to preserve setuid bits.
473d1333322Smrg    #
474d1333322Smrg    # If any of these fail, we abort the whole thing.  If we want to
475d1333322Smrg    # ignore errors from any of these, just make sure not to ignore
476d1333322Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
477d1333322Smrg    #
4784f9ac78aSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
4794f9ac78aSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
4804f9ac78aSmrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
4814f9ac78aSmrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4824f9ac78aSmrg
4834f9ac78aSmrg    # If -C, don't bother to copy if it wouldn't change the file.
4844f9ac78aSmrg    if $copy_on_change &&
485d04472e1Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
486d04472e1Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
487d04472e1Smrg       set -f &&
4884f9ac78aSmrg       set X $old && old=:$2:$4:$5:$6 &&
4894f9ac78aSmrg       set X $new && new=:$2:$4:$5:$6 &&
490d04472e1Smrg       set +f &&
4914f9ac78aSmrg       test "$old" = "$new" &&
4924f9ac78aSmrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
4934f9ac78aSmrg    then
4944f9ac78aSmrg      rm -f "$dsttmp"
4954f9ac78aSmrg    else
496d04472e1Smrg      # If $backupsuffix is set, and the file being installed
497d04472e1Smrg      # already exists, attempt a backup.  Don't worry if it fails,
498d04472e1Smrg      # e.g., if mv doesn't support -f.
499d04472e1Smrg      if test -n "$backupsuffix" && test -f "$dst"; then
500d04472e1Smrg        $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null
501d04472e1Smrg      fi
502d04472e1Smrg
5034f9ac78aSmrg      # Rename the file to the real destination.
5044f9ac78aSmrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
5054f9ac78aSmrg
5064f9ac78aSmrg      # The rename failed, perhaps because mv can't rename something else
5074f9ac78aSmrg      # to itself, or perhaps because mv is so ancient that it does not
5084f9ac78aSmrg      # support -f.
5094f9ac78aSmrg      {
510d04472e1Smrg        # Now remove or move aside any old file at destination location.
511d04472e1Smrg        # We try this two ways since rm can't unlink itself on some
512d04472e1Smrg        # systems and the destination file might be busy for other
513d04472e1Smrg        # reasons.  In this case, the final cleanup might fail but the new
514d04472e1Smrg        # file should still install successfully.
515d04472e1Smrg        {
516d04472e1Smrg          test ! -f "$dst" ||
517d04472e1Smrg          $doit $rmcmd "$dst" 2>/dev/null ||
518d04472e1Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
519d04472e1Smrg            { $doit $rmcmd "$rmtmp" 2>/dev/null; :; }
520d04472e1Smrg          } ||
521d04472e1Smrg          { echo "$0: cannot unlink or rename $dst" >&2
522d04472e1Smrg            (exit 1); exit 1
523d04472e1Smrg          }
524d04472e1Smrg        } &&
525d04472e1Smrg
526d04472e1Smrg        # Now rename the file to the real destination.
527d04472e1Smrg        $doit $mvcmd "$dsttmp" "$dst"
5284f9ac78aSmrg      }
5294f9ac78aSmrg    fi || exit 1
530d1333322Smrg
531d1333322Smrg    trap '' 0
532d1333322Smrg  fi
533d1333322Smrgdone
534d1333322Smrg
535d1333322Smrg# Local variables:
536d04472e1Smrg# eval: (add-hook 'before-save-hook 'time-stamp)
537d1333322Smrg# time-stamp-start: "scriptversion="
538d1333322Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
539d04472e1Smrg# time-stamp-time-zone: "UTC0"
5404f9ac78aSmrg# time-stamp-end: "; # UTC"
541d1333322Smrg# End:
542