install-sh revision 7322289d
11d54945dSmrg#!/bin/sh
21d54945dSmrg# install - install a program, script, or datafile
31d54945dSmrg
47322289dSmrgscriptversion=2013-12-25.23; # UTC
51d54945dSmrg
61d54945dSmrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
71d54945dSmrg# later released in X11R6 (xc/config/util/install.sh) with the
81d54945dSmrg# following copyright and license.
91d54945dSmrg#
101d54945dSmrg# Copyright (C) 1994 X Consortium
111d54945dSmrg#
121d54945dSmrg# Permission is hereby granted, free of charge, to any person obtaining a copy
131d54945dSmrg# of this software and associated documentation files (the "Software"), to
141d54945dSmrg# deal in the Software without restriction, including without limitation the
151d54945dSmrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
161d54945dSmrg# sell copies of the Software, and to permit persons to whom the Software is
171d54945dSmrg# furnished to do so, subject to the following conditions:
181d54945dSmrg#
191d54945dSmrg# The above copyright notice and this permission notice shall be included in
201d54945dSmrg# all copies or substantial portions of the Software.
211d54945dSmrg#
221d54945dSmrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
231d54945dSmrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
241d54945dSmrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
251d54945dSmrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
261d54945dSmrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
271d54945dSmrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
281d54945dSmrg#
291d54945dSmrg# Except as contained in this notice, the name of the X Consortium shall not
301d54945dSmrg# be used in advertising or otherwise to promote the sale, use or other deal-
311d54945dSmrg# ings in this Software without prior written authorization from the X Consor-
321d54945dSmrg# tium.
331d54945dSmrg#
341d54945dSmrg#
351d54945dSmrg# FSF changes to this file are in the public domain.
361d54945dSmrg#
371d54945dSmrg# Calling this script install-sh is preferred over install.sh, to prevent
387322289dSmrg# 'make' implicit rules from creating a file called install from it
391d54945dSmrg# when there is no Makefile.
401d54945dSmrg#
411d54945dSmrg# This script is compatible with the BSD install script, but was written
42ba85709eSmrg# from scratch.
43ba85709eSmrg
447322289dSmrgtab='	'
45ba85709eSmrgnl='
46ba85709eSmrg'
477322289dSmrgIFS=" $tab$nl"
481d54945dSmrg
497322289dSmrg# Set DOITPROG to "echo" to test this script.
501d54945dSmrg
51786a6f21Smrgdoit=${DOITPROG-}
527322289dSmrgdoit_exec=${doit:-exec}
531d54945dSmrg
54ba85709eSmrg# Put in absolute file names if you don't have them in your path;
55ba85709eSmrg# or use environment vars.
561d54945dSmrg
57786a6f21Smrgchgrpprog=${CHGRPPROG-chgrp}
58786a6f21Smrgchmodprog=${CHMODPROG-chmod}
59786a6f21Smrgchownprog=${CHOWNPROG-chown}
60786a6f21Smrgcmpprog=${CMPPROG-cmp}
61786a6f21Smrgcpprog=${CPPROG-cp}
62786a6f21Smrgmkdirprog=${MKDIRPROG-mkdir}
63786a6f21Smrgmvprog=${MVPROG-mv}
64786a6f21Smrgrmprog=${RMPROG-rm}
65786a6f21Smrgstripprog=${STRIPPROG-strip}
66786a6f21Smrg
67ba85709eSmrgposix_mkdir=
68ba85709eSmrg
69ba85709eSmrg# Desired mode of installed file.
70ba85709eSmrgmode=0755
71ba85709eSmrg
72786a6f21Smrgchgrpcmd=
73ba85709eSmrgchmodcmd=$chmodprog
741d54945dSmrgchowncmd=
75786a6f21Smrgmvcmd=$mvprog
761d54945dSmrgrmcmd="$rmprog -f"
77786a6f21Smrgstripcmd=
78786a6f21Smrg
791d54945dSmrgsrc=
801d54945dSmrgdst=
811d54945dSmrgdir_arg=
82786a6f21Smrgdst_arg=
83786a6f21Smrg
84786a6f21Smrgcopy_on_change=false
857322289dSmrgis_target_a_directory=possibly
861d54945dSmrg
87786a6f21Smrgusage="\
88786a6f21SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
891d54945dSmrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
901d54945dSmrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
911d54945dSmrg   or: $0 [OPTION]... -d DIRECTORIES...
921d54945dSmrg
931d54945dSmrgIn the 1st form, copy SRCFILE to DSTFILE.
941d54945dSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
951d54945dSmrgIn the 4th, create DIRECTORIES.
961d54945dSmrg
971d54945dSmrgOptions:
98786a6f21Smrg     --help     display this help and exit.
99786a6f21Smrg     --version  display version info and exit.
100786a6f21Smrg
101786a6f21Smrg  -c            (ignored)
102786a6f21Smrg  -C            install only if different (preserve the last data modification time)
103786a6f21Smrg  -d            create directories instead of installing files.
104786a6f21Smrg  -g GROUP      $chgrpprog installed files to GROUP.
105786a6f21Smrg  -m MODE       $chmodprog installed files to MODE.
106786a6f21Smrg  -o USER       $chownprog installed files to USER.
107786a6f21Smrg  -s            $stripprog installed files.
108786a6f21Smrg  -t DIRECTORY  install into DIRECTORY.
109786a6f21Smrg  -T            report an error if DSTFILE is a directory.
1101d54945dSmrg
1111d54945dSmrgEnvironment variables override the default commands:
112786a6f21Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
113786a6f21Smrg  RMPROG STRIPPROG
1141d54945dSmrg"
1151d54945dSmrg
116ba85709eSmrgwhile test $# -ne 0; do
1171d54945dSmrg  case $1 in
118786a6f21Smrg    -c) ;;
119786a6f21Smrg
120786a6f21Smrg    -C) copy_on_change=true;;
1211d54945dSmrg
122786a6f21Smrg    -d) dir_arg=true;;
1231d54945dSmrg
1241d54945dSmrg    -g) chgrpcmd="$chgrpprog $2"
1257322289dSmrg        shift;;
1261d54945dSmrg
1271d54945dSmrg    --help) echo "$usage"; exit $?;;
1281d54945dSmrg
129ba85709eSmrg    -m) mode=$2
1307322289dSmrg        case $mode in
1317322289dSmrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
1327322289dSmrg            echo "$0: invalid mode: $mode" >&2
1337322289dSmrg            exit 1;;
1347322289dSmrg        esac
1357322289dSmrg        shift;;
1361d54945dSmrg
1371d54945dSmrg    -o) chowncmd="$chownprog $2"
1387322289dSmrg        shift;;
1391d54945dSmrg
140786a6f21Smrg    -s) stripcmd=$stripprog;;
1411d54945dSmrg
1427322289dSmrg    -t)
1437322289dSmrg        is_target_a_directory=always
1447322289dSmrg        dst_arg=$2
1457322289dSmrg        # Protect names problematic for 'test' and other utilities.
1467322289dSmrg        case $dst_arg in
1477322289dSmrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
1487322289dSmrg        esac
1497322289dSmrg        shift;;
1501d54945dSmrg
1517322289dSmrg    -T) is_target_a_directory=never;;
1521d54945dSmrg
1531d54945dSmrg    --version) echo "$0 $scriptversion"; exit $?;;
1541d54945dSmrg
1557322289dSmrg    --) shift
1567322289dSmrg        break;;
157ba85709eSmrg
1587322289dSmrg    -*) echo "$0: invalid option: $1" >&2
1597322289dSmrg        exit 1;;
160ba85709eSmrg
161ba85709eSmrg    *)  break;;
1621d54945dSmrg  esac
163786a6f21Smrg  shift
1641d54945dSmrgdone
1651d54945dSmrg
1667322289dSmrg# We allow the use of options -d and -T together, by making -d
1677322289dSmrg# take the precedence; this is for compatibility with GNU install.
1687322289dSmrg
1697322289dSmrgif test -n "$dir_arg"; then
1707322289dSmrg  if test -n "$dst_arg"; then
1717322289dSmrg    echo "$0: target directory not allowed when installing a directory." >&2
1727322289dSmrg    exit 1
1737322289dSmrg  fi
1747322289dSmrgfi
1757322289dSmrg
176786a6f21Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
177ba85709eSmrg  # When -d is used, all remaining arguments are directories to create.
178ba85709eSmrg  # When -t is used, the destination is already specified.
179ba85709eSmrg  # Otherwise, the last argument is the destination.  Remove it from $@.
180ba85709eSmrg  for arg
181ba85709eSmrg  do
182786a6f21Smrg    if test -n "$dst_arg"; then
183ba85709eSmrg      # $@ is not empty: it contains at least $arg.
184786a6f21Smrg      set fnord "$@" "$dst_arg"
185ba85709eSmrg      shift # fnord
186ba85709eSmrg    fi
187ba85709eSmrg    shift # arg
188786a6f21Smrg    dst_arg=$arg
1897322289dSmrg    # Protect names problematic for 'test' and other utilities.
190d769e936Smrg    case $dst_arg in
191d769e936Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
192d769e936Smrg    esac
193ba85709eSmrg  done
194ba85709eSmrgfi
195ba85709eSmrg
196ba85709eSmrgif test $# -eq 0; then
1971d54945dSmrg  if test -z "$dir_arg"; then
1981d54945dSmrg    echo "$0: no input file specified." >&2
1991d54945dSmrg    exit 1
2001d54945dSmrg  fi
2017322289dSmrg  # It's OK to call 'install-sh -d' without argument.
2021d54945dSmrg  # This can happen when creating conditional directories.
2031d54945dSmrg  exit 0
2041d54945dSmrgfi
2051d54945dSmrg
2067322289dSmrgif test -z "$dir_arg"; then
2077322289dSmrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
2087322289dSmrg    if test ! -d "$dst_arg"; then
2097322289dSmrg      echo "$0: $dst_arg: Is not a directory." >&2
2107322289dSmrg      exit 1
2117322289dSmrg    fi
2127322289dSmrg  fi
2137322289dSmrgfi
2147322289dSmrg
215ba85709eSmrgif test -z "$dir_arg"; then
216d769e936Smrg  do_exit='(exit $ret); exit $ret'
217d769e936Smrg  trap "ret=129; $do_exit" 1
218d769e936Smrg  trap "ret=130; $do_exit" 2
219d769e936Smrg  trap "ret=141; $do_exit" 13
220d769e936Smrg  trap "ret=143; $do_exit" 15
221ba85709eSmrg
222ba85709eSmrg  # Set umask so as not to create temps with too-generous modes.
223ba85709eSmrg  # However, 'strip' requires both read and write access to temps.
224ba85709eSmrg  case $mode in
225ba85709eSmrg    # Optimize common cases.
226ba85709eSmrg    *644) cp_umask=133;;
227ba85709eSmrg    *755) cp_umask=22;;
228ba85709eSmrg
229ba85709eSmrg    *[0-7])
230ba85709eSmrg      if test -z "$stripcmd"; then
2317322289dSmrg        u_plus_rw=
232ba85709eSmrg      else
2337322289dSmrg        u_plus_rw='% 200'
234ba85709eSmrg      fi
235ba85709eSmrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
236ba85709eSmrg    *)
237ba85709eSmrg      if test -z "$stripcmd"; then
2387322289dSmrg        u_plus_rw=
239ba85709eSmrg      else
2407322289dSmrg        u_plus_rw=,u+rw
241ba85709eSmrg      fi
242ba85709eSmrg      cp_umask=$mode$u_plus_rw;;
243ba85709eSmrg  esac
244ba85709eSmrgfi
245ba85709eSmrg
2461d54945dSmrgfor src
2471d54945dSmrgdo
2487322289dSmrg  # Protect names problematic for 'test' and other utilities.
2491d54945dSmrg  case $src in
250d769e936Smrg    -* | [=\(\)!]) src=./$src;;
2511d54945dSmrg  esac
2521d54945dSmrg
2531d54945dSmrg  if test -n "$dir_arg"; then
2541d54945dSmrg    dst=$src
255ba85709eSmrg    dstdir=$dst
256ba85709eSmrg    test -d "$dstdir"
257ba85709eSmrg    dstdir_status=$?
2581d54945dSmrg  else
259ba85709eSmrg
2601d54945dSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
2611d54945dSmrg    # might cause directories to be created, which would be especially bad
2621d54945dSmrg    # if $src (and thus $dsttmp) contains '*'.
2631d54945dSmrg    if test ! -f "$src" && test ! -d "$src"; then
2641d54945dSmrg      echo "$0: $src does not exist." >&2
2651d54945dSmrg      exit 1
2661d54945dSmrg    fi
2671d54945dSmrg
268786a6f21Smrg    if test -z "$dst_arg"; then
2691d54945dSmrg      echo "$0: no destination specified." >&2
2701d54945dSmrg      exit 1
2711d54945dSmrg    fi
272786a6f21Smrg    dst=$dst_arg
2731d54945dSmrg
2741d54945dSmrg    # If destination is a directory, append the input filename; won't work
2751d54945dSmrg    # if double slashes aren't ignored.
2761d54945dSmrg    if test -d "$dst"; then
2777322289dSmrg      if test "$is_target_a_directory" = never; then
2787322289dSmrg        echo "$0: $dst_arg: Is a directory" >&2
2797322289dSmrg        exit 1
2801d54945dSmrg      fi
281ba85709eSmrg      dstdir=$dst
282ba85709eSmrg      dst=$dstdir/`basename "$src"`
283ba85709eSmrg      dstdir_status=0
284ba85709eSmrg    else
2857322289dSmrg      dstdir=`dirname "$dst"`
286ba85709eSmrg      test -d "$dstdir"
287ba85709eSmrg      dstdir_status=$?
2881d54945dSmrg    fi
2891d54945dSmrg  fi
2901d54945dSmrg
291ba85709eSmrg  obsolete_mkdir_used=false
292ba85709eSmrg
293ba85709eSmrg  if test $dstdir_status != 0; then
294ba85709eSmrg    case $posix_mkdir in
295ba85709eSmrg      '')
2967322289dSmrg        # Create intermediate dirs using mode 755 as modified by the umask.
2977322289dSmrg        # This is like FreeBSD 'install' as of 1997-10-28.
2987322289dSmrg        umask=`umask`
2997322289dSmrg        case $stripcmd.$umask in
3007322289dSmrg          # Optimize common cases.
3017322289dSmrg          *[2367][2367]) mkdir_umask=$umask;;
3027322289dSmrg          .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
3037322289dSmrg
3047322289dSmrg          *[0-7])
3057322289dSmrg            mkdir_umask=`expr $umask + 22 \
3067322289dSmrg              - $umask % 100 % 40 + $umask % 20 \
3077322289dSmrg              - $umask % 10 % 4 + $umask % 2
3087322289dSmrg            `;;
3097322289dSmrg          *) mkdir_umask=$umask,go-w;;
3107322289dSmrg        esac
3117322289dSmrg
3127322289dSmrg        # With -d, create the new directory with the user-specified mode.
3137322289dSmrg        # Otherwise, rely on $mkdir_umask.
3147322289dSmrg        if test -n "$dir_arg"; then
3157322289dSmrg          mkdir_mode=-m$mode
3167322289dSmrg        else
3177322289dSmrg          mkdir_mode=
3187322289dSmrg        fi
3197322289dSmrg
3207322289dSmrg        posix_mkdir=false
3217322289dSmrg        case $umask in
3227322289dSmrg          *[123567][0-7][0-7])
3237322289dSmrg            # POSIX mkdir -p sets u+wx bits regardless of umask, which
3247322289dSmrg            # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
3257322289dSmrg            ;;
3267322289dSmrg          *)
3277322289dSmrg            tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
3287322289dSmrg            trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
3297322289dSmrg
3307322289dSmrg            if (umask $mkdir_umask &&
3317322289dSmrg                exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
3327322289dSmrg            then
3337322289dSmrg              if test -z "$dir_arg" || {
3347322289dSmrg                   # Check for POSIX incompatibilities with -m.
3357322289dSmrg                   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
3367322289dSmrg                   # other-writable bit of parent directory when it shouldn't.
3377322289dSmrg                   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
3387322289dSmrg                   ls_ld_tmpdir=`ls -ld "$tmpdir"`
3397322289dSmrg                   case $ls_ld_tmpdir in
3407322289dSmrg                     d????-?r-*) different_mode=700;;
3417322289dSmrg                     d????-?--*) different_mode=755;;
3427322289dSmrg                     *) false;;
3437322289dSmrg                   esac &&
3447322289dSmrg                   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
3457322289dSmrg                     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
3467322289dSmrg                     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
3477322289dSmrg                   }
3487322289dSmrg                 }
3497322289dSmrg              then posix_mkdir=:
3507322289dSmrg              fi
3517322289dSmrg              rmdir "$tmpdir/d" "$tmpdir"
3527322289dSmrg            else
3537322289dSmrg              # Remove any dirs left behind by ancient mkdir implementations.
3547322289dSmrg              rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
3557322289dSmrg            fi
3567322289dSmrg            trap '' 0;;
3577322289dSmrg        esac;;
358ba85709eSmrg    esac
3591d54945dSmrg
360ba85709eSmrg    if
361ba85709eSmrg      $posix_mkdir && (
3627322289dSmrg        umask $mkdir_umask &&
3637322289dSmrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
364ba85709eSmrg      )
365ba85709eSmrg    then :
366ba85709eSmrg    else
3671d54945dSmrg
368ba85709eSmrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
369ba85709eSmrg      # or it failed possibly due to a race condition.  Create the
370ba85709eSmrg      # directory the slow way, step by step, checking for races as we go.
371ba85709eSmrg
372ba85709eSmrg      case $dstdir in
3737322289dSmrg        /*) prefix='/';;
3747322289dSmrg        [-=\(\)!]*) prefix='./';;
3757322289dSmrg        *)  prefix='';;
376ba85709eSmrg      esac
377ba85709eSmrg
378ba85709eSmrg      oIFS=$IFS
379ba85709eSmrg      IFS=/
3807322289dSmrg      set -f
381ba85709eSmrg      set fnord $dstdir
3821d54945dSmrg      shift
3837322289dSmrg      set +f
384ba85709eSmrg      IFS=$oIFS
385ba85709eSmrg
386ba85709eSmrg      prefixes=
387ba85709eSmrg
388ba85709eSmrg      for d
389ba85709eSmrg      do
3907322289dSmrg        test X"$d" = X && continue
3917322289dSmrg
3927322289dSmrg        prefix=$prefix$d
3937322289dSmrg        if test -d "$prefix"; then
3947322289dSmrg          prefixes=
3957322289dSmrg        else
3967322289dSmrg          if $posix_mkdir; then
3977322289dSmrg            (umask=$mkdir_umask &&
3987322289dSmrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
3997322289dSmrg            # Don't fail if two instances are running concurrently.
4007322289dSmrg            test -d "$prefix" || exit 1
4017322289dSmrg          else
4027322289dSmrg            case $prefix in
4037322289dSmrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
4047322289dSmrg              *) qprefix=$prefix;;
4057322289dSmrg            esac
4067322289dSmrg            prefixes="$prefixes '$qprefix'"
4077322289dSmrg          fi
4087322289dSmrg        fi
4097322289dSmrg        prefix=$prefix/
410ba85709eSmrg      done
411ba85709eSmrg
412ba85709eSmrg      if test -n "$prefixes"; then
4137322289dSmrg        # Don't fail if two instances are running concurrently.
4147322289dSmrg        (umask $mkdir_umask &&
4157322289dSmrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
4167322289dSmrg          test -d "$dstdir" || exit 1
4177322289dSmrg        obsolete_mkdir_used=true
4181d54945dSmrg      fi
419ba85709eSmrg    fi
4201d54945dSmrg  fi
4211d54945dSmrg
4221d54945dSmrg  if test -n "$dir_arg"; then
423ba85709eSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
424ba85709eSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
425ba85709eSmrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
426ba85709eSmrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
4271d54945dSmrg  else
4281d54945dSmrg
4291d54945dSmrg    # Make a couple of temp file names in the proper directory.
4301d54945dSmrg    dsttmp=$dstdir/_inst.$$_
4311d54945dSmrg    rmtmp=$dstdir/_rm.$$_
4321d54945dSmrg
4331d54945dSmrg    # Trap to clean up those temp files at exit.
4341d54945dSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
4351d54945dSmrg
4361d54945dSmrg    # Copy the file name to the temp name.
437ba85709eSmrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
4381d54945dSmrg
4391d54945dSmrg    # and set any options; do chmod last to preserve setuid bits.
4401d54945dSmrg    #
4411d54945dSmrg    # If any of these fail, we abort the whole thing.  If we want to
4421d54945dSmrg    # ignore errors from any of these, just make sure not to ignore
4431d54945dSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
4441d54945dSmrg    #
445786a6f21Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
446786a6f21Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
447786a6f21Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
448786a6f21Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
449786a6f21Smrg
450786a6f21Smrg    # If -C, don't bother to copy if it wouldn't change the file.
451786a6f21Smrg    if $copy_on_change &&
4527322289dSmrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
4537322289dSmrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
4547322289dSmrg       set -f &&
455786a6f21Smrg       set X $old && old=:$2:$4:$5:$6 &&
456786a6f21Smrg       set X $new && new=:$2:$4:$5:$6 &&
4577322289dSmrg       set +f &&
458786a6f21Smrg       test "$old" = "$new" &&
459786a6f21Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
460786a6f21Smrg    then
461786a6f21Smrg      rm -f "$dsttmp"
462786a6f21Smrg    else
463786a6f21Smrg      # Rename the file to the real destination.
464786a6f21Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
465786a6f21Smrg
466786a6f21Smrg      # The rename failed, perhaps because mv can't rename something else
467786a6f21Smrg      # to itself, or perhaps because mv is so ancient that it does not
468786a6f21Smrg      # support -f.
469786a6f21Smrg      {
4707322289dSmrg        # Now remove or move aside any old file at destination location.
4717322289dSmrg        # We try this two ways since rm can't unlink itself on some
4727322289dSmrg        # systems and the destination file might be busy for other
4737322289dSmrg        # reasons.  In this case, the final cleanup might fail but the new
4747322289dSmrg        # file should still install successfully.
4757322289dSmrg        {
4767322289dSmrg          test ! -f "$dst" ||
4777322289dSmrg          $doit $rmcmd -f "$dst" 2>/dev/null ||
4787322289dSmrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
4797322289dSmrg            { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
4807322289dSmrg          } ||
4817322289dSmrg          { echo "$0: cannot unlink or rename $dst" >&2
4827322289dSmrg            (exit 1); exit 1
4837322289dSmrg          }
4847322289dSmrg        } &&
4857322289dSmrg
4867322289dSmrg        # Now rename the file to the real destination.
4877322289dSmrg        $doit $mvcmd "$dsttmp" "$dst"
488786a6f21Smrg      }
489786a6f21Smrg    fi || exit 1
4901d54945dSmrg
491ba85709eSmrg    trap '' 0
492ba85709eSmrg  fi
493ba85709eSmrgdone
4941d54945dSmrg
4951d54945dSmrg# Local variables:
4961d54945dSmrg# eval: (add-hook 'write-file-hooks 'time-stamp)
4971d54945dSmrg# time-stamp-start: "scriptversion="
4981d54945dSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
499ec713c28Smrg# time-stamp-time-zone: "UTC"
500ec713c28Smrg# time-stamp-end: "; # UTC"
5011d54945dSmrg# End:
502