install-sh revision a3129944
1266e564dSmrg#!/bin/sh
2266e564dSmrg# install - install a program, script, or datafile
3266e564dSmrg
4a3129944Smrgscriptversion=2013-12-25.23; # UTC
5266e564dSmrg
6266e564dSmrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7266e564dSmrg# later released in X11R6 (xc/config/util/install.sh) with the
8266e564dSmrg# following copyright and license.
9266e564dSmrg#
10266e564dSmrg# Copyright (C) 1994 X Consortium
11266e564dSmrg#
12266e564dSmrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13266e564dSmrg# of this software and associated documentation files (the "Software"), to
14266e564dSmrg# deal in the Software without restriction, including without limitation the
15266e564dSmrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16266e564dSmrg# sell copies of the Software, and to permit persons to whom the Software is
17266e564dSmrg# furnished to do so, subject to the following conditions:
18266e564dSmrg#
19266e564dSmrg# The above copyright notice and this permission notice shall be included in
20266e564dSmrg# all copies or substantial portions of the Software.
21266e564dSmrg#
22266e564dSmrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23266e564dSmrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24266e564dSmrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25266e564dSmrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26266e564dSmrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27266e564dSmrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28266e564dSmrg#
29266e564dSmrg# Except as contained in this notice, the name of the X Consortium shall not
30266e564dSmrg# be used in advertising or otherwise to promote the sale, use or other deal-
31266e564dSmrg# ings in this Software without prior written authorization from the X Consor-
32266e564dSmrg# tium.
33266e564dSmrg#
34266e564dSmrg#
35266e564dSmrg# FSF changes to this file are in the public domain.
36266e564dSmrg#
37266e564dSmrg# Calling this script install-sh is preferred over install.sh, to prevent
38fb5e8d76Smrg# 'make' implicit rules from creating a file called install from it
39266e564dSmrg# when there is no Makefile.
40266e564dSmrg#
41266e564dSmrg# This script is compatible with the BSD install script, but was written
42266e564dSmrg# from scratch.
43266e564dSmrg
44a3129944Smrgtab='	'
45266e564dSmrgnl='
46266e564dSmrg'
47a3129944SmrgIFS=" $tab$nl"
48266e564dSmrg
49a3129944Smrg# Set DOITPROG to "echo" to test this script.
50266e564dSmrg
51c5629e66Smrgdoit=${DOITPROG-}
52a3129944Smrgdoit_exec=${doit:-exec}
53266e564dSmrg
54266e564dSmrg# Put in absolute file names if you don't have them in your path;
55266e564dSmrg# or use environment vars.
56266e564dSmrg
57c5629e66Smrgchgrpprog=${CHGRPPROG-chgrp}
58c5629e66Smrgchmodprog=${CHMODPROG-chmod}
59c5629e66Smrgchownprog=${CHOWNPROG-chown}
60c5629e66Smrgcmpprog=${CMPPROG-cmp}
61c5629e66Smrgcpprog=${CPPROG-cp}
62c5629e66Smrgmkdirprog=${MKDIRPROG-mkdir}
63c5629e66Smrgmvprog=${MVPROG-mv}
64c5629e66Smrgrmprog=${RMPROG-rm}
65c5629e66Smrgstripprog=${STRIPPROG-strip}
66c5629e66Smrg
67266e564dSmrgposix_mkdir=
68266e564dSmrg
69266e564dSmrg# Desired mode of installed file.
70266e564dSmrgmode=0755
71266e564dSmrg
72c5629e66Smrgchgrpcmd=
73266e564dSmrgchmodcmd=$chmodprog
74266e564dSmrgchowncmd=
75c5629e66Smrgmvcmd=$mvprog
76266e564dSmrgrmcmd="$rmprog -f"
77c5629e66Smrgstripcmd=
78c5629e66Smrg
79266e564dSmrgsrc=
80266e564dSmrgdst=
81266e564dSmrgdir_arg=
82c5629e66Smrgdst_arg=
83c5629e66Smrg
84c5629e66Smrgcopy_on_change=false
85a3129944Smrgis_target_a_directory=possibly
86266e564dSmrg
87c5629e66Smrgusage="\
88c5629e66SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
89266e564dSmrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
90266e564dSmrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
91266e564dSmrg   or: $0 [OPTION]... -d DIRECTORIES...
92266e564dSmrg
93266e564dSmrgIn the 1st form, copy SRCFILE to DSTFILE.
94266e564dSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
95266e564dSmrgIn the 4th, create DIRECTORIES.
96266e564dSmrg
97266e564dSmrgOptions:
98c5629e66Smrg     --help     display this help and exit.
99c5629e66Smrg     --version  display version info and exit.
100c5629e66Smrg
101c5629e66Smrg  -c            (ignored)
102c5629e66Smrg  -C            install only if different (preserve the last data modification time)
103c5629e66Smrg  -d            create directories instead of installing files.
104c5629e66Smrg  -g GROUP      $chgrpprog installed files to GROUP.
105c5629e66Smrg  -m MODE       $chmodprog installed files to MODE.
106c5629e66Smrg  -o USER       $chownprog installed files to USER.
107c5629e66Smrg  -s            $stripprog installed files.
108c5629e66Smrg  -t DIRECTORY  install into DIRECTORY.
109c5629e66Smrg  -T            report an error if DSTFILE is a directory.
110266e564dSmrg
111266e564dSmrgEnvironment variables override the default commands:
112c5629e66Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
113c5629e66Smrg  RMPROG STRIPPROG
114266e564dSmrg"
115266e564dSmrg
116266e564dSmrgwhile test $# -ne 0; do
117266e564dSmrg  case $1 in
118c5629e66Smrg    -c) ;;
119c5629e66Smrg
120c5629e66Smrg    -C) copy_on_change=true;;
121266e564dSmrg
122c5629e66Smrg    -d) dir_arg=true;;
123266e564dSmrg
124266e564dSmrg    -g) chgrpcmd="$chgrpprog $2"
125a3129944Smrg        shift;;
126266e564dSmrg
127266e564dSmrg    --help) echo "$usage"; exit $?;;
128266e564dSmrg
129266e564dSmrg    -m) mode=$2
130a3129944Smrg        case $mode in
131a3129944Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
132a3129944Smrg            echo "$0: invalid mode: $mode" >&2
133a3129944Smrg            exit 1;;
134a3129944Smrg        esac
135a3129944Smrg        shift;;
136266e564dSmrg
137266e564dSmrg    -o) chowncmd="$chownprog $2"
138a3129944Smrg        shift;;
139266e564dSmrg
140c5629e66Smrg    -s) stripcmd=$stripprog;;
141266e564dSmrg
142a3129944Smrg    -t)
143a3129944Smrg        is_target_a_directory=always
144a3129944Smrg        dst_arg=$2
145a3129944Smrg        # Protect names problematic for 'test' and other utilities.
146a3129944Smrg        case $dst_arg in
147a3129944Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
148a3129944Smrg        esac
149a3129944Smrg        shift;;
150266e564dSmrg
151a3129944Smrg    -T) is_target_a_directory=never;;
152266e564dSmrg
153266e564dSmrg    --version) echo "$0 $scriptversion"; exit $?;;
154266e564dSmrg
155a3129944Smrg    --) shift
156a3129944Smrg        break;;
157266e564dSmrg
158a3129944Smrg    -*) echo "$0: invalid option: $1" >&2
159a3129944Smrg        exit 1;;
160266e564dSmrg
161266e564dSmrg    *)  break;;
162266e564dSmrg  esac
163c5629e66Smrg  shift
164266e564dSmrgdone
165266e564dSmrg
166a3129944Smrg# We allow the use of options -d and -T together, by making -d
167a3129944Smrg# take the precedence; this is for compatibility with GNU install.
168a3129944Smrg
169a3129944Smrgif test -n "$dir_arg"; then
170a3129944Smrg  if test -n "$dst_arg"; then
171a3129944Smrg    echo "$0: target directory not allowed when installing a directory." >&2
172a3129944Smrg    exit 1
173a3129944Smrg  fi
174a3129944Smrgfi
175a3129944Smrg
176c5629e66Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
177266e564dSmrg  # When -d is used, all remaining arguments are directories to create.
178266e564dSmrg  # When -t is used, the destination is already specified.
179266e564dSmrg  # Otherwise, the last argument is the destination.  Remove it from $@.
180266e564dSmrg  for arg
181266e564dSmrg  do
182c5629e66Smrg    if test -n "$dst_arg"; then
183266e564dSmrg      # $@ is not empty: it contains at least $arg.
184c5629e66Smrg      set fnord "$@" "$dst_arg"
185266e564dSmrg      shift # fnord
186266e564dSmrg    fi
187266e564dSmrg    shift # arg
188c5629e66Smrg    dst_arg=$arg
189fb5e8d76Smrg    # Protect names problematic for 'test' and other utilities.
190fb5e8d76Smrg    case $dst_arg in
191fb5e8d76Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
192fb5e8d76Smrg    esac
193266e564dSmrg  done
194266e564dSmrgfi
195266e564dSmrg
196266e564dSmrgif test $# -eq 0; then
197266e564dSmrg  if test -z "$dir_arg"; then
198266e564dSmrg    echo "$0: no input file specified." >&2
199266e564dSmrg    exit 1
200266e564dSmrg  fi
201fb5e8d76Smrg  # It's OK to call 'install-sh -d' without argument.
202266e564dSmrg  # This can happen when creating conditional directories.
203266e564dSmrg  exit 0
204266e564dSmrgfi
205266e564dSmrg
206a3129944Smrgif test -z "$dir_arg"; then
207a3129944Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
208a3129944Smrg    if test ! -d "$dst_arg"; then
209a3129944Smrg      echo "$0: $dst_arg: Is not a directory." >&2
210a3129944Smrg      exit 1
211a3129944Smrg    fi
212a3129944Smrg  fi
213a3129944Smrgfi
214a3129944Smrg
215266e564dSmrgif test -z "$dir_arg"; then
216fb5e8d76Smrg  do_exit='(exit $ret); exit $ret'
217fb5e8d76Smrg  trap "ret=129; $do_exit" 1
218fb5e8d76Smrg  trap "ret=130; $do_exit" 2
219fb5e8d76Smrg  trap "ret=141; $do_exit" 13
220fb5e8d76Smrg  trap "ret=143; $do_exit" 15
221266e564dSmrg
222266e564dSmrg  # Set umask so as not to create temps with too-generous modes.
223266e564dSmrg  # However, 'strip' requires both read and write access to temps.
224266e564dSmrg  case $mode in
225266e564dSmrg    # Optimize common cases.
226266e564dSmrg    *644) cp_umask=133;;
227266e564dSmrg    *755) cp_umask=22;;
228266e564dSmrg
229266e564dSmrg    *[0-7])
230266e564dSmrg      if test -z "$stripcmd"; then
231a3129944Smrg        u_plus_rw=
232266e564dSmrg      else
233a3129944Smrg        u_plus_rw='% 200'
234266e564dSmrg      fi
235266e564dSmrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
236266e564dSmrg    *)
237266e564dSmrg      if test -z "$stripcmd"; then
238a3129944Smrg        u_plus_rw=
239266e564dSmrg      else
240a3129944Smrg        u_plus_rw=,u+rw
241266e564dSmrg      fi
242266e564dSmrg      cp_umask=$mode$u_plus_rw;;
243266e564dSmrg  esac
244266e564dSmrgfi
245266e564dSmrg
246266e564dSmrgfor src
247266e564dSmrgdo
248fb5e8d76Smrg  # Protect names problematic for 'test' and other utilities.
249266e564dSmrg  case $src in
250fb5e8d76Smrg    -* | [=\(\)!]) src=./$src;;
251266e564dSmrg  esac
252266e564dSmrg
253266e564dSmrg  if test -n "$dir_arg"; then
254266e564dSmrg    dst=$src
255266e564dSmrg    dstdir=$dst
256266e564dSmrg    test -d "$dstdir"
257266e564dSmrg    dstdir_status=$?
258266e564dSmrg  else
259266e564dSmrg
260266e564dSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
261266e564dSmrg    # might cause directories to be created, which would be especially bad
262266e564dSmrg    # if $src (and thus $dsttmp) contains '*'.
263266e564dSmrg    if test ! -f "$src" && test ! -d "$src"; then
264266e564dSmrg      echo "$0: $src does not exist." >&2
265266e564dSmrg      exit 1
266266e564dSmrg    fi
267266e564dSmrg
268c5629e66Smrg    if test -z "$dst_arg"; then
269266e564dSmrg      echo "$0: no destination specified." >&2
270266e564dSmrg      exit 1
271266e564dSmrg    fi
272c5629e66Smrg    dst=$dst_arg
273266e564dSmrg
274266e564dSmrg    # If destination is a directory, append the input filename; won't work
275266e564dSmrg    # if double slashes aren't ignored.
276266e564dSmrg    if test -d "$dst"; then
277a3129944Smrg      if test "$is_target_a_directory" = never; then
278a3129944Smrg        echo "$0: $dst_arg: Is a directory" >&2
279a3129944Smrg        exit 1
280266e564dSmrg      fi
281266e564dSmrg      dstdir=$dst
282266e564dSmrg      dst=$dstdir/`basename "$src"`
283266e564dSmrg      dstdir_status=0
284266e564dSmrg    else
285a3129944Smrg      dstdir=`dirname "$dst"`
286266e564dSmrg      test -d "$dstdir"
287266e564dSmrg      dstdir_status=$?
288266e564dSmrg    fi
289266e564dSmrg  fi
290266e564dSmrg
291266e564dSmrg  obsolete_mkdir_used=false
292266e564dSmrg
293266e564dSmrg  if test $dstdir_status != 0; then
294266e564dSmrg    case $posix_mkdir in
295266e564dSmrg      '')
296a3129944Smrg        # Create intermediate dirs using mode 755 as modified by the umask.
297a3129944Smrg        # This is like FreeBSD 'install' as of 1997-10-28.
298a3129944Smrg        umask=`umask`
299a3129944Smrg        case $stripcmd.$umask in
300a3129944Smrg          # Optimize common cases.
301a3129944Smrg          *[2367][2367]) mkdir_umask=$umask;;
302a3129944Smrg          .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
303a3129944Smrg
304a3129944Smrg          *[0-7])
305a3129944Smrg            mkdir_umask=`expr $umask + 22 \
306a3129944Smrg              - $umask % 100 % 40 + $umask % 20 \
307a3129944Smrg              - $umask % 10 % 4 + $umask % 2
308a3129944Smrg            `;;
309a3129944Smrg          *) mkdir_umask=$umask,go-w;;
310a3129944Smrg        esac
311a3129944Smrg
312a3129944Smrg        # With -d, create the new directory with the user-specified mode.
313a3129944Smrg        # Otherwise, rely on $mkdir_umask.
314a3129944Smrg        if test -n "$dir_arg"; then
315a3129944Smrg          mkdir_mode=-m$mode
316a3129944Smrg        else
317a3129944Smrg          mkdir_mode=
318a3129944Smrg        fi
319a3129944Smrg
320a3129944Smrg        posix_mkdir=false
321a3129944Smrg        case $umask in
322a3129944Smrg          *[123567][0-7][0-7])
323a3129944Smrg            # POSIX mkdir -p sets u+wx bits regardless of umask, which
324a3129944Smrg            # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
325a3129944Smrg            ;;
326a3129944Smrg          *)
327a3129944Smrg            tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
328a3129944Smrg            trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
329a3129944Smrg
330a3129944Smrg            if (umask $mkdir_umask &&
331a3129944Smrg                exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
332a3129944Smrg            then
333a3129944Smrg              if test -z "$dir_arg" || {
334a3129944Smrg                   # Check for POSIX incompatibilities with -m.
335a3129944Smrg                   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
336a3129944Smrg                   # other-writable bit of parent directory when it shouldn't.
337a3129944Smrg                   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
338a3129944Smrg                   ls_ld_tmpdir=`ls -ld "$tmpdir"`
339a3129944Smrg                   case $ls_ld_tmpdir in
340a3129944Smrg                     d????-?r-*) different_mode=700;;
341a3129944Smrg                     d????-?--*) different_mode=755;;
342a3129944Smrg                     *) false;;
343a3129944Smrg                   esac &&
344a3129944Smrg                   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
345a3129944Smrg                     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
346a3129944Smrg                     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
347a3129944Smrg                   }
348a3129944Smrg                 }
349a3129944Smrg              then posix_mkdir=:
350a3129944Smrg              fi
351a3129944Smrg              rmdir "$tmpdir/d" "$tmpdir"
352a3129944Smrg            else
353a3129944Smrg              # Remove any dirs left behind by ancient mkdir implementations.
354a3129944Smrg              rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
355a3129944Smrg            fi
356a3129944Smrg            trap '' 0;;
357a3129944Smrg        esac;;
358266e564dSmrg    esac
359266e564dSmrg
360266e564dSmrg    if
361266e564dSmrg      $posix_mkdir && (
362a3129944Smrg        umask $mkdir_umask &&
363a3129944Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
364266e564dSmrg      )
365266e564dSmrg    then :
366266e564dSmrg    else
367266e564dSmrg
368266e564dSmrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
369266e564dSmrg      # or it failed possibly due to a race condition.  Create the
370266e564dSmrg      # directory the slow way, step by step, checking for races as we go.
371266e564dSmrg
372266e564dSmrg      case $dstdir in
373a3129944Smrg        /*) prefix='/';;
374a3129944Smrg        [-=\(\)!]*) prefix='./';;
375a3129944Smrg        *)  prefix='';;
376266e564dSmrg      esac
377266e564dSmrg
378266e564dSmrg      oIFS=$IFS
379266e564dSmrg      IFS=/
380a3129944Smrg      set -f
381266e564dSmrg      set fnord $dstdir
382266e564dSmrg      shift
383a3129944Smrg      set +f
384266e564dSmrg      IFS=$oIFS
385266e564dSmrg
386266e564dSmrg      prefixes=
387266e564dSmrg
388266e564dSmrg      for d
389266e564dSmrg      do
390a3129944Smrg        test X"$d" = X && continue
391a3129944Smrg
392a3129944Smrg        prefix=$prefix$d
393a3129944Smrg        if test -d "$prefix"; then
394a3129944Smrg          prefixes=
395a3129944Smrg        else
396a3129944Smrg          if $posix_mkdir; then
397a3129944Smrg            (umask=$mkdir_umask &&
398a3129944Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
399a3129944Smrg            # Don't fail if two instances are running concurrently.
400a3129944Smrg            test -d "$prefix" || exit 1
401a3129944Smrg          else
402a3129944Smrg            case $prefix in
403a3129944Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
404a3129944Smrg              *) qprefix=$prefix;;
405a3129944Smrg            esac
406a3129944Smrg            prefixes="$prefixes '$qprefix'"
407a3129944Smrg          fi
408a3129944Smrg        fi
409a3129944Smrg        prefix=$prefix/
410266e564dSmrg      done
411266e564dSmrg
412266e564dSmrg      if test -n "$prefixes"; then
413a3129944Smrg        # Don't fail if two instances are running concurrently.
414a3129944Smrg        (umask $mkdir_umask &&
415a3129944Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
416a3129944Smrg          test -d "$dstdir" || exit 1
417a3129944Smrg        obsolete_mkdir_used=true
418266e564dSmrg      fi
419266e564dSmrg    fi
420266e564dSmrg  fi
421266e564dSmrg
422266e564dSmrg  if test -n "$dir_arg"; then
423266e564dSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
424266e564dSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
425266e564dSmrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
426266e564dSmrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
427266e564dSmrg  else
428266e564dSmrg
429266e564dSmrg    # Make a couple of temp file names in the proper directory.
430266e564dSmrg    dsttmp=$dstdir/_inst.$$_
431266e564dSmrg    rmtmp=$dstdir/_rm.$$_
432266e564dSmrg
433266e564dSmrg    # Trap to clean up those temp files at exit.
434266e564dSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
435266e564dSmrg
436266e564dSmrg    # Copy the file name to the temp name.
437266e564dSmrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
438266e564dSmrg
439266e564dSmrg    # and set any options; do chmod last to preserve setuid bits.
440266e564dSmrg    #
441266e564dSmrg    # If any of these fail, we abort the whole thing.  If we want to
442266e564dSmrg    # ignore errors from any of these, just make sure not to ignore
443266e564dSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
444266e564dSmrg    #
445c5629e66Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
446c5629e66Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
447c5629e66Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
448c5629e66Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
449c5629e66Smrg
450c5629e66Smrg    # If -C, don't bother to copy if it wouldn't change the file.
451c5629e66Smrg    if $copy_on_change &&
452a3129944Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
453a3129944Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
454a3129944Smrg       set -f &&
455c5629e66Smrg       set X $old && old=:$2:$4:$5:$6 &&
456c5629e66Smrg       set X $new && new=:$2:$4:$5:$6 &&
457a3129944Smrg       set +f &&
458c5629e66Smrg       test "$old" = "$new" &&
459c5629e66Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
460c5629e66Smrg    then
461c5629e66Smrg      rm -f "$dsttmp"
462c5629e66Smrg    else
463c5629e66Smrg      # Rename the file to the real destination.
464c5629e66Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
465c5629e66Smrg
466c5629e66Smrg      # The rename failed, perhaps because mv can't rename something else
467c5629e66Smrg      # to itself, or perhaps because mv is so ancient that it does not
468c5629e66Smrg      # support -f.
469c5629e66Smrg      {
470a3129944Smrg        # Now remove or move aside any old file at destination location.
471a3129944Smrg        # We try this two ways since rm can't unlink itself on some
472a3129944Smrg        # systems and the destination file might be busy for other
473a3129944Smrg        # reasons.  In this case, the final cleanup might fail but the new
474a3129944Smrg        # file should still install successfully.
475a3129944Smrg        {
476a3129944Smrg          test ! -f "$dst" ||
477a3129944Smrg          $doit $rmcmd -f "$dst" 2>/dev/null ||
478a3129944Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
479a3129944Smrg            { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
480a3129944Smrg          } ||
481a3129944Smrg          { echo "$0: cannot unlink or rename $dst" >&2
482a3129944Smrg            (exit 1); exit 1
483a3129944Smrg          }
484a3129944Smrg        } &&
485a3129944Smrg
486a3129944Smrg        # Now rename the file to the real destination.
487a3129944Smrg        $doit $mvcmd "$dsttmp" "$dst"
488c5629e66Smrg      }
489c5629e66Smrg    fi || exit 1
490266e564dSmrg
491266e564dSmrg    trap '' 0
492266e564dSmrg  fi
493266e564dSmrgdone
494266e564dSmrg
495266e564dSmrg# Local variables:
496266e564dSmrg# eval: (add-hook 'write-file-hooks 'time-stamp)
497266e564dSmrg# time-stamp-start: "scriptversion="
498266e564dSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
499d17cd367Smrg# time-stamp-time-zone: "UTC"
500d17cd367Smrg# time-stamp-end: "; # UTC"
501266e564dSmrg# End:
502