install-sh revision b312a94c
1ef981d24Smrg#!/bin/sh
2ef981d24Smrg# install - install a program, script, or datafile
3ef981d24Smrg
4b312a94cSmrgscriptversion=2016-01-11.22; # UTC
5ef981d24Smrg
6ef981d24Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7ef981d24Smrg# later released in X11R6 (xc/config/util/install.sh) with the
8ef981d24Smrg# following copyright and license.
9ef981d24Smrg#
10ef981d24Smrg# Copyright (C) 1994 X Consortium
11ef981d24Smrg#
12ef981d24Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13ef981d24Smrg# of this software and associated documentation files (the "Software"), to
14ef981d24Smrg# deal in the Software without restriction, including without limitation the
15ef981d24Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16ef981d24Smrg# sell copies of the Software, and to permit persons to whom the Software is
17ef981d24Smrg# furnished to do so, subject to the following conditions:
18ef981d24Smrg#
19ef981d24Smrg# The above copyright notice and this permission notice shall be included in
20ef981d24Smrg# all copies or substantial portions of the Software.
21ef981d24Smrg#
22ef981d24Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23ef981d24Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24ef981d24Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25ef981d24Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26ef981d24Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27ef981d24Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28ef981d24Smrg#
29ef981d24Smrg# Except as contained in this notice, the name of the X Consortium shall not
30ef981d24Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
31ef981d24Smrg# ings in this Software without prior written authorization from the X Consor-
32ef981d24Smrg# tium.
33ef981d24Smrg#
34ef981d24Smrg#
35ef981d24Smrg# FSF changes to this file are in the public domain.
36ef981d24Smrg#
37ef981d24Smrg# Calling this script install-sh is preferred over install.sh, to prevent
38b312a94cSmrg# 'make' implicit rules from creating a file called install from it
39ef981d24Smrg# when there is no Makefile.
40ef981d24Smrg#
41ef981d24Smrg# This script is compatible with the BSD install script, but was written
42ef981d24Smrg# from scratch.
43ef981d24Smrg
44b312a94cSmrgtab='	'
45ef981d24Smrgnl='
46ef981d24Smrg'
47b312a94cSmrgIFS=" $tab$nl"
48ef981d24Smrg
49b312a94cSmrg# Set DOITPROG to "echo" to test this script.
50ef981d24Smrg
5165eef222Smrgdoit=${DOITPROG-}
52b312a94cSmrgdoit_exec=${doit:-exec}
53ef981d24Smrg
54ef981d24Smrg# Put in absolute file names if you don't have them in your path;
55ef981d24Smrg# or use environment vars.
56ef981d24Smrg
5765eef222Smrgchgrpprog=${CHGRPPROG-chgrp}
5865eef222Smrgchmodprog=${CHMODPROG-chmod}
5965eef222Smrgchownprog=${CHOWNPROG-chown}
6065eef222Smrgcmpprog=${CMPPROG-cmp}
6165eef222Smrgcpprog=${CPPROG-cp}
6265eef222Smrgmkdirprog=${MKDIRPROG-mkdir}
6365eef222Smrgmvprog=${MVPROG-mv}
6465eef222Smrgrmprog=${RMPROG-rm}
6565eef222Smrgstripprog=${STRIPPROG-strip}
6665eef222Smrg
67ef981d24Smrgposix_mkdir=
68ef981d24Smrg
69ef981d24Smrg# Desired mode of installed file.
70ef981d24Smrgmode=0755
71ef981d24Smrg
7265eef222Smrgchgrpcmd=
73ef981d24Smrgchmodcmd=$chmodprog
74ef981d24Smrgchowncmd=
7565eef222Smrgmvcmd=$mvprog
76ef981d24Smrgrmcmd="$rmprog -f"
7765eef222Smrgstripcmd=
7865eef222Smrg
79ef981d24Smrgsrc=
80ef981d24Smrgdst=
81ef981d24Smrgdir_arg=
8265eef222Smrgdst_arg=
8365eef222Smrg
8465eef222Smrgcopy_on_change=false
85b312a94cSmrgis_target_a_directory=possibly
86ef981d24Smrg
8765eef222Smrgusage="\
8865eef222SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
89ef981d24Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
90ef981d24Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
91ef981d24Smrg   or: $0 [OPTION]... -d DIRECTORIES...
92ef981d24Smrg
93ef981d24SmrgIn the 1st form, copy SRCFILE to DSTFILE.
94ef981d24SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
95ef981d24SmrgIn the 4th, create DIRECTORIES.
96ef981d24Smrg
97ef981d24SmrgOptions:
9865eef222Smrg     --help     display this help and exit.
9965eef222Smrg     --version  display version info and exit.
10065eef222Smrg
10165eef222Smrg  -c            (ignored)
10265eef222Smrg  -C            install only if different (preserve the last data modification time)
10365eef222Smrg  -d            create directories instead of installing files.
10465eef222Smrg  -g GROUP      $chgrpprog installed files to GROUP.
10565eef222Smrg  -m MODE       $chmodprog installed files to MODE.
10665eef222Smrg  -o USER       $chownprog installed files to USER.
10765eef222Smrg  -s            $stripprog installed files.
10865eef222Smrg  -t DIRECTORY  install into DIRECTORY.
10965eef222Smrg  -T            report an error if DSTFILE is a directory.
110ef981d24Smrg
111ef981d24SmrgEnvironment variables override the default commands:
11265eef222Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
11365eef222Smrg  RMPROG STRIPPROG
114ef981d24Smrg"
115ef981d24Smrg
116ef981d24Smrgwhile test $# -ne 0; do
117ef981d24Smrg  case $1 in
11865eef222Smrg    -c) ;;
11965eef222Smrg
12065eef222Smrg    -C) copy_on_change=true;;
121ef981d24Smrg
12265eef222Smrg    -d) dir_arg=true;;
123ef981d24Smrg
124ef981d24Smrg    -g) chgrpcmd="$chgrpprog $2"
125b312a94cSmrg        shift;;
126ef981d24Smrg
127ef981d24Smrg    --help) echo "$usage"; exit $?;;
128ef981d24Smrg
129ef981d24Smrg    -m) mode=$2
130b312a94cSmrg        case $mode in
131b312a94cSmrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
132b312a94cSmrg            echo "$0: invalid mode: $mode" >&2
133b312a94cSmrg            exit 1;;
134b312a94cSmrg        esac
135b312a94cSmrg        shift;;
136ef981d24Smrg
137ef981d24Smrg    -o) chowncmd="$chownprog $2"
138b312a94cSmrg        shift;;
139ef981d24Smrg
14065eef222Smrg    -s) stripcmd=$stripprog;;
141ef981d24Smrg
142b312a94cSmrg    -t)
143b312a94cSmrg        is_target_a_directory=always
144b312a94cSmrg        dst_arg=$2
145b312a94cSmrg        # Protect names problematic for 'test' and other utilities.
146b312a94cSmrg        case $dst_arg in
147b312a94cSmrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
148b312a94cSmrg        esac
149b312a94cSmrg        shift;;
150ef981d24Smrg
151b312a94cSmrg    -T) is_target_a_directory=never;;
152ef981d24Smrg
153ef981d24Smrg    --version) echo "$0 $scriptversion"; exit $?;;
154ef981d24Smrg
155b312a94cSmrg    --) shift
156b312a94cSmrg        break;;
157ef981d24Smrg
158b312a94cSmrg    -*) echo "$0: invalid option: $1" >&2
159b312a94cSmrg        exit 1;;
160ef981d24Smrg
161ef981d24Smrg    *)  break;;
162ef981d24Smrg  esac
16365eef222Smrg  shift
164ef981d24Smrgdone
165ef981d24Smrg
166b312a94cSmrg# We allow the use of options -d and -T together, by making -d
167b312a94cSmrg# take the precedence; this is for compatibility with GNU install.
168b312a94cSmrg
169b312a94cSmrgif test -n "$dir_arg"; then
170b312a94cSmrg  if test -n "$dst_arg"; then
171b312a94cSmrg    echo "$0: target directory not allowed when installing a directory." >&2
172b312a94cSmrg    exit 1
173b312a94cSmrg  fi
174b312a94cSmrgfi
175b312a94cSmrg
17665eef222Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
177ef981d24Smrg  # When -d is used, all remaining arguments are directories to create.
178ef981d24Smrg  # When -t is used, the destination is already specified.
179ef981d24Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
180ef981d24Smrg  for arg
181ef981d24Smrg  do
18265eef222Smrg    if test -n "$dst_arg"; then
183ef981d24Smrg      # $@ is not empty: it contains at least $arg.
18465eef222Smrg      set fnord "$@" "$dst_arg"
185ef981d24Smrg      shift # fnord
186ef981d24Smrg    fi
187ef981d24Smrg    shift # arg
18865eef222Smrg    dst_arg=$arg
189b312a94cSmrg    # Protect names problematic for 'test' and other utilities.
190b312a94cSmrg    case $dst_arg in
191b312a94cSmrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
192b312a94cSmrg    esac
193ef981d24Smrg  done
194ef981d24Smrgfi
195ef981d24Smrg
196ef981d24Smrgif test $# -eq 0; then
197ef981d24Smrg  if test -z "$dir_arg"; then
198ef981d24Smrg    echo "$0: no input file specified." >&2
199ef981d24Smrg    exit 1
200ef981d24Smrg  fi
201b312a94cSmrg  # It's OK to call 'install-sh -d' without argument.
202ef981d24Smrg  # This can happen when creating conditional directories.
203ef981d24Smrg  exit 0
204ef981d24Smrgfi
205ef981d24Smrg
206ef981d24Smrgif test -z "$dir_arg"; then
207b312a94cSmrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
208b312a94cSmrg    if test ! -d "$dst_arg"; then
209b312a94cSmrg      echo "$0: $dst_arg: Is not a directory." >&2
210b312a94cSmrg      exit 1
211b312a94cSmrg    fi
212b312a94cSmrg  fi
213b312a94cSmrgfi
214b312a94cSmrg
215b312a94cSmrgif test -z "$dir_arg"; then
216b312a94cSmrg  do_exit='(exit $ret); exit $ret'
217b312a94cSmrg  trap "ret=129; $do_exit" 1
218b312a94cSmrg  trap "ret=130; $do_exit" 2
219b312a94cSmrg  trap "ret=141; $do_exit" 13
220b312a94cSmrg  trap "ret=143; $do_exit" 15
221ef981d24Smrg
222ef981d24Smrg  # Set umask so as not to create temps with too-generous modes.
223ef981d24Smrg  # However, 'strip' requires both read and write access to temps.
224ef981d24Smrg  case $mode in
225ef981d24Smrg    # Optimize common cases.
226ef981d24Smrg    *644) cp_umask=133;;
227ef981d24Smrg    *755) cp_umask=22;;
228ef981d24Smrg
229ef981d24Smrg    *[0-7])
230ef981d24Smrg      if test -z "$stripcmd"; then
231b312a94cSmrg        u_plus_rw=
232ef981d24Smrg      else
233b312a94cSmrg        u_plus_rw='% 200'
234ef981d24Smrg      fi
235ef981d24Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
236ef981d24Smrg    *)
237ef981d24Smrg      if test -z "$stripcmd"; then
238b312a94cSmrg        u_plus_rw=
239ef981d24Smrg      else
240b312a94cSmrg        u_plus_rw=,u+rw
241ef981d24Smrg      fi
242ef981d24Smrg      cp_umask=$mode$u_plus_rw;;
243ef981d24Smrg  esac
244ef981d24Smrgfi
245ef981d24Smrg
246ef981d24Smrgfor src
247ef981d24Smrgdo
248b312a94cSmrg  # Protect names problematic for 'test' and other utilities.
249ef981d24Smrg  case $src in
250b312a94cSmrg    -* | [=\(\)!]) src=./$src;;
251ef981d24Smrg  esac
252ef981d24Smrg
253ef981d24Smrg  if test -n "$dir_arg"; then
254ef981d24Smrg    dst=$src
255ef981d24Smrg    dstdir=$dst
256ef981d24Smrg    test -d "$dstdir"
257ef981d24Smrg    dstdir_status=$?
258ef981d24Smrg  else
259ef981d24Smrg
260ef981d24Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
261ef981d24Smrg    # might cause directories to be created, which would be especially bad
262ef981d24Smrg    # if $src (and thus $dsttmp) contains '*'.
263ef981d24Smrg    if test ! -f "$src" && test ! -d "$src"; then
264ef981d24Smrg      echo "$0: $src does not exist." >&2
265ef981d24Smrg      exit 1
266ef981d24Smrg    fi
267ef981d24Smrg
26865eef222Smrg    if test -z "$dst_arg"; then
269ef981d24Smrg      echo "$0: no destination specified." >&2
270ef981d24Smrg      exit 1
271ef981d24Smrg    fi
27265eef222Smrg    dst=$dst_arg
273ef981d24Smrg
274ef981d24Smrg    # If destination is a directory, append the input filename; won't work
275ef981d24Smrg    # if double slashes aren't ignored.
276ef981d24Smrg    if test -d "$dst"; then
277b312a94cSmrg      if test "$is_target_a_directory" = never; then
278b312a94cSmrg        echo "$0: $dst_arg: Is a directory" >&2
279b312a94cSmrg        exit 1
280ef981d24Smrg      fi
281ef981d24Smrg      dstdir=$dst
282ef981d24Smrg      dst=$dstdir/`basename "$src"`
283ef981d24Smrg      dstdir_status=0
284ef981d24Smrg    else
285b312a94cSmrg      dstdir=`dirname "$dst"`
286ef981d24Smrg      test -d "$dstdir"
287ef981d24Smrg      dstdir_status=$?
288ef981d24Smrg    fi
289ef981d24Smrg  fi
290ef981d24Smrg
291ef981d24Smrg  obsolete_mkdir_used=false
292ef981d24Smrg
293ef981d24Smrg  if test $dstdir_status != 0; then
294ef981d24Smrg    case $posix_mkdir in
295ef981d24Smrg      '')
296b312a94cSmrg        # Create intermediate dirs using mode 755 as modified by the umask.
297b312a94cSmrg        # This is like FreeBSD 'install' as of 1997-10-28.
298b312a94cSmrg        umask=`umask`
299b312a94cSmrg        case $stripcmd.$umask in
300b312a94cSmrg          # Optimize common cases.
301b312a94cSmrg          *[2367][2367]) mkdir_umask=$umask;;
302b312a94cSmrg          .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
303b312a94cSmrg
304b312a94cSmrg          *[0-7])
305b312a94cSmrg            mkdir_umask=`expr $umask + 22 \
306b312a94cSmrg              - $umask % 100 % 40 + $umask % 20 \
307b312a94cSmrg              - $umask % 10 % 4 + $umask % 2
308b312a94cSmrg            `;;
309b312a94cSmrg          *) mkdir_umask=$umask,go-w;;
310b312a94cSmrg        esac
311b312a94cSmrg
312b312a94cSmrg        # With -d, create the new directory with the user-specified mode.
313b312a94cSmrg        # Otherwise, rely on $mkdir_umask.
314b312a94cSmrg        if test -n "$dir_arg"; then
315b312a94cSmrg          mkdir_mode=-m$mode
316b312a94cSmrg        else
317b312a94cSmrg          mkdir_mode=
318b312a94cSmrg        fi
319b312a94cSmrg
320b312a94cSmrg        posix_mkdir=false
321b312a94cSmrg        case $umask in
322b312a94cSmrg          *[123567][0-7][0-7])
323b312a94cSmrg            # POSIX mkdir -p sets u+wx bits regardless of umask, which
324b312a94cSmrg            # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
325b312a94cSmrg            ;;
326b312a94cSmrg          *)
327b312a94cSmrg            tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
328b312a94cSmrg            trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
329b312a94cSmrg
330b312a94cSmrg            if (umask $mkdir_umask &&
331b312a94cSmrg                exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
332b312a94cSmrg            then
333b312a94cSmrg              if test -z "$dir_arg" || {
334b312a94cSmrg                   # Check for POSIX incompatibilities with -m.
335b312a94cSmrg                   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
336b312a94cSmrg                   # other-writable bit of parent directory when it shouldn't.
337b312a94cSmrg                   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
338b312a94cSmrg                   ls_ld_tmpdir=`ls -ld "$tmpdir"`
339b312a94cSmrg                   case $ls_ld_tmpdir in
340b312a94cSmrg                     d????-?r-*) different_mode=700;;
341b312a94cSmrg                     d????-?--*) different_mode=755;;
342b312a94cSmrg                     *) false;;
343b312a94cSmrg                   esac &&
344b312a94cSmrg                   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
345b312a94cSmrg                     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
346b312a94cSmrg                     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
347b312a94cSmrg                   }
348b312a94cSmrg                 }
349b312a94cSmrg              then posix_mkdir=:
350b312a94cSmrg              fi
351b312a94cSmrg              rmdir "$tmpdir/d" "$tmpdir"
352b312a94cSmrg            else
353b312a94cSmrg              # Remove any dirs left behind by ancient mkdir implementations.
354b312a94cSmrg              rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
355b312a94cSmrg            fi
356b312a94cSmrg            trap '' 0;;
357b312a94cSmrg        esac;;
358ef981d24Smrg    esac
359ef981d24Smrg
360ef981d24Smrg    if
361ef981d24Smrg      $posix_mkdir && (
362b312a94cSmrg        umask $mkdir_umask &&
363b312a94cSmrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
364ef981d24Smrg      )
365ef981d24Smrg    then :
366ef981d24Smrg    else
367ef981d24Smrg
368ef981d24Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
369ef981d24Smrg      # or it failed possibly due to a race condition.  Create the
370ef981d24Smrg      # directory the slow way, step by step, checking for races as we go.
371ef981d24Smrg
372ef981d24Smrg      case $dstdir in
373b312a94cSmrg        /*) prefix='/';;
374b312a94cSmrg        [-=\(\)!]*) prefix='./';;
375b312a94cSmrg        *)  prefix='';;
376ef981d24Smrg      esac
377ef981d24Smrg
378ef981d24Smrg      oIFS=$IFS
379ef981d24Smrg      IFS=/
380b312a94cSmrg      set -f
381ef981d24Smrg      set fnord $dstdir
382ef981d24Smrg      shift
383b312a94cSmrg      set +f
384ef981d24Smrg      IFS=$oIFS
385ef981d24Smrg
386ef981d24Smrg      prefixes=
387ef981d24Smrg
388ef981d24Smrg      for d
389ef981d24Smrg      do
390b312a94cSmrg        test X"$d" = X && continue
391b312a94cSmrg
392b312a94cSmrg        prefix=$prefix$d
393b312a94cSmrg        if test -d "$prefix"; then
394b312a94cSmrg          prefixes=
395b312a94cSmrg        else
396b312a94cSmrg          if $posix_mkdir; then
397b312a94cSmrg            (umask=$mkdir_umask &&
398b312a94cSmrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
399b312a94cSmrg            # Don't fail if two instances are running concurrently.
400b312a94cSmrg            test -d "$prefix" || exit 1
401b312a94cSmrg          else
402b312a94cSmrg            case $prefix in
403b312a94cSmrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
404b312a94cSmrg              *) qprefix=$prefix;;
405b312a94cSmrg            esac
406b312a94cSmrg            prefixes="$prefixes '$qprefix'"
407b312a94cSmrg          fi
408b312a94cSmrg        fi
409b312a94cSmrg        prefix=$prefix/
410ef981d24Smrg      done
411ef981d24Smrg
412ef981d24Smrg      if test -n "$prefixes"; then
413b312a94cSmrg        # Don't fail if two instances are running concurrently.
414b312a94cSmrg        (umask $mkdir_umask &&
415b312a94cSmrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
416b312a94cSmrg          test -d "$dstdir" || exit 1
417b312a94cSmrg        obsolete_mkdir_used=true
418ef981d24Smrg      fi
419ef981d24Smrg    fi
420ef981d24Smrg  fi
421ef981d24Smrg
422ef981d24Smrg  if test -n "$dir_arg"; then
423ef981d24Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
424ef981d24Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
425ef981d24Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
426ef981d24Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
427ef981d24Smrg  else
428ef981d24Smrg
429ef981d24Smrg    # Make a couple of temp file names in the proper directory.
430ef981d24Smrg    dsttmp=$dstdir/_inst.$$_
431ef981d24Smrg    rmtmp=$dstdir/_rm.$$_
432ef981d24Smrg
433ef981d24Smrg    # Trap to clean up those temp files at exit.
434ef981d24Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
435ef981d24Smrg
436ef981d24Smrg    # Copy the file name to the temp name.
437ef981d24Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
438ef981d24Smrg
439ef981d24Smrg    # and set any options; do chmod last to preserve setuid bits.
440ef981d24Smrg    #
441ef981d24Smrg    # If any of these fail, we abort the whole thing.  If we want to
442ef981d24Smrg    # ignore errors from any of these, just make sure not to ignore
443ef981d24Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
444ef981d24Smrg    #
44565eef222Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
44665eef222Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
44765eef222Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
44865eef222Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
44965eef222Smrg
45065eef222Smrg    # If -C, don't bother to copy if it wouldn't change the file.
45165eef222Smrg    if $copy_on_change &&
452b312a94cSmrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
453b312a94cSmrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
454b312a94cSmrg       set -f &&
45565eef222Smrg       set X $old && old=:$2:$4:$5:$6 &&
45665eef222Smrg       set X $new && new=:$2:$4:$5:$6 &&
457b312a94cSmrg       set +f &&
45865eef222Smrg       test "$old" = "$new" &&
45965eef222Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
46065eef222Smrg    then
46165eef222Smrg      rm -f "$dsttmp"
46265eef222Smrg    else
46365eef222Smrg      # Rename the file to the real destination.
46465eef222Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
46565eef222Smrg
46665eef222Smrg      # The rename failed, perhaps because mv can't rename something else
46765eef222Smrg      # to itself, or perhaps because mv is so ancient that it does not
46865eef222Smrg      # support -f.
46965eef222Smrg      {
470b312a94cSmrg        # Now remove or move aside any old file at destination location.
471b312a94cSmrg        # We try this two ways since rm can't unlink itself on some
472b312a94cSmrg        # systems and the destination file might be busy for other
473b312a94cSmrg        # reasons.  In this case, the final cleanup might fail but the new
474b312a94cSmrg        # file should still install successfully.
475b312a94cSmrg        {
476b312a94cSmrg          test ! -f "$dst" ||
477b312a94cSmrg          $doit $rmcmd -f "$dst" 2>/dev/null ||
478b312a94cSmrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
479b312a94cSmrg            { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
480b312a94cSmrg          } ||
481b312a94cSmrg          { echo "$0: cannot unlink or rename $dst" >&2
482b312a94cSmrg            (exit 1); exit 1
483b312a94cSmrg          }
484b312a94cSmrg        } &&
485b312a94cSmrg
486b312a94cSmrg        # Now rename the file to the real destination.
487b312a94cSmrg        $doit $mvcmd "$dsttmp" "$dst"
48865eef222Smrg      }
48965eef222Smrg    fi || exit 1
490ef981d24Smrg
491ef981d24Smrg    trap '' 0
492ef981d24Smrg  fi
493ef981d24Smrgdone
494ef981d24Smrg
495ef981d24Smrg# Local variables:
496ef981d24Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
497ef981d24Smrg# time-stamp-start: "scriptversion="
498ef981d24Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
499b312a94cSmrg# time-stamp-time-zone: "UTC0"
50056957a04Smrg# time-stamp-end: "; # UTC"
501ef981d24Smrg# End:
502