install-sh revision 4d9306db
1067610f1Smrg#!/bin/sh
2067610f1Smrg# install - install a program, script, or datafile
3067610f1Smrg
44d9306dbSmrgscriptversion=2013-12-25.23; # UTC
5067610f1Smrg
6067610f1Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7067610f1Smrg# later released in X11R6 (xc/config/util/install.sh) with the
8067610f1Smrg# following copyright and license.
9067610f1Smrg#
10067610f1Smrg# Copyright (C) 1994 X Consortium
11067610f1Smrg#
12067610f1Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13067610f1Smrg# of this software and associated documentation files (the "Software"), to
14067610f1Smrg# deal in the Software without restriction, including without limitation the
15067610f1Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16067610f1Smrg# sell copies of the Software, and to permit persons to whom the Software is
17067610f1Smrg# furnished to do so, subject to the following conditions:
18067610f1Smrg#
19067610f1Smrg# The above copyright notice and this permission notice shall be included in
20067610f1Smrg# all copies or substantial portions of the Software.
21067610f1Smrg#
22067610f1Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23067610f1Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24067610f1Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25067610f1Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26067610f1Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27067610f1Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28067610f1Smrg#
29067610f1Smrg# Except as contained in this notice, the name of the X Consortium shall not
30067610f1Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
31067610f1Smrg# ings in this Software without prior written authorization from the X Consor-
32067610f1Smrg# tium.
33067610f1Smrg#
34067610f1Smrg#
35067610f1Smrg# FSF changes to this file are in the public domain.
36067610f1Smrg#
37067610f1Smrg# Calling this script install-sh is preferred over install.sh, to prevent
38e7fdea5fSmrg# 'make' implicit rules from creating a file called install from it
39067610f1Smrg# when there is no Makefile.
40067610f1Smrg#
41067610f1Smrg# This script is compatible with the BSD install script, but was written
42067610f1Smrg# from scratch.
43067610f1Smrg
444d9306dbSmrgtab='	'
45067610f1Smrgnl='
46067610f1Smrg'
474d9306dbSmrgIFS=" $tab$nl"
48067610f1Smrg
494d9306dbSmrg# Set DOITPROG to "echo" to test this script.
50067610f1Smrg
517e6533d5Smrgdoit=${DOITPROG-}
524d9306dbSmrgdoit_exec=${doit:-exec}
53067610f1Smrg
54067610f1Smrg# Put in absolute file names if you don't have them in your path;
55067610f1Smrg# or use environment vars.
56067610f1Smrg
577e6533d5Smrgchgrpprog=${CHGRPPROG-chgrp}
587e6533d5Smrgchmodprog=${CHMODPROG-chmod}
597e6533d5Smrgchownprog=${CHOWNPROG-chown}
607e6533d5Smrgcmpprog=${CMPPROG-cmp}
617e6533d5Smrgcpprog=${CPPROG-cp}
627e6533d5Smrgmkdirprog=${MKDIRPROG-mkdir}
637e6533d5Smrgmvprog=${MVPROG-mv}
647e6533d5Smrgrmprog=${RMPROG-rm}
657e6533d5Smrgstripprog=${STRIPPROG-strip}
667e6533d5Smrg
67067610f1Smrgposix_mkdir=
68067610f1Smrg
69067610f1Smrg# Desired mode of installed file.
70067610f1Smrgmode=0755
71067610f1Smrg
727e6533d5Smrgchgrpcmd=
73067610f1Smrgchmodcmd=$chmodprog
74067610f1Smrgchowncmd=
757e6533d5Smrgmvcmd=$mvprog
76067610f1Smrgrmcmd="$rmprog -f"
777e6533d5Smrgstripcmd=
787e6533d5Smrg
79067610f1Smrgsrc=
80067610f1Smrgdst=
81067610f1Smrgdir_arg=
827e6533d5Smrgdst_arg=
837e6533d5Smrg
847e6533d5Smrgcopy_on_change=false
854d9306dbSmrgis_target_a_directory=possibly
86067610f1Smrg
877e6533d5Smrgusage="\
887e6533d5SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
89067610f1Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
90067610f1Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
91067610f1Smrg   or: $0 [OPTION]... -d DIRECTORIES...
92067610f1Smrg
93067610f1SmrgIn the 1st form, copy SRCFILE to DSTFILE.
94067610f1SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
95067610f1SmrgIn the 4th, create DIRECTORIES.
96067610f1Smrg
97067610f1SmrgOptions:
987e6533d5Smrg     --help     display this help and exit.
997e6533d5Smrg     --version  display version info and exit.
1007e6533d5Smrg
1017e6533d5Smrg  -c            (ignored)
1027e6533d5Smrg  -C            install only if different (preserve the last data modification time)
1037e6533d5Smrg  -d            create directories instead of installing files.
1047e6533d5Smrg  -g GROUP      $chgrpprog installed files to GROUP.
1057e6533d5Smrg  -m MODE       $chmodprog installed files to MODE.
1067e6533d5Smrg  -o USER       $chownprog installed files to USER.
1077e6533d5Smrg  -s            $stripprog installed files.
1087e6533d5Smrg  -t DIRECTORY  install into DIRECTORY.
1097e6533d5Smrg  -T            report an error if DSTFILE is a directory.
110067610f1Smrg
111067610f1SmrgEnvironment variables override the default commands:
1127e6533d5Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
1137e6533d5Smrg  RMPROG STRIPPROG
114067610f1Smrg"
115067610f1Smrg
116067610f1Smrgwhile test $# -ne 0; do
117067610f1Smrg  case $1 in
1187e6533d5Smrg    -c) ;;
1197e6533d5Smrg
1207e6533d5Smrg    -C) copy_on_change=true;;
121067610f1Smrg
1227e6533d5Smrg    -d) dir_arg=true;;
123067610f1Smrg
124067610f1Smrg    -g) chgrpcmd="$chgrpprog $2"
1254d9306dbSmrg        shift;;
126067610f1Smrg
127067610f1Smrg    --help) echo "$usage"; exit $?;;
128067610f1Smrg
129067610f1Smrg    -m) mode=$2
1304d9306dbSmrg        case $mode in
1314d9306dbSmrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
1324d9306dbSmrg            echo "$0: invalid mode: $mode" >&2
1334d9306dbSmrg            exit 1;;
1344d9306dbSmrg        esac
1354d9306dbSmrg        shift;;
136067610f1Smrg
137067610f1Smrg    -o) chowncmd="$chownprog $2"
1384d9306dbSmrg        shift;;
139067610f1Smrg
1407e6533d5Smrg    -s) stripcmd=$stripprog;;
141067610f1Smrg
1424d9306dbSmrg    -t)
1434d9306dbSmrg        is_target_a_directory=always
1444d9306dbSmrg        dst_arg=$2
1454d9306dbSmrg        # Protect names problematic for 'test' and other utilities.
1464d9306dbSmrg        case $dst_arg in
1474d9306dbSmrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
1484d9306dbSmrg        esac
1494d9306dbSmrg        shift;;
150067610f1Smrg
1514d9306dbSmrg    -T) is_target_a_directory=never;;
152067610f1Smrg
153067610f1Smrg    --version) echo "$0 $scriptversion"; exit $?;;
154067610f1Smrg
1554d9306dbSmrg    --) shift
1564d9306dbSmrg        break;;
157067610f1Smrg
1584d9306dbSmrg    -*) echo "$0: invalid option: $1" >&2
1594d9306dbSmrg        exit 1;;
160067610f1Smrg
161067610f1Smrg    *)  break;;
162067610f1Smrg  esac
1637e6533d5Smrg  shift
164067610f1Smrgdone
165067610f1Smrg
1664d9306dbSmrg# We allow the use of options -d and -T together, by making -d
1674d9306dbSmrg# take the precedence; this is for compatibility with GNU install.
1684d9306dbSmrg
1694d9306dbSmrgif test -n "$dir_arg"; then
1704d9306dbSmrg  if test -n "$dst_arg"; then
1714d9306dbSmrg    echo "$0: target directory not allowed when installing a directory." >&2
1724d9306dbSmrg    exit 1
1734d9306dbSmrg  fi
1744d9306dbSmrgfi
1754d9306dbSmrg
1767e6533d5Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
177067610f1Smrg  # When -d is used, all remaining arguments are directories to create.
178067610f1Smrg  # When -t is used, the destination is already specified.
179067610f1Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
180067610f1Smrg  for arg
181067610f1Smrg  do
1827e6533d5Smrg    if test -n "$dst_arg"; then
183067610f1Smrg      # $@ is not empty: it contains at least $arg.
1847e6533d5Smrg      set fnord "$@" "$dst_arg"
185067610f1Smrg      shift # fnord
186067610f1Smrg    fi
187067610f1Smrg    shift # arg
1887e6533d5Smrg    dst_arg=$arg
189e7fdea5fSmrg    # Protect names problematic for 'test' and other utilities.
190aea1d7ffSmrg    case $dst_arg in
191aea1d7ffSmrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
192aea1d7ffSmrg    esac
193067610f1Smrg  done
194067610f1Smrgfi
195067610f1Smrg
196067610f1Smrgif test $# -eq 0; then
197067610f1Smrg  if test -z "$dir_arg"; then
198067610f1Smrg    echo "$0: no input file specified." >&2
199067610f1Smrg    exit 1
200067610f1Smrg  fi
201e7fdea5fSmrg  # It's OK to call 'install-sh -d' without argument.
202067610f1Smrg  # This can happen when creating conditional directories.
203067610f1Smrg  exit 0
204067610f1Smrgfi
205067610f1Smrg
2064d9306dbSmrgif test -z "$dir_arg"; then
2074d9306dbSmrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
2084d9306dbSmrg    if test ! -d "$dst_arg"; then
2094d9306dbSmrg      echo "$0: $dst_arg: Is not a directory." >&2
2104d9306dbSmrg      exit 1
2114d9306dbSmrg    fi
2124d9306dbSmrg  fi
2134d9306dbSmrgfi
2144d9306dbSmrg
215067610f1Smrgif test -z "$dir_arg"; then
216aea1d7ffSmrg  do_exit='(exit $ret); exit $ret'
217aea1d7ffSmrg  trap "ret=129; $do_exit" 1
218aea1d7ffSmrg  trap "ret=130; $do_exit" 2
219aea1d7ffSmrg  trap "ret=141; $do_exit" 13
220aea1d7ffSmrg  trap "ret=143; $do_exit" 15
221067610f1Smrg
222067610f1Smrg  # Set umask so as not to create temps with too-generous modes.
223067610f1Smrg  # However, 'strip' requires both read and write access to temps.
224067610f1Smrg  case $mode in
225067610f1Smrg    # Optimize common cases.
226067610f1Smrg    *644) cp_umask=133;;
227067610f1Smrg    *755) cp_umask=22;;
228067610f1Smrg
229067610f1Smrg    *[0-7])
230067610f1Smrg      if test -z "$stripcmd"; then
2314d9306dbSmrg        u_plus_rw=
232067610f1Smrg      else
2334d9306dbSmrg        u_plus_rw='% 200'
234067610f1Smrg      fi
235067610f1Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
236067610f1Smrg    *)
237067610f1Smrg      if test -z "$stripcmd"; then
2384d9306dbSmrg        u_plus_rw=
239067610f1Smrg      else
2404d9306dbSmrg        u_plus_rw=,u+rw
241067610f1Smrg      fi
242067610f1Smrg      cp_umask=$mode$u_plus_rw;;
243067610f1Smrg  esac
244067610f1Smrgfi
245067610f1Smrg
246067610f1Smrgfor src
247067610f1Smrgdo
248e7fdea5fSmrg  # Protect names problematic for 'test' and other utilities.
249067610f1Smrg  case $src in
250aea1d7ffSmrg    -* | [=\(\)!]) src=./$src;;
251067610f1Smrg  esac
252067610f1Smrg
253067610f1Smrg  if test -n "$dir_arg"; then
254067610f1Smrg    dst=$src
255067610f1Smrg    dstdir=$dst
256067610f1Smrg    test -d "$dstdir"
257067610f1Smrg    dstdir_status=$?
258067610f1Smrg  else
259067610f1Smrg
260067610f1Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
261067610f1Smrg    # might cause directories to be created, which would be especially bad
262067610f1Smrg    # if $src (and thus $dsttmp) contains '*'.
263067610f1Smrg    if test ! -f "$src" && test ! -d "$src"; then
264067610f1Smrg      echo "$0: $src does not exist." >&2
265067610f1Smrg      exit 1
266067610f1Smrg    fi
267067610f1Smrg
2687e6533d5Smrg    if test -z "$dst_arg"; then
269067610f1Smrg      echo "$0: no destination specified." >&2
270067610f1Smrg      exit 1
271067610f1Smrg    fi
2727e6533d5Smrg    dst=$dst_arg
273067610f1Smrg
274067610f1Smrg    # If destination is a directory, append the input filename; won't work
275067610f1Smrg    # if double slashes aren't ignored.
276067610f1Smrg    if test -d "$dst"; then
2774d9306dbSmrg      if test "$is_target_a_directory" = never; then
2784d9306dbSmrg        echo "$0: $dst_arg: Is a directory" >&2
2794d9306dbSmrg        exit 1
280067610f1Smrg      fi
281067610f1Smrg      dstdir=$dst
282067610f1Smrg      dst=$dstdir/`basename "$src"`
283067610f1Smrg      dstdir_status=0
284067610f1Smrg    else
2854d9306dbSmrg      dstdir=`dirname "$dst"`
286067610f1Smrg      test -d "$dstdir"
287067610f1Smrg      dstdir_status=$?
288067610f1Smrg    fi
289067610f1Smrg  fi
290067610f1Smrg
291067610f1Smrg  obsolete_mkdir_used=false
292067610f1Smrg
293067610f1Smrg  if test $dstdir_status != 0; then
294067610f1Smrg    case $posix_mkdir in
295067610f1Smrg      '')
2964d9306dbSmrg        # Create intermediate dirs using mode 755 as modified by the umask.
2974d9306dbSmrg        # This is like FreeBSD 'install' as of 1997-10-28.
2984d9306dbSmrg        umask=`umask`
2994d9306dbSmrg        case $stripcmd.$umask in
3004d9306dbSmrg          # Optimize common cases.
3014d9306dbSmrg          *[2367][2367]) mkdir_umask=$umask;;
3024d9306dbSmrg          .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
3034d9306dbSmrg
3044d9306dbSmrg          *[0-7])
3054d9306dbSmrg            mkdir_umask=`expr $umask + 22 \
3064d9306dbSmrg              - $umask % 100 % 40 + $umask % 20 \
3074d9306dbSmrg              - $umask % 10 % 4 + $umask % 2
3084d9306dbSmrg            `;;
3094d9306dbSmrg          *) mkdir_umask=$umask,go-w;;
3104d9306dbSmrg        esac
3114d9306dbSmrg
3124d9306dbSmrg        # With -d, create the new directory with the user-specified mode.
3134d9306dbSmrg        # Otherwise, rely on $mkdir_umask.
3144d9306dbSmrg        if test -n "$dir_arg"; then
3154d9306dbSmrg          mkdir_mode=-m$mode
3164d9306dbSmrg        else
3174d9306dbSmrg          mkdir_mode=
3184d9306dbSmrg        fi
3194d9306dbSmrg
3204d9306dbSmrg        posix_mkdir=false
3214d9306dbSmrg        case $umask in
3224d9306dbSmrg          *[123567][0-7][0-7])
3234d9306dbSmrg            # POSIX mkdir -p sets u+wx bits regardless of umask, which
3244d9306dbSmrg            # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
3254d9306dbSmrg            ;;
3264d9306dbSmrg          *)
3274d9306dbSmrg            tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
3284d9306dbSmrg            trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
3294d9306dbSmrg
3304d9306dbSmrg            if (umask $mkdir_umask &&
3314d9306dbSmrg                exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
3324d9306dbSmrg            then
3334d9306dbSmrg              if test -z "$dir_arg" || {
3344d9306dbSmrg                   # Check for POSIX incompatibilities with -m.
3354d9306dbSmrg                   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
3364d9306dbSmrg                   # other-writable bit of parent directory when it shouldn't.
3374d9306dbSmrg                   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
3384d9306dbSmrg                   ls_ld_tmpdir=`ls -ld "$tmpdir"`
3394d9306dbSmrg                   case $ls_ld_tmpdir in
3404d9306dbSmrg                     d????-?r-*) different_mode=700;;
3414d9306dbSmrg                     d????-?--*) different_mode=755;;
3424d9306dbSmrg                     *) false;;
3434d9306dbSmrg                   esac &&
3444d9306dbSmrg                   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
3454d9306dbSmrg                     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
3464d9306dbSmrg                     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
3474d9306dbSmrg                   }
3484d9306dbSmrg                 }
3494d9306dbSmrg              then posix_mkdir=:
3504d9306dbSmrg              fi
3514d9306dbSmrg              rmdir "$tmpdir/d" "$tmpdir"
3524d9306dbSmrg            else
3534d9306dbSmrg              # Remove any dirs left behind by ancient mkdir implementations.
3544d9306dbSmrg              rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
3554d9306dbSmrg            fi
3564d9306dbSmrg            trap '' 0;;
3574d9306dbSmrg        esac;;
358067610f1Smrg    esac
359067610f1Smrg
360067610f1Smrg    if
361067610f1Smrg      $posix_mkdir && (
3624d9306dbSmrg        umask $mkdir_umask &&
3634d9306dbSmrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
364067610f1Smrg      )
365067610f1Smrg    then :
366067610f1Smrg    else
367067610f1Smrg
368067610f1Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
369067610f1Smrg      # or it failed possibly due to a race condition.  Create the
370067610f1Smrg      # directory the slow way, step by step, checking for races as we go.
371067610f1Smrg
372067610f1Smrg      case $dstdir in
3734d9306dbSmrg        /*) prefix='/';;
3744d9306dbSmrg        [-=\(\)!]*) prefix='./';;
3754d9306dbSmrg        *)  prefix='';;
376067610f1Smrg      esac
377067610f1Smrg
378067610f1Smrg      oIFS=$IFS
379067610f1Smrg      IFS=/
3804d9306dbSmrg      set -f
381067610f1Smrg      set fnord $dstdir
382067610f1Smrg      shift
3834d9306dbSmrg      set +f
384067610f1Smrg      IFS=$oIFS
385067610f1Smrg
386067610f1Smrg      prefixes=
387067610f1Smrg
388067610f1Smrg      for d
389067610f1Smrg      do
3904d9306dbSmrg        test X"$d" = X && continue
3914d9306dbSmrg
3924d9306dbSmrg        prefix=$prefix$d
3934d9306dbSmrg        if test -d "$prefix"; then
3944d9306dbSmrg          prefixes=
3954d9306dbSmrg        else
3964d9306dbSmrg          if $posix_mkdir; then
3974d9306dbSmrg            (umask=$mkdir_umask &&
3984d9306dbSmrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
3994d9306dbSmrg            # Don't fail if two instances are running concurrently.
4004d9306dbSmrg            test -d "$prefix" || exit 1
4014d9306dbSmrg          else
4024d9306dbSmrg            case $prefix in
4034d9306dbSmrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
4044d9306dbSmrg              *) qprefix=$prefix;;
4054d9306dbSmrg            esac
4064d9306dbSmrg            prefixes="$prefixes '$qprefix'"
4074d9306dbSmrg          fi
4084d9306dbSmrg        fi
4094d9306dbSmrg        prefix=$prefix/
410067610f1Smrg      done
411067610f1Smrg
412067610f1Smrg      if test -n "$prefixes"; then
4134d9306dbSmrg        # Don't fail if two instances are running concurrently.
4144d9306dbSmrg        (umask $mkdir_umask &&
4154d9306dbSmrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
4164d9306dbSmrg          test -d "$dstdir" || exit 1
4174d9306dbSmrg        obsolete_mkdir_used=true
418067610f1Smrg      fi
419067610f1Smrg    fi
420067610f1Smrg  fi
421067610f1Smrg
422067610f1Smrg  if test -n "$dir_arg"; then
423067610f1Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
424067610f1Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
425067610f1Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
426067610f1Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
427067610f1Smrg  else
428067610f1Smrg
429067610f1Smrg    # Make a couple of temp file names in the proper directory.
430067610f1Smrg    dsttmp=$dstdir/_inst.$$_
431067610f1Smrg    rmtmp=$dstdir/_rm.$$_
432067610f1Smrg
433067610f1Smrg    # Trap to clean up those temp files at exit.
434067610f1Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
435067610f1Smrg
436067610f1Smrg    # Copy the file name to the temp name.
437067610f1Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
438067610f1Smrg
439067610f1Smrg    # and set any options; do chmod last to preserve setuid bits.
440067610f1Smrg    #
441067610f1Smrg    # If any of these fail, we abort the whole thing.  If we want to
442067610f1Smrg    # ignore errors from any of these, just make sure not to ignore
443067610f1Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
444067610f1Smrg    #
4457e6533d5Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
4467e6533d5Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
4477e6533d5Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
4487e6533d5Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4497e6533d5Smrg
4507e6533d5Smrg    # If -C, don't bother to copy if it wouldn't change the file.
4517e6533d5Smrg    if $copy_on_change &&
4524d9306dbSmrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
4534d9306dbSmrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
4544d9306dbSmrg       set -f &&
4557e6533d5Smrg       set X $old && old=:$2:$4:$5:$6 &&
4567e6533d5Smrg       set X $new && new=:$2:$4:$5:$6 &&
4574d9306dbSmrg       set +f &&
4587e6533d5Smrg       test "$old" = "$new" &&
4597e6533d5Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
4607e6533d5Smrg    then
4617e6533d5Smrg      rm -f "$dsttmp"
4627e6533d5Smrg    else
4637e6533d5Smrg      # Rename the file to the real destination.
4647e6533d5Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
4657e6533d5Smrg
4667e6533d5Smrg      # The rename failed, perhaps because mv can't rename something else
4677e6533d5Smrg      # to itself, or perhaps because mv is so ancient that it does not
4687e6533d5Smrg      # support -f.
4697e6533d5Smrg      {
4704d9306dbSmrg        # Now remove or move aside any old file at destination location.
4714d9306dbSmrg        # We try this two ways since rm can't unlink itself on some
4724d9306dbSmrg        # systems and the destination file might be busy for other
4734d9306dbSmrg        # reasons.  In this case, the final cleanup might fail but the new
4744d9306dbSmrg        # file should still install successfully.
4754d9306dbSmrg        {
4764d9306dbSmrg          test ! -f "$dst" ||
4774d9306dbSmrg          $doit $rmcmd -f "$dst" 2>/dev/null ||
4784d9306dbSmrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
4794d9306dbSmrg            { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
4804d9306dbSmrg          } ||
4814d9306dbSmrg          { echo "$0: cannot unlink or rename $dst" >&2
4824d9306dbSmrg            (exit 1); exit 1
4834d9306dbSmrg          }
4844d9306dbSmrg        } &&
4854d9306dbSmrg
4864d9306dbSmrg        # Now rename the file to the real destination.
4874d9306dbSmrg        $doit $mvcmd "$dsttmp" "$dst"
4887e6533d5Smrg      }
4897e6533d5Smrg    fi || exit 1
490067610f1Smrg
491067610f1Smrg    trap '' 0
492067610f1Smrg  fi
493067610f1Smrgdone
494067610f1Smrg
495067610f1Smrg# Local variables:
496067610f1Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
497067610f1Smrg# time-stamp-start: "scriptversion="
498067610f1Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
4997e6533d5Smrg# time-stamp-time-zone: "UTC"
5007e6533d5Smrg# time-stamp-end: "; # UTC"
501067610f1Smrg# End:
502