install-sh revision b0c24a08
16ea72052Smrg#!/bin/sh
26ea72052Smrg# install - install a program, script, or datafile
3168023feSmrg
4b0c24a08Smrgscriptversion=2013-12-25.23; # UTC
5168023feSmrg
66ea72052Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
76ea72052Smrg# later released in X11R6 (xc/config/util/install.sh) with the
86ea72052Smrg# following copyright and license.
96ea72052Smrg#
106ea72052Smrg# Copyright (C) 1994 X Consortium
116ea72052Smrg#
126ea72052Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
136ea72052Smrg# of this software and associated documentation files (the "Software"), to
146ea72052Smrg# deal in the Software without restriction, including without limitation the
156ea72052Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
166ea72052Smrg# sell copies of the Software, and to permit persons to whom the Software is
176ea72052Smrg# furnished to do so, subject to the following conditions:
186ea72052Smrg#
196ea72052Smrg# The above copyright notice and this permission notice shall be included in
206ea72052Smrg# all copies or substantial portions of the Software.
216ea72052Smrg#
226ea72052Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
236ea72052Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
246ea72052Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
256ea72052Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
266ea72052Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
276ea72052Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
286ea72052Smrg#
296ea72052Smrg# Except as contained in this notice, the name of the X Consortium shall not
306ea72052Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
316ea72052Smrg# ings in this Software without prior written authorization from the X Consor-
326ea72052Smrg# tium.
336ea72052Smrg#
346ea72052Smrg#
356ea72052Smrg# FSF changes to this file are in the public domain.
366ea72052Smrg#
376ea72052Smrg# Calling this script install-sh is preferred over install.sh, to prevent
38b0c24a08Smrg# 'make' implicit rules from creating a file called install from it
396ea72052Smrg# when there is no Makefile.
406ea72052Smrg#
416ea72052Smrg# This script is compatible with the BSD install script, but was written
42168023feSmrg# from scratch.
436ea72052Smrg
44b0c24a08Smrgtab='	'
45168023feSmrgnl='
46168023feSmrg'
47b0c24a08SmrgIFS=" $tab$nl"
486ea72052Smrg
49b0c24a08Smrg# Set DOITPROG to "echo" to test this script.
506ea72052Smrg
51168023feSmrgdoit=${DOITPROG-}
52b0c24a08Smrgdoit_exec=${doit:-exec}
536ea72052Smrg
54168023feSmrg# Put in absolute file names if you don't have them in your path;
55168023feSmrg# or use environment vars.
56168023feSmrg
57168023feSmrgchgrpprog=${CHGRPPROG-chgrp}
58168023feSmrgchmodprog=${CHMODPROG-chmod}
59168023feSmrgchownprog=${CHOWNPROG-chown}
60168023feSmrgcmpprog=${CMPPROG-cmp}
61168023feSmrgcpprog=${CPPROG-cp}
62168023feSmrgmkdirprog=${MKDIRPROG-mkdir}
63168023feSmrgmvprog=${MVPROG-mv}
64168023feSmrgrmprog=${RMPROG-rm}
65168023feSmrgstripprog=${STRIPPROG-strip}
66168023feSmrg
67168023feSmrgposix_mkdir=
68168023feSmrg
69168023feSmrg# Desired mode of installed file.
70168023feSmrgmode=0755
71168023feSmrg
72168023feSmrgchgrpcmd=
73168023feSmrgchmodcmd=$chmodprog
74168023feSmrgchowncmd=
75168023feSmrgmvcmd=$mvprog
76168023feSmrgrmcmd="$rmprog -f"
77168023feSmrgstripcmd=
786ea72052Smrg
79168023feSmrgsrc=
80168023feSmrgdst=
81168023feSmrgdir_arg=
82168023feSmrgdst_arg=
836ea72052Smrg
84168023feSmrgcopy_on_change=false
85b0c24a08Smrgis_target_a_directory=possibly
866ea72052Smrg
87168023feSmrgusage="\
88168023feSmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
89168023feSmrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
90168023feSmrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
91168023feSmrg   or: $0 [OPTION]... -d DIRECTORIES...
926ea72052Smrg
93168023feSmrgIn the 1st form, copy SRCFILE to DSTFILE.
94168023feSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
95168023feSmrgIn the 4th, create DIRECTORIES.
966ea72052Smrg
97168023feSmrgOptions:
98168023feSmrg     --help     display this help and exit.
99168023feSmrg     --version  display version info and exit.
1006ea72052Smrg
101168023feSmrg  -c            (ignored)
102168023feSmrg  -C            install only if different (preserve the last data modification time)
103168023feSmrg  -d            create directories instead of installing files.
104168023feSmrg  -g GROUP      $chgrpprog installed files to GROUP.
105168023feSmrg  -m MODE       $chmodprog installed files to MODE.
106168023feSmrg  -o USER       $chownprog installed files to USER.
107168023feSmrg  -s            $stripprog installed files.
108168023feSmrg  -t DIRECTORY  install into DIRECTORY.
109168023feSmrg  -T            report an error if DSTFILE is a directory.
1106ea72052Smrg
111168023feSmrgEnvironment variables override the default commands:
112168023feSmrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
113168023feSmrg  RMPROG STRIPPROG
114168023feSmrg"
1156ea72052Smrg
116168023feSmrgwhile test $# -ne 0; do
117168023feSmrg  case $1 in
118168023feSmrg    -c) ;;
1196ea72052Smrg
120168023feSmrg    -C) copy_on_change=true;;
1216ea72052Smrg
122168023feSmrg    -d) dir_arg=true;;
1236ea72052Smrg
124168023feSmrg    -g) chgrpcmd="$chgrpprog $2"
125b0c24a08Smrg        shift;;
1266ea72052Smrg
127168023feSmrg    --help) echo "$usage"; exit $?;;
1286ea72052Smrg
129168023feSmrg    -m) mode=$2
130b0c24a08Smrg        case $mode in
131b0c24a08Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
132b0c24a08Smrg            echo "$0: invalid mode: $mode" >&2
133b0c24a08Smrg            exit 1;;
134b0c24a08Smrg        esac
135b0c24a08Smrg        shift;;
1366ea72052Smrg
137168023feSmrg    -o) chowncmd="$chownprog $2"
138b0c24a08Smrg        shift;;
1396ea72052Smrg
140168023feSmrg    -s) stripcmd=$stripprog;;
1416ea72052Smrg
142b0c24a08Smrg    -t)
143b0c24a08Smrg        is_target_a_directory=always
144b0c24a08Smrg        dst_arg=$2
145b0c24a08Smrg        # Protect names problematic for 'test' and other utilities.
146b0c24a08Smrg        case $dst_arg in
147b0c24a08Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
148b0c24a08Smrg        esac
149b0c24a08Smrg        shift;;
1506ea72052Smrg
151b0c24a08Smrg    -T) is_target_a_directory=never;;
1526ea72052Smrg
153168023feSmrg    --version) echo "$0 $scriptversion"; exit $?;;
1546ea72052Smrg
155b0c24a08Smrg    --) shift
156b0c24a08Smrg        break;;
1576ea72052Smrg
158b0c24a08Smrg    -*) echo "$0: invalid option: $1" >&2
159b0c24a08Smrg        exit 1;;
1606ea72052Smrg
161168023feSmrg    *)  break;;
162168023feSmrg  esac
163168023feSmrg  shift
164168023feSmrgdone
1656ea72052Smrg
166b0c24a08Smrg# We allow the use of options -d and -T together, by making -d
167b0c24a08Smrg# take the precedence; this is for compatibility with GNU install.
168b0c24a08Smrg
169b0c24a08Smrgif test -n "$dir_arg"; then
170b0c24a08Smrg  if test -n "$dst_arg"; then
171b0c24a08Smrg    echo "$0: target directory not allowed when installing a directory." >&2
172b0c24a08Smrg    exit 1
173b0c24a08Smrg  fi
174b0c24a08Smrgfi
175b0c24a08Smrg
176168023feSmrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
177168023feSmrg  # When -d is used, all remaining arguments are directories to create.
178168023feSmrg  # When -t is used, the destination is already specified.
179168023feSmrg  # Otherwise, the last argument is the destination.  Remove it from $@.
180168023feSmrg  for arg
181168023feSmrg  do
182168023feSmrg    if test -n "$dst_arg"; then
183168023feSmrg      # $@ is not empty: it contains at least $arg.
184168023feSmrg      set fnord "$@" "$dst_arg"
185168023feSmrg      shift # fnord
186168023feSmrg    fi
187168023feSmrg    shift # arg
188168023feSmrg    dst_arg=$arg
189b0c24a08Smrg    # Protect names problematic for 'test' and other utilities.
190b0c24a08Smrg    case $dst_arg in
191b0c24a08Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
192b0c24a08Smrg    esac
193168023feSmrg  done
194168023feSmrgfi
1956ea72052Smrg
196168023feSmrgif test $# -eq 0; then
197168023feSmrg  if test -z "$dir_arg"; then
198168023feSmrg    echo "$0: no input file specified." >&2
199168023feSmrg    exit 1
200168023feSmrg  fi
201b0c24a08Smrg  # It's OK to call 'install-sh -d' without argument.
202168023feSmrg  # This can happen when creating conditional directories.
203168023feSmrg  exit 0
204168023feSmrgfi
2056ea72052Smrg
206168023feSmrgif test -z "$dir_arg"; then
207b0c24a08Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
208b0c24a08Smrg    if test ! -d "$dst_arg"; then
209b0c24a08Smrg      echo "$0: $dst_arg: Is not a directory." >&2
210b0c24a08Smrg      exit 1
211b0c24a08Smrg    fi
212b0c24a08Smrg  fi
213b0c24a08Smrgfi
214b0c24a08Smrg
215b0c24a08Smrgif test -z "$dir_arg"; then
216b0c24a08Smrg  do_exit='(exit $ret); exit $ret'
217b0c24a08Smrg  trap "ret=129; $do_exit" 1
218b0c24a08Smrg  trap "ret=130; $do_exit" 2
219b0c24a08Smrg  trap "ret=141; $do_exit" 13
220b0c24a08Smrg  trap "ret=143; $do_exit" 15
221168023feSmrg
222168023feSmrg  # Set umask so as not to create temps with too-generous modes.
223168023feSmrg  # However, 'strip' requires both read and write access to temps.
224168023feSmrg  case $mode in
225168023feSmrg    # Optimize common cases.
226168023feSmrg    *644) cp_umask=133;;
227168023feSmrg    *755) cp_umask=22;;
228168023feSmrg
229168023feSmrg    *[0-7])
230168023feSmrg      if test -z "$stripcmd"; then
231b0c24a08Smrg        u_plus_rw=
232168023feSmrg      else
233b0c24a08Smrg        u_plus_rw='% 200'
234168023feSmrg      fi
235168023feSmrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
236168023feSmrg    *)
237168023feSmrg      if test -z "$stripcmd"; then
238b0c24a08Smrg        u_plus_rw=
239168023feSmrg      else
240b0c24a08Smrg        u_plus_rw=,u+rw
241168023feSmrg      fi
242168023feSmrg      cp_umask=$mode$u_plus_rw;;
243168023feSmrg  esac
244168023feSmrgfi
2456ea72052Smrg
246168023feSmrgfor src
247168023feSmrgdo
248b0c24a08Smrg  # Protect names problematic for 'test' and other utilities.
249168023feSmrg  case $src in
250b0c24a08Smrg    -* | [=\(\)!]) src=./$src;;
251168023feSmrg  esac
252168023feSmrg
253168023feSmrg  if test -n "$dir_arg"; then
254168023feSmrg    dst=$src
255168023feSmrg    dstdir=$dst
256168023feSmrg    test -d "$dstdir"
257168023feSmrg    dstdir_status=$?
258168023feSmrg  else
259168023feSmrg
260168023feSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
261168023feSmrg    # might cause directories to be created, which would be especially bad
262168023feSmrg    # if $src (and thus $dsttmp) contains '*'.
263168023feSmrg    if test ! -f "$src" && test ! -d "$src"; then
264168023feSmrg      echo "$0: $src does not exist." >&2
265168023feSmrg      exit 1
266168023feSmrg    fi
267168023feSmrg
268168023feSmrg    if test -z "$dst_arg"; then
269168023feSmrg      echo "$0: no destination specified." >&2
270168023feSmrg      exit 1
271168023feSmrg    fi
272168023feSmrg    dst=$dst_arg
2736ea72052Smrg
274168023feSmrg    # If destination is a directory, append the input filename; won't work
275168023feSmrg    # if double slashes aren't ignored.
276168023feSmrg    if test -d "$dst"; then
277b0c24a08Smrg      if test "$is_target_a_directory" = never; then
278b0c24a08Smrg        echo "$0: $dst_arg: Is a directory" >&2
279b0c24a08Smrg        exit 1
280168023feSmrg      fi
281168023feSmrg      dstdir=$dst
282168023feSmrg      dst=$dstdir/`basename "$src"`
283168023feSmrg      dstdir_status=0
284168023feSmrg    else
285b0c24a08Smrg      dstdir=`dirname "$dst"`
286168023feSmrg      test -d "$dstdir"
287168023feSmrg      dstdir_status=$?
288168023feSmrg    fi
289168023feSmrg  fi
290168023feSmrg
291168023feSmrg  obsolete_mkdir_used=false
292168023feSmrg
293168023feSmrg  if test $dstdir_status != 0; then
294168023feSmrg    case $posix_mkdir in
295168023feSmrg      '')
296b0c24a08Smrg        # Create intermediate dirs using mode 755 as modified by the umask.
297b0c24a08Smrg        # This is like FreeBSD 'install' as of 1997-10-28.
298b0c24a08Smrg        umask=`umask`
299b0c24a08Smrg        case $stripcmd.$umask in
300b0c24a08Smrg          # Optimize common cases.
301b0c24a08Smrg          *[2367][2367]) mkdir_umask=$umask;;
302b0c24a08Smrg          .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
303b0c24a08Smrg
304b0c24a08Smrg          *[0-7])
305b0c24a08Smrg            mkdir_umask=`expr $umask + 22 \
306b0c24a08Smrg              - $umask % 100 % 40 + $umask % 20 \
307b0c24a08Smrg              - $umask % 10 % 4 + $umask % 2
308b0c24a08Smrg            `;;
309b0c24a08Smrg          *) mkdir_umask=$umask,go-w;;
310b0c24a08Smrg        esac
311b0c24a08Smrg
312b0c24a08Smrg        # With -d, create the new directory with the user-specified mode.
313b0c24a08Smrg        # Otherwise, rely on $mkdir_umask.
314b0c24a08Smrg        if test -n "$dir_arg"; then
315b0c24a08Smrg          mkdir_mode=-m$mode
316b0c24a08Smrg        else
317b0c24a08Smrg          mkdir_mode=
318b0c24a08Smrg        fi
319b0c24a08Smrg
320b0c24a08Smrg        posix_mkdir=false
321b0c24a08Smrg        case $umask in
322b0c24a08Smrg          *[123567][0-7][0-7])
323b0c24a08Smrg            # POSIX mkdir -p sets u+wx bits regardless of umask, which
324b0c24a08Smrg            # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
325b0c24a08Smrg            ;;
326b0c24a08Smrg          *)
327b0c24a08Smrg            tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
328b0c24a08Smrg            trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
329b0c24a08Smrg
330b0c24a08Smrg            if (umask $mkdir_umask &&
331b0c24a08Smrg                exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
332b0c24a08Smrg            then
333b0c24a08Smrg              if test -z "$dir_arg" || {
334b0c24a08Smrg                   # Check for POSIX incompatibilities with -m.
335b0c24a08Smrg                   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
336b0c24a08Smrg                   # other-writable bit of parent directory when it shouldn't.
337b0c24a08Smrg                   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
338b0c24a08Smrg                   ls_ld_tmpdir=`ls -ld "$tmpdir"`
339b0c24a08Smrg                   case $ls_ld_tmpdir in
340b0c24a08Smrg                     d????-?r-*) different_mode=700;;
341b0c24a08Smrg                     d????-?--*) different_mode=755;;
342b0c24a08Smrg                     *) false;;
343b0c24a08Smrg                   esac &&
344b0c24a08Smrg                   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
345b0c24a08Smrg                     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
346b0c24a08Smrg                     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
347b0c24a08Smrg                   }
348b0c24a08Smrg                 }
349b0c24a08Smrg              then posix_mkdir=:
350b0c24a08Smrg              fi
351b0c24a08Smrg              rmdir "$tmpdir/d" "$tmpdir"
352b0c24a08Smrg            else
353b0c24a08Smrg              # Remove any dirs left behind by ancient mkdir implementations.
354b0c24a08Smrg              rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
355b0c24a08Smrg            fi
356b0c24a08Smrg            trap '' 0;;
357b0c24a08Smrg        esac;;
358168023feSmrg    esac
3596ea72052Smrg
360168023feSmrg    if
361168023feSmrg      $posix_mkdir && (
362b0c24a08Smrg        umask $mkdir_umask &&
363b0c24a08Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
364168023feSmrg      )
365168023feSmrg    then :
366168023feSmrg    else
367168023feSmrg
368168023feSmrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
369168023feSmrg      # or it failed possibly due to a race condition.  Create the
370168023feSmrg      # directory the slow way, step by step, checking for races as we go.
371168023feSmrg
372168023feSmrg      case $dstdir in
373b0c24a08Smrg        /*) prefix='/';;
374b0c24a08Smrg        [-=\(\)!]*) prefix='./';;
375b0c24a08Smrg        *)  prefix='';;
376168023feSmrg      esac
377168023feSmrg
378168023feSmrg      oIFS=$IFS
379168023feSmrg      IFS=/
380b0c24a08Smrg      set -f
381168023feSmrg      set fnord $dstdir
382168023feSmrg      shift
383b0c24a08Smrg      set +f
384168023feSmrg      IFS=$oIFS
385168023feSmrg
386168023feSmrg      prefixes=
387168023feSmrg
388168023feSmrg      for d
389168023feSmrg      do
390b0c24a08Smrg        test X"$d" = X && continue
391b0c24a08Smrg
392b0c24a08Smrg        prefix=$prefix$d
393b0c24a08Smrg        if test -d "$prefix"; then
394b0c24a08Smrg          prefixes=
395b0c24a08Smrg        else
396b0c24a08Smrg          if $posix_mkdir; then
397b0c24a08Smrg            (umask=$mkdir_umask &&
398b0c24a08Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
399b0c24a08Smrg            # Don't fail if two instances are running concurrently.
400b0c24a08Smrg            test -d "$prefix" || exit 1
401b0c24a08Smrg          else
402b0c24a08Smrg            case $prefix in
403b0c24a08Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
404b0c24a08Smrg              *) qprefix=$prefix;;
405b0c24a08Smrg            esac
406b0c24a08Smrg            prefixes="$prefixes '$qprefix'"
407b0c24a08Smrg          fi
408b0c24a08Smrg        fi
409b0c24a08Smrg        prefix=$prefix/
410168023feSmrg      done
411168023feSmrg
412168023feSmrg      if test -n "$prefixes"; then
413b0c24a08Smrg        # Don't fail if two instances are running concurrently.
414b0c24a08Smrg        (umask $mkdir_umask &&
415b0c24a08Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
416b0c24a08Smrg          test -d "$dstdir" || exit 1
417b0c24a08Smrg        obsolete_mkdir_used=true
418168023feSmrg      fi
419168023feSmrg    fi
420168023feSmrg  fi
421168023feSmrg
422168023feSmrg  if test -n "$dir_arg"; then
423168023feSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
424168023feSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
425168023feSmrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
426168023feSmrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
427168023feSmrg  else
428168023feSmrg
429168023feSmrg    # Make a couple of temp file names in the proper directory.
430168023feSmrg    dsttmp=$dstdir/_inst.$$_
431168023feSmrg    rmtmp=$dstdir/_rm.$$_
432168023feSmrg
433168023feSmrg    # Trap to clean up those temp files at exit.
434168023feSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
435168023feSmrg
436168023feSmrg    # Copy the file name to the temp name.
437168023feSmrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
438168023feSmrg
439168023feSmrg    # and set any options; do chmod last to preserve setuid bits.
440168023feSmrg    #
441168023feSmrg    # If any of these fail, we abort the whole thing.  If we want to
442168023feSmrg    # ignore errors from any of these, just make sure not to ignore
443168023feSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
444168023feSmrg    #
445168023feSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
446168023feSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
447168023feSmrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
448168023feSmrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
449168023feSmrg
450168023feSmrg    # If -C, don't bother to copy if it wouldn't change the file.
451168023feSmrg    if $copy_on_change &&
452b0c24a08Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
453b0c24a08Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
454b0c24a08Smrg       set -f &&
455168023feSmrg       set X $old && old=:$2:$4:$5:$6 &&
456168023feSmrg       set X $new && new=:$2:$4:$5:$6 &&
457b0c24a08Smrg       set +f &&
458168023feSmrg       test "$old" = "$new" &&
459168023feSmrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
460168023feSmrg    then
461168023feSmrg      rm -f "$dsttmp"
462168023feSmrg    else
463168023feSmrg      # Rename the file to the real destination.
464168023feSmrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
465168023feSmrg
466168023feSmrg      # The rename failed, perhaps because mv can't rename something else
467168023feSmrg      # to itself, or perhaps because mv is so ancient that it does not
468168023feSmrg      # support -f.
469168023feSmrg      {
470b0c24a08Smrg        # Now remove or move aside any old file at destination location.
471b0c24a08Smrg        # We try this two ways since rm can't unlink itself on some
472b0c24a08Smrg        # systems and the destination file might be busy for other
473b0c24a08Smrg        # reasons.  In this case, the final cleanup might fail but the new
474b0c24a08Smrg        # file should still install successfully.
475b0c24a08Smrg        {
476b0c24a08Smrg          test ! -f "$dst" ||
477b0c24a08Smrg          $doit $rmcmd -f "$dst" 2>/dev/null ||
478b0c24a08Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
479b0c24a08Smrg            { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
480b0c24a08Smrg          } ||
481b0c24a08Smrg          { echo "$0: cannot unlink or rename $dst" >&2
482b0c24a08Smrg            (exit 1); exit 1
483b0c24a08Smrg          }
484b0c24a08Smrg        } &&
485b0c24a08Smrg
486b0c24a08Smrg        # Now rename the file to the real destination.
487b0c24a08Smrg        $doit $mvcmd "$dsttmp" "$dst"
488168023feSmrg      }
489168023feSmrg    fi || exit 1
490168023feSmrg
491168023feSmrg    trap '' 0
492168023feSmrg  fi
493168023feSmrgdone
4946ea72052Smrg
495168023feSmrg# Local variables:
496168023feSmrg# eval: (add-hook 'write-file-hooks 'time-stamp)
497168023feSmrg# time-stamp-start: "scriptversion="
498168023feSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
499168023feSmrg# time-stamp-time-zone: "UTC"
500168023feSmrg# time-stamp-end: "; # UTC"
501168023feSmrg# End:
502