install-sh revision f395c03e
109885543Smrg#!/bin/sh
209885543Smrg# install - install a program, script, or datafile
309885543Smrg
4f395c03eSmrgscriptversion=2013-12-25.23; # UTC
509885543Smrg
609885543Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
709885543Smrg# later released in X11R6 (xc/config/util/install.sh) with the
809885543Smrg# following copyright and license.
909885543Smrg#
1009885543Smrg# Copyright (C) 1994 X Consortium
1109885543Smrg#
1209885543Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
1309885543Smrg# of this software and associated documentation files (the "Software"), to
1409885543Smrg# deal in the Software without restriction, including without limitation the
1509885543Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
1609885543Smrg# sell copies of the Software, and to permit persons to whom the Software is
1709885543Smrg# furnished to do so, subject to the following conditions:
1809885543Smrg#
1909885543Smrg# The above copyright notice and this permission notice shall be included in
2009885543Smrg# all copies or substantial portions of the Software.
2109885543Smrg#
2209885543Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2309885543Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2409885543Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
2509885543Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
2609885543Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
2709885543Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2809885543Smrg#
2909885543Smrg# Except as contained in this notice, the name of the X Consortium shall not
3009885543Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
3109885543Smrg# ings in this Software without prior written authorization from the X Consor-
3209885543Smrg# tium.
3309885543Smrg#
3409885543Smrg#
3509885543Smrg# FSF changes to this file are in the public domain.
3609885543Smrg#
3709885543Smrg# Calling this script install-sh is preferred over install.sh, to prevent
38df9ffc95Smrg# 'make' implicit rules from creating a file called install from it
3909885543Smrg# when there is no Makefile.
4009885543Smrg#
4109885543Smrg# This script is compatible with the BSD install script, but was written
427104f784Smrg# from scratch.
437104f784Smrg
44f395c03eSmrgtab='	'
457104f784Smrgnl='
467104f784Smrg'
47f395c03eSmrgIFS=" $tab$nl"
4809885543Smrg
49f395c03eSmrg# Set DOITPROG to "echo" to test this script.
5009885543Smrg
517104f784Smrgdoit=${DOITPROG-}
52f395c03eSmrgdoit_exec=${doit:-exec}
5309885543Smrg
547104f784Smrg# Put in absolute file names if you don't have them in your path;
557104f784Smrg# or use environment vars.
567104f784Smrg
577104f784Smrgchgrpprog=${CHGRPPROG-chgrp}
587104f784Smrgchmodprog=${CHMODPROG-chmod}
597104f784Smrgchownprog=${CHOWNPROG-chown}
607104f784Smrgcmpprog=${CMPPROG-cmp}
617104f784Smrgcpprog=${CPPROG-cp}
627104f784Smrgmkdirprog=${MKDIRPROG-mkdir}
637104f784Smrgmvprog=${MVPROG-mv}
647104f784Smrgrmprog=${RMPROG-rm}
657104f784Smrgstripprog=${STRIPPROG-strip}
667104f784Smrg
677104f784Smrgposix_mkdir=
687104f784Smrg
697104f784Smrg# Desired mode of installed file.
707104f784Smrgmode=0755
7109885543Smrg
7209885543Smrgchgrpcmd=
737104f784Smrgchmodcmd=$chmodprog
747104f784Smrgchowncmd=
757104f784Smrgmvcmd=$mvprog
7609885543Smrgrmcmd="$rmprog -f"
777104f784Smrgstripcmd=
787104f784Smrg
7909885543Smrgsrc=
8009885543Smrgdst=
8109885543Smrgdir_arg=
827104f784Smrgdst_arg=
837104f784Smrg
847104f784Smrgcopy_on_change=false
85f395c03eSmrgis_target_a_directory=possibly
8609885543Smrg
877104f784Smrgusage="\
887104f784SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
8909885543Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
9009885543Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
9109885543Smrg   or: $0 [OPTION]... -d DIRECTORIES...
9209885543Smrg
9309885543SmrgIn the 1st form, copy SRCFILE to DSTFILE.
9409885543SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
9509885543SmrgIn the 4th, create DIRECTORIES.
9609885543Smrg
9709885543SmrgOptions:
987104f784Smrg     --help     display this help and exit.
997104f784Smrg     --version  display version info and exit.
1007104f784Smrg
1017104f784Smrg  -c            (ignored)
1027104f784Smrg  -C            install only if different (preserve the last data modification time)
1037104f784Smrg  -d            create directories instead of installing files.
1047104f784Smrg  -g GROUP      $chgrpprog installed files to GROUP.
1057104f784Smrg  -m MODE       $chmodprog installed files to MODE.
1067104f784Smrg  -o USER       $chownprog installed files to USER.
1077104f784Smrg  -s            $stripprog installed files.
1087104f784Smrg  -t DIRECTORY  install into DIRECTORY.
1097104f784Smrg  -T            report an error if DSTFILE is a directory.
11009885543Smrg
11109885543SmrgEnvironment variables override the default commands:
1127104f784Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
1137104f784Smrg  RMPROG STRIPPROG
11409885543Smrg"
11509885543Smrg
1167104f784Smrgwhile test $# -ne 0; do
11709885543Smrg  case $1 in
1187104f784Smrg    -c) ;;
1197104f784Smrg
1207104f784Smrg    -C) copy_on_change=true;;
12109885543Smrg
1227104f784Smrg    -d) dir_arg=true;;
12309885543Smrg
12409885543Smrg    -g) chgrpcmd="$chgrpprog $2"
125f395c03eSmrg        shift;;
12609885543Smrg
12709885543Smrg    --help) echo "$usage"; exit $?;;
12809885543Smrg
1297104f784Smrg    -m) mode=$2
130f395c03eSmrg        case $mode in
131f395c03eSmrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
132f395c03eSmrg            echo "$0: invalid mode: $mode" >&2
133f395c03eSmrg            exit 1;;
134f395c03eSmrg        esac
135f395c03eSmrg        shift;;
13609885543Smrg
13709885543Smrg    -o) chowncmd="$chownprog $2"
138f395c03eSmrg        shift;;
13909885543Smrg
1407104f784Smrg    -s) stripcmd=$stripprog;;
14109885543Smrg
142f395c03eSmrg    -t)
143f395c03eSmrg        is_target_a_directory=always
144f395c03eSmrg        dst_arg=$2
145f395c03eSmrg        # Protect names problematic for 'test' and other utilities.
146f395c03eSmrg        case $dst_arg in
147f395c03eSmrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
148f395c03eSmrg        esac
149f395c03eSmrg        shift;;
15009885543Smrg
151f395c03eSmrg    -T) is_target_a_directory=never;;
15209885543Smrg
15309885543Smrg    --version) echo "$0 $scriptversion"; exit $?;;
15409885543Smrg
155f395c03eSmrg    --) shift
156f395c03eSmrg        break;;
1577104f784Smrg
158f395c03eSmrg    -*) echo "$0: invalid option: $1" >&2
159f395c03eSmrg        exit 1;;
1607104f784Smrg
1617104f784Smrg    *)  break;;
16209885543Smrg  esac
1637104f784Smrg  shift
16409885543Smrgdone
16509885543Smrg
166f395c03eSmrg# We allow the use of options -d and -T together, by making -d
167f395c03eSmrg# take the precedence; this is for compatibility with GNU install.
168f395c03eSmrg
169f395c03eSmrgif test -n "$dir_arg"; then
170f395c03eSmrg  if test -n "$dst_arg"; then
171f395c03eSmrg    echo "$0: target directory not allowed when installing a directory." >&2
172f395c03eSmrg    exit 1
173f395c03eSmrg  fi
174f395c03eSmrgfi
175f395c03eSmrg
1767104f784Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1777104f784Smrg  # When -d is used, all remaining arguments are directories to create.
1787104f784Smrg  # When -t is used, the destination is already specified.
1797104f784Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
1807104f784Smrg  for arg
1817104f784Smrg  do
1827104f784Smrg    if test -n "$dst_arg"; then
1837104f784Smrg      # $@ is not empty: it contains at least $arg.
1847104f784Smrg      set fnord "$@" "$dst_arg"
1857104f784Smrg      shift # fnord
1867104f784Smrg    fi
1877104f784Smrg    shift # arg
1887104f784Smrg    dst_arg=$arg
189df9ffc95Smrg    # Protect names problematic for 'test' and other utilities.
190b12e5c03Smrg    case $dst_arg in
191b12e5c03Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
192b12e5c03Smrg    esac
1937104f784Smrg  done
1947104f784Smrgfi
1957104f784Smrg
1967104f784Smrgif test $# -eq 0; then
19709885543Smrg  if test -z "$dir_arg"; then
19809885543Smrg    echo "$0: no input file specified." >&2
19909885543Smrg    exit 1
20009885543Smrg  fi
201df9ffc95Smrg  # It's OK to call 'install-sh -d' without argument.
20209885543Smrg  # This can happen when creating conditional directories.
20309885543Smrg  exit 0
20409885543Smrgfi
20509885543Smrg
206f395c03eSmrgif test -z "$dir_arg"; then
207f395c03eSmrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
208f395c03eSmrg    if test ! -d "$dst_arg"; then
209f395c03eSmrg      echo "$0: $dst_arg: Is not a directory." >&2
210f395c03eSmrg      exit 1
211f395c03eSmrg    fi
212f395c03eSmrg  fi
213f395c03eSmrgfi
214f395c03eSmrg
2157104f784Smrgif test -z "$dir_arg"; then
216b12e5c03Smrg  do_exit='(exit $ret); exit $ret'
217b12e5c03Smrg  trap "ret=129; $do_exit" 1
218b12e5c03Smrg  trap "ret=130; $do_exit" 2
219b12e5c03Smrg  trap "ret=141; $do_exit" 13
220b12e5c03Smrg  trap "ret=143; $do_exit" 15
2217104f784Smrg
2227104f784Smrg  # Set umask so as not to create temps with too-generous modes.
2237104f784Smrg  # However, 'strip' requires both read and write access to temps.
2247104f784Smrg  case $mode in
2257104f784Smrg    # Optimize common cases.
2267104f784Smrg    *644) cp_umask=133;;
2277104f784Smrg    *755) cp_umask=22;;
2287104f784Smrg
2297104f784Smrg    *[0-7])
2307104f784Smrg      if test -z "$stripcmd"; then
231f395c03eSmrg        u_plus_rw=
2327104f784Smrg      else
233f395c03eSmrg        u_plus_rw='% 200'
2347104f784Smrg      fi
2357104f784Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2367104f784Smrg    *)
2377104f784Smrg      if test -z "$stripcmd"; then
238f395c03eSmrg        u_plus_rw=
2397104f784Smrg      else
240f395c03eSmrg        u_plus_rw=,u+rw
2417104f784Smrg      fi
2427104f784Smrg      cp_umask=$mode$u_plus_rw;;
2437104f784Smrg  esac
2447104f784Smrgfi
2457104f784Smrg
24609885543Smrgfor src
24709885543Smrgdo
248df9ffc95Smrg  # Protect names problematic for 'test' and other utilities.
24909885543Smrg  case $src in
250b12e5c03Smrg    -* | [=\(\)!]) src=./$src;;
25109885543Smrg  esac
25209885543Smrg
25309885543Smrg  if test -n "$dir_arg"; then
25409885543Smrg    dst=$src
2557104f784Smrg    dstdir=$dst
2567104f784Smrg    test -d "$dstdir"
2577104f784Smrg    dstdir_status=$?
25809885543Smrg  else
2597104f784Smrg
26009885543Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
26109885543Smrg    # might cause directories to be created, which would be especially bad
26209885543Smrg    # if $src (and thus $dsttmp) contains '*'.
26309885543Smrg    if test ! -f "$src" && test ! -d "$src"; then
26409885543Smrg      echo "$0: $src does not exist." >&2
26509885543Smrg      exit 1
26609885543Smrg    fi
26709885543Smrg
2687104f784Smrg    if test -z "$dst_arg"; then
26909885543Smrg      echo "$0: no destination specified." >&2
27009885543Smrg      exit 1
27109885543Smrg    fi
2727104f784Smrg    dst=$dst_arg
27309885543Smrg
27409885543Smrg    # If destination is a directory, append the input filename; won't work
27509885543Smrg    # if double slashes aren't ignored.
27609885543Smrg    if test -d "$dst"; then
277f395c03eSmrg      if test "$is_target_a_directory" = never; then
278f395c03eSmrg        echo "$0: $dst_arg: Is a directory" >&2
279f395c03eSmrg        exit 1
28009885543Smrg      fi
2817104f784Smrg      dstdir=$dst
2827104f784Smrg      dst=$dstdir/`basename "$src"`
2837104f784Smrg      dstdir_status=0
2847104f784Smrg    else
285f395c03eSmrg      dstdir=`dirname "$dst"`
2867104f784Smrg      test -d "$dstdir"
2877104f784Smrg      dstdir_status=$?
28809885543Smrg    fi
28909885543Smrg  fi
29009885543Smrg
2917104f784Smrg  obsolete_mkdir_used=false
2927104f784Smrg
2937104f784Smrg  if test $dstdir_status != 0; then
2947104f784Smrg    case $posix_mkdir in
2957104f784Smrg      '')
296f395c03eSmrg        # Create intermediate dirs using mode 755 as modified by the umask.
297f395c03eSmrg        # This is like FreeBSD 'install' as of 1997-10-28.
298f395c03eSmrg        umask=`umask`
299f395c03eSmrg        case $stripcmd.$umask in
300f395c03eSmrg          # Optimize common cases.
301f395c03eSmrg          *[2367][2367]) mkdir_umask=$umask;;
302f395c03eSmrg          .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
303f395c03eSmrg
304f395c03eSmrg          *[0-7])
305f395c03eSmrg            mkdir_umask=`expr $umask + 22 \
306f395c03eSmrg              - $umask % 100 % 40 + $umask % 20 \
307f395c03eSmrg              - $umask % 10 % 4 + $umask % 2
308f395c03eSmrg            `;;
309f395c03eSmrg          *) mkdir_umask=$umask,go-w;;
310f395c03eSmrg        esac
311f395c03eSmrg
312f395c03eSmrg        # With -d, create the new directory with the user-specified mode.
313f395c03eSmrg        # Otherwise, rely on $mkdir_umask.
314f395c03eSmrg        if test -n "$dir_arg"; then
315f395c03eSmrg          mkdir_mode=-m$mode
316f395c03eSmrg        else
317f395c03eSmrg          mkdir_mode=
318f395c03eSmrg        fi
319f395c03eSmrg
320f395c03eSmrg        posix_mkdir=false
321f395c03eSmrg        case $umask in
322f395c03eSmrg          *[123567][0-7][0-7])
323f395c03eSmrg            # POSIX mkdir -p sets u+wx bits regardless of umask, which
324f395c03eSmrg            # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
325f395c03eSmrg            ;;
326f395c03eSmrg          *)
327f395c03eSmrg            tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
328f395c03eSmrg            trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
329f395c03eSmrg
330f395c03eSmrg            if (umask $mkdir_umask &&
331f395c03eSmrg                exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
332f395c03eSmrg            then
333f395c03eSmrg              if test -z "$dir_arg" || {
334f395c03eSmrg                   # Check for POSIX incompatibilities with -m.
335f395c03eSmrg                   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
336f395c03eSmrg                   # other-writable bit of parent directory when it shouldn't.
337f395c03eSmrg                   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
338f395c03eSmrg                   ls_ld_tmpdir=`ls -ld "$tmpdir"`
339f395c03eSmrg                   case $ls_ld_tmpdir in
340f395c03eSmrg                     d????-?r-*) different_mode=700;;
341f395c03eSmrg                     d????-?--*) different_mode=755;;
342f395c03eSmrg                     *) false;;
343f395c03eSmrg                   esac &&
344f395c03eSmrg                   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
345f395c03eSmrg                     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
346f395c03eSmrg                     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
347f395c03eSmrg                   }
348f395c03eSmrg                 }
349f395c03eSmrg              then posix_mkdir=:
350f395c03eSmrg              fi
351f395c03eSmrg              rmdir "$tmpdir/d" "$tmpdir"
352f395c03eSmrg            else
353f395c03eSmrg              # Remove any dirs left behind by ancient mkdir implementations.
354f395c03eSmrg              rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
355f395c03eSmrg            fi
356f395c03eSmrg            trap '' 0;;
357f395c03eSmrg        esac;;
3587104f784Smrg    esac
35909885543Smrg
3607104f784Smrg    if
3617104f784Smrg      $posix_mkdir && (
362f395c03eSmrg        umask $mkdir_umask &&
363f395c03eSmrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3647104f784Smrg      )
3657104f784Smrg    then :
3667104f784Smrg    else
36709885543Smrg
3687104f784Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
3697104f784Smrg      # or it failed possibly due to a race condition.  Create the
3707104f784Smrg      # directory the slow way, step by step, checking for races as we go.
37109885543Smrg
3727104f784Smrg      case $dstdir in
373f395c03eSmrg        /*) prefix='/';;
374f395c03eSmrg        [-=\(\)!]*) prefix='./';;
375f395c03eSmrg        *)  prefix='';;
3767104f784Smrg      esac
37709885543Smrg
3787104f784Smrg      oIFS=$IFS
3797104f784Smrg      IFS=/
380f395c03eSmrg      set -f
3817104f784Smrg      set fnord $dstdir
38209885543Smrg      shift
383f395c03eSmrg      set +f
3847104f784Smrg      IFS=$oIFS
3857104f784Smrg
3867104f784Smrg      prefixes=
3877104f784Smrg
3887104f784Smrg      for d
3897104f784Smrg      do
390f395c03eSmrg        test X"$d" = X && continue
391f395c03eSmrg
392f395c03eSmrg        prefix=$prefix$d
393f395c03eSmrg        if test -d "$prefix"; then
394f395c03eSmrg          prefixes=
395f395c03eSmrg        else
396f395c03eSmrg          if $posix_mkdir; then
397f395c03eSmrg            (umask=$mkdir_umask &&
398f395c03eSmrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
399f395c03eSmrg            # Don't fail if two instances are running concurrently.
400f395c03eSmrg            test -d "$prefix" || exit 1
401f395c03eSmrg          else
402f395c03eSmrg            case $prefix in
403f395c03eSmrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
404f395c03eSmrg              *) qprefix=$prefix;;
405f395c03eSmrg            esac
406f395c03eSmrg            prefixes="$prefixes '$qprefix'"
407f395c03eSmrg          fi
408f395c03eSmrg        fi
409f395c03eSmrg        prefix=$prefix/
4107104f784Smrg      done
4117104f784Smrg
4127104f784Smrg      if test -n "$prefixes"; then
413f395c03eSmrg        # Don't fail if two instances are running concurrently.
414f395c03eSmrg        (umask $mkdir_umask &&
415f395c03eSmrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
416f395c03eSmrg          test -d "$dstdir" || exit 1
417f395c03eSmrg        obsolete_mkdir_used=true
41809885543Smrg      fi
4197104f784Smrg    fi
42009885543Smrg  fi
42109885543Smrg
42209885543Smrg  if test -n "$dir_arg"; then
4237104f784Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4247104f784Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4257104f784Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4267104f784Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
42709885543Smrg  else
42809885543Smrg
42909885543Smrg    # Make a couple of temp file names in the proper directory.
43009885543Smrg    dsttmp=$dstdir/_inst.$$_
43109885543Smrg    rmtmp=$dstdir/_rm.$$_
43209885543Smrg
43309885543Smrg    # Trap to clean up those temp files at exit.
43409885543Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
43509885543Smrg
43609885543Smrg    # Copy the file name to the temp name.
4377104f784Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
43809885543Smrg
43909885543Smrg    # and set any options; do chmod last to preserve setuid bits.
44009885543Smrg    #
44109885543Smrg    # If any of these fail, we abort the whole thing.  If we want to
44209885543Smrg    # ignore errors from any of these, just make sure not to ignore
44309885543Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
44409885543Smrg    #
4457104f784Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
4467104f784Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
4477104f784Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
4487104f784Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4497104f784Smrg
4507104f784Smrg    # If -C, don't bother to copy if it wouldn't change the file.
4517104f784Smrg    if $copy_on_change &&
452f395c03eSmrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
453f395c03eSmrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
454f395c03eSmrg       set -f &&
4557104f784Smrg       set X $old && old=:$2:$4:$5:$6 &&
4567104f784Smrg       set X $new && new=:$2:$4:$5:$6 &&
457f395c03eSmrg       set +f &&
4587104f784Smrg       test "$old" = "$new" &&
4597104f784Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
4607104f784Smrg    then
4617104f784Smrg      rm -f "$dsttmp"
4627104f784Smrg    else
4637104f784Smrg      # Rename the file to the real destination.
4647104f784Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
4657104f784Smrg
4667104f784Smrg      # The rename failed, perhaps because mv can't rename something else
4677104f784Smrg      # to itself, or perhaps because mv is so ancient that it does not
4687104f784Smrg      # support -f.
4697104f784Smrg      {
470f395c03eSmrg        # Now remove or move aside any old file at destination location.
471f395c03eSmrg        # We try this two ways since rm can't unlink itself on some
472f395c03eSmrg        # systems and the destination file might be busy for other
473f395c03eSmrg        # reasons.  In this case, the final cleanup might fail but the new
474f395c03eSmrg        # file should still install successfully.
475f395c03eSmrg        {
476f395c03eSmrg          test ! -f "$dst" ||
477f395c03eSmrg          $doit $rmcmd -f "$dst" 2>/dev/null ||
478f395c03eSmrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
479f395c03eSmrg            { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
480f395c03eSmrg          } ||
481f395c03eSmrg          { echo "$0: cannot unlink or rename $dst" >&2
482f395c03eSmrg            (exit 1); exit 1
483f395c03eSmrg          }
484f395c03eSmrg        } &&
485f395c03eSmrg
486f395c03eSmrg        # Now rename the file to the real destination.
487f395c03eSmrg        $doit $mvcmd "$dsttmp" "$dst"
4887104f784Smrg      }
4897104f784Smrg    fi || exit 1
4907104f784Smrg
4917104f784Smrg    trap '' 0
4927104f784Smrg  fi
49309885543Smrgdone
49409885543Smrg
49509885543Smrg# Local variables:
49609885543Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
49709885543Smrg# time-stamp-start: "scriptversion="
49809885543Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
499b698ba48Smrg# time-stamp-time-zone: "UTC"
500b698ba48Smrg# time-stamp-end: "; # UTC"
50109885543Smrg# End:
502