install-sh revision 121a5469
123a0898aSmrg#!/bin/sh
223a0898aSmrg# install - install a program, script, or datafile
323a0898aSmrg
4121a5469Smrgscriptversion=2013-12-25.23; # UTC
523a0898aSmrg
623a0898aSmrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
723a0898aSmrg# later released in X11R6 (xc/config/util/install.sh) with the
823a0898aSmrg# following copyright and license.
923a0898aSmrg#
1023a0898aSmrg# Copyright (C) 1994 X Consortium
1123a0898aSmrg#
1223a0898aSmrg# Permission is hereby granted, free of charge, to any person obtaining a copy
1323a0898aSmrg# of this software and associated documentation files (the "Software"), to
1423a0898aSmrg# deal in the Software without restriction, including without limitation the
1523a0898aSmrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
1623a0898aSmrg# sell copies of the Software, and to permit persons to whom the Software is
1723a0898aSmrg# furnished to do so, subject to the following conditions:
1823a0898aSmrg#
1923a0898aSmrg# The above copyright notice and this permission notice shall be included in
2023a0898aSmrg# all copies or substantial portions of the Software.
2123a0898aSmrg#
2223a0898aSmrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2323a0898aSmrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2423a0898aSmrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
2523a0898aSmrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
2623a0898aSmrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
2723a0898aSmrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2823a0898aSmrg#
2923a0898aSmrg# Except as contained in this notice, the name of the X Consortium shall not
3023a0898aSmrg# be used in advertising or otherwise to promote the sale, use or other deal-
3123a0898aSmrg# ings in this Software without prior written authorization from the X Consor-
3223a0898aSmrg# tium.
3323a0898aSmrg#
3423a0898aSmrg#
3523a0898aSmrg# FSF changes to this file are in the public domain.
3623a0898aSmrg#
3723a0898aSmrg# Calling this script install-sh is preferred over install.sh, to prevent
38b020570bSmrg# 'make' implicit rules from creating a file called install from it
3923a0898aSmrg# when there is no Makefile.
4023a0898aSmrg#
4123a0898aSmrg# This script is compatible with the BSD install script, but was written
4223a0898aSmrg# from scratch.
4323a0898aSmrg
44121a5469Smrgtab='	'
4523a0898aSmrgnl='
4623a0898aSmrg'
47121a5469SmrgIFS=" $tab$nl"
4823a0898aSmrg
49121a5469Smrg# Set DOITPROG to "echo" to test this script.
5023a0898aSmrg
5123a0898aSmrgdoit=${DOITPROG-}
52121a5469Smrgdoit_exec=${doit:-exec}
5323a0898aSmrg
5423a0898aSmrg# Put in absolute file names if you don't have them in your path;
5523a0898aSmrg# or use environment vars.
5623a0898aSmrg
5723a0898aSmrgchgrpprog=${CHGRPPROG-chgrp}
5823a0898aSmrgchmodprog=${CHMODPROG-chmod}
5923a0898aSmrgchownprog=${CHOWNPROG-chown}
6023a0898aSmrgcmpprog=${CMPPROG-cmp}
6123a0898aSmrgcpprog=${CPPROG-cp}
6223a0898aSmrgmkdirprog=${MKDIRPROG-mkdir}
6323a0898aSmrgmvprog=${MVPROG-mv}
6423a0898aSmrgrmprog=${RMPROG-rm}
6523a0898aSmrgstripprog=${STRIPPROG-strip}
6623a0898aSmrg
6723a0898aSmrgposix_mkdir=
6823a0898aSmrg
6923a0898aSmrg# Desired mode of installed file.
7023a0898aSmrgmode=0755
7123a0898aSmrg
7223a0898aSmrgchgrpcmd=
7323a0898aSmrgchmodcmd=$chmodprog
7423a0898aSmrgchowncmd=
7523a0898aSmrgmvcmd=$mvprog
7623a0898aSmrgrmcmd="$rmprog -f"
7723a0898aSmrgstripcmd=
7823a0898aSmrg
7923a0898aSmrgsrc=
8023a0898aSmrgdst=
8123a0898aSmrgdir_arg=
8223a0898aSmrgdst_arg=
8323a0898aSmrg
8423a0898aSmrgcopy_on_change=false
85121a5469Smrgis_target_a_directory=possibly
8623a0898aSmrg
8723a0898aSmrgusage="\
8823a0898aSmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
8923a0898aSmrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
9023a0898aSmrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
9123a0898aSmrg   or: $0 [OPTION]... -d DIRECTORIES...
9223a0898aSmrg
9323a0898aSmrgIn the 1st form, copy SRCFILE to DSTFILE.
9423a0898aSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
9523a0898aSmrgIn the 4th, create DIRECTORIES.
9623a0898aSmrg
9723a0898aSmrgOptions:
9823a0898aSmrg     --help     display this help and exit.
9923a0898aSmrg     --version  display version info and exit.
10023a0898aSmrg
10123a0898aSmrg  -c            (ignored)
10223a0898aSmrg  -C            install only if different (preserve the last data modification time)
10323a0898aSmrg  -d            create directories instead of installing files.
10423a0898aSmrg  -g GROUP      $chgrpprog installed files to GROUP.
10523a0898aSmrg  -m MODE       $chmodprog installed files to MODE.
10623a0898aSmrg  -o USER       $chownprog installed files to USER.
10723a0898aSmrg  -s            $stripprog installed files.
10823a0898aSmrg  -t DIRECTORY  install into DIRECTORY.
10923a0898aSmrg  -T            report an error if DSTFILE is a directory.
11023a0898aSmrg
11123a0898aSmrgEnvironment variables override the default commands:
11223a0898aSmrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
11323a0898aSmrg  RMPROG STRIPPROG
11423a0898aSmrg"
11523a0898aSmrg
11623a0898aSmrgwhile test $# -ne 0; do
11723a0898aSmrg  case $1 in
11823a0898aSmrg    -c) ;;
11923a0898aSmrg
12023a0898aSmrg    -C) copy_on_change=true;;
12123a0898aSmrg
12223a0898aSmrg    -d) dir_arg=true;;
12323a0898aSmrg
12423a0898aSmrg    -g) chgrpcmd="$chgrpprog $2"
125121a5469Smrg        shift;;
12623a0898aSmrg
12723a0898aSmrg    --help) echo "$usage"; exit $?;;
12823a0898aSmrg
12923a0898aSmrg    -m) mode=$2
130121a5469Smrg        case $mode in
131121a5469Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
132121a5469Smrg            echo "$0: invalid mode: $mode" >&2
133121a5469Smrg            exit 1;;
134121a5469Smrg        esac
135121a5469Smrg        shift;;
13623a0898aSmrg
13723a0898aSmrg    -o) chowncmd="$chownprog $2"
138121a5469Smrg        shift;;
13923a0898aSmrg
14023a0898aSmrg    -s) stripcmd=$stripprog;;
14123a0898aSmrg
142121a5469Smrg    -t)
143121a5469Smrg        is_target_a_directory=always
144121a5469Smrg        dst_arg=$2
145121a5469Smrg        # Protect names problematic for 'test' and other utilities.
146121a5469Smrg        case $dst_arg in
147121a5469Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
148121a5469Smrg        esac
149121a5469Smrg        shift;;
15023a0898aSmrg
151121a5469Smrg    -T) is_target_a_directory=never;;
15223a0898aSmrg
15323a0898aSmrg    --version) echo "$0 $scriptversion"; exit $?;;
15423a0898aSmrg
155121a5469Smrg    --) shift
156121a5469Smrg        break;;
15723a0898aSmrg
158121a5469Smrg    -*) echo "$0: invalid option: $1" >&2
159121a5469Smrg        exit 1;;
16023a0898aSmrg
16123a0898aSmrg    *)  break;;
16223a0898aSmrg  esac
16323a0898aSmrg  shift
16423a0898aSmrgdone
16523a0898aSmrg
166121a5469Smrg# We allow the use of options -d and -T together, by making -d
167121a5469Smrg# take the precedence; this is for compatibility with GNU install.
168121a5469Smrg
169121a5469Smrgif test -n "$dir_arg"; then
170121a5469Smrg  if test -n "$dst_arg"; then
171121a5469Smrg    echo "$0: target directory not allowed when installing a directory." >&2
172121a5469Smrg    exit 1
173121a5469Smrg  fi
174121a5469Smrgfi
175121a5469Smrg
17623a0898aSmrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
17723a0898aSmrg  # When -d is used, all remaining arguments are directories to create.
17823a0898aSmrg  # When -t is used, the destination is already specified.
17923a0898aSmrg  # Otherwise, the last argument is the destination.  Remove it from $@.
18023a0898aSmrg  for arg
18123a0898aSmrg  do
18223a0898aSmrg    if test -n "$dst_arg"; then
18323a0898aSmrg      # $@ is not empty: it contains at least $arg.
18423a0898aSmrg      set fnord "$@" "$dst_arg"
18523a0898aSmrg      shift # fnord
18623a0898aSmrg    fi
18723a0898aSmrg    shift # arg
18823a0898aSmrg    dst_arg=$arg
189b020570bSmrg    # Protect names problematic for 'test' and other utilities.
190b020570bSmrg    case $dst_arg in
191b020570bSmrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
192b020570bSmrg    esac
19323a0898aSmrg  done
19423a0898aSmrgfi
19523a0898aSmrg
19623a0898aSmrgif test $# -eq 0; then
19723a0898aSmrg  if test -z "$dir_arg"; then
19823a0898aSmrg    echo "$0: no input file specified." >&2
19923a0898aSmrg    exit 1
20023a0898aSmrg  fi
201b020570bSmrg  # It's OK to call 'install-sh -d' without argument.
20223a0898aSmrg  # This can happen when creating conditional directories.
20323a0898aSmrg  exit 0
20423a0898aSmrgfi
20523a0898aSmrg
206121a5469Smrgif test -z "$dir_arg"; then
207121a5469Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
208121a5469Smrg    if test ! -d "$dst_arg"; then
209121a5469Smrg      echo "$0: $dst_arg: Is not a directory." >&2
210121a5469Smrg      exit 1
211121a5469Smrg    fi
212121a5469Smrg  fi
213121a5469Smrgfi
214121a5469Smrg
21523a0898aSmrgif test -z "$dir_arg"; then
216b020570bSmrg  do_exit='(exit $ret); exit $ret'
217b020570bSmrg  trap "ret=129; $do_exit" 1
218b020570bSmrg  trap "ret=130; $do_exit" 2
219b020570bSmrg  trap "ret=141; $do_exit" 13
220b020570bSmrg  trap "ret=143; $do_exit" 15
22123a0898aSmrg
22223a0898aSmrg  # Set umask so as not to create temps with too-generous modes.
22323a0898aSmrg  # However, 'strip' requires both read and write access to temps.
22423a0898aSmrg  case $mode in
22523a0898aSmrg    # Optimize common cases.
22623a0898aSmrg    *644) cp_umask=133;;
22723a0898aSmrg    *755) cp_umask=22;;
22823a0898aSmrg
22923a0898aSmrg    *[0-7])
23023a0898aSmrg      if test -z "$stripcmd"; then
231121a5469Smrg        u_plus_rw=
23223a0898aSmrg      else
233121a5469Smrg        u_plus_rw='% 200'
23423a0898aSmrg      fi
23523a0898aSmrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
23623a0898aSmrg    *)
23723a0898aSmrg      if test -z "$stripcmd"; then
238121a5469Smrg        u_plus_rw=
23923a0898aSmrg      else
240121a5469Smrg        u_plus_rw=,u+rw
24123a0898aSmrg      fi
24223a0898aSmrg      cp_umask=$mode$u_plus_rw;;
24323a0898aSmrg  esac
24423a0898aSmrgfi
24523a0898aSmrg
24623a0898aSmrgfor src
24723a0898aSmrgdo
248b020570bSmrg  # Protect names problematic for 'test' and other utilities.
24923a0898aSmrg  case $src in
250b020570bSmrg    -* | [=\(\)!]) src=./$src;;
25123a0898aSmrg  esac
25223a0898aSmrg
25323a0898aSmrg  if test -n "$dir_arg"; then
25423a0898aSmrg    dst=$src
25523a0898aSmrg    dstdir=$dst
25623a0898aSmrg    test -d "$dstdir"
25723a0898aSmrg    dstdir_status=$?
25823a0898aSmrg  else
25923a0898aSmrg
26023a0898aSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
26123a0898aSmrg    # might cause directories to be created, which would be especially bad
26223a0898aSmrg    # if $src (and thus $dsttmp) contains '*'.
26323a0898aSmrg    if test ! -f "$src" && test ! -d "$src"; then
26423a0898aSmrg      echo "$0: $src does not exist." >&2
26523a0898aSmrg      exit 1
26623a0898aSmrg    fi
26723a0898aSmrg
26823a0898aSmrg    if test -z "$dst_arg"; then
26923a0898aSmrg      echo "$0: no destination specified." >&2
27023a0898aSmrg      exit 1
27123a0898aSmrg    fi
27223a0898aSmrg    dst=$dst_arg
27323a0898aSmrg
27423a0898aSmrg    # If destination is a directory, append the input filename; won't work
27523a0898aSmrg    # if double slashes aren't ignored.
27623a0898aSmrg    if test -d "$dst"; then
277121a5469Smrg      if test "$is_target_a_directory" = never; then
278121a5469Smrg        echo "$0: $dst_arg: Is a directory" >&2
279121a5469Smrg        exit 1
28023a0898aSmrg      fi
28123a0898aSmrg      dstdir=$dst
28223a0898aSmrg      dst=$dstdir/`basename "$src"`
28323a0898aSmrg      dstdir_status=0
28423a0898aSmrg    else
285121a5469Smrg      dstdir=`dirname "$dst"`
28623a0898aSmrg      test -d "$dstdir"
28723a0898aSmrg      dstdir_status=$?
28823a0898aSmrg    fi
28923a0898aSmrg  fi
29023a0898aSmrg
29123a0898aSmrg  obsolete_mkdir_used=false
29223a0898aSmrg
29323a0898aSmrg  if test $dstdir_status != 0; then
29423a0898aSmrg    case $posix_mkdir in
29523a0898aSmrg      '')
296121a5469Smrg        # Create intermediate dirs using mode 755 as modified by the umask.
297121a5469Smrg        # This is like FreeBSD 'install' as of 1997-10-28.
298121a5469Smrg        umask=`umask`
299121a5469Smrg        case $stripcmd.$umask in
300121a5469Smrg          # Optimize common cases.
301121a5469Smrg          *[2367][2367]) mkdir_umask=$umask;;
302121a5469Smrg          .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
303121a5469Smrg
304121a5469Smrg          *[0-7])
305121a5469Smrg            mkdir_umask=`expr $umask + 22 \
306121a5469Smrg              - $umask % 100 % 40 + $umask % 20 \
307121a5469Smrg              - $umask % 10 % 4 + $umask % 2
308121a5469Smrg            `;;
309121a5469Smrg          *) mkdir_umask=$umask,go-w;;
310121a5469Smrg        esac
311121a5469Smrg
312121a5469Smrg        # With -d, create the new directory with the user-specified mode.
313121a5469Smrg        # Otherwise, rely on $mkdir_umask.
314121a5469Smrg        if test -n "$dir_arg"; then
315121a5469Smrg          mkdir_mode=-m$mode
316121a5469Smrg        else
317121a5469Smrg          mkdir_mode=
318121a5469Smrg        fi
319121a5469Smrg
320121a5469Smrg        posix_mkdir=false
321121a5469Smrg        case $umask in
322121a5469Smrg          *[123567][0-7][0-7])
323121a5469Smrg            # POSIX mkdir -p sets u+wx bits regardless of umask, which
324121a5469Smrg            # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
325121a5469Smrg            ;;
326121a5469Smrg          *)
327121a5469Smrg            tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
328121a5469Smrg            trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
329121a5469Smrg
330121a5469Smrg            if (umask $mkdir_umask &&
331121a5469Smrg                exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
332121a5469Smrg            then
333121a5469Smrg              if test -z "$dir_arg" || {
334121a5469Smrg                   # Check for POSIX incompatibilities with -m.
335121a5469Smrg                   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
336121a5469Smrg                   # other-writable bit of parent directory when it shouldn't.
337121a5469Smrg                   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
338121a5469Smrg                   ls_ld_tmpdir=`ls -ld "$tmpdir"`
339121a5469Smrg                   case $ls_ld_tmpdir in
340121a5469Smrg                     d????-?r-*) different_mode=700;;
341121a5469Smrg                     d????-?--*) different_mode=755;;
342121a5469Smrg                     *) false;;
343121a5469Smrg                   esac &&
344121a5469Smrg                   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
345121a5469Smrg                     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
346121a5469Smrg                     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
347121a5469Smrg                   }
348121a5469Smrg                 }
349121a5469Smrg              then posix_mkdir=:
350121a5469Smrg              fi
351121a5469Smrg              rmdir "$tmpdir/d" "$tmpdir"
352121a5469Smrg            else
353121a5469Smrg              # Remove any dirs left behind by ancient mkdir implementations.
354121a5469Smrg              rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
355121a5469Smrg            fi
356121a5469Smrg            trap '' 0;;
357121a5469Smrg        esac;;
35823a0898aSmrg    esac
35923a0898aSmrg
36023a0898aSmrg    if
36123a0898aSmrg      $posix_mkdir && (
362121a5469Smrg        umask $mkdir_umask &&
363121a5469Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
36423a0898aSmrg      )
36523a0898aSmrg    then :
36623a0898aSmrg    else
36723a0898aSmrg
36823a0898aSmrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
36923a0898aSmrg      # or it failed possibly due to a race condition.  Create the
37023a0898aSmrg      # directory the slow way, step by step, checking for races as we go.
37123a0898aSmrg
37223a0898aSmrg      case $dstdir in
373121a5469Smrg        /*) prefix='/';;
374121a5469Smrg        [-=\(\)!]*) prefix='./';;
375121a5469Smrg        *)  prefix='';;
37623a0898aSmrg      esac
37723a0898aSmrg
37823a0898aSmrg      oIFS=$IFS
37923a0898aSmrg      IFS=/
380121a5469Smrg      set -f
38123a0898aSmrg      set fnord $dstdir
38223a0898aSmrg      shift
383121a5469Smrg      set +f
38423a0898aSmrg      IFS=$oIFS
38523a0898aSmrg
38623a0898aSmrg      prefixes=
38723a0898aSmrg
38823a0898aSmrg      for d
38923a0898aSmrg      do
390121a5469Smrg        test X"$d" = X && continue
391121a5469Smrg
392121a5469Smrg        prefix=$prefix$d
393121a5469Smrg        if test -d "$prefix"; then
394121a5469Smrg          prefixes=
395121a5469Smrg        else
396121a5469Smrg          if $posix_mkdir; then
397121a5469Smrg            (umask=$mkdir_umask &&
398121a5469Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
399121a5469Smrg            # Don't fail if two instances are running concurrently.
400121a5469Smrg            test -d "$prefix" || exit 1
401121a5469Smrg          else
402121a5469Smrg            case $prefix in
403121a5469Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
404121a5469Smrg              *) qprefix=$prefix;;
405121a5469Smrg            esac
406121a5469Smrg            prefixes="$prefixes '$qprefix'"
407121a5469Smrg          fi
408121a5469Smrg        fi
409121a5469Smrg        prefix=$prefix/
41023a0898aSmrg      done
41123a0898aSmrg
41223a0898aSmrg      if test -n "$prefixes"; then
413121a5469Smrg        # Don't fail if two instances are running concurrently.
414121a5469Smrg        (umask $mkdir_umask &&
415121a5469Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
416121a5469Smrg          test -d "$dstdir" || exit 1
417121a5469Smrg        obsolete_mkdir_used=true
41823a0898aSmrg      fi
41923a0898aSmrg    fi
42023a0898aSmrg  fi
42123a0898aSmrg
42223a0898aSmrg  if test -n "$dir_arg"; then
42323a0898aSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
42423a0898aSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
42523a0898aSmrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
42623a0898aSmrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
42723a0898aSmrg  else
42823a0898aSmrg
42923a0898aSmrg    # Make a couple of temp file names in the proper directory.
43023a0898aSmrg    dsttmp=$dstdir/_inst.$$_
43123a0898aSmrg    rmtmp=$dstdir/_rm.$$_
43223a0898aSmrg
43323a0898aSmrg    # Trap to clean up those temp files at exit.
43423a0898aSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
43523a0898aSmrg
43623a0898aSmrg    # Copy the file name to the temp name.
43723a0898aSmrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
43823a0898aSmrg
43923a0898aSmrg    # and set any options; do chmod last to preserve setuid bits.
44023a0898aSmrg    #
44123a0898aSmrg    # If any of these fail, we abort the whole thing.  If we want to
44223a0898aSmrg    # ignore errors from any of these, just make sure not to ignore
44323a0898aSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
44423a0898aSmrg    #
44523a0898aSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
44623a0898aSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
44723a0898aSmrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
44823a0898aSmrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
44923a0898aSmrg
45023a0898aSmrg    # If -C, don't bother to copy if it wouldn't change the file.
45123a0898aSmrg    if $copy_on_change &&
452121a5469Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
453121a5469Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
454121a5469Smrg       set -f &&
45523a0898aSmrg       set X $old && old=:$2:$4:$5:$6 &&
45623a0898aSmrg       set X $new && new=:$2:$4:$5:$6 &&
457121a5469Smrg       set +f &&
45823a0898aSmrg       test "$old" = "$new" &&
45923a0898aSmrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
46023a0898aSmrg    then
46123a0898aSmrg      rm -f "$dsttmp"
46223a0898aSmrg    else
46323a0898aSmrg      # Rename the file to the real destination.
46423a0898aSmrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
46523a0898aSmrg
46623a0898aSmrg      # The rename failed, perhaps because mv can't rename something else
46723a0898aSmrg      # to itself, or perhaps because mv is so ancient that it does not
46823a0898aSmrg      # support -f.
46923a0898aSmrg      {
470121a5469Smrg        # Now remove or move aside any old file at destination location.
471121a5469Smrg        # We try this two ways since rm can't unlink itself on some
472121a5469Smrg        # systems and the destination file might be busy for other
473121a5469Smrg        # reasons.  In this case, the final cleanup might fail but the new
474121a5469Smrg        # file should still install successfully.
475121a5469Smrg        {
476121a5469Smrg          test ! -f "$dst" ||
477121a5469Smrg          $doit $rmcmd -f "$dst" 2>/dev/null ||
478121a5469Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
479121a5469Smrg            { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
480121a5469Smrg          } ||
481121a5469Smrg          { echo "$0: cannot unlink or rename $dst" >&2
482121a5469Smrg            (exit 1); exit 1
483121a5469Smrg          }
484121a5469Smrg        } &&
485121a5469Smrg
486121a5469Smrg        # Now rename the file to the real destination.
487121a5469Smrg        $doit $mvcmd "$dsttmp" "$dst"
48823a0898aSmrg      }
48923a0898aSmrg    fi || exit 1
49023a0898aSmrg
49123a0898aSmrg    trap '' 0
49223a0898aSmrg  fi
49323a0898aSmrgdone
49423a0898aSmrg
49523a0898aSmrg# Local variables:
49623a0898aSmrg# eval: (add-hook 'write-file-hooks 'time-stamp)
49723a0898aSmrg# time-stamp-start: "scriptversion="
49823a0898aSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
49941c30155Smrg# time-stamp-time-zone: "UTC"
50041c30155Smrg# time-stamp-end: "; # UTC"
50123a0898aSmrg# End:
502