1d4401354Smrg#!/bin/sh
2d4401354Smrg# install - install a program, script, or datafile
3d4401354Smrg
4d5c9b07bSmrgscriptversion=2020-11-14.01; # UTC
5d4401354Smrg
6d4401354Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7d4401354Smrg# later released in X11R6 (xc/config/util/install.sh) with the
8d4401354Smrg# following copyright and license.
9d4401354Smrg#
10d4401354Smrg# Copyright (C) 1994 X Consortium
11d4401354Smrg#
12d4401354Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13d4401354Smrg# of this software and associated documentation files (the "Software"), to
14d4401354Smrg# deal in the Software without restriction, including without limitation the
15d4401354Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16d4401354Smrg# sell copies of the Software, and to permit persons to whom the Software is
17d4401354Smrg# furnished to do so, subject to the following conditions:
18d4401354Smrg#
19d4401354Smrg# The above copyright notice and this permission notice shall be included in
20d4401354Smrg# all copies or substantial portions of the Software.
21d4401354Smrg#
22d4401354Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23d4401354Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24d4401354Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25d4401354Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26d4401354Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27d4401354Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28d4401354Smrg#
29d4401354Smrg# Except as contained in this notice, the name of the X Consortium shall not
30d4401354Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
31d4401354Smrg# ings in this Software without prior written authorization from the X Consor-
32d4401354Smrg# tium.
33d4401354Smrg#
34d4401354Smrg#
35d4401354Smrg# FSF changes to this file are in the public domain.
36d4401354Smrg#
37d4401354Smrg# Calling this script install-sh is preferred over install.sh, to prevent
38f591e195Smrg# 'make' implicit rules from creating a file called install from it
39d4401354Smrg# when there is no Makefile.
40d4401354Smrg#
41d4401354Smrg# This script is compatible with the BSD install script, but was written
42d4401354Smrg# from scratch.
43d4401354Smrg
44891601f5Smrgtab='	'
45d4401354Smrgnl='
46d4401354Smrg'
47891601f5SmrgIFS=" $tab$nl"
48d4401354Smrg
49891601f5Smrg# Set DOITPROG to "echo" to test this script.
50d4401354Smrg
51d4401354Smrgdoit=${DOITPROG-}
52891601f5Smrgdoit_exec=${doit:-exec}
53d4401354Smrg
54d4401354Smrg# Put in absolute file names if you don't have them in your path;
55d4401354Smrg# or use environment vars.
56d4401354Smrg
57d4401354Smrgchgrpprog=${CHGRPPROG-chgrp}
58d4401354Smrgchmodprog=${CHMODPROG-chmod}
59d4401354Smrgchownprog=${CHOWNPROG-chown}
60d4401354Smrgcmpprog=${CMPPROG-cmp}
61d4401354Smrgcpprog=${CPPROG-cp}
62d4401354Smrgmkdirprog=${MKDIRPROG-mkdir}
63d4401354Smrgmvprog=${MVPROG-mv}
64d4401354Smrgrmprog=${RMPROG-rm}
65d4401354Smrgstripprog=${STRIPPROG-strip}
66d4401354Smrg
67d4401354Smrgposix_mkdir=
68d4401354Smrg
69d4401354Smrg# Desired mode of installed file.
70d4401354Smrgmode=0755
71d4401354Smrg
72d5c9b07bSmrg# Create dirs (including intermediate dirs) using mode 755.
73d5c9b07bSmrg# This is like GNU 'install' as of coreutils 8.32 (2020).
74d5c9b07bSmrgmkdir_umask=22
75d5c9b07bSmrg
76d5c9b07bSmrgbackupsuffix=
77d4401354Smrgchgrpcmd=
78d4401354Smrgchmodcmd=$chmodprog
79d4401354Smrgchowncmd=
80d4401354Smrgmvcmd=$mvprog
81d4401354Smrgrmcmd="$rmprog -f"
82d4401354Smrgstripcmd=
83d4401354Smrg
84d4401354Smrgsrc=
85d4401354Smrgdst=
86d4401354Smrgdir_arg=
87d4401354Smrgdst_arg=
88d4401354Smrg
89d4401354Smrgcopy_on_change=false
90891601f5Smrgis_target_a_directory=possibly
91d4401354Smrg
92d4401354Smrgusage="\
93d4401354SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
94d4401354Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
95d4401354Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
96d4401354Smrg   or: $0 [OPTION]... -d DIRECTORIES...
97d4401354Smrg
98d4401354SmrgIn the 1st form, copy SRCFILE to DSTFILE.
99d4401354SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
100d4401354SmrgIn the 4th, create DIRECTORIES.
101d4401354Smrg
102d4401354SmrgOptions:
103d4401354Smrg     --help     display this help and exit.
104d4401354Smrg     --version  display version info and exit.
105d4401354Smrg
106d4401354Smrg  -c            (ignored)
107d5c9b07bSmrg  -C            install only if different (preserve data modification time)
108d4401354Smrg  -d            create directories instead of installing files.
109d4401354Smrg  -g GROUP      $chgrpprog installed files to GROUP.
110d4401354Smrg  -m MODE       $chmodprog installed files to MODE.
111d4401354Smrg  -o USER       $chownprog installed files to USER.
112d5c9b07bSmrg  -p            pass -p to $cpprog.
113d4401354Smrg  -s            $stripprog installed files.
114d5c9b07bSmrg  -S SUFFIX     attempt to back up existing files, with suffix SUFFIX.
115d4401354Smrg  -t DIRECTORY  install into DIRECTORY.
116d4401354Smrg  -T            report an error if DSTFILE is a directory.
117d4401354Smrg
118d4401354SmrgEnvironment variables override the default commands:
119d4401354Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
120d4401354Smrg  RMPROG STRIPPROG
121d5c9b07bSmrg
122d5c9b07bSmrgBy default, rm is invoked with -f; when overridden with RMPROG,
123d5c9b07bSmrgit's up to you to specify -f if you want it.
124d5c9b07bSmrg
125d5c9b07bSmrgIf -S is not specified, no backups are attempted.
126d5c9b07bSmrg
127d5c9b07bSmrgEmail bug reports to bug-automake@gnu.org.
128d5c9b07bSmrgAutomake home page: https://www.gnu.org/software/automake/
129d4401354Smrg"
130d4401354Smrg
131d4401354Smrgwhile test $# -ne 0; do
132d4401354Smrg  case $1 in
133d4401354Smrg    -c) ;;
134d4401354Smrg
135d4401354Smrg    -C) copy_on_change=true;;
136d4401354Smrg
137d4401354Smrg    -d) dir_arg=true;;
138d4401354Smrg
139d4401354Smrg    -g) chgrpcmd="$chgrpprog $2"
140891601f5Smrg        shift;;
141d4401354Smrg
142d4401354Smrg    --help) echo "$usage"; exit $?;;
143d4401354Smrg
144d4401354Smrg    -m) mode=$2
145891601f5Smrg        case $mode in
146891601f5Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
147891601f5Smrg            echo "$0: invalid mode: $mode" >&2
148891601f5Smrg            exit 1;;
149891601f5Smrg        esac
150891601f5Smrg        shift;;
151d4401354Smrg
152d4401354Smrg    -o) chowncmd="$chownprog $2"
153891601f5Smrg        shift;;
154d4401354Smrg
155d5c9b07bSmrg    -p) cpprog="$cpprog -p";;
156d5c9b07bSmrg
157d4401354Smrg    -s) stripcmd=$stripprog;;
158d4401354Smrg
159d5c9b07bSmrg    -S) backupsuffix="$2"
160d5c9b07bSmrg        shift;;
161d5c9b07bSmrg
162891601f5Smrg    -t)
163891601f5Smrg        is_target_a_directory=always
164891601f5Smrg        dst_arg=$2
165891601f5Smrg        # Protect names problematic for 'test' and other utilities.
166891601f5Smrg        case $dst_arg in
167891601f5Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
168891601f5Smrg        esac
169891601f5Smrg        shift;;
170d4401354Smrg
171891601f5Smrg    -T) is_target_a_directory=never;;
172d4401354Smrg
173d4401354Smrg    --version) echo "$0 $scriptversion"; exit $?;;
174d4401354Smrg
175891601f5Smrg    --) shift
176891601f5Smrg        break;;
177d4401354Smrg
178891601f5Smrg    -*) echo "$0: invalid option: $1" >&2
179891601f5Smrg        exit 1;;
180d4401354Smrg
181d4401354Smrg    *)  break;;
182d4401354Smrg  esac
183d4401354Smrg  shift
184d4401354Smrgdone
185d4401354Smrg
186891601f5Smrg# We allow the use of options -d and -T together, by making -d
187891601f5Smrg# take the precedence; this is for compatibility with GNU install.
188891601f5Smrg
189891601f5Smrgif test -n "$dir_arg"; then
190891601f5Smrg  if test -n "$dst_arg"; then
191891601f5Smrg    echo "$0: target directory not allowed when installing a directory." >&2
192891601f5Smrg    exit 1
193891601f5Smrg  fi
194891601f5Smrgfi
195891601f5Smrg
196d4401354Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
197d4401354Smrg  # When -d is used, all remaining arguments are directories to create.
198d4401354Smrg  # When -t is used, the destination is already specified.
199d4401354Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
200d4401354Smrg  for arg
201d4401354Smrg  do
202d4401354Smrg    if test -n "$dst_arg"; then
203d4401354Smrg      # $@ is not empty: it contains at least $arg.
204d4401354Smrg      set fnord "$@" "$dst_arg"
205d4401354Smrg      shift # fnord
206d4401354Smrg    fi
207d4401354Smrg    shift # arg
208d4401354Smrg    dst_arg=$arg
209f591e195Smrg    # Protect names problematic for 'test' and other utilities.
210986c8b3dSmrg    case $dst_arg in
211986c8b3dSmrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
212986c8b3dSmrg    esac
213d4401354Smrg  done
214d4401354Smrgfi
215d4401354Smrg
216d4401354Smrgif test $# -eq 0; then
217d4401354Smrg  if test -z "$dir_arg"; then
218d4401354Smrg    echo "$0: no input file specified." >&2
219d4401354Smrg    exit 1
220d4401354Smrg  fi
221f591e195Smrg  # It's OK to call 'install-sh -d' without argument.
222d4401354Smrg  # This can happen when creating conditional directories.
223d4401354Smrg  exit 0
224d4401354Smrgfi
225d4401354Smrg
226891601f5Smrgif test -z "$dir_arg"; then
227891601f5Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
228891601f5Smrg    if test ! -d "$dst_arg"; then
229891601f5Smrg      echo "$0: $dst_arg: Is not a directory." >&2
230891601f5Smrg      exit 1
231891601f5Smrg    fi
232891601f5Smrg  fi
233891601f5Smrgfi
234891601f5Smrg
235d4401354Smrgif test -z "$dir_arg"; then
236986c8b3dSmrg  do_exit='(exit $ret); exit $ret'
237986c8b3dSmrg  trap "ret=129; $do_exit" 1
238986c8b3dSmrg  trap "ret=130; $do_exit" 2
239986c8b3dSmrg  trap "ret=141; $do_exit" 13
240986c8b3dSmrg  trap "ret=143; $do_exit" 15
241d4401354Smrg
242d4401354Smrg  # Set umask so as not to create temps with too-generous modes.
243d4401354Smrg  # However, 'strip' requires both read and write access to temps.
244d4401354Smrg  case $mode in
245d4401354Smrg    # Optimize common cases.
246d4401354Smrg    *644) cp_umask=133;;
247d4401354Smrg    *755) cp_umask=22;;
248d4401354Smrg
249d4401354Smrg    *[0-7])
250d4401354Smrg      if test -z "$stripcmd"; then
251891601f5Smrg        u_plus_rw=
252d4401354Smrg      else
253891601f5Smrg        u_plus_rw='% 200'
254d4401354Smrg      fi
255d4401354Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
256d4401354Smrg    *)
257d4401354Smrg      if test -z "$stripcmd"; then
258891601f5Smrg        u_plus_rw=
259d4401354Smrg      else
260891601f5Smrg        u_plus_rw=,u+rw
261d4401354Smrg      fi
262d4401354Smrg      cp_umask=$mode$u_plus_rw;;
263d4401354Smrg  esac
264d4401354Smrgfi
265d4401354Smrg
266d4401354Smrgfor src
267d4401354Smrgdo
268f591e195Smrg  # Protect names problematic for 'test' and other utilities.
269d4401354Smrg  case $src in
270986c8b3dSmrg    -* | [=\(\)!]) src=./$src;;
271d4401354Smrg  esac
272d4401354Smrg
273d4401354Smrg  if test -n "$dir_arg"; then
274d4401354Smrg    dst=$src
275d4401354Smrg    dstdir=$dst
276d4401354Smrg    test -d "$dstdir"
277d4401354Smrg    dstdir_status=$?
278d5c9b07bSmrg    # Don't chown directories that already exist.
279d5c9b07bSmrg    if test $dstdir_status = 0; then
280d5c9b07bSmrg      chowncmd=""
281d5c9b07bSmrg    fi
282d4401354Smrg  else
283d4401354Smrg
284d4401354Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
285d4401354Smrg    # might cause directories to be created, which would be especially bad
286d4401354Smrg    # if $src (and thus $dsttmp) contains '*'.
287d4401354Smrg    if test ! -f "$src" && test ! -d "$src"; then
288d4401354Smrg      echo "$0: $src does not exist." >&2
289d4401354Smrg      exit 1
290d4401354Smrg    fi
291d4401354Smrg
292d4401354Smrg    if test -z "$dst_arg"; then
293d4401354Smrg      echo "$0: no destination specified." >&2
294d4401354Smrg      exit 1
295d4401354Smrg    fi
296d4401354Smrg    dst=$dst_arg
297d4401354Smrg
2988d8e295fSmrg    # If destination is a directory, append the input filename.
299d4401354Smrg    if test -d "$dst"; then
300891601f5Smrg      if test "$is_target_a_directory" = never; then
301891601f5Smrg        echo "$0: $dst_arg: Is a directory" >&2
302891601f5Smrg        exit 1
303d4401354Smrg      fi
304d4401354Smrg      dstdir=$dst
3058d8e295fSmrg      dstbase=`basename "$src"`
3068d8e295fSmrg      case $dst in
3078d8e295fSmrg	*/) dst=$dst$dstbase;;
3088d8e295fSmrg	*)  dst=$dst/$dstbase;;
3098d8e295fSmrg      esac
310d4401354Smrg      dstdir_status=0
311d4401354Smrg    else
312891601f5Smrg      dstdir=`dirname "$dst"`
313d4401354Smrg      test -d "$dstdir"
314d4401354Smrg      dstdir_status=$?
315d4401354Smrg    fi
316d4401354Smrg  fi
317d4401354Smrg
3188d8e295fSmrg  case $dstdir in
3198d8e295fSmrg    */) dstdirslash=$dstdir;;
3208d8e295fSmrg    *)  dstdirslash=$dstdir/;;
3218d8e295fSmrg  esac
3228d8e295fSmrg
323d4401354Smrg  obsolete_mkdir_used=false
324d4401354Smrg
325d4401354Smrg  if test $dstdir_status != 0; then
326d4401354Smrg    case $posix_mkdir in
327d4401354Smrg      '')
328891601f5Smrg        # With -d, create the new directory with the user-specified mode.
329891601f5Smrg        # Otherwise, rely on $mkdir_umask.
330891601f5Smrg        if test -n "$dir_arg"; then
331891601f5Smrg          mkdir_mode=-m$mode
332891601f5Smrg        else
333891601f5Smrg          mkdir_mode=
334891601f5Smrg        fi
335891601f5Smrg
336891601f5Smrg        posix_mkdir=false
337d5c9b07bSmrg	# The $RANDOM variable is not portable (e.g., dash).  Use it
338d5c9b07bSmrg	# here however when possible just to lower collision chance.
339d5c9b07bSmrg	tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
340d5c9b07bSmrg
341d5c9b07bSmrg	trap '
342d5c9b07bSmrg	  ret=$?
343d5c9b07bSmrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null
344d5c9b07bSmrg	  exit $ret
345d5c9b07bSmrg	' 0
346d5c9b07bSmrg
347d5c9b07bSmrg	# Because "mkdir -p" follows existing symlinks and we likely work
348d5c9b07bSmrg	# directly in world-writeable /tmp, make sure that the '$tmpdir'
349d5c9b07bSmrg	# directory is successfully created first before we actually test
350d5c9b07bSmrg	# 'mkdir -p'.
351d5c9b07bSmrg	if (umask $mkdir_umask &&
352d5c9b07bSmrg	    $mkdirprog $mkdir_mode "$tmpdir" &&
353d5c9b07bSmrg	    exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
354d5c9b07bSmrg	then
355d5c9b07bSmrg	  if test -z "$dir_arg" || {
356d5c9b07bSmrg	       # Check for POSIX incompatibilities with -m.
357d5c9b07bSmrg	       # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
358d5c9b07bSmrg	       # other-writable bit of parent directory when it shouldn't.
359d5c9b07bSmrg	       # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
360d5c9b07bSmrg	       test_tmpdir="$tmpdir/a"
361d5c9b07bSmrg	       ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
362d5c9b07bSmrg	       case $ls_ld_tmpdir in
363d5c9b07bSmrg		 d????-?r-*) different_mode=700;;
364d5c9b07bSmrg		 d????-?--*) different_mode=755;;
365d5c9b07bSmrg		 *) false;;
366d5c9b07bSmrg	       esac &&
367d5c9b07bSmrg	       $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
368d5c9b07bSmrg		 ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
369d5c9b07bSmrg		 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
370d5c9b07bSmrg	       }
371d5c9b07bSmrg	     }
372d5c9b07bSmrg	  then posix_mkdir=:
373d5c9b07bSmrg	  fi
374d5c9b07bSmrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
375d5c9b07bSmrg	else
376d5c9b07bSmrg	  # Remove any dirs left behind by ancient mkdir implementations.
377d5c9b07bSmrg	  rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
378d5c9b07bSmrg	fi
379d5c9b07bSmrg	trap '' 0;;
380d4401354Smrg    esac
381d4401354Smrg
382d4401354Smrg    if
383d4401354Smrg      $posix_mkdir && (
384891601f5Smrg        umask $mkdir_umask &&
385891601f5Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
386d4401354Smrg      )
387d4401354Smrg    then :
388d4401354Smrg    else
389d4401354Smrg
390d5c9b07bSmrg      # mkdir does not conform to POSIX,
391d4401354Smrg      # or it failed possibly due to a race condition.  Create the
392d4401354Smrg      # directory the slow way, step by step, checking for races as we go.
393d4401354Smrg
394d4401354Smrg      case $dstdir in
395891601f5Smrg        /*) prefix='/';;
396891601f5Smrg        [-=\(\)!]*) prefix='./';;
397891601f5Smrg        *)  prefix='';;
398d4401354Smrg      esac
399d4401354Smrg
400d4401354Smrg      oIFS=$IFS
401d4401354Smrg      IFS=/
402891601f5Smrg      set -f
403d4401354Smrg      set fnord $dstdir
404d4401354Smrg      shift
405891601f5Smrg      set +f
406d4401354Smrg      IFS=$oIFS
407d4401354Smrg
408d4401354Smrg      prefixes=
409d4401354Smrg
410d4401354Smrg      for d
411d4401354Smrg      do
412891601f5Smrg        test X"$d" = X && continue
413891601f5Smrg
414891601f5Smrg        prefix=$prefix$d
415891601f5Smrg        if test -d "$prefix"; then
416891601f5Smrg          prefixes=
417891601f5Smrg        else
418891601f5Smrg          if $posix_mkdir; then
419d5c9b07bSmrg            (umask $mkdir_umask &&
420891601f5Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
421891601f5Smrg            # Don't fail if two instances are running concurrently.
422891601f5Smrg            test -d "$prefix" || exit 1
423891601f5Smrg          else
424891601f5Smrg            case $prefix in
425891601f5Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
426891601f5Smrg              *) qprefix=$prefix;;
427891601f5Smrg            esac
428891601f5Smrg            prefixes="$prefixes '$qprefix'"
429891601f5Smrg          fi
430891601f5Smrg        fi
431891601f5Smrg        prefix=$prefix/
432d4401354Smrg      done
433d4401354Smrg
434d4401354Smrg      if test -n "$prefixes"; then
435891601f5Smrg        # Don't fail if two instances are running concurrently.
436891601f5Smrg        (umask $mkdir_umask &&
437891601f5Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
438891601f5Smrg          test -d "$dstdir" || exit 1
439891601f5Smrg        obsolete_mkdir_used=true
440d4401354Smrg      fi
441d4401354Smrg    fi
442d4401354Smrg  fi
443d4401354Smrg
444d4401354Smrg  if test -n "$dir_arg"; then
445d4401354Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
446d4401354Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
447d4401354Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
448d4401354Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
449d4401354Smrg  else
450d4401354Smrg
451d4401354Smrg    # Make a couple of temp file names in the proper directory.
4528d8e295fSmrg    dsttmp=${dstdirslash}_inst.$$_
4538d8e295fSmrg    rmtmp=${dstdirslash}_rm.$$_
454d4401354Smrg
455d4401354Smrg    # Trap to clean up those temp files at exit.
456d4401354Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
457d4401354Smrg
458d4401354Smrg    # Copy the file name to the temp name.
459d5c9b07bSmrg    (umask $cp_umask &&
460d5c9b07bSmrg     { test -z "$stripcmd" || {
461d5c9b07bSmrg	 # Create $dsttmp read-write so that cp doesn't create it read-only,
462d5c9b07bSmrg	 # which would cause strip to fail.
463d5c9b07bSmrg	 if test -z "$doit"; then
464d5c9b07bSmrg	   : >"$dsttmp" # No need to fork-exec 'touch'.
465d5c9b07bSmrg	 else
466d5c9b07bSmrg	   $doit touch "$dsttmp"
467d5c9b07bSmrg	 fi
468d5c9b07bSmrg       }
469d5c9b07bSmrg     } &&
470d5c9b07bSmrg     $doit_exec $cpprog "$src" "$dsttmp") &&
471d4401354Smrg
472d4401354Smrg    # and set any options; do chmod last to preserve setuid bits.
473d4401354Smrg    #
474d4401354Smrg    # If any of these fail, we abort the whole thing.  If we want to
475d4401354Smrg    # ignore errors from any of these, just make sure not to ignore
476d4401354Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
477d4401354Smrg    #
478d4401354Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
479d4401354Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
480d4401354Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
481d4401354Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
482d4401354Smrg
483d4401354Smrg    # If -C, don't bother to copy if it wouldn't change the file.
484d4401354Smrg    if $copy_on_change &&
485891601f5Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
486891601f5Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
487891601f5Smrg       set -f &&
488d4401354Smrg       set X $old && old=:$2:$4:$5:$6 &&
489d4401354Smrg       set X $new && new=:$2:$4:$5:$6 &&
490891601f5Smrg       set +f &&
491d4401354Smrg       test "$old" = "$new" &&
492d4401354Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
493d4401354Smrg    then
494d4401354Smrg      rm -f "$dsttmp"
495d4401354Smrg    else
496d5c9b07bSmrg      # If $backupsuffix is set, and the file being installed
497d5c9b07bSmrg      # already exists, attempt a backup.  Don't worry if it fails,
498d5c9b07bSmrg      # e.g., if mv doesn't support -f.
499d5c9b07bSmrg      if test -n "$backupsuffix" && test -f "$dst"; then
500d5c9b07bSmrg        $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null
501d5c9b07bSmrg      fi
502d5c9b07bSmrg
503d4401354Smrg      # Rename the file to the real destination.
504d4401354Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
505d4401354Smrg
506d4401354Smrg      # The rename failed, perhaps because mv can't rename something else
507d4401354Smrg      # to itself, or perhaps because mv is so ancient that it does not
508d4401354Smrg      # support -f.
509d4401354Smrg      {
510891601f5Smrg        # Now remove or move aside any old file at destination location.
511891601f5Smrg        # We try this two ways since rm can't unlink itself on some
512891601f5Smrg        # systems and the destination file might be busy for other
513891601f5Smrg        # reasons.  In this case, the final cleanup might fail but the new
514891601f5Smrg        # file should still install successfully.
515891601f5Smrg        {
516891601f5Smrg          test ! -f "$dst" ||
517d5c9b07bSmrg          $doit $rmcmd "$dst" 2>/dev/null ||
518891601f5Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
519d5c9b07bSmrg            { $doit $rmcmd "$rmtmp" 2>/dev/null; :; }
520891601f5Smrg          } ||
521891601f5Smrg          { echo "$0: cannot unlink or rename $dst" >&2
522891601f5Smrg            (exit 1); exit 1
523891601f5Smrg          }
524891601f5Smrg        } &&
525891601f5Smrg
526891601f5Smrg        # Now rename the file to the real destination.
527891601f5Smrg        $doit $mvcmd "$dsttmp" "$dst"
528d4401354Smrg      }
529d4401354Smrg    fi || exit 1
530d4401354Smrg
531d4401354Smrg    trap '' 0
532d4401354Smrg  fi
533d4401354Smrgdone
534d4401354Smrg
535d4401354Smrg# Local variables:
5368d8e295fSmrg# eval: (add-hook 'before-save-hook 'time-stamp)
537d4401354Smrg# time-stamp-start: "scriptversion="
538d4401354Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
539a27842ffSmrg# time-stamp-time-zone: "UTC0"
540d4401354Smrg# time-stamp-end: "; # UTC"
541d4401354Smrg# End:
542