install-sh revision 3fb97780
1126a8a12Smrg#!/bin/sh
2126a8a12Smrg# install - install a program, script, or datafile
3126a8a12Smrg
43fb97780Smrgscriptversion=2020-11-14.01; # UTC
5126a8a12Smrg
6126a8a12Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7126a8a12Smrg# later released in X11R6 (xc/config/util/install.sh) with the
8126a8a12Smrg# following copyright and license.
9126a8a12Smrg#
10126a8a12Smrg# Copyright (C) 1994 X Consortium
11126a8a12Smrg#
12126a8a12Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13126a8a12Smrg# of this software and associated documentation files (the "Software"), to
14126a8a12Smrg# deal in the Software without restriction, including without limitation the
15126a8a12Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16126a8a12Smrg# sell copies of the Software, and to permit persons to whom the Software is
17126a8a12Smrg# furnished to do so, subject to the following conditions:
18126a8a12Smrg#
19126a8a12Smrg# The above copyright notice and this permission notice shall be included in
20126a8a12Smrg# all copies or substantial portions of the Software.
21126a8a12Smrg#
22126a8a12Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23126a8a12Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24126a8a12Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25126a8a12Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26126a8a12Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27126a8a12Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28126a8a12Smrg#
29126a8a12Smrg# Except as contained in this notice, the name of the X Consortium shall not
30126a8a12Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
31126a8a12Smrg# ings in this Software without prior written authorization from the X Consor-
32126a8a12Smrg# tium.
33126a8a12Smrg#
34126a8a12Smrg#
35126a8a12Smrg# FSF changes to this file are in the public domain.
36126a8a12Smrg#
37126a8a12Smrg# Calling this script install-sh is preferred over install.sh, to prevent
380a6b08f8Smrg# 'make' implicit rules from creating a file called install from it
39126a8a12Smrg# when there is no Makefile.
40126a8a12Smrg#
41126a8a12Smrg# This script is compatible with the BSD install script, but was written
42126a8a12Smrg# from scratch.
43126a8a12Smrg
44300346aeSmrgtab='	'
45126a8a12Smrgnl='
46126a8a12Smrg'
47300346aeSmrgIFS=" $tab$nl"
48126a8a12Smrg
49300346aeSmrg# Set DOITPROG to "echo" to test this script.
50126a8a12Smrg
51d656433aSmrgdoit=${DOITPROG-}
52300346aeSmrgdoit_exec=${doit:-exec}
53126a8a12Smrg
54126a8a12Smrg# Put in absolute file names if you don't have them in your path;
55126a8a12Smrg# or use environment vars.
56126a8a12Smrg
57d656433aSmrgchgrpprog=${CHGRPPROG-chgrp}
58d656433aSmrgchmodprog=${CHMODPROG-chmod}
59d656433aSmrgchownprog=${CHOWNPROG-chown}
60d656433aSmrgcmpprog=${CMPPROG-cmp}
61d656433aSmrgcpprog=${CPPROG-cp}
62d656433aSmrgmkdirprog=${MKDIRPROG-mkdir}
63d656433aSmrgmvprog=${MVPROG-mv}
64d656433aSmrgrmprog=${RMPROG-rm}
65d656433aSmrgstripprog=${STRIPPROG-strip}
66d656433aSmrg
67126a8a12Smrgposix_mkdir=
68126a8a12Smrg
69126a8a12Smrg# Desired mode of installed file.
70126a8a12Smrgmode=0755
71126a8a12Smrg
723fb97780Smrg# Create dirs (including intermediate dirs) using mode 755.
733fb97780Smrg# This is like GNU 'install' as of coreutils 8.32 (2020).
743fb97780Smrgmkdir_umask=22
753fb97780Smrg
763fb97780Smrgbackupsuffix=
77d656433aSmrgchgrpcmd=
78126a8a12Smrgchmodcmd=$chmodprog
79126a8a12Smrgchowncmd=
80d656433aSmrgmvcmd=$mvprog
81126a8a12Smrgrmcmd="$rmprog -f"
82d656433aSmrgstripcmd=
83d656433aSmrg
84126a8a12Smrgsrc=
85126a8a12Smrgdst=
86126a8a12Smrgdir_arg=
87d656433aSmrgdst_arg=
88d656433aSmrg
89d656433aSmrgcopy_on_change=false
90300346aeSmrgis_target_a_directory=possibly
91126a8a12Smrg
92d656433aSmrgusage="\
93d656433aSmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
94126a8a12Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
95126a8a12Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
96126a8a12Smrg   or: $0 [OPTION]... -d DIRECTORIES...
97126a8a12Smrg
98126a8a12SmrgIn the 1st form, copy SRCFILE to DSTFILE.
99126a8a12SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
100126a8a12SmrgIn the 4th, create DIRECTORIES.
101126a8a12Smrg
102126a8a12SmrgOptions:
103d656433aSmrg     --help     display this help and exit.
104d656433aSmrg     --version  display version info and exit.
105d656433aSmrg
106d656433aSmrg  -c            (ignored)
1073fb97780Smrg  -C            install only if different (preserve data modification time)
108d656433aSmrg  -d            create directories instead of installing files.
109d656433aSmrg  -g GROUP      $chgrpprog installed files to GROUP.
110d656433aSmrg  -m MODE       $chmodprog installed files to MODE.
111d656433aSmrg  -o USER       $chownprog installed files to USER.
1123fb97780Smrg  -p            pass -p to $cpprog.
113d656433aSmrg  -s            $stripprog installed files.
1143fb97780Smrg  -S SUFFIX     attempt to back up existing files, with suffix SUFFIX.
115d656433aSmrg  -t DIRECTORY  install into DIRECTORY.
116d656433aSmrg  -T            report an error if DSTFILE is a directory.
117126a8a12Smrg
118126a8a12SmrgEnvironment variables override the default commands:
119d656433aSmrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
120d656433aSmrg  RMPROG STRIPPROG
1213fb97780Smrg
1223fb97780SmrgBy default, rm is invoked with -f; when overridden with RMPROG,
1233fb97780Smrgit's up to you to specify -f if you want it.
1243fb97780Smrg
1253fb97780SmrgIf -S is not specified, no backups are attempted.
1263fb97780Smrg
1273fb97780SmrgEmail bug reports to bug-automake@gnu.org.
1283fb97780SmrgAutomake home page: https://www.gnu.org/software/automake/
129126a8a12Smrg"
130126a8a12Smrg
131126a8a12Smrgwhile test $# -ne 0; do
132126a8a12Smrg  case $1 in
133d656433aSmrg    -c) ;;
134d656433aSmrg
135d656433aSmrg    -C) copy_on_change=true;;
136126a8a12Smrg
137d656433aSmrg    -d) dir_arg=true;;
138126a8a12Smrg
139126a8a12Smrg    -g) chgrpcmd="$chgrpprog $2"
140300346aeSmrg        shift;;
141126a8a12Smrg
142126a8a12Smrg    --help) echo "$usage"; exit $?;;
143126a8a12Smrg
144126a8a12Smrg    -m) mode=$2
145300346aeSmrg        case $mode in
146300346aeSmrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
147300346aeSmrg            echo "$0: invalid mode: $mode" >&2
148300346aeSmrg            exit 1;;
149300346aeSmrg        esac
150300346aeSmrg        shift;;
151126a8a12Smrg
152126a8a12Smrg    -o) chowncmd="$chownprog $2"
153300346aeSmrg        shift;;
154126a8a12Smrg
1553fb97780Smrg    -p) cpprog="$cpprog -p";;
1563fb97780Smrg
157d656433aSmrg    -s) stripcmd=$stripprog;;
158126a8a12Smrg
1593fb97780Smrg    -S) backupsuffix="$2"
1603fb97780Smrg        shift;;
1613fb97780Smrg
162300346aeSmrg    -t)
163300346aeSmrg        is_target_a_directory=always
164300346aeSmrg        dst_arg=$2
165300346aeSmrg        # Protect names problematic for 'test' and other utilities.
166300346aeSmrg        case $dst_arg in
167300346aeSmrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
168300346aeSmrg        esac
169300346aeSmrg        shift;;
170126a8a12Smrg
171300346aeSmrg    -T) is_target_a_directory=never;;
172126a8a12Smrg
173126a8a12Smrg    --version) echo "$0 $scriptversion"; exit $?;;
174126a8a12Smrg
175300346aeSmrg    --) shift
176300346aeSmrg        break;;
177126a8a12Smrg
178300346aeSmrg    -*) echo "$0: invalid option: $1" >&2
179300346aeSmrg        exit 1;;
180126a8a12Smrg
181126a8a12Smrg    *)  break;;
182126a8a12Smrg  esac
183d656433aSmrg  shift
184126a8a12Smrgdone
185126a8a12Smrg
186300346aeSmrg# We allow the use of options -d and -T together, by making -d
187300346aeSmrg# take the precedence; this is for compatibility with GNU install.
188300346aeSmrg
189300346aeSmrgif test -n "$dir_arg"; then
190300346aeSmrg  if test -n "$dst_arg"; then
191300346aeSmrg    echo "$0: target directory not allowed when installing a directory." >&2
192300346aeSmrg    exit 1
193300346aeSmrg  fi
194300346aeSmrgfi
195300346aeSmrg
196d656433aSmrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
197126a8a12Smrg  # When -d is used, all remaining arguments are directories to create.
198126a8a12Smrg  # When -t is used, the destination is already specified.
199126a8a12Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
200126a8a12Smrg  for arg
201126a8a12Smrg  do
202d656433aSmrg    if test -n "$dst_arg"; then
203126a8a12Smrg      # $@ is not empty: it contains at least $arg.
204d656433aSmrg      set fnord "$@" "$dst_arg"
205126a8a12Smrg      shift # fnord
206126a8a12Smrg    fi
207126a8a12Smrg    shift # arg
208d656433aSmrg    dst_arg=$arg
2090a6b08f8Smrg    # Protect names problematic for 'test' and other utilities.
2100a6b08f8Smrg    case $dst_arg in
2110a6b08f8Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
2120a6b08f8Smrg    esac
213126a8a12Smrg  done
214126a8a12Smrgfi
215126a8a12Smrg
216126a8a12Smrgif test $# -eq 0; then
217126a8a12Smrg  if test -z "$dir_arg"; then
218126a8a12Smrg    echo "$0: no input file specified." >&2
219126a8a12Smrg    exit 1
220126a8a12Smrg  fi
2210a6b08f8Smrg  # It's OK to call 'install-sh -d' without argument.
222126a8a12Smrg  # This can happen when creating conditional directories.
223126a8a12Smrg  exit 0
224126a8a12Smrgfi
225126a8a12Smrg
226300346aeSmrgif test -z "$dir_arg"; then
227300346aeSmrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
228300346aeSmrg    if test ! -d "$dst_arg"; then
229300346aeSmrg      echo "$0: $dst_arg: Is not a directory." >&2
230300346aeSmrg      exit 1
231300346aeSmrg    fi
232300346aeSmrg  fi
233300346aeSmrgfi
234300346aeSmrg
235126a8a12Smrgif test -z "$dir_arg"; then
2360a6b08f8Smrg  do_exit='(exit $ret); exit $ret'
2370a6b08f8Smrg  trap "ret=129; $do_exit" 1
2380a6b08f8Smrg  trap "ret=130; $do_exit" 2
2390a6b08f8Smrg  trap "ret=141; $do_exit" 13
2400a6b08f8Smrg  trap "ret=143; $do_exit" 15
241126a8a12Smrg
242126a8a12Smrg  # Set umask so as not to create temps with too-generous modes.
243126a8a12Smrg  # However, 'strip' requires both read and write access to temps.
244126a8a12Smrg  case $mode in
245126a8a12Smrg    # Optimize common cases.
246126a8a12Smrg    *644) cp_umask=133;;
247126a8a12Smrg    *755) cp_umask=22;;
248126a8a12Smrg
249126a8a12Smrg    *[0-7])
250126a8a12Smrg      if test -z "$stripcmd"; then
251300346aeSmrg        u_plus_rw=
252126a8a12Smrg      else
253300346aeSmrg        u_plus_rw='% 200'
254126a8a12Smrg      fi
255126a8a12Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
256126a8a12Smrg    *)
257126a8a12Smrg      if test -z "$stripcmd"; then
258300346aeSmrg        u_plus_rw=
259126a8a12Smrg      else
260300346aeSmrg        u_plus_rw=,u+rw
261126a8a12Smrg      fi
262126a8a12Smrg      cp_umask=$mode$u_plus_rw;;
263126a8a12Smrg  esac
264126a8a12Smrgfi
265126a8a12Smrg
266126a8a12Smrgfor src
267126a8a12Smrgdo
2680a6b08f8Smrg  # Protect names problematic for 'test' and other utilities.
269126a8a12Smrg  case $src in
2700a6b08f8Smrg    -* | [=\(\)!]) src=./$src;;
271126a8a12Smrg  esac
272126a8a12Smrg
273126a8a12Smrg  if test -n "$dir_arg"; then
274126a8a12Smrg    dst=$src
275126a8a12Smrg    dstdir=$dst
276126a8a12Smrg    test -d "$dstdir"
277126a8a12Smrg    dstdir_status=$?
2783fb97780Smrg    # Don't chown directories that already exist.
2793fb97780Smrg    if test $dstdir_status = 0; then
2803fb97780Smrg      chowncmd=""
2813fb97780Smrg    fi
282126a8a12Smrg  else
283126a8a12Smrg
284126a8a12Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
285126a8a12Smrg    # might cause directories to be created, which would be especially bad
286126a8a12Smrg    # if $src (and thus $dsttmp) contains '*'.
287126a8a12Smrg    if test ! -f "$src" && test ! -d "$src"; then
288126a8a12Smrg      echo "$0: $src does not exist." >&2
289126a8a12Smrg      exit 1
290126a8a12Smrg    fi
291126a8a12Smrg
292d656433aSmrg    if test -z "$dst_arg"; then
293126a8a12Smrg      echo "$0: no destination specified." >&2
294126a8a12Smrg      exit 1
295126a8a12Smrg    fi
296d656433aSmrg    dst=$dst_arg
297126a8a12Smrg
2983fb97780Smrg    # If destination is a directory, append the input filename.
299126a8a12Smrg    if test -d "$dst"; then
300300346aeSmrg      if test "$is_target_a_directory" = never; then
301300346aeSmrg        echo "$0: $dst_arg: Is a directory" >&2
302300346aeSmrg        exit 1
303126a8a12Smrg      fi
304126a8a12Smrg      dstdir=$dst
3053fb97780Smrg      dstbase=`basename "$src"`
3063fb97780Smrg      case $dst in
3073fb97780Smrg	*/) dst=$dst$dstbase;;
3083fb97780Smrg	*)  dst=$dst/$dstbase;;
3093fb97780Smrg      esac
310126a8a12Smrg      dstdir_status=0
311126a8a12Smrg    else
312300346aeSmrg      dstdir=`dirname "$dst"`
313126a8a12Smrg      test -d "$dstdir"
314126a8a12Smrg      dstdir_status=$?
315126a8a12Smrg    fi
316126a8a12Smrg  fi
317126a8a12Smrg
3183fb97780Smrg  case $dstdir in
3193fb97780Smrg    */) dstdirslash=$dstdir;;
3203fb97780Smrg    *)  dstdirslash=$dstdir/;;
3213fb97780Smrg  esac
3223fb97780Smrg
323126a8a12Smrg  obsolete_mkdir_used=false
324126a8a12Smrg
325126a8a12Smrg  if test $dstdir_status != 0; then
326126a8a12Smrg    case $posix_mkdir in
327126a8a12Smrg      '')
328300346aeSmrg        # With -d, create the new directory with the user-specified mode.
329300346aeSmrg        # Otherwise, rely on $mkdir_umask.
330300346aeSmrg        if test -n "$dir_arg"; then
331300346aeSmrg          mkdir_mode=-m$mode
332300346aeSmrg        else
333300346aeSmrg          mkdir_mode=
334300346aeSmrg        fi
335300346aeSmrg
336300346aeSmrg        posix_mkdir=false
3373fb97780Smrg	# The $RANDOM variable is not portable (e.g., dash).  Use it
3383fb97780Smrg	# here however when possible just to lower collision chance.
3393fb97780Smrg	tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
3403fb97780Smrg
3413fb97780Smrg	trap '
3423fb97780Smrg	  ret=$?
3433fb97780Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null
3443fb97780Smrg	  exit $ret
3453fb97780Smrg	' 0
3463fb97780Smrg
3473fb97780Smrg	# Because "mkdir -p" follows existing symlinks and we likely work
3483fb97780Smrg	# directly in world-writeable /tmp, make sure that the '$tmpdir'
3493fb97780Smrg	# directory is successfully created first before we actually test
3503fb97780Smrg	# 'mkdir -p'.
3513fb97780Smrg	if (umask $mkdir_umask &&
3523fb97780Smrg	    $mkdirprog $mkdir_mode "$tmpdir" &&
3533fb97780Smrg	    exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
3543fb97780Smrg	then
3553fb97780Smrg	  if test -z "$dir_arg" || {
3563fb97780Smrg	       # Check for POSIX incompatibilities with -m.
3573fb97780Smrg	       # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
3583fb97780Smrg	       # other-writable bit of parent directory when it shouldn't.
3593fb97780Smrg	       # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
3603fb97780Smrg	       test_tmpdir="$tmpdir/a"
3613fb97780Smrg	       ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
3623fb97780Smrg	       case $ls_ld_tmpdir in
3633fb97780Smrg		 d????-?r-*) different_mode=700;;
3643fb97780Smrg		 d????-?--*) different_mode=755;;
3653fb97780Smrg		 *) false;;
3663fb97780Smrg	       esac &&
3673fb97780Smrg	       $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
3683fb97780Smrg		 ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
3693fb97780Smrg		 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
3703fb97780Smrg	       }
3713fb97780Smrg	     }
3723fb97780Smrg	  then posix_mkdir=:
3733fb97780Smrg	  fi
3743fb97780Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
3753fb97780Smrg	else
3763fb97780Smrg	  # Remove any dirs left behind by ancient mkdir implementations.
3773fb97780Smrg	  rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
3783fb97780Smrg	fi
3793fb97780Smrg	trap '' 0;;
380126a8a12Smrg    esac
381126a8a12Smrg
382126a8a12Smrg    if
383126a8a12Smrg      $posix_mkdir && (
384300346aeSmrg        umask $mkdir_umask &&
385300346aeSmrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
386126a8a12Smrg      )
387126a8a12Smrg    then :
388126a8a12Smrg    else
389126a8a12Smrg
3903fb97780Smrg      # mkdir does not conform to POSIX,
391126a8a12Smrg      # or it failed possibly due to a race condition.  Create the
392126a8a12Smrg      # directory the slow way, step by step, checking for races as we go.
393126a8a12Smrg
394126a8a12Smrg      case $dstdir in
395300346aeSmrg        /*) prefix='/';;
396300346aeSmrg        [-=\(\)!]*) prefix='./';;
397300346aeSmrg        *)  prefix='';;
398126a8a12Smrg      esac
399126a8a12Smrg
400126a8a12Smrg      oIFS=$IFS
401126a8a12Smrg      IFS=/
402300346aeSmrg      set -f
403126a8a12Smrg      set fnord $dstdir
404126a8a12Smrg      shift
405300346aeSmrg      set +f
406126a8a12Smrg      IFS=$oIFS
407126a8a12Smrg
408126a8a12Smrg      prefixes=
409126a8a12Smrg
410126a8a12Smrg      for d
411126a8a12Smrg      do
412300346aeSmrg        test X"$d" = X && continue
413300346aeSmrg
414300346aeSmrg        prefix=$prefix$d
415300346aeSmrg        if test -d "$prefix"; then
416300346aeSmrg          prefixes=
417300346aeSmrg        else
418300346aeSmrg          if $posix_mkdir; then
4193fb97780Smrg            (umask $mkdir_umask &&
420300346aeSmrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
421300346aeSmrg            # Don't fail if two instances are running concurrently.
422300346aeSmrg            test -d "$prefix" || exit 1
423300346aeSmrg          else
424300346aeSmrg            case $prefix in
425300346aeSmrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
426300346aeSmrg              *) qprefix=$prefix;;
427300346aeSmrg            esac
428300346aeSmrg            prefixes="$prefixes '$qprefix'"
429300346aeSmrg          fi
430300346aeSmrg        fi
431300346aeSmrg        prefix=$prefix/
432126a8a12Smrg      done
433126a8a12Smrg
434126a8a12Smrg      if test -n "$prefixes"; then
435300346aeSmrg        # Don't fail if two instances are running concurrently.
436300346aeSmrg        (umask $mkdir_umask &&
437300346aeSmrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
438300346aeSmrg          test -d "$dstdir" || exit 1
439300346aeSmrg        obsolete_mkdir_used=true
440126a8a12Smrg      fi
441126a8a12Smrg    fi
442126a8a12Smrg  fi
443126a8a12Smrg
444126a8a12Smrg  if test -n "$dir_arg"; then
445126a8a12Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
446126a8a12Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
447126a8a12Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
448126a8a12Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
449126a8a12Smrg  else
450126a8a12Smrg
451126a8a12Smrg    # Make a couple of temp file names in the proper directory.
4523fb97780Smrg    dsttmp=${dstdirslash}_inst.$$_
4533fb97780Smrg    rmtmp=${dstdirslash}_rm.$$_
454126a8a12Smrg
455126a8a12Smrg    # Trap to clean up those temp files at exit.
456126a8a12Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
457126a8a12Smrg
458126a8a12Smrg    # Copy the file name to the temp name.
4593fb97780Smrg    (umask $cp_umask &&
4603fb97780Smrg     { test -z "$stripcmd" || {
4613fb97780Smrg	 # Create $dsttmp read-write so that cp doesn't create it read-only,
4623fb97780Smrg	 # which would cause strip to fail.
4633fb97780Smrg	 if test -z "$doit"; then
4643fb97780Smrg	   : >"$dsttmp" # No need to fork-exec 'touch'.
4653fb97780Smrg	 else
4663fb97780Smrg	   $doit touch "$dsttmp"
4673fb97780Smrg	 fi
4683fb97780Smrg       }
4693fb97780Smrg     } &&
4703fb97780Smrg     $doit_exec $cpprog "$src" "$dsttmp") &&
471126a8a12Smrg
472126a8a12Smrg    # and set any options; do chmod last to preserve setuid bits.
473126a8a12Smrg    #
474126a8a12Smrg    # If any of these fail, we abort the whole thing.  If we want to
475126a8a12Smrg    # ignore errors from any of these, just make sure not to ignore
476126a8a12Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
477126a8a12Smrg    #
478d656433aSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
479d656433aSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
480d656433aSmrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
481d656433aSmrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
482d656433aSmrg
483d656433aSmrg    # If -C, don't bother to copy if it wouldn't change the file.
484d656433aSmrg    if $copy_on_change &&
485300346aeSmrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
486300346aeSmrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
487300346aeSmrg       set -f &&
488d656433aSmrg       set X $old && old=:$2:$4:$5:$6 &&
489d656433aSmrg       set X $new && new=:$2:$4:$5:$6 &&
490300346aeSmrg       set +f &&
491d656433aSmrg       test "$old" = "$new" &&
492d656433aSmrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
493d656433aSmrg    then
494d656433aSmrg      rm -f "$dsttmp"
495d656433aSmrg    else
4963fb97780Smrg      # If $backupsuffix is set, and the file being installed
4973fb97780Smrg      # already exists, attempt a backup.  Don't worry if it fails,
4983fb97780Smrg      # e.g., if mv doesn't support -f.
4993fb97780Smrg      if test -n "$backupsuffix" && test -f "$dst"; then
5003fb97780Smrg        $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null
5013fb97780Smrg      fi
5023fb97780Smrg
503d656433aSmrg      # Rename the file to the real destination.
504d656433aSmrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
505d656433aSmrg
506d656433aSmrg      # The rename failed, perhaps because mv can't rename something else
507d656433aSmrg      # to itself, or perhaps because mv is so ancient that it does not
508d656433aSmrg      # support -f.
509d656433aSmrg      {
510300346aeSmrg        # Now remove or move aside any old file at destination location.
511300346aeSmrg        # We try this two ways since rm can't unlink itself on some
512300346aeSmrg        # systems and the destination file might be busy for other
513300346aeSmrg        # reasons.  In this case, the final cleanup might fail but the new
514300346aeSmrg        # file should still install successfully.
515300346aeSmrg        {
516300346aeSmrg          test ! -f "$dst" ||
5173fb97780Smrg          $doit $rmcmd "$dst" 2>/dev/null ||
518300346aeSmrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
5193fb97780Smrg            { $doit $rmcmd "$rmtmp" 2>/dev/null; :; }
520300346aeSmrg          } ||
521300346aeSmrg          { echo "$0: cannot unlink or rename $dst" >&2
522300346aeSmrg            (exit 1); exit 1
523300346aeSmrg          }
524300346aeSmrg        } &&
525300346aeSmrg
526300346aeSmrg        # Now rename the file to the real destination.
527300346aeSmrg        $doit $mvcmd "$dsttmp" "$dst"
528d656433aSmrg      }
529d656433aSmrg    fi || exit 1
530126a8a12Smrg
531126a8a12Smrg    trap '' 0
532126a8a12Smrg  fi
533126a8a12Smrgdone
534126a8a12Smrg
535126a8a12Smrg# Local variables:
5363fb97780Smrg# eval: (add-hook 'before-save-hook 'time-stamp)
537126a8a12Smrg# time-stamp-start: "scriptversion="
538126a8a12Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
5393fb97780Smrg# time-stamp-time-zone: "UTC0"
540d656433aSmrg# time-stamp-end: "; # UTC"
541126a8a12Smrg# End:
542