install-sh revision 8292847c
1c76ae52dSmrg#!/bin/sh
2c76ae52dSmrg# install - install a program, script, or datafile
3c76ae52dSmrg
48292847cSmrgscriptversion=2013-12-25.23; # UTC
5c76ae52dSmrg
6c76ae52dSmrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7c76ae52dSmrg# later released in X11R6 (xc/config/util/install.sh) with the
8c76ae52dSmrg# following copyright and license.
9c76ae52dSmrg#
10c76ae52dSmrg# Copyright (C) 1994 X Consortium
11c76ae52dSmrg#
12c76ae52dSmrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13c76ae52dSmrg# of this software and associated documentation files (the "Software"), to
14c76ae52dSmrg# deal in the Software without restriction, including without limitation the
15c76ae52dSmrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16c76ae52dSmrg# sell copies of the Software, and to permit persons to whom the Software is
17c76ae52dSmrg# furnished to do so, subject to the following conditions:
18c76ae52dSmrg#
19c76ae52dSmrg# The above copyright notice and this permission notice shall be included in
20c76ae52dSmrg# all copies or substantial portions of the Software.
21c76ae52dSmrg#
22c76ae52dSmrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23c76ae52dSmrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24c76ae52dSmrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25c76ae52dSmrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26c76ae52dSmrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27c76ae52dSmrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28c76ae52dSmrg#
29c76ae52dSmrg# Except as contained in this notice, the name of the X Consortium shall not
30c76ae52dSmrg# be used in advertising or otherwise to promote the sale, use or other deal-
31c76ae52dSmrg# ings in this Software without prior written authorization from the X Consor-
32c76ae52dSmrg# tium.
33c76ae52dSmrg#
34c76ae52dSmrg#
35c76ae52dSmrg# FSF changes to this file are in the public domain.
36c76ae52dSmrg#
37c76ae52dSmrg# Calling this script install-sh is preferred over install.sh, to prevent
38dff01e5aSmrg# 'make' implicit rules from creating a file called install from it
39c76ae52dSmrg# when there is no Makefile.
40c76ae52dSmrg#
41c76ae52dSmrg# This script is compatible with the BSD install script, but was written
420d590c07Smrg# from scratch.
430d590c07Smrg
448292847cSmrgtab='	'
450d590c07Smrgnl='
460d590c07Smrg'
478292847cSmrgIFS=" $tab$nl"
48c76ae52dSmrg
498292847cSmrg# Set DOITPROG to "echo" to test this script.
50c76ae52dSmrg
510d590c07Smrgdoit=${DOITPROG-}
528292847cSmrgdoit_exec=${doit:-exec}
53c76ae52dSmrg
540d590c07Smrg# Put in absolute file names if you don't have them in your path;
550d590c07Smrg# or use environment vars.
560d590c07Smrg
570d590c07Smrgchgrpprog=${CHGRPPROG-chgrp}
580d590c07Smrgchmodprog=${CHMODPROG-chmod}
590d590c07Smrgchownprog=${CHOWNPROG-chown}
600d590c07Smrgcmpprog=${CMPPROG-cmp}
610d590c07Smrgcpprog=${CPPROG-cp}
620d590c07Smrgmkdirprog=${MKDIRPROG-mkdir}
630d590c07Smrgmvprog=${MVPROG-mv}
640d590c07Smrgrmprog=${RMPROG-rm}
650d590c07Smrgstripprog=${STRIPPROG-strip}
660d590c07Smrg
670d590c07Smrgposix_mkdir=
680d590c07Smrg
690d590c07Smrg# Desired mode of installed file.
700d590c07Smrgmode=0755
71c76ae52dSmrg
72c76ae52dSmrgchgrpcmd=
730d590c07Smrgchmodcmd=$chmodprog
740d590c07Smrgchowncmd=
750d590c07Smrgmvcmd=$mvprog
76c76ae52dSmrgrmcmd="$rmprog -f"
770d590c07Smrgstripcmd=
780d590c07Smrg
79c76ae52dSmrgsrc=
80c76ae52dSmrgdst=
81c76ae52dSmrgdir_arg=
820d590c07Smrgdst_arg=
830d590c07Smrg
840d590c07Smrgcopy_on_change=false
858292847cSmrgis_target_a_directory=possibly
86c76ae52dSmrg
870d590c07Smrgusage="\
880d590c07SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
89c76ae52dSmrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
90c76ae52dSmrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
91c76ae52dSmrg   or: $0 [OPTION]... -d DIRECTORIES...
92c76ae52dSmrg
93c76ae52dSmrgIn the 1st form, copy SRCFILE to DSTFILE.
94c76ae52dSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
95c76ae52dSmrgIn the 4th, create DIRECTORIES.
96c76ae52dSmrg
97c76ae52dSmrgOptions:
980d590c07Smrg     --help     display this help and exit.
990d590c07Smrg     --version  display version info and exit.
1000d590c07Smrg
1010d590c07Smrg  -c            (ignored)
1020d590c07Smrg  -C            install only if different (preserve the last data modification time)
1030d590c07Smrg  -d            create directories instead of installing files.
1040d590c07Smrg  -g GROUP      $chgrpprog installed files to GROUP.
1050d590c07Smrg  -m MODE       $chmodprog installed files to MODE.
1060d590c07Smrg  -o USER       $chownprog installed files to USER.
1070d590c07Smrg  -s            $stripprog installed files.
1080d590c07Smrg  -t DIRECTORY  install into DIRECTORY.
1090d590c07Smrg  -T            report an error if DSTFILE is a directory.
110c76ae52dSmrg
111c76ae52dSmrgEnvironment variables override the default commands:
1120d590c07Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
1130d590c07Smrg  RMPROG STRIPPROG
114c76ae52dSmrg"
115c76ae52dSmrg
1160d590c07Smrgwhile test $# -ne 0; do
117c76ae52dSmrg  case $1 in
1180d590c07Smrg    -c) ;;
1190d590c07Smrg
1200d590c07Smrg    -C) copy_on_change=true;;
121c76ae52dSmrg
1220d590c07Smrg    -d) dir_arg=true;;
123c76ae52dSmrg
124c76ae52dSmrg    -g) chgrpcmd="$chgrpprog $2"
1258292847cSmrg        shift;;
126c76ae52dSmrg
127c76ae52dSmrg    --help) echo "$usage"; exit $?;;
128c76ae52dSmrg
1290d590c07Smrg    -m) mode=$2
1308292847cSmrg        case $mode in
1318292847cSmrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
1328292847cSmrg            echo "$0: invalid mode: $mode" >&2
1338292847cSmrg            exit 1;;
1348292847cSmrg        esac
1358292847cSmrg        shift;;
136c76ae52dSmrg
137c76ae52dSmrg    -o) chowncmd="$chownprog $2"
1388292847cSmrg        shift;;
139c76ae52dSmrg
1400d590c07Smrg    -s) stripcmd=$stripprog;;
141c76ae52dSmrg
1428292847cSmrg    -t)
1438292847cSmrg        is_target_a_directory=always
1448292847cSmrg        dst_arg=$2
1458292847cSmrg        # Protect names problematic for 'test' and other utilities.
1468292847cSmrg        case $dst_arg in
1478292847cSmrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
1488292847cSmrg        esac
1498292847cSmrg        shift;;
150c76ae52dSmrg
1518292847cSmrg    -T) is_target_a_directory=never;;
152c76ae52dSmrg
153c76ae52dSmrg    --version) echo "$0 $scriptversion"; exit $?;;
154c76ae52dSmrg
1558292847cSmrg    --) shift
1568292847cSmrg        break;;
1570d590c07Smrg
1588292847cSmrg    -*) echo "$0: invalid option: $1" >&2
1598292847cSmrg        exit 1;;
1600d590c07Smrg
1610d590c07Smrg    *)  break;;
162c76ae52dSmrg  esac
1630d590c07Smrg  shift
164c76ae52dSmrgdone
165c76ae52dSmrg
1668292847cSmrg# We allow the use of options -d and -T together, by making -d
1678292847cSmrg# take the precedence; this is for compatibility with GNU install.
1688292847cSmrg
1698292847cSmrgif test -n "$dir_arg"; then
1708292847cSmrg  if test -n "$dst_arg"; then
1718292847cSmrg    echo "$0: target directory not allowed when installing a directory." >&2
1728292847cSmrg    exit 1
1738292847cSmrg  fi
1748292847cSmrgfi
1758292847cSmrg
1760d590c07Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1770d590c07Smrg  # When -d is used, all remaining arguments are directories to create.
1780d590c07Smrg  # When -t is used, the destination is already specified.
1790d590c07Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
1800d590c07Smrg  for arg
1810d590c07Smrg  do
1820d590c07Smrg    if test -n "$dst_arg"; then
1830d590c07Smrg      # $@ is not empty: it contains at least $arg.
1840d590c07Smrg      set fnord "$@" "$dst_arg"
1850d590c07Smrg      shift # fnord
1860d590c07Smrg    fi
1870d590c07Smrg    shift # arg
1880d590c07Smrg    dst_arg=$arg
189dff01e5aSmrg    # Protect names problematic for 'test' and other utilities.
1902836776bSmrg    case $dst_arg in
1912836776bSmrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
1922836776bSmrg    esac
1930d590c07Smrg  done
1940d590c07Smrgfi
1950d590c07Smrg
1960d590c07Smrgif test $# -eq 0; then
197c76ae52dSmrg  if test -z "$dir_arg"; then
198c76ae52dSmrg    echo "$0: no input file specified." >&2
199c76ae52dSmrg    exit 1
200c76ae52dSmrg  fi
201dff01e5aSmrg  # It's OK to call 'install-sh -d' without argument.
202c76ae52dSmrg  # This can happen when creating conditional directories.
203c76ae52dSmrg  exit 0
204c76ae52dSmrgfi
205c76ae52dSmrg
2068292847cSmrgif test -z "$dir_arg"; then
2078292847cSmrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
2088292847cSmrg    if test ! -d "$dst_arg"; then
2098292847cSmrg      echo "$0: $dst_arg: Is not a directory." >&2
2108292847cSmrg      exit 1
2118292847cSmrg    fi
2128292847cSmrg  fi
2138292847cSmrgfi
2148292847cSmrg
2150d590c07Smrgif test -z "$dir_arg"; then
2162836776bSmrg  do_exit='(exit $ret); exit $ret'
2172836776bSmrg  trap "ret=129; $do_exit" 1
2182836776bSmrg  trap "ret=130; $do_exit" 2
2192836776bSmrg  trap "ret=141; $do_exit" 13
2202836776bSmrg  trap "ret=143; $do_exit" 15
2210d590c07Smrg
2220d590c07Smrg  # Set umask so as not to create temps with too-generous modes.
2230d590c07Smrg  # However, 'strip' requires both read and write access to temps.
2240d590c07Smrg  case $mode in
2250d590c07Smrg    # Optimize common cases.
2260d590c07Smrg    *644) cp_umask=133;;
2270d590c07Smrg    *755) cp_umask=22;;
2280d590c07Smrg
2290d590c07Smrg    *[0-7])
2300d590c07Smrg      if test -z "$stripcmd"; then
2318292847cSmrg        u_plus_rw=
2320d590c07Smrg      else
2338292847cSmrg        u_plus_rw='% 200'
2340d590c07Smrg      fi
2350d590c07Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2360d590c07Smrg    *)
2370d590c07Smrg      if test -z "$stripcmd"; then
2388292847cSmrg        u_plus_rw=
2390d590c07Smrg      else
2408292847cSmrg        u_plus_rw=,u+rw
2410d590c07Smrg      fi
2420d590c07Smrg      cp_umask=$mode$u_plus_rw;;
2430d590c07Smrg  esac
2440d590c07Smrgfi
2450d590c07Smrg
246c76ae52dSmrgfor src
247c76ae52dSmrgdo
248dff01e5aSmrg  # Protect names problematic for 'test' and other utilities.
249c76ae52dSmrg  case $src in
2502836776bSmrg    -* | [=\(\)!]) src=./$src;;
251c76ae52dSmrg  esac
252c76ae52dSmrg
253c76ae52dSmrg  if test -n "$dir_arg"; then
254c76ae52dSmrg    dst=$src
2550d590c07Smrg    dstdir=$dst
2560d590c07Smrg    test -d "$dstdir"
2570d590c07Smrg    dstdir_status=$?
258c76ae52dSmrg  else
2590d590c07Smrg
260c76ae52dSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
261c76ae52dSmrg    # might cause directories to be created, which would be especially bad
262c76ae52dSmrg    # if $src (and thus $dsttmp) contains '*'.
263c76ae52dSmrg    if test ! -f "$src" && test ! -d "$src"; then
264c76ae52dSmrg      echo "$0: $src does not exist." >&2
265c76ae52dSmrg      exit 1
266c76ae52dSmrg    fi
267c76ae52dSmrg
2680d590c07Smrg    if test -z "$dst_arg"; then
269c76ae52dSmrg      echo "$0: no destination specified." >&2
270c76ae52dSmrg      exit 1
271c76ae52dSmrg    fi
2720d590c07Smrg    dst=$dst_arg
273c76ae52dSmrg
274c76ae52dSmrg    # If destination is a directory, append the input filename; won't work
275c76ae52dSmrg    # if double slashes aren't ignored.
276c76ae52dSmrg    if test -d "$dst"; then
2778292847cSmrg      if test "$is_target_a_directory" = never; then
2788292847cSmrg        echo "$0: $dst_arg: Is a directory" >&2
2798292847cSmrg        exit 1
280c76ae52dSmrg      fi
2810d590c07Smrg      dstdir=$dst
2820d590c07Smrg      dst=$dstdir/`basename "$src"`
2830d590c07Smrg      dstdir_status=0
2840d590c07Smrg    else
2858292847cSmrg      dstdir=`dirname "$dst"`
2860d590c07Smrg      test -d "$dstdir"
2870d590c07Smrg      dstdir_status=$?
288c76ae52dSmrg    fi
289c76ae52dSmrg  fi
290c76ae52dSmrg
2910d590c07Smrg  obsolete_mkdir_used=false
2920d590c07Smrg
2930d590c07Smrg  if test $dstdir_status != 0; then
2940d590c07Smrg    case $posix_mkdir in
2950d590c07Smrg      '')
2968292847cSmrg        # Create intermediate dirs using mode 755 as modified by the umask.
2978292847cSmrg        # This is like FreeBSD 'install' as of 1997-10-28.
2988292847cSmrg        umask=`umask`
2998292847cSmrg        case $stripcmd.$umask in
3008292847cSmrg          # Optimize common cases.
3018292847cSmrg          *[2367][2367]) mkdir_umask=$umask;;
3028292847cSmrg          .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
3038292847cSmrg
3048292847cSmrg          *[0-7])
3058292847cSmrg            mkdir_umask=`expr $umask + 22 \
3068292847cSmrg              - $umask % 100 % 40 + $umask % 20 \
3078292847cSmrg              - $umask % 10 % 4 + $umask % 2
3088292847cSmrg            `;;
3098292847cSmrg          *) mkdir_umask=$umask,go-w;;
3108292847cSmrg        esac
3118292847cSmrg
3128292847cSmrg        # With -d, create the new directory with the user-specified mode.
3138292847cSmrg        # Otherwise, rely on $mkdir_umask.
3148292847cSmrg        if test -n "$dir_arg"; then
3158292847cSmrg          mkdir_mode=-m$mode
3168292847cSmrg        else
3178292847cSmrg          mkdir_mode=
3188292847cSmrg        fi
3198292847cSmrg
3208292847cSmrg        posix_mkdir=false
3218292847cSmrg        case $umask in
3228292847cSmrg          *[123567][0-7][0-7])
3238292847cSmrg            # POSIX mkdir -p sets u+wx bits regardless of umask, which
3248292847cSmrg            # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
3258292847cSmrg            ;;
3268292847cSmrg          *)
3278292847cSmrg            tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
3288292847cSmrg            trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
3298292847cSmrg
3308292847cSmrg            if (umask $mkdir_umask &&
3318292847cSmrg                exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
3328292847cSmrg            then
3338292847cSmrg              if test -z "$dir_arg" || {
3348292847cSmrg                   # Check for POSIX incompatibilities with -m.
3358292847cSmrg                   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
3368292847cSmrg                   # other-writable bit of parent directory when it shouldn't.
3378292847cSmrg                   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
3388292847cSmrg                   ls_ld_tmpdir=`ls -ld "$tmpdir"`
3398292847cSmrg                   case $ls_ld_tmpdir in
3408292847cSmrg                     d????-?r-*) different_mode=700;;
3418292847cSmrg                     d????-?--*) different_mode=755;;
3428292847cSmrg                     *) false;;
3438292847cSmrg                   esac &&
3448292847cSmrg                   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
3458292847cSmrg                     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
3468292847cSmrg                     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
3478292847cSmrg                   }
3488292847cSmrg                 }
3498292847cSmrg              then posix_mkdir=:
3508292847cSmrg              fi
3518292847cSmrg              rmdir "$tmpdir/d" "$tmpdir"
3528292847cSmrg            else
3538292847cSmrg              # Remove any dirs left behind by ancient mkdir implementations.
3548292847cSmrg              rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
3558292847cSmrg            fi
3568292847cSmrg            trap '' 0;;
3578292847cSmrg        esac;;
3580d590c07Smrg    esac
359c76ae52dSmrg
3600d590c07Smrg    if
3610d590c07Smrg      $posix_mkdir && (
3628292847cSmrg        umask $mkdir_umask &&
3638292847cSmrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3640d590c07Smrg      )
3650d590c07Smrg    then :
3660d590c07Smrg    else
367c76ae52dSmrg
3680d590c07Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
3690d590c07Smrg      # or it failed possibly due to a race condition.  Create the
3700d590c07Smrg      # directory the slow way, step by step, checking for races as we go.
371c76ae52dSmrg
3720d590c07Smrg      case $dstdir in
3738292847cSmrg        /*) prefix='/';;
3748292847cSmrg        [-=\(\)!]*) prefix='./';;
3758292847cSmrg        *)  prefix='';;
3760d590c07Smrg      esac
377c76ae52dSmrg
3780d590c07Smrg      oIFS=$IFS
3790d590c07Smrg      IFS=/
3808292847cSmrg      set -f
3810d590c07Smrg      set fnord $dstdir
382c76ae52dSmrg      shift
3838292847cSmrg      set +f
3840d590c07Smrg      IFS=$oIFS
3850d590c07Smrg
3860d590c07Smrg      prefixes=
3870d590c07Smrg
3880d590c07Smrg      for d
3890d590c07Smrg      do
3908292847cSmrg        test X"$d" = X && continue
3918292847cSmrg
3928292847cSmrg        prefix=$prefix$d
3938292847cSmrg        if test -d "$prefix"; then
3948292847cSmrg          prefixes=
3958292847cSmrg        else
3968292847cSmrg          if $posix_mkdir; then
3978292847cSmrg            (umask=$mkdir_umask &&
3988292847cSmrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
3998292847cSmrg            # Don't fail if two instances are running concurrently.
4008292847cSmrg            test -d "$prefix" || exit 1
4018292847cSmrg          else
4028292847cSmrg            case $prefix in
4038292847cSmrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
4048292847cSmrg              *) qprefix=$prefix;;
4058292847cSmrg            esac
4068292847cSmrg            prefixes="$prefixes '$qprefix'"
4078292847cSmrg          fi
4088292847cSmrg        fi
4098292847cSmrg        prefix=$prefix/
4100d590c07Smrg      done
4110d590c07Smrg
4120d590c07Smrg      if test -n "$prefixes"; then
4138292847cSmrg        # Don't fail if two instances are running concurrently.
4148292847cSmrg        (umask $mkdir_umask &&
4158292847cSmrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
4168292847cSmrg          test -d "$dstdir" || exit 1
4178292847cSmrg        obsolete_mkdir_used=true
418c76ae52dSmrg      fi
4190d590c07Smrg    fi
420c76ae52dSmrg  fi
421c76ae52dSmrg
422c76ae52dSmrg  if test -n "$dir_arg"; then
4230d590c07Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4240d590c07Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4250d590c07Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4260d590c07Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
427c76ae52dSmrg  else
428c76ae52dSmrg
429c76ae52dSmrg    # Make a couple of temp file names in the proper directory.
430c76ae52dSmrg    dsttmp=$dstdir/_inst.$$_
431c76ae52dSmrg    rmtmp=$dstdir/_rm.$$_
432c76ae52dSmrg
433c76ae52dSmrg    # Trap to clean up those temp files at exit.
434c76ae52dSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
435c76ae52dSmrg
436c76ae52dSmrg    # Copy the file name to the temp name.
4370d590c07Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
438c76ae52dSmrg
439c76ae52dSmrg    # and set any options; do chmod last to preserve setuid bits.
440c76ae52dSmrg    #
441c76ae52dSmrg    # If any of these fail, we abort the whole thing.  If we want to
442c76ae52dSmrg    # ignore errors from any of these, just make sure not to ignore
443c76ae52dSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
444c76ae52dSmrg    #
4450d590c07Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
4460d590c07Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
4470d590c07Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
4480d590c07Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4490d590c07Smrg
4500d590c07Smrg    # If -C, don't bother to copy if it wouldn't change the file.
4510d590c07Smrg    if $copy_on_change &&
4528292847cSmrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
4538292847cSmrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
4548292847cSmrg       set -f &&
4550d590c07Smrg       set X $old && old=:$2:$4:$5:$6 &&
4560d590c07Smrg       set X $new && new=:$2:$4:$5:$6 &&
4578292847cSmrg       set +f &&
4580d590c07Smrg       test "$old" = "$new" &&
4590d590c07Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
4600d590c07Smrg    then
4610d590c07Smrg      rm -f "$dsttmp"
4620d590c07Smrg    else
4630d590c07Smrg      # Rename the file to the real destination.
4640d590c07Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
4650d590c07Smrg
4660d590c07Smrg      # The rename failed, perhaps because mv can't rename something else
4670d590c07Smrg      # to itself, or perhaps because mv is so ancient that it does not
4680d590c07Smrg      # support -f.
4690d590c07Smrg      {
4708292847cSmrg        # Now remove or move aside any old file at destination location.
4718292847cSmrg        # We try this two ways since rm can't unlink itself on some
4728292847cSmrg        # systems and the destination file might be busy for other
4738292847cSmrg        # reasons.  In this case, the final cleanup might fail but the new
4748292847cSmrg        # file should still install successfully.
4758292847cSmrg        {
4768292847cSmrg          test ! -f "$dst" ||
4778292847cSmrg          $doit $rmcmd -f "$dst" 2>/dev/null ||
4788292847cSmrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
4798292847cSmrg            { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
4808292847cSmrg          } ||
4818292847cSmrg          { echo "$0: cannot unlink or rename $dst" >&2
4828292847cSmrg            (exit 1); exit 1
4838292847cSmrg          }
4848292847cSmrg        } &&
4858292847cSmrg
4868292847cSmrg        # Now rename the file to the real destination.
4878292847cSmrg        $doit $mvcmd "$dsttmp" "$dst"
4880d590c07Smrg      }
4890d590c07Smrg    fi || exit 1
4900d590c07Smrg
4910d590c07Smrg    trap '' 0
4920d590c07Smrg  fi
493c76ae52dSmrgdone
494c76ae52dSmrg
495c76ae52dSmrg# Local variables:
496c76ae52dSmrg# eval: (add-hook 'write-file-hooks 'time-stamp)
497c76ae52dSmrg# time-stamp-start: "scriptversion="
498c76ae52dSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
4990d590c07Smrg# time-stamp-time-zone: "UTC"
5000d590c07Smrg# time-stamp-end: "; # UTC"
501c76ae52dSmrg# End:
502