install-sh revision 688b3aec
1e9554658Smrg#!/bin/sh
2e9554658Smrg# install - install a program, script, or datafile
3e9554658Smrg
4688b3aecSmrgscriptversion=2013-12-25.23; # UTC
5e9554658Smrg
6e9554658Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7e9554658Smrg# later released in X11R6 (xc/config/util/install.sh) with the
8e9554658Smrg# following copyright and license.
9e9554658Smrg#
10e9554658Smrg# Copyright (C) 1994 X Consortium
11e9554658Smrg#
12e9554658Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13e9554658Smrg# of this software and associated documentation files (the "Software"), to
14e9554658Smrg# deal in the Software without restriction, including without limitation the
15e9554658Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16e9554658Smrg# sell copies of the Software, and to permit persons to whom the Software is
17e9554658Smrg# furnished to do so, subject to the following conditions:
18e9554658Smrg#
19e9554658Smrg# The above copyright notice and this permission notice shall be included in
20e9554658Smrg# all copies or substantial portions of the Software.
21e9554658Smrg#
22e9554658Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23e9554658Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24e9554658Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25e9554658Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26e9554658Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27e9554658Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28e9554658Smrg#
29e9554658Smrg# Except as contained in this notice, the name of the X Consortium shall not
30e9554658Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
31e9554658Smrg# ings in this Software without prior written authorization from the X Consor-
32e9554658Smrg# tium.
33e9554658Smrg#
34e9554658Smrg#
35e9554658Smrg# FSF changes to this file are in the public domain.
36e9554658Smrg#
37e9554658Smrg# Calling this script install-sh is preferred over install.sh, to prevent
385dd5f640Smrg# 'make' implicit rules from creating a file called install from it
39e9554658Smrg# when there is no Makefile.
40e9554658Smrg#
41e9554658Smrg# This script is compatible with the BSD install script, but was written
42e9554658Smrg# from scratch.
43e9554658Smrg
44688b3aecSmrgtab='	'
45e9554658Smrgnl='
46e9554658Smrg'
47688b3aecSmrgIFS=" $tab$nl"
48e9554658Smrg
49688b3aecSmrg# Set DOITPROG to "echo" to test this script.
50e9554658Smrg
51c2b339b4Smrgdoit=${DOITPROG-}
52688b3aecSmrgdoit_exec=${doit:-exec}
53e9554658Smrg
54e9554658Smrg# Put in absolute file names if you don't have them in your path;
55e9554658Smrg# or use environment vars.
56e9554658Smrg
57c2b339b4Smrgchgrpprog=${CHGRPPROG-chgrp}
58c2b339b4Smrgchmodprog=${CHMODPROG-chmod}
59c2b339b4Smrgchownprog=${CHOWNPROG-chown}
60c2b339b4Smrgcmpprog=${CMPPROG-cmp}
61c2b339b4Smrgcpprog=${CPPROG-cp}
62c2b339b4Smrgmkdirprog=${MKDIRPROG-mkdir}
63c2b339b4Smrgmvprog=${MVPROG-mv}
64c2b339b4Smrgrmprog=${RMPROG-rm}
65c2b339b4Smrgstripprog=${STRIPPROG-strip}
66c2b339b4Smrg
67e9554658Smrgposix_mkdir=
68e9554658Smrg
69e9554658Smrg# Desired mode of installed file.
70e9554658Smrgmode=0755
71e9554658Smrg
72c2b339b4Smrgchgrpcmd=
73e9554658Smrgchmodcmd=$chmodprog
74e9554658Smrgchowncmd=
75c2b339b4Smrgmvcmd=$mvprog
76e9554658Smrgrmcmd="$rmprog -f"
77c2b339b4Smrgstripcmd=
78c2b339b4Smrg
79e9554658Smrgsrc=
80e9554658Smrgdst=
81e9554658Smrgdir_arg=
82c2b339b4Smrgdst_arg=
83c2b339b4Smrg
84c2b339b4Smrgcopy_on_change=false
85688b3aecSmrgis_target_a_directory=possibly
86e9554658Smrg
87c2b339b4Smrgusage="\
88c2b339b4SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
89e9554658Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
90e9554658Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
91e9554658Smrg   or: $0 [OPTION]... -d DIRECTORIES...
92e9554658Smrg
93e9554658SmrgIn the 1st form, copy SRCFILE to DSTFILE.
94e9554658SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
95e9554658SmrgIn the 4th, create DIRECTORIES.
96e9554658Smrg
97e9554658SmrgOptions:
98c2b339b4Smrg     --help     display this help and exit.
99c2b339b4Smrg     --version  display version info and exit.
100c2b339b4Smrg
101c2b339b4Smrg  -c            (ignored)
102c2b339b4Smrg  -C            install only if different (preserve the last data modification time)
103c2b339b4Smrg  -d            create directories instead of installing files.
104c2b339b4Smrg  -g GROUP      $chgrpprog installed files to GROUP.
105c2b339b4Smrg  -m MODE       $chmodprog installed files to MODE.
106c2b339b4Smrg  -o USER       $chownprog installed files to USER.
107c2b339b4Smrg  -s            $stripprog installed files.
108c2b339b4Smrg  -t DIRECTORY  install into DIRECTORY.
109c2b339b4Smrg  -T            report an error if DSTFILE is a directory.
110e9554658Smrg
111e9554658SmrgEnvironment variables override the default commands:
112c2b339b4Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
113c2b339b4Smrg  RMPROG STRIPPROG
114e9554658Smrg"
115e9554658Smrg
116e9554658Smrgwhile test $# -ne 0; do
117e9554658Smrg  case $1 in
118c2b339b4Smrg    -c) ;;
119c2b339b4Smrg
120c2b339b4Smrg    -C) copy_on_change=true;;
121e9554658Smrg
122c2b339b4Smrg    -d) dir_arg=true;;
123e9554658Smrg
124e9554658Smrg    -g) chgrpcmd="$chgrpprog $2"
125688b3aecSmrg        shift;;
126e9554658Smrg
127e9554658Smrg    --help) echo "$usage"; exit $?;;
128e9554658Smrg
129e9554658Smrg    -m) mode=$2
130688b3aecSmrg        case $mode in
131688b3aecSmrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
132688b3aecSmrg            echo "$0: invalid mode: $mode" >&2
133688b3aecSmrg            exit 1;;
134688b3aecSmrg        esac
135688b3aecSmrg        shift;;
136e9554658Smrg
137e9554658Smrg    -o) chowncmd="$chownprog $2"
138688b3aecSmrg        shift;;
139e9554658Smrg
140c2b339b4Smrg    -s) stripcmd=$stripprog;;
141e9554658Smrg
142688b3aecSmrg    -t)
143688b3aecSmrg        is_target_a_directory=always
144688b3aecSmrg        dst_arg=$2
145688b3aecSmrg        # Protect names problematic for 'test' and other utilities.
146688b3aecSmrg        case $dst_arg in
147688b3aecSmrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
148688b3aecSmrg        esac
149688b3aecSmrg        shift;;
150e9554658Smrg
151688b3aecSmrg    -T) is_target_a_directory=never;;
152e9554658Smrg
153e9554658Smrg    --version) echo "$0 $scriptversion"; exit $?;;
154e9554658Smrg
155688b3aecSmrg    --) shift
156688b3aecSmrg        break;;
157e9554658Smrg
158688b3aecSmrg    -*) echo "$0: invalid option: $1" >&2
159688b3aecSmrg        exit 1;;
160e9554658Smrg
161e9554658Smrg    *)  break;;
162e9554658Smrg  esac
163c2b339b4Smrg  shift
164e9554658Smrgdone
165e9554658Smrg
166688b3aecSmrg# We allow the use of options -d and -T together, by making -d
167688b3aecSmrg# take the precedence; this is for compatibility with GNU install.
168688b3aecSmrg
169688b3aecSmrgif test -n "$dir_arg"; then
170688b3aecSmrg  if test -n "$dst_arg"; then
171688b3aecSmrg    echo "$0: target directory not allowed when installing a directory." >&2
172688b3aecSmrg    exit 1
173688b3aecSmrg  fi
174688b3aecSmrgfi
175688b3aecSmrg
176c2b339b4Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
177e9554658Smrg  # When -d is used, all remaining arguments are directories to create.
178e9554658Smrg  # When -t is used, the destination is already specified.
179e9554658Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
180e9554658Smrg  for arg
181e9554658Smrg  do
182c2b339b4Smrg    if test -n "$dst_arg"; then
183e9554658Smrg      # $@ is not empty: it contains at least $arg.
184c2b339b4Smrg      set fnord "$@" "$dst_arg"
185e9554658Smrg      shift # fnord
186e9554658Smrg    fi
187e9554658Smrg    shift # arg
188c2b339b4Smrg    dst_arg=$arg
1895dd5f640Smrg    # Protect names problematic for 'test' and other utilities.
1905dd5f640Smrg    case $dst_arg in
1915dd5f640Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
1925dd5f640Smrg    esac
193e9554658Smrg  done
194e9554658Smrgfi
195e9554658Smrg
196e9554658Smrgif test $# -eq 0; then
197e9554658Smrg  if test -z "$dir_arg"; then
198e9554658Smrg    echo "$0: no input file specified." >&2
199e9554658Smrg    exit 1
200e9554658Smrg  fi
2015dd5f640Smrg  # It's OK to call 'install-sh -d' without argument.
202e9554658Smrg  # This can happen when creating conditional directories.
203e9554658Smrg  exit 0
204e9554658Smrgfi
205e9554658Smrg
206688b3aecSmrgif test -z "$dir_arg"; then
207688b3aecSmrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
208688b3aecSmrg    if test ! -d "$dst_arg"; then
209688b3aecSmrg      echo "$0: $dst_arg: Is not a directory." >&2
210688b3aecSmrg      exit 1
211688b3aecSmrg    fi
212688b3aecSmrg  fi
213688b3aecSmrgfi
214688b3aecSmrg
215e9554658Smrgif test -z "$dir_arg"; then
2165dd5f640Smrg  do_exit='(exit $ret); exit $ret'
2175dd5f640Smrg  trap "ret=129; $do_exit" 1
2185dd5f640Smrg  trap "ret=130; $do_exit" 2
2195dd5f640Smrg  trap "ret=141; $do_exit" 13
2205dd5f640Smrg  trap "ret=143; $do_exit" 15
221e9554658Smrg
222e9554658Smrg  # Set umask so as not to create temps with too-generous modes.
223e9554658Smrg  # However, 'strip' requires both read and write access to temps.
224e9554658Smrg  case $mode in
225e9554658Smrg    # Optimize common cases.
226e9554658Smrg    *644) cp_umask=133;;
227e9554658Smrg    *755) cp_umask=22;;
228e9554658Smrg
229e9554658Smrg    *[0-7])
230e9554658Smrg      if test -z "$stripcmd"; then
231688b3aecSmrg        u_plus_rw=
232e9554658Smrg      else
233688b3aecSmrg        u_plus_rw='% 200'
234e9554658Smrg      fi
235e9554658Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
236e9554658Smrg    *)
237e9554658Smrg      if test -z "$stripcmd"; then
238688b3aecSmrg        u_plus_rw=
239e9554658Smrg      else
240688b3aecSmrg        u_plus_rw=,u+rw
241e9554658Smrg      fi
242e9554658Smrg      cp_umask=$mode$u_plus_rw;;
243e9554658Smrg  esac
244e9554658Smrgfi
245e9554658Smrg
246e9554658Smrgfor src
247e9554658Smrgdo
2485dd5f640Smrg  # Protect names problematic for 'test' and other utilities.
249e9554658Smrg  case $src in
2505dd5f640Smrg    -* | [=\(\)!]) src=./$src;;
251e9554658Smrg  esac
252e9554658Smrg
253e9554658Smrg  if test -n "$dir_arg"; then
254e9554658Smrg    dst=$src
255e9554658Smrg    dstdir=$dst
256e9554658Smrg    test -d "$dstdir"
257e9554658Smrg    dstdir_status=$?
258e9554658Smrg  else
259e9554658Smrg
260e9554658Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
261e9554658Smrg    # might cause directories to be created, which would be especially bad
262e9554658Smrg    # if $src (and thus $dsttmp) contains '*'.
263e9554658Smrg    if test ! -f "$src" && test ! -d "$src"; then
264e9554658Smrg      echo "$0: $src does not exist." >&2
265e9554658Smrg      exit 1
266e9554658Smrg    fi
267e9554658Smrg
268c2b339b4Smrg    if test -z "$dst_arg"; then
269e9554658Smrg      echo "$0: no destination specified." >&2
270e9554658Smrg      exit 1
271e9554658Smrg    fi
272c2b339b4Smrg    dst=$dst_arg
273e9554658Smrg
274e9554658Smrg    # If destination is a directory, append the input filename; won't work
275e9554658Smrg    # if double slashes aren't ignored.
276e9554658Smrg    if test -d "$dst"; then
277688b3aecSmrg      if test "$is_target_a_directory" = never; then
278688b3aecSmrg        echo "$0: $dst_arg: Is a directory" >&2
279688b3aecSmrg        exit 1
280e9554658Smrg      fi
281e9554658Smrg      dstdir=$dst
282e9554658Smrg      dst=$dstdir/`basename "$src"`
283e9554658Smrg      dstdir_status=0
284e9554658Smrg    else
285688b3aecSmrg      dstdir=`dirname "$dst"`
286e9554658Smrg      test -d "$dstdir"
287e9554658Smrg      dstdir_status=$?
288e9554658Smrg    fi
289e9554658Smrg  fi
290e9554658Smrg
291e9554658Smrg  obsolete_mkdir_used=false
292e9554658Smrg
293e9554658Smrg  if test $dstdir_status != 0; then
294e9554658Smrg    case $posix_mkdir in
295e9554658Smrg      '')
296688b3aecSmrg        # Create intermediate dirs using mode 755 as modified by the umask.
297688b3aecSmrg        # This is like FreeBSD 'install' as of 1997-10-28.
298688b3aecSmrg        umask=`umask`
299688b3aecSmrg        case $stripcmd.$umask in
300688b3aecSmrg          # Optimize common cases.
301688b3aecSmrg          *[2367][2367]) mkdir_umask=$umask;;
302688b3aecSmrg          .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
303688b3aecSmrg
304688b3aecSmrg          *[0-7])
305688b3aecSmrg            mkdir_umask=`expr $umask + 22 \
306688b3aecSmrg              - $umask % 100 % 40 + $umask % 20 \
307688b3aecSmrg              - $umask % 10 % 4 + $umask % 2
308688b3aecSmrg            `;;
309688b3aecSmrg          *) mkdir_umask=$umask,go-w;;
310688b3aecSmrg        esac
311688b3aecSmrg
312688b3aecSmrg        # With -d, create the new directory with the user-specified mode.
313688b3aecSmrg        # Otherwise, rely on $mkdir_umask.
314688b3aecSmrg        if test -n "$dir_arg"; then
315688b3aecSmrg          mkdir_mode=-m$mode
316688b3aecSmrg        else
317688b3aecSmrg          mkdir_mode=
318688b3aecSmrg        fi
319688b3aecSmrg
320688b3aecSmrg        posix_mkdir=false
321688b3aecSmrg        case $umask in
322688b3aecSmrg          *[123567][0-7][0-7])
323688b3aecSmrg            # POSIX mkdir -p sets u+wx bits regardless of umask, which
324688b3aecSmrg            # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
325688b3aecSmrg            ;;
326688b3aecSmrg          *)
327688b3aecSmrg            tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
328688b3aecSmrg            trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
329688b3aecSmrg
330688b3aecSmrg            if (umask $mkdir_umask &&
331688b3aecSmrg                exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
332688b3aecSmrg            then
333688b3aecSmrg              if test -z "$dir_arg" || {
334688b3aecSmrg                   # Check for POSIX incompatibilities with -m.
335688b3aecSmrg                   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
336688b3aecSmrg                   # other-writable bit of parent directory when it shouldn't.
337688b3aecSmrg                   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
338688b3aecSmrg                   ls_ld_tmpdir=`ls -ld "$tmpdir"`
339688b3aecSmrg                   case $ls_ld_tmpdir in
340688b3aecSmrg                     d????-?r-*) different_mode=700;;
341688b3aecSmrg                     d????-?--*) different_mode=755;;
342688b3aecSmrg                     *) false;;
343688b3aecSmrg                   esac &&
344688b3aecSmrg                   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
345688b3aecSmrg                     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
346688b3aecSmrg                     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
347688b3aecSmrg                   }
348688b3aecSmrg                 }
349688b3aecSmrg              then posix_mkdir=:
350688b3aecSmrg              fi
351688b3aecSmrg              rmdir "$tmpdir/d" "$tmpdir"
352688b3aecSmrg            else
353688b3aecSmrg              # Remove any dirs left behind by ancient mkdir implementations.
354688b3aecSmrg              rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
355688b3aecSmrg            fi
356688b3aecSmrg            trap '' 0;;
357688b3aecSmrg        esac;;
358e9554658Smrg    esac
359e9554658Smrg
360e9554658Smrg    if
361e9554658Smrg      $posix_mkdir && (
362688b3aecSmrg        umask $mkdir_umask &&
363688b3aecSmrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
364e9554658Smrg      )
365e9554658Smrg    then :
366e9554658Smrg    else
367e9554658Smrg
368e9554658Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
369e9554658Smrg      # or it failed possibly due to a race condition.  Create the
370e9554658Smrg      # directory the slow way, step by step, checking for races as we go.
371e9554658Smrg
372e9554658Smrg      case $dstdir in
373688b3aecSmrg        /*) prefix='/';;
374688b3aecSmrg        [-=\(\)!]*) prefix='./';;
375688b3aecSmrg        *)  prefix='';;
376e9554658Smrg      esac
377e9554658Smrg
378e9554658Smrg      oIFS=$IFS
379e9554658Smrg      IFS=/
380688b3aecSmrg      set -f
381e9554658Smrg      set fnord $dstdir
382e9554658Smrg      shift
383688b3aecSmrg      set +f
384e9554658Smrg      IFS=$oIFS
385e9554658Smrg
386e9554658Smrg      prefixes=
387e9554658Smrg
388e9554658Smrg      for d
389e9554658Smrg      do
390688b3aecSmrg        test X"$d" = X && continue
391688b3aecSmrg
392688b3aecSmrg        prefix=$prefix$d
393688b3aecSmrg        if test -d "$prefix"; then
394688b3aecSmrg          prefixes=
395688b3aecSmrg        else
396688b3aecSmrg          if $posix_mkdir; then
397688b3aecSmrg            (umask=$mkdir_umask &&
398688b3aecSmrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
399688b3aecSmrg            # Don't fail if two instances are running concurrently.
400688b3aecSmrg            test -d "$prefix" || exit 1
401688b3aecSmrg          else
402688b3aecSmrg            case $prefix in
403688b3aecSmrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
404688b3aecSmrg              *) qprefix=$prefix;;
405688b3aecSmrg            esac
406688b3aecSmrg            prefixes="$prefixes '$qprefix'"
407688b3aecSmrg          fi
408688b3aecSmrg        fi
409688b3aecSmrg        prefix=$prefix/
410e9554658Smrg      done
411e9554658Smrg
412e9554658Smrg      if test -n "$prefixes"; then
413688b3aecSmrg        # Don't fail if two instances are running concurrently.
414688b3aecSmrg        (umask $mkdir_umask &&
415688b3aecSmrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
416688b3aecSmrg          test -d "$dstdir" || exit 1
417688b3aecSmrg        obsolete_mkdir_used=true
418e9554658Smrg      fi
419e9554658Smrg    fi
420e9554658Smrg  fi
421e9554658Smrg
422e9554658Smrg  if test -n "$dir_arg"; then
423e9554658Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
424e9554658Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
425e9554658Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
426e9554658Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
427e9554658Smrg  else
428e9554658Smrg
429e9554658Smrg    # Make a couple of temp file names in the proper directory.
430e9554658Smrg    dsttmp=$dstdir/_inst.$$_
431e9554658Smrg    rmtmp=$dstdir/_rm.$$_
432e9554658Smrg
433e9554658Smrg    # Trap to clean up those temp files at exit.
434e9554658Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
435e9554658Smrg
436e9554658Smrg    # Copy the file name to the temp name.
437e9554658Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
438e9554658Smrg
439e9554658Smrg    # and set any options; do chmod last to preserve setuid bits.
440e9554658Smrg    #
441e9554658Smrg    # If any of these fail, we abort the whole thing.  If we want to
442e9554658Smrg    # ignore errors from any of these, just make sure not to ignore
443e9554658Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
444e9554658Smrg    #
445c2b339b4Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
446c2b339b4Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
447c2b339b4Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
448c2b339b4Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
449c2b339b4Smrg
450c2b339b4Smrg    # If -C, don't bother to copy if it wouldn't change the file.
451c2b339b4Smrg    if $copy_on_change &&
452688b3aecSmrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
453688b3aecSmrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
454688b3aecSmrg       set -f &&
455c2b339b4Smrg       set X $old && old=:$2:$4:$5:$6 &&
456c2b339b4Smrg       set X $new && new=:$2:$4:$5:$6 &&
457688b3aecSmrg       set +f &&
458c2b339b4Smrg       test "$old" = "$new" &&
459c2b339b4Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
460c2b339b4Smrg    then
461c2b339b4Smrg      rm -f "$dsttmp"
462c2b339b4Smrg    else
463c2b339b4Smrg      # Rename the file to the real destination.
464c2b339b4Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
465c2b339b4Smrg
466c2b339b4Smrg      # The rename failed, perhaps because mv can't rename something else
467c2b339b4Smrg      # to itself, or perhaps because mv is so ancient that it does not
468c2b339b4Smrg      # support -f.
469c2b339b4Smrg      {
470688b3aecSmrg        # Now remove or move aside any old file at destination location.
471688b3aecSmrg        # We try this two ways since rm can't unlink itself on some
472688b3aecSmrg        # systems and the destination file might be busy for other
473688b3aecSmrg        # reasons.  In this case, the final cleanup might fail but the new
474688b3aecSmrg        # file should still install successfully.
475688b3aecSmrg        {
476688b3aecSmrg          test ! -f "$dst" ||
477688b3aecSmrg          $doit $rmcmd -f "$dst" 2>/dev/null ||
478688b3aecSmrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
479688b3aecSmrg            { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
480688b3aecSmrg          } ||
481688b3aecSmrg          { echo "$0: cannot unlink or rename $dst" >&2
482688b3aecSmrg            (exit 1); exit 1
483688b3aecSmrg          }
484688b3aecSmrg        } &&
485688b3aecSmrg
486688b3aecSmrg        # Now rename the file to the real destination.
487688b3aecSmrg        $doit $mvcmd "$dsttmp" "$dst"
488c2b339b4Smrg      }
489c2b339b4Smrg    fi || exit 1
490e9554658Smrg
491e9554658Smrg    trap '' 0
492e9554658Smrg  fi
493e9554658Smrgdone
494e9554658Smrg
495e9554658Smrg# Local variables:
496e9554658Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
497e9554658Smrg# time-stamp-start: "scriptversion="
498e9554658Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
499c2b339b4Smrg# time-stamp-time-zone: "UTC"
500c2b339b4Smrg# time-stamp-end: "; # UTC"
501e9554658Smrg# End:
502