install-sh revision c8571806
17a84e134Smrg#!/bin/sh
27a84e134Smrg# install - install a program, script, or datafile
37a84e134Smrg
4c8571806Smrgscriptversion=2013-12-25.23; # UTC
57a84e134Smrg
67a84e134Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
77a84e134Smrg# later released in X11R6 (xc/config/util/install.sh) with the
87a84e134Smrg# following copyright and license.
97a84e134Smrg#
107a84e134Smrg# Copyright (C) 1994 X Consortium
117a84e134Smrg#
127a84e134Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
137a84e134Smrg# of this software and associated documentation files (the "Software"), to
147a84e134Smrg# deal in the Software without restriction, including without limitation the
157a84e134Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
167a84e134Smrg# sell copies of the Software, and to permit persons to whom the Software is
177a84e134Smrg# furnished to do so, subject to the following conditions:
187a84e134Smrg#
197a84e134Smrg# The above copyright notice and this permission notice shall be included in
207a84e134Smrg# all copies or substantial portions of the Software.
217a84e134Smrg#
227a84e134Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
237a84e134Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
247a84e134Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
257a84e134Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
267a84e134Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
277a84e134Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
287a84e134Smrg#
297a84e134Smrg# Except as contained in this notice, the name of the X Consortium shall not
307a84e134Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
317a84e134Smrg# ings in this Software without prior written authorization from the X Consor-
327a84e134Smrg# tium.
337a84e134Smrg#
347a84e134Smrg#
357a84e134Smrg# FSF changes to this file are in the public domain.
367a84e134Smrg#
377a84e134Smrg# Calling this script install-sh is preferred over install.sh, to prevent
38c889a3bfSmrg# 'make' implicit rules from creating a file called install from it
397a84e134Smrg# when there is no Makefile.
407a84e134Smrg#
417a84e134Smrg# This script is compatible with the BSD install script, but was written
427a84e134Smrg# from scratch.
437a84e134Smrg
44c8571806Smrgtab='	'
457a84e134Smrgnl='
467a84e134Smrg'
47c8571806SmrgIFS=" $tab$nl"
487a84e134Smrg
49c8571806Smrg# Set DOITPROG to "echo" to test this script.
507a84e134Smrg
51ab902922Smrgdoit=${DOITPROG-}
52c8571806Smrgdoit_exec=${doit:-exec}
537a84e134Smrg
547a84e134Smrg# Put in absolute file names if you don't have them in your path;
557a84e134Smrg# or use environment vars.
567a84e134Smrg
57ab902922Smrgchgrpprog=${CHGRPPROG-chgrp}
58ab902922Smrgchmodprog=${CHMODPROG-chmod}
59ab902922Smrgchownprog=${CHOWNPROG-chown}
60ab902922Smrgcmpprog=${CMPPROG-cmp}
61ab902922Smrgcpprog=${CPPROG-cp}
62ab902922Smrgmkdirprog=${MKDIRPROG-mkdir}
63ab902922Smrgmvprog=${MVPROG-mv}
64ab902922Smrgrmprog=${RMPROG-rm}
65ab902922Smrgstripprog=${STRIPPROG-strip}
66ab902922Smrg
677a84e134Smrgposix_mkdir=
687a84e134Smrg
697a84e134Smrg# Desired mode of installed file.
707a84e134Smrgmode=0755
717a84e134Smrg
72ab902922Smrgchgrpcmd=
737a84e134Smrgchmodcmd=$chmodprog
747a84e134Smrgchowncmd=
75ab902922Smrgmvcmd=$mvprog
767a84e134Smrgrmcmd="$rmprog -f"
77ab902922Smrgstripcmd=
78ab902922Smrg
797a84e134Smrgsrc=
807a84e134Smrgdst=
817a84e134Smrgdir_arg=
82ab902922Smrgdst_arg=
83ab902922Smrg
84ab902922Smrgcopy_on_change=false
85c8571806Smrgis_target_a_directory=possibly
867a84e134Smrg
87ab902922Smrgusage="\
88ab902922SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
897a84e134Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
907a84e134Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
917a84e134Smrg   or: $0 [OPTION]... -d DIRECTORIES...
927a84e134Smrg
937a84e134SmrgIn the 1st form, copy SRCFILE to DSTFILE.
947a84e134SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
957a84e134SmrgIn the 4th, create DIRECTORIES.
967a84e134Smrg
977a84e134SmrgOptions:
98ab902922Smrg     --help     display this help and exit.
99ab902922Smrg     --version  display version info and exit.
100ab902922Smrg
101ab902922Smrg  -c            (ignored)
102ab902922Smrg  -C            install only if different (preserve the last data modification time)
103ab902922Smrg  -d            create directories instead of installing files.
104ab902922Smrg  -g GROUP      $chgrpprog installed files to GROUP.
105ab902922Smrg  -m MODE       $chmodprog installed files to MODE.
106ab902922Smrg  -o USER       $chownprog installed files to USER.
107ab902922Smrg  -s            $stripprog installed files.
108ab902922Smrg  -t DIRECTORY  install into DIRECTORY.
109ab902922Smrg  -T            report an error if DSTFILE is a directory.
1107a84e134Smrg
1117a84e134SmrgEnvironment variables override the default commands:
112ab902922Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
113ab902922Smrg  RMPROG STRIPPROG
1147a84e134Smrg"
1157a84e134Smrg
1167a84e134Smrgwhile test $# -ne 0; do
1177a84e134Smrg  case $1 in
118ab902922Smrg    -c) ;;
119ab902922Smrg
120ab902922Smrg    -C) copy_on_change=true;;
1217a84e134Smrg
122ab902922Smrg    -d) dir_arg=true;;
1237a84e134Smrg
1247a84e134Smrg    -g) chgrpcmd="$chgrpprog $2"
125c8571806Smrg        shift;;
1267a84e134Smrg
1277a84e134Smrg    --help) echo "$usage"; exit $?;;
1287a84e134Smrg
1297a84e134Smrg    -m) mode=$2
130c8571806Smrg        case $mode in
131c8571806Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
132c8571806Smrg            echo "$0: invalid mode: $mode" >&2
133c8571806Smrg            exit 1;;
134c8571806Smrg        esac
135c8571806Smrg        shift;;
1367a84e134Smrg
1377a84e134Smrg    -o) chowncmd="$chownprog $2"
138c8571806Smrg        shift;;
1397a84e134Smrg
140ab902922Smrg    -s) stripcmd=$stripprog;;
1417a84e134Smrg
142c8571806Smrg    -t)
143c8571806Smrg        is_target_a_directory=always
144c8571806Smrg        dst_arg=$2
145c8571806Smrg        # Protect names problematic for 'test' and other utilities.
146c8571806Smrg        case $dst_arg in
147c8571806Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
148c8571806Smrg        esac
149c8571806Smrg        shift;;
1507a84e134Smrg
151c8571806Smrg    -T) is_target_a_directory=never;;
1527a84e134Smrg
1537a84e134Smrg    --version) echo "$0 $scriptversion"; exit $?;;
1547a84e134Smrg
155c8571806Smrg    --) shift
156c8571806Smrg        break;;
1577a84e134Smrg
158c8571806Smrg    -*) echo "$0: invalid option: $1" >&2
159c8571806Smrg        exit 1;;
1607a84e134Smrg
1617a84e134Smrg    *)  break;;
1627a84e134Smrg  esac
163ab902922Smrg  shift
1647a84e134Smrgdone
1657a84e134Smrg
166c8571806Smrg# We allow the use of options -d and -T together, by making -d
167c8571806Smrg# take the precedence; this is for compatibility with GNU install.
168c8571806Smrg
169c8571806Smrgif test -n "$dir_arg"; then
170c8571806Smrg  if test -n "$dst_arg"; then
171c8571806Smrg    echo "$0: target directory not allowed when installing a directory." >&2
172c8571806Smrg    exit 1
173c8571806Smrg  fi
174c8571806Smrgfi
175c8571806Smrg
176ab902922Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1777a84e134Smrg  # When -d is used, all remaining arguments are directories to create.
1787a84e134Smrg  # When -t is used, the destination is already specified.
1797a84e134Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
1807a84e134Smrg  for arg
1817a84e134Smrg  do
182ab902922Smrg    if test -n "$dst_arg"; then
1837a84e134Smrg      # $@ is not empty: it contains at least $arg.
184ab902922Smrg      set fnord "$@" "$dst_arg"
1857a84e134Smrg      shift # fnord
1867a84e134Smrg    fi
1877a84e134Smrg    shift # arg
188ab902922Smrg    dst_arg=$arg
189c889a3bfSmrg    # Protect names problematic for 'test' and other utilities.
190421c997bSmrg    case $dst_arg in
191421c997bSmrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
192421c997bSmrg    esac
1937a84e134Smrg  done
1947a84e134Smrgfi
1957a84e134Smrg
1967a84e134Smrgif test $# -eq 0; then
1977a84e134Smrg  if test -z "$dir_arg"; then
1987a84e134Smrg    echo "$0: no input file specified." >&2
1997a84e134Smrg    exit 1
2007a84e134Smrg  fi
201c889a3bfSmrg  # It's OK to call 'install-sh -d' without argument.
2027a84e134Smrg  # This can happen when creating conditional directories.
2037a84e134Smrg  exit 0
2047a84e134Smrgfi
2057a84e134Smrg
206c8571806Smrgif test -z "$dir_arg"; then
207c8571806Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
208c8571806Smrg    if test ! -d "$dst_arg"; then
209c8571806Smrg      echo "$0: $dst_arg: Is not a directory." >&2
210c8571806Smrg      exit 1
211c8571806Smrg    fi
212c8571806Smrg  fi
213c8571806Smrgfi
214c8571806Smrg
2157a84e134Smrgif test -z "$dir_arg"; then
216421c997bSmrg  do_exit='(exit $ret); exit $ret'
217421c997bSmrg  trap "ret=129; $do_exit" 1
218421c997bSmrg  trap "ret=130; $do_exit" 2
219421c997bSmrg  trap "ret=141; $do_exit" 13
220421c997bSmrg  trap "ret=143; $do_exit" 15
2217a84e134Smrg
2227a84e134Smrg  # Set umask so as not to create temps with too-generous modes.
2237a84e134Smrg  # However, 'strip' requires both read and write access to temps.
2247a84e134Smrg  case $mode in
2257a84e134Smrg    # Optimize common cases.
2267a84e134Smrg    *644) cp_umask=133;;
2277a84e134Smrg    *755) cp_umask=22;;
2287a84e134Smrg
2297a84e134Smrg    *[0-7])
2307a84e134Smrg      if test -z "$stripcmd"; then
231c8571806Smrg        u_plus_rw=
2327a84e134Smrg      else
233c8571806Smrg        u_plus_rw='% 200'
2347a84e134Smrg      fi
2357a84e134Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2367a84e134Smrg    *)
2377a84e134Smrg      if test -z "$stripcmd"; then
238c8571806Smrg        u_plus_rw=
2397a84e134Smrg      else
240c8571806Smrg        u_plus_rw=,u+rw
2417a84e134Smrg      fi
2427a84e134Smrg      cp_umask=$mode$u_plus_rw;;
2437a84e134Smrg  esac
2447a84e134Smrgfi
2457a84e134Smrg
2467a84e134Smrgfor src
2477a84e134Smrgdo
248c889a3bfSmrg  # Protect names problematic for 'test' and other utilities.
2497a84e134Smrg  case $src in
250421c997bSmrg    -* | [=\(\)!]) src=./$src;;
2517a84e134Smrg  esac
2527a84e134Smrg
2537a84e134Smrg  if test -n "$dir_arg"; then
2547a84e134Smrg    dst=$src
2557a84e134Smrg    dstdir=$dst
2567a84e134Smrg    test -d "$dstdir"
2577a84e134Smrg    dstdir_status=$?
2587a84e134Smrg  else
2597a84e134Smrg
2607a84e134Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
2617a84e134Smrg    # might cause directories to be created, which would be especially bad
2627a84e134Smrg    # if $src (and thus $dsttmp) contains '*'.
2637a84e134Smrg    if test ! -f "$src" && test ! -d "$src"; then
2647a84e134Smrg      echo "$0: $src does not exist." >&2
2657a84e134Smrg      exit 1
2667a84e134Smrg    fi
2677a84e134Smrg
268ab902922Smrg    if test -z "$dst_arg"; then
2697a84e134Smrg      echo "$0: no destination specified." >&2
2707a84e134Smrg      exit 1
2717a84e134Smrg    fi
272ab902922Smrg    dst=$dst_arg
2737a84e134Smrg
2747a84e134Smrg    # If destination is a directory, append the input filename; won't work
2757a84e134Smrg    # if double slashes aren't ignored.
2767a84e134Smrg    if test -d "$dst"; then
277c8571806Smrg      if test "$is_target_a_directory" = never; then
278c8571806Smrg        echo "$0: $dst_arg: Is a directory" >&2
279c8571806Smrg        exit 1
2807a84e134Smrg      fi
2817a84e134Smrg      dstdir=$dst
2827a84e134Smrg      dst=$dstdir/`basename "$src"`
2837a84e134Smrg      dstdir_status=0
2847a84e134Smrg    else
285c8571806Smrg      dstdir=`dirname "$dst"`
2867a84e134Smrg      test -d "$dstdir"
2877a84e134Smrg      dstdir_status=$?
2887a84e134Smrg    fi
2897a84e134Smrg  fi
2907a84e134Smrg
2917a84e134Smrg  obsolete_mkdir_used=false
2927a84e134Smrg
2937a84e134Smrg  if test $dstdir_status != 0; then
2947a84e134Smrg    case $posix_mkdir in
2957a84e134Smrg      '')
296c8571806Smrg        # Create intermediate dirs using mode 755 as modified by the umask.
297c8571806Smrg        # This is like FreeBSD 'install' as of 1997-10-28.
298c8571806Smrg        umask=`umask`
299c8571806Smrg        case $stripcmd.$umask in
300c8571806Smrg          # Optimize common cases.
301c8571806Smrg          *[2367][2367]) mkdir_umask=$umask;;
302c8571806Smrg          .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
303c8571806Smrg
304c8571806Smrg          *[0-7])
305c8571806Smrg            mkdir_umask=`expr $umask + 22 \
306c8571806Smrg              - $umask % 100 % 40 + $umask % 20 \
307c8571806Smrg              - $umask % 10 % 4 + $umask % 2
308c8571806Smrg            `;;
309c8571806Smrg          *) mkdir_umask=$umask,go-w;;
310c8571806Smrg        esac
311c8571806Smrg
312c8571806Smrg        # With -d, create the new directory with the user-specified mode.
313c8571806Smrg        # Otherwise, rely on $mkdir_umask.
314c8571806Smrg        if test -n "$dir_arg"; then
315c8571806Smrg          mkdir_mode=-m$mode
316c8571806Smrg        else
317c8571806Smrg          mkdir_mode=
318c8571806Smrg        fi
319c8571806Smrg
320c8571806Smrg        posix_mkdir=false
321c8571806Smrg        case $umask in
322c8571806Smrg          *[123567][0-7][0-7])
323c8571806Smrg            # POSIX mkdir -p sets u+wx bits regardless of umask, which
324c8571806Smrg            # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
325c8571806Smrg            ;;
326c8571806Smrg          *)
327c8571806Smrg            tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
328c8571806Smrg            trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
329c8571806Smrg
330c8571806Smrg            if (umask $mkdir_umask &&
331c8571806Smrg                exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
332c8571806Smrg            then
333c8571806Smrg              if test -z "$dir_arg" || {
334c8571806Smrg                   # Check for POSIX incompatibilities with -m.
335c8571806Smrg                   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
336c8571806Smrg                   # other-writable bit of parent directory when it shouldn't.
337c8571806Smrg                   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
338c8571806Smrg                   ls_ld_tmpdir=`ls -ld "$tmpdir"`
339c8571806Smrg                   case $ls_ld_tmpdir in
340c8571806Smrg                     d????-?r-*) different_mode=700;;
341c8571806Smrg                     d????-?--*) different_mode=755;;
342c8571806Smrg                     *) false;;
343c8571806Smrg                   esac &&
344c8571806Smrg                   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
345c8571806Smrg                     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
346c8571806Smrg                     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
347c8571806Smrg                   }
348c8571806Smrg                 }
349c8571806Smrg              then posix_mkdir=:
350c8571806Smrg              fi
351c8571806Smrg              rmdir "$tmpdir/d" "$tmpdir"
352c8571806Smrg            else
353c8571806Smrg              # Remove any dirs left behind by ancient mkdir implementations.
354c8571806Smrg              rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
355c8571806Smrg            fi
356c8571806Smrg            trap '' 0;;
357c8571806Smrg        esac;;
3587a84e134Smrg    esac
3597a84e134Smrg
3607a84e134Smrg    if
3617a84e134Smrg      $posix_mkdir && (
362c8571806Smrg        umask $mkdir_umask &&
363c8571806Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3647a84e134Smrg      )
3657a84e134Smrg    then :
3667a84e134Smrg    else
3677a84e134Smrg
3687a84e134Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
3697a84e134Smrg      # or it failed possibly due to a race condition.  Create the
3707a84e134Smrg      # directory the slow way, step by step, checking for races as we go.
3717a84e134Smrg
3727a84e134Smrg      case $dstdir in
373c8571806Smrg        /*) prefix='/';;
374c8571806Smrg        [-=\(\)!]*) prefix='./';;
375c8571806Smrg        *)  prefix='';;
3767a84e134Smrg      esac
3777a84e134Smrg
3787a84e134Smrg      oIFS=$IFS
3797a84e134Smrg      IFS=/
380c8571806Smrg      set -f
3817a84e134Smrg      set fnord $dstdir
3827a84e134Smrg      shift
383c8571806Smrg      set +f
3847a84e134Smrg      IFS=$oIFS
3857a84e134Smrg
3867a84e134Smrg      prefixes=
3877a84e134Smrg
3887a84e134Smrg      for d
3897a84e134Smrg      do
390c8571806Smrg        test X"$d" = X && continue
391c8571806Smrg
392c8571806Smrg        prefix=$prefix$d
393c8571806Smrg        if test -d "$prefix"; then
394c8571806Smrg          prefixes=
395c8571806Smrg        else
396c8571806Smrg          if $posix_mkdir; then
397c8571806Smrg            (umask=$mkdir_umask &&
398c8571806Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
399c8571806Smrg            # Don't fail if two instances are running concurrently.
400c8571806Smrg            test -d "$prefix" || exit 1
401c8571806Smrg          else
402c8571806Smrg            case $prefix in
403c8571806Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
404c8571806Smrg              *) qprefix=$prefix;;
405c8571806Smrg            esac
406c8571806Smrg            prefixes="$prefixes '$qprefix'"
407c8571806Smrg          fi
408c8571806Smrg        fi
409c8571806Smrg        prefix=$prefix/
4107a84e134Smrg      done
4117a84e134Smrg
4127a84e134Smrg      if test -n "$prefixes"; then
413c8571806Smrg        # Don't fail if two instances are running concurrently.
414c8571806Smrg        (umask $mkdir_umask &&
415c8571806Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
416c8571806Smrg          test -d "$dstdir" || exit 1
417c8571806Smrg        obsolete_mkdir_used=true
4187a84e134Smrg      fi
4197a84e134Smrg    fi
4207a84e134Smrg  fi
4217a84e134Smrg
4227a84e134Smrg  if test -n "$dir_arg"; then
4237a84e134Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4247a84e134Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4257a84e134Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4267a84e134Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
4277a84e134Smrg  else
4287a84e134Smrg
4297a84e134Smrg    # Make a couple of temp file names in the proper directory.
4307a84e134Smrg    dsttmp=$dstdir/_inst.$$_
4317a84e134Smrg    rmtmp=$dstdir/_rm.$$_
4327a84e134Smrg
4337a84e134Smrg    # Trap to clean up those temp files at exit.
4347a84e134Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
4357a84e134Smrg
4367a84e134Smrg    # Copy the file name to the temp name.
4377a84e134Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
4387a84e134Smrg
4397a84e134Smrg    # and set any options; do chmod last to preserve setuid bits.
4407a84e134Smrg    #
4417a84e134Smrg    # If any of these fail, we abort the whole thing.  If we want to
4427a84e134Smrg    # ignore errors from any of these, just make sure not to ignore
4437a84e134Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
4447a84e134Smrg    #
445ab902922Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
446ab902922Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
447ab902922Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
448ab902922Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
449ab902922Smrg
450ab902922Smrg    # If -C, don't bother to copy if it wouldn't change the file.
451ab902922Smrg    if $copy_on_change &&
452c8571806Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
453c8571806Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
454c8571806Smrg       set -f &&
455ab902922Smrg       set X $old && old=:$2:$4:$5:$6 &&
456ab902922Smrg       set X $new && new=:$2:$4:$5:$6 &&
457c8571806Smrg       set +f &&
458ab902922Smrg       test "$old" = "$new" &&
459ab902922Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
460ab902922Smrg    then
461ab902922Smrg      rm -f "$dsttmp"
462ab902922Smrg    else
463ab902922Smrg      # Rename the file to the real destination.
464ab902922Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
465ab902922Smrg
466ab902922Smrg      # The rename failed, perhaps because mv can't rename something else
467ab902922Smrg      # to itself, or perhaps because mv is so ancient that it does not
468ab902922Smrg      # support -f.
469ab902922Smrg      {
470c8571806Smrg        # Now remove or move aside any old file at destination location.
471c8571806Smrg        # We try this two ways since rm can't unlink itself on some
472c8571806Smrg        # systems and the destination file might be busy for other
473c8571806Smrg        # reasons.  In this case, the final cleanup might fail but the new
474c8571806Smrg        # file should still install successfully.
475c8571806Smrg        {
476c8571806Smrg          test ! -f "$dst" ||
477c8571806Smrg          $doit $rmcmd -f "$dst" 2>/dev/null ||
478c8571806Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
479c8571806Smrg            { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
480c8571806Smrg          } ||
481c8571806Smrg          { echo "$0: cannot unlink or rename $dst" >&2
482c8571806Smrg            (exit 1); exit 1
483c8571806Smrg          }
484c8571806Smrg        } &&
485c8571806Smrg
486c8571806Smrg        # Now rename the file to the real destination.
487c8571806Smrg        $doit $mvcmd "$dsttmp" "$dst"
488ab902922Smrg      }
489ab902922Smrg    fi || exit 1
4907a84e134Smrg
4917a84e134Smrg    trap '' 0
4927a84e134Smrg  fi
4937a84e134Smrgdone
4947a84e134Smrg
4957a84e134Smrg# Local variables:
4967a84e134Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
4977a84e134Smrg# time-stamp-start: "scriptversion="
4987a84e134Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
499e1e1713cSmrg# time-stamp-time-zone: "UTC"
500e1e1713cSmrg# time-stamp-end: "; # UTC"
5017a84e134Smrg# End:
502