install-sh revision 198e4c3c
16c321187Smrg#!/bin/sh
26c321187Smrg# install - install a program, script, or datafile
36c321187Smrg
4198e4c3cSmrgscriptversion=2013-12-25.23; # UTC
56c321187Smrg
66c321187Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
76c321187Smrg# later released in X11R6 (xc/config/util/install.sh) with the
86c321187Smrg# following copyright and license.
96c321187Smrg#
106c321187Smrg# Copyright (C) 1994 X Consortium
116c321187Smrg#
126c321187Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
136c321187Smrg# of this software and associated documentation files (the "Software"), to
146c321187Smrg# deal in the Software without restriction, including without limitation the
156c321187Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
166c321187Smrg# sell copies of the Software, and to permit persons to whom the Software is
176c321187Smrg# furnished to do so, subject to the following conditions:
186c321187Smrg#
196c321187Smrg# The above copyright notice and this permission notice shall be included in
206c321187Smrg# all copies or substantial portions of the Software.
216c321187Smrg#
226c321187Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
236c321187Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
246c321187Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
256c321187Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
266c321187Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
276c321187Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
286c321187Smrg#
296c321187Smrg# Except as contained in this notice, the name of the X Consortium shall not
306c321187Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
316c321187Smrg# ings in this Software without prior written authorization from the X Consor-
326c321187Smrg# tium.
336c321187Smrg#
346c321187Smrg#
356c321187Smrg# FSF changes to this file are in the public domain.
366c321187Smrg#
376c321187Smrg# Calling this script install-sh is preferred over install.sh, to prevent
389d0b5e55Smrg# 'make' implicit rules from creating a file called install from it
396c321187Smrg# when there is no Makefile.
406c321187Smrg#
416c321187Smrg# This script is compatible with the BSD install script, but was written
426c321187Smrg# from scratch.
436c321187Smrg
44198e4c3cSmrgtab='	'
456c321187Smrgnl='
466c321187Smrg'
47198e4c3cSmrgIFS=" $tab$nl"
486c321187Smrg
49198e4c3cSmrg# Set DOITPROG to "echo" to test this script.
506c321187Smrg
5193493779Smrgdoit=${DOITPROG-}
52198e4c3cSmrgdoit_exec=${doit:-exec}
536c321187Smrg
546c321187Smrg# Put in absolute file names if you don't have them in your path;
556c321187Smrg# or use environment vars.
566c321187Smrg
5793493779Smrgchgrpprog=${CHGRPPROG-chgrp}
5893493779Smrgchmodprog=${CHMODPROG-chmod}
5993493779Smrgchownprog=${CHOWNPROG-chown}
6093493779Smrgcmpprog=${CMPPROG-cmp}
6193493779Smrgcpprog=${CPPROG-cp}
6293493779Smrgmkdirprog=${MKDIRPROG-mkdir}
6393493779Smrgmvprog=${MVPROG-mv}
6493493779Smrgrmprog=${RMPROG-rm}
6593493779Smrgstripprog=${STRIPPROG-strip}
6693493779Smrg
676c321187Smrgposix_mkdir=
686c321187Smrg
696c321187Smrg# Desired mode of installed file.
706c321187Smrgmode=0755
716c321187Smrg
7293493779Smrgchgrpcmd=
736c321187Smrgchmodcmd=$chmodprog
746c321187Smrgchowncmd=
7593493779Smrgmvcmd=$mvprog
766c321187Smrgrmcmd="$rmprog -f"
7793493779Smrgstripcmd=
7893493779Smrg
796c321187Smrgsrc=
806c321187Smrgdst=
816c321187Smrgdir_arg=
8293493779Smrgdst_arg=
8393493779Smrg
8493493779Smrgcopy_on_change=false
85198e4c3cSmrgis_target_a_directory=possibly
866c321187Smrg
8793493779Smrgusage="\
8893493779SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
896c321187Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
906c321187Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
916c321187Smrg   or: $0 [OPTION]... -d DIRECTORIES...
926c321187Smrg
936c321187SmrgIn the 1st form, copy SRCFILE to DSTFILE.
946c321187SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
956c321187SmrgIn the 4th, create DIRECTORIES.
966c321187Smrg
976c321187SmrgOptions:
9893493779Smrg     --help     display this help and exit.
9993493779Smrg     --version  display version info and exit.
10093493779Smrg
10193493779Smrg  -c            (ignored)
10293493779Smrg  -C            install only if different (preserve the last data modification time)
10393493779Smrg  -d            create directories instead of installing files.
10493493779Smrg  -g GROUP      $chgrpprog installed files to GROUP.
10593493779Smrg  -m MODE       $chmodprog installed files to MODE.
10693493779Smrg  -o USER       $chownprog installed files to USER.
10793493779Smrg  -s            $stripprog installed files.
10893493779Smrg  -t DIRECTORY  install into DIRECTORY.
10993493779Smrg  -T            report an error if DSTFILE is a directory.
1106c321187Smrg
1116c321187SmrgEnvironment variables override the default commands:
11293493779Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
11393493779Smrg  RMPROG STRIPPROG
1146c321187Smrg"
1156c321187Smrg
1166c321187Smrgwhile test $# -ne 0; do
1176c321187Smrg  case $1 in
11893493779Smrg    -c) ;;
11993493779Smrg
12093493779Smrg    -C) copy_on_change=true;;
1216c321187Smrg
12293493779Smrg    -d) dir_arg=true;;
1236c321187Smrg
1246c321187Smrg    -g) chgrpcmd="$chgrpprog $2"
125198e4c3cSmrg        shift;;
1266c321187Smrg
1276c321187Smrg    --help) echo "$usage"; exit $?;;
1286c321187Smrg
1296c321187Smrg    -m) mode=$2
130198e4c3cSmrg        case $mode in
131198e4c3cSmrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
132198e4c3cSmrg            echo "$0: invalid mode: $mode" >&2
133198e4c3cSmrg            exit 1;;
134198e4c3cSmrg        esac
135198e4c3cSmrg        shift;;
1366c321187Smrg
1376c321187Smrg    -o) chowncmd="$chownprog $2"
138198e4c3cSmrg        shift;;
1396c321187Smrg
14093493779Smrg    -s) stripcmd=$stripprog;;
1416c321187Smrg
142198e4c3cSmrg    -t)
143198e4c3cSmrg        is_target_a_directory=always
144198e4c3cSmrg        dst_arg=$2
145198e4c3cSmrg        # Protect names problematic for 'test' and other utilities.
146198e4c3cSmrg        case $dst_arg in
147198e4c3cSmrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
148198e4c3cSmrg        esac
149198e4c3cSmrg        shift;;
1506c321187Smrg
151198e4c3cSmrg    -T) is_target_a_directory=never;;
1526c321187Smrg
1536c321187Smrg    --version) echo "$0 $scriptversion"; exit $?;;
1546c321187Smrg
155198e4c3cSmrg    --) shift
156198e4c3cSmrg        break;;
1576c321187Smrg
158198e4c3cSmrg    -*) echo "$0: invalid option: $1" >&2
159198e4c3cSmrg        exit 1;;
1606c321187Smrg
1616c321187Smrg    *)  break;;
1626c321187Smrg  esac
16393493779Smrg  shift
1646c321187Smrgdone
1656c321187Smrg
166198e4c3cSmrg# We allow the use of options -d and -T together, by making -d
167198e4c3cSmrg# take the precedence; this is for compatibility with GNU install.
168198e4c3cSmrg
169198e4c3cSmrgif test -n "$dir_arg"; then
170198e4c3cSmrg  if test -n "$dst_arg"; then
171198e4c3cSmrg    echo "$0: target directory not allowed when installing a directory." >&2
172198e4c3cSmrg    exit 1
173198e4c3cSmrg  fi
174198e4c3cSmrgfi
175198e4c3cSmrg
17693493779Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1776c321187Smrg  # When -d is used, all remaining arguments are directories to create.
1786c321187Smrg  # When -t is used, the destination is already specified.
1796c321187Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
1806c321187Smrg  for arg
1816c321187Smrg  do
18293493779Smrg    if test -n "$dst_arg"; then
1836c321187Smrg      # $@ is not empty: it contains at least $arg.
18493493779Smrg      set fnord "$@" "$dst_arg"
1856c321187Smrg      shift # fnord
1866c321187Smrg    fi
1876c321187Smrg    shift # arg
18893493779Smrg    dst_arg=$arg
1899d0b5e55Smrg    # Protect names problematic for 'test' and other utilities.
1909d0b5e55Smrg    case $dst_arg in
1919d0b5e55Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
1929d0b5e55Smrg    esac
1936c321187Smrg  done
1946c321187Smrgfi
1956c321187Smrg
1966c321187Smrgif test $# -eq 0; then
1976c321187Smrg  if test -z "$dir_arg"; then
1986c321187Smrg    echo "$0: no input file specified." >&2
1996c321187Smrg    exit 1
2006c321187Smrg  fi
2019d0b5e55Smrg  # It's OK to call 'install-sh -d' without argument.
2026c321187Smrg  # This can happen when creating conditional directories.
2036c321187Smrg  exit 0
2046c321187Smrgfi
2056c321187Smrg
206198e4c3cSmrgif test -z "$dir_arg"; then
207198e4c3cSmrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
208198e4c3cSmrg    if test ! -d "$dst_arg"; then
209198e4c3cSmrg      echo "$0: $dst_arg: Is not a directory." >&2
210198e4c3cSmrg      exit 1
211198e4c3cSmrg    fi
212198e4c3cSmrg  fi
213198e4c3cSmrgfi
214198e4c3cSmrg
2156c321187Smrgif test -z "$dir_arg"; then
2169d0b5e55Smrg  do_exit='(exit $ret); exit $ret'
2179d0b5e55Smrg  trap "ret=129; $do_exit" 1
2189d0b5e55Smrg  trap "ret=130; $do_exit" 2
2199d0b5e55Smrg  trap "ret=141; $do_exit" 13
2209d0b5e55Smrg  trap "ret=143; $do_exit" 15
2216c321187Smrg
2226c321187Smrg  # Set umask so as not to create temps with too-generous modes.
2236c321187Smrg  # However, 'strip' requires both read and write access to temps.
2246c321187Smrg  case $mode in
2256c321187Smrg    # Optimize common cases.
2266c321187Smrg    *644) cp_umask=133;;
2276c321187Smrg    *755) cp_umask=22;;
2286c321187Smrg
2296c321187Smrg    *[0-7])
2306c321187Smrg      if test -z "$stripcmd"; then
231198e4c3cSmrg        u_plus_rw=
2326c321187Smrg      else
233198e4c3cSmrg        u_plus_rw='% 200'
2346c321187Smrg      fi
2356c321187Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2366c321187Smrg    *)
2376c321187Smrg      if test -z "$stripcmd"; then
238198e4c3cSmrg        u_plus_rw=
2396c321187Smrg      else
240198e4c3cSmrg        u_plus_rw=,u+rw
2416c321187Smrg      fi
2426c321187Smrg      cp_umask=$mode$u_plus_rw;;
2436c321187Smrg  esac
2446c321187Smrgfi
2456c321187Smrg
2466c321187Smrgfor src
2476c321187Smrgdo
2489d0b5e55Smrg  # Protect names problematic for 'test' and other utilities.
2496c321187Smrg  case $src in
2509d0b5e55Smrg    -* | [=\(\)!]) src=./$src;;
2516c321187Smrg  esac
2526c321187Smrg
2536c321187Smrg  if test -n "$dir_arg"; then
2546c321187Smrg    dst=$src
2556c321187Smrg    dstdir=$dst
2566c321187Smrg    test -d "$dstdir"
2576c321187Smrg    dstdir_status=$?
2586c321187Smrg  else
2596c321187Smrg
2606c321187Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
2616c321187Smrg    # might cause directories to be created, which would be especially bad
2626c321187Smrg    # if $src (and thus $dsttmp) contains '*'.
2636c321187Smrg    if test ! -f "$src" && test ! -d "$src"; then
2646c321187Smrg      echo "$0: $src does not exist." >&2
2656c321187Smrg      exit 1
2666c321187Smrg    fi
2676c321187Smrg
26893493779Smrg    if test -z "$dst_arg"; then
2696c321187Smrg      echo "$0: no destination specified." >&2
2706c321187Smrg      exit 1
2716c321187Smrg    fi
27293493779Smrg    dst=$dst_arg
2736c321187Smrg
2746c321187Smrg    # If destination is a directory, append the input filename; won't work
2756c321187Smrg    # if double slashes aren't ignored.
2766c321187Smrg    if test -d "$dst"; then
277198e4c3cSmrg      if test "$is_target_a_directory" = never; then
278198e4c3cSmrg        echo "$0: $dst_arg: Is a directory" >&2
279198e4c3cSmrg        exit 1
2806c321187Smrg      fi
2816c321187Smrg      dstdir=$dst
2826c321187Smrg      dst=$dstdir/`basename "$src"`
2836c321187Smrg      dstdir_status=0
2846c321187Smrg    else
285198e4c3cSmrg      dstdir=`dirname "$dst"`
2866c321187Smrg      test -d "$dstdir"
2876c321187Smrg      dstdir_status=$?
2886c321187Smrg    fi
2896c321187Smrg  fi
2906c321187Smrg
2916c321187Smrg  obsolete_mkdir_used=false
2926c321187Smrg
2936c321187Smrg  if test $dstdir_status != 0; then
2946c321187Smrg    case $posix_mkdir in
2956c321187Smrg      '')
296198e4c3cSmrg        # Create intermediate dirs using mode 755 as modified by the umask.
297198e4c3cSmrg        # This is like FreeBSD 'install' as of 1997-10-28.
298198e4c3cSmrg        umask=`umask`
299198e4c3cSmrg        case $stripcmd.$umask in
300198e4c3cSmrg          # Optimize common cases.
301198e4c3cSmrg          *[2367][2367]) mkdir_umask=$umask;;
302198e4c3cSmrg          .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
303198e4c3cSmrg
304198e4c3cSmrg          *[0-7])
305198e4c3cSmrg            mkdir_umask=`expr $umask + 22 \
306198e4c3cSmrg              - $umask % 100 % 40 + $umask % 20 \
307198e4c3cSmrg              - $umask % 10 % 4 + $umask % 2
308198e4c3cSmrg            `;;
309198e4c3cSmrg          *) mkdir_umask=$umask,go-w;;
310198e4c3cSmrg        esac
311198e4c3cSmrg
312198e4c3cSmrg        # With -d, create the new directory with the user-specified mode.
313198e4c3cSmrg        # Otherwise, rely on $mkdir_umask.
314198e4c3cSmrg        if test -n "$dir_arg"; then
315198e4c3cSmrg          mkdir_mode=-m$mode
316198e4c3cSmrg        else
317198e4c3cSmrg          mkdir_mode=
318198e4c3cSmrg        fi
319198e4c3cSmrg
320198e4c3cSmrg        posix_mkdir=false
321198e4c3cSmrg        case $umask in
322198e4c3cSmrg          *[123567][0-7][0-7])
323198e4c3cSmrg            # POSIX mkdir -p sets u+wx bits regardless of umask, which
324198e4c3cSmrg            # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
325198e4c3cSmrg            ;;
326198e4c3cSmrg          *)
327198e4c3cSmrg            tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
328198e4c3cSmrg            trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
329198e4c3cSmrg
330198e4c3cSmrg            if (umask $mkdir_umask &&
331198e4c3cSmrg                exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
332198e4c3cSmrg            then
333198e4c3cSmrg              if test -z "$dir_arg" || {
334198e4c3cSmrg                   # Check for POSIX incompatibilities with -m.
335198e4c3cSmrg                   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
336198e4c3cSmrg                   # other-writable bit of parent directory when it shouldn't.
337198e4c3cSmrg                   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
338198e4c3cSmrg                   ls_ld_tmpdir=`ls -ld "$tmpdir"`
339198e4c3cSmrg                   case $ls_ld_tmpdir in
340198e4c3cSmrg                     d????-?r-*) different_mode=700;;
341198e4c3cSmrg                     d????-?--*) different_mode=755;;
342198e4c3cSmrg                     *) false;;
343198e4c3cSmrg                   esac &&
344198e4c3cSmrg                   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
345198e4c3cSmrg                     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
346198e4c3cSmrg                     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
347198e4c3cSmrg                   }
348198e4c3cSmrg                 }
349198e4c3cSmrg              then posix_mkdir=:
350198e4c3cSmrg              fi
351198e4c3cSmrg              rmdir "$tmpdir/d" "$tmpdir"
352198e4c3cSmrg            else
353198e4c3cSmrg              # Remove any dirs left behind by ancient mkdir implementations.
354198e4c3cSmrg              rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
355198e4c3cSmrg            fi
356198e4c3cSmrg            trap '' 0;;
357198e4c3cSmrg        esac;;
3586c321187Smrg    esac
3596c321187Smrg
3606c321187Smrg    if
3616c321187Smrg      $posix_mkdir && (
362198e4c3cSmrg        umask $mkdir_umask &&
363198e4c3cSmrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3646c321187Smrg      )
3656c321187Smrg    then :
3666c321187Smrg    else
3676c321187Smrg
3686c321187Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
3696c321187Smrg      # or it failed possibly due to a race condition.  Create the
3706c321187Smrg      # directory the slow way, step by step, checking for races as we go.
3716c321187Smrg
3726c321187Smrg      case $dstdir in
373198e4c3cSmrg        /*) prefix='/';;
374198e4c3cSmrg        [-=\(\)!]*) prefix='./';;
375198e4c3cSmrg        *)  prefix='';;
3766c321187Smrg      esac
3776c321187Smrg
3786c321187Smrg      oIFS=$IFS
3796c321187Smrg      IFS=/
380198e4c3cSmrg      set -f
3816c321187Smrg      set fnord $dstdir
3826c321187Smrg      shift
383198e4c3cSmrg      set +f
3846c321187Smrg      IFS=$oIFS
3856c321187Smrg
3866c321187Smrg      prefixes=
3876c321187Smrg
3886c321187Smrg      for d
3896c321187Smrg      do
390198e4c3cSmrg        test X"$d" = X && continue
391198e4c3cSmrg
392198e4c3cSmrg        prefix=$prefix$d
393198e4c3cSmrg        if test -d "$prefix"; then
394198e4c3cSmrg          prefixes=
395198e4c3cSmrg        else
396198e4c3cSmrg          if $posix_mkdir; then
397198e4c3cSmrg            (umask=$mkdir_umask &&
398198e4c3cSmrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
399198e4c3cSmrg            # Don't fail if two instances are running concurrently.
400198e4c3cSmrg            test -d "$prefix" || exit 1
401198e4c3cSmrg          else
402198e4c3cSmrg            case $prefix in
403198e4c3cSmrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
404198e4c3cSmrg              *) qprefix=$prefix;;
405198e4c3cSmrg            esac
406198e4c3cSmrg            prefixes="$prefixes '$qprefix'"
407198e4c3cSmrg          fi
408198e4c3cSmrg        fi
409198e4c3cSmrg        prefix=$prefix/
4106c321187Smrg      done
4116c321187Smrg
4126c321187Smrg      if test -n "$prefixes"; then
413198e4c3cSmrg        # Don't fail if two instances are running concurrently.
414198e4c3cSmrg        (umask $mkdir_umask &&
415198e4c3cSmrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
416198e4c3cSmrg          test -d "$dstdir" || exit 1
417198e4c3cSmrg        obsolete_mkdir_used=true
4186c321187Smrg      fi
4196c321187Smrg    fi
4206c321187Smrg  fi
4216c321187Smrg
4226c321187Smrg  if test -n "$dir_arg"; then
4236c321187Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4246c321187Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4256c321187Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4266c321187Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
4276c321187Smrg  else
4286c321187Smrg
4296c321187Smrg    # Make a couple of temp file names in the proper directory.
4306c321187Smrg    dsttmp=$dstdir/_inst.$$_
4316c321187Smrg    rmtmp=$dstdir/_rm.$$_
4326c321187Smrg
4336c321187Smrg    # Trap to clean up those temp files at exit.
4346c321187Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
4356c321187Smrg
4366c321187Smrg    # Copy the file name to the temp name.
4376c321187Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
4386c321187Smrg
4396c321187Smrg    # and set any options; do chmod last to preserve setuid bits.
4406c321187Smrg    #
4416c321187Smrg    # If any of these fail, we abort the whole thing.  If we want to
4426c321187Smrg    # ignore errors from any of these, just make sure not to ignore
4436c321187Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
4446c321187Smrg    #
44593493779Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
44693493779Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
44793493779Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
44893493779Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
44993493779Smrg
45093493779Smrg    # If -C, don't bother to copy if it wouldn't change the file.
45193493779Smrg    if $copy_on_change &&
452198e4c3cSmrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
453198e4c3cSmrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
454198e4c3cSmrg       set -f &&
45593493779Smrg       set X $old && old=:$2:$4:$5:$6 &&
45693493779Smrg       set X $new && new=:$2:$4:$5:$6 &&
457198e4c3cSmrg       set +f &&
45893493779Smrg       test "$old" = "$new" &&
45993493779Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
46093493779Smrg    then
46193493779Smrg      rm -f "$dsttmp"
46293493779Smrg    else
46393493779Smrg      # Rename the file to the real destination.
46493493779Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
46593493779Smrg
46693493779Smrg      # The rename failed, perhaps because mv can't rename something else
46793493779Smrg      # to itself, or perhaps because mv is so ancient that it does not
46893493779Smrg      # support -f.
46993493779Smrg      {
470198e4c3cSmrg        # Now remove or move aside any old file at destination location.
471198e4c3cSmrg        # We try this two ways since rm can't unlink itself on some
472198e4c3cSmrg        # systems and the destination file might be busy for other
473198e4c3cSmrg        # reasons.  In this case, the final cleanup might fail but the new
474198e4c3cSmrg        # file should still install successfully.
475198e4c3cSmrg        {
476198e4c3cSmrg          test ! -f "$dst" ||
477198e4c3cSmrg          $doit $rmcmd -f "$dst" 2>/dev/null ||
478198e4c3cSmrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
479198e4c3cSmrg            { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
480198e4c3cSmrg          } ||
481198e4c3cSmrg          { echo "$0: cannot unlink or rename $dst" >&2
482198e4c3cSmrg            (exit 1); exit 1
483198e4c3cSmrg          }
484198e4c3cSmrg        } &&
485198e4c3cSmrg
486198e4c3cSmrg        # Now rename the file to the real destination.
487198e4c3cSmrg        $doit $mvcmd "$dsttmp" "$dst"
48893493779Smrg      }
48993493779Smrg    fi || exit 1
4906c321187Smrg
4916c321187Smrg    trap '' 0
4926c321187Smrg  fi
4936c321187Smrgdone
4946c321187Smrg
4956c321187Smrg# Local variables:
4966c321187Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
4976c321187Smrg# time-stamp-start: "scriptversion="
4986c321187Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
49993493779Smrg# time-stamp-time-zone: "UTC"
50093493779Smrg# time-stamp-end: "; # UTC"
5016c321187Smrg# End:
502