install-sh revision 891601f5
1d4401354Smrg#!/bin/sh
2d4401354Smrg# install - install a program, script, or datafile
3d4401354Smrg
4891601f5Smrgscriptversion=2014-09-12.12; # UTC
5d4401354Smrg
6d4401354Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7d4401354Smrg# later released in X11R6 (xc/config/util/install.sh) with the
8d4401354Smrg# following copyright and license.
9d4401354Smrg#
10d4401354Smrg# Copyright (C) 1994 X Consortium
11d4401354Smrg#
12d4401354Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13d4401354Smrg# of this software and associated documentation files (the "Software"), to
14d4401354Smrg# deal in the Software without restriction, including without limitation the
15d4401354Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16d4401354Smrg# sell copies of the Software, and to permit persons to whom the Software is
17d4401354Smrg# furnished to do so, subject to the following conditions:
18d4401354Smrg#
19d4401354Smrg# The above copyright notice and this permission notice shall be included in
20d4401354Smrg# all copies or substantial portions of the Software.
21d4401354Smrg#
22d4401354Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23d4401354Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24d4401354Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25d4401354Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26d4401354Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27d4401354Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28d4401354Smrg#
29d4401354Smrg# Except as contained in this notice, the name of the X Consortium shall not
30d4401354Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
31d4401354Smrg# ings in this Software without prior written authorization from the X Consor-
32d4401354Smrg# tium.
33d4401354Smrg#
34d4401354Smrg#
35d4401354Smrg# FSF changes to this file are in the public domain.
36d4401354Smrg#
37d4401354Smrg# Calling this script install-sh is preferred over install.sh, to prevent
38f591e195Smrg# 'make' implicit rules from creating a file called install from it
39d4401354Smrg# when there is no Makefile.
40d4401354Smrg#
41d4401354Smrg# This script is compatible with the BSD install script, but was written
42d4401354Smrg# from scratch.
43d4401354Smrg
44891601f5Smrgtab='	'
45d4401354Smrgnl='
46d4401354Smrg'
47891601f5SmrgIFS=" $tab$nl"
48d4401354Smrg
49891601f5Smrg# Set DOITPROG to "echo" to test this script.
50d4401354Smrg
51d4401354Smrgdoit=${DOITPROG-}
52891601f5Smrgdoit_exec=${doit:-exec}
53d4401354Smrg
54d4401354Smrg# Put in absolute file names if you don't have them in your path;
55d4401354Smrg# or use environment vars.
56d4401354Smrg
57d4401354Smrgchgrpprog=${CHGRPPROG-chgrp}
58d4401354Smrgchmodprog=${CHMODPROG-chmod}
59d4401354Smrgchownprog=${CHOWNPROG-chown}
60d4401354Smrgcmpprog=${CMPPROG-cmp}
61d4401354Smrgcpprog=${CPPROG-cp}
62d4401354Smrgmkdirprog=${MKDIRPROG-mkdir}
63d4401354Smrgmvprog=${MVPROG-mv}
64d4401354Smrgrmprog=${RMPROG-rm}
65d4401354Smrgstripprog=${STRIPPROG-strip}
66d4401354Smrg
67d4401354Smrgposix_mkdir=
68d4401354Smrg
69d4401354Smrg# Desired mode of installed file.
70d4401354Smrgmode=0755
71d4401354Smrg
72d4401354Smrgchgrpcmd=
73d4401354Smrgchmodcmd=$chmodprog
74d4401354Smrgchowncmd=
75d4401354Smrgmvcmd=$mvprog
76d4401354Smrgrmcmd="$rmprog -f"
77d4401354Smrgstripcmd=
78d4401354Smrg
79d4401354Smrgsrc=
80d4401354Smrgdst=
81d4401354Smrgdir_arg=
82d4401354Smrgdst_arg=
83d4401354Smrg
84d4401354Smrgcopy_on_change=false
85891601f5Smrgis_target_a_directory=possibly
86d4401354Smrg
87d4401354Smrgusage="\
88d4401354SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
89d4401354Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
90d4401354Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
91d4401354Smrg   or: $0 [OPTION]... -d DIRECTORIES...
92d4401354Smrg
93d4401354SmrgIn the 1st form, copy SRCFILE to DSTFILE.
94d4401354SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
95d4401354SmrgIn the 4th, create DIRECTORIES.
96d4401354Smrg
97d4401354SmrgOptions:
98d4401354Smrg     --help     display this help and exit.
99d4401354Smrg     --version  display version info and exit.
100d4401354Smrg
101d4401354Smrg  -c            (ignored)
102d4401354Smrg  -C            install only if different (preserve the last data modification time)
103d4401354Smrg  -d            create directories instead of installing files.
104d4401354Smrg  -g GROUP      $chgrpprog installed files to GROUP.
105d4401354Smrg  -m MODE       $chmodprog installed files to MODE.
106d4401354Smrg  -o USER       $chownprog installed files to USER.
107d4401354Smrg  -s            $stripprog installed files.
108d4401354Smrg  -t DIRECTORY  install into DIRECTORY.
109d4401354Smrg  -T            report an error if DSTFILE is a directory.
110d4401354Smrg
111d4401354SmrgEnvironment variables override the default commands:
112d4401354Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
113d4401354Smrg  RMPROG STRIPPROG
114d4401354Smrg"
115d4401354Smrg
116d4401354Smrgwhile test $# -ne 0; do
117d4401354Smrg  case $1 in
118d4401354Smrg    -c) ;;
119d4401354Smrg
120d4401354Smrg    -C) copy_on_change=true;;
121d4401354Smrg
122d4401354Smrg    -d) dir_arg=true;;
123d4401354Smrg
124d4401354Smrg    -g) chgrpcmd="$chgrpprog $2"
125891601f5Smrg        shift;;
126d4401354Smrg
127d4401354Smrg    --help) echo "$usage"; exit $?;;
128d4401354Smrg
129d4401354Smrg    -m) mode=$2
130891601f5Smrg        case $mode in
131891601f5Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
132891601f5Smrg            echo "$0: invalid mode: $mode" >&2
133891601f5Smrg            exit 1;;
134891601f5Smrg        esac
135891601f5Smrg        shift;;
136d4401354Smrg
137d4401354Smrg    -o) chowncmd="$chownprog $2"
138891601f5Smrg        shift;;
139d4401354Smrg
140d4401354Smrg    -s) stripcmd=$stripprog;;
141d4401354Smrg
142891601f5Smrg    -t)
143891601f5Smrg        is_target_a_directory=always
144891601f5Smrg        dst_arg=$2
145891601f5Smrg        # Protect names problematic for 'test' and other utilities.
146891601f5Smrg        case $dst_arg in
147891601f5Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
148891601f5Smrg        esac
149891601f5Smrg        shift;;
150d4401354Smrg
151891601f5Smrg    -T) is_target_a_directory=never;;
152d4401354Smrg
153d4401354Smrg    --version) echo "$0 $scriptversion"; exit $?;;
154d4401354Smrg
155891601f5Smrg    --) shift
156891601f5Smrg        break;;
157d4401354Smrg
158891601f5Smrg    -*) echo "$0: invalid option: $1" >&2
159891601f5Smrg        exit 1;;
160d4401354Smrg
161d4401354Smrg    *)  break;;
162d4401354Smrg  esac
163d4401354Smrg  shift
164d4401354Smrgdone
165d4401354Smrg
166891601f5Smrg# We allow the use of options -d and -T together, by making -d
167891601f5Smrg# take the precedence; this is for compatibility with GNU install.
168891601f5Smrg
169891601f5Smrgif test -n "$dir_arg"; then
170891601f5Smrg  if test -n "$dst_arg"; then
171891601f5Smrg    echo "$0: target directory not allowed when installing a directory." >&2
172891601f5Smrg    exit 1
173891601f5Smrg  fi
174891601f5Smrgfi
175891601f5Smrg
176d4401354Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
177d4401354Smrg  # When -d is used, all remaining arguments are directories to create.
178d4401354Smrg  # When -t is used, the destination is already specified.
179d4401354Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
180d4401354Smrg  for arg
181d4401354Smrg  do
182d4401354Smrg    if test -n "$dst_arg"; then
183d4401354Smrg      # $@ is not empty: it contains at least $arg.
184d4401354Smrg      set fnord "$@" "$dst_arg"
185d4401354Smrg      shift # fnord
186d4401354Smrg    fi
187d4401354Smrg    shift # arg
188d4401354Smrg    dst_arg=$arg
189f591e195Smrg    # Protect names problematic for 'test' and other utilities.
190986c8b3dSmrg    case $dst_arg in
191986c8b3dSmrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
192986c8b3dSmrg    esac
193d4401354Smrg  done
194d4401354Smrgfi
195d4401354Smrg
196d4401354Smrgif test $# -eq 0; then
197d4401354Smrg  if test -z "$dir_arg"; then
198d4401354Smrg    echo "$0: no input file specified." >&2
199d4401354Smrg    exit 1
200d4401354Smrg  fi
201f591e195Smrg  # It's OK to call 'install-sh -d' without argument.
202d4401354Smrg  # This can happen when creating conditional directories.
203d4401354Smrg  exit 0
204d4401354Smrgfi
205d4401354Smrg
206891601f5Smrgif test -z "$dir_arg"; then
207891601f5Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
208891601f5Smrg    if test ! -d "$dst_arg"; then
209891601f5Smrg      echo "$0: $dst_arg: Is not a directory." >&2
210891601f5Smrg      exit 1
211891601f5Smrg    fi
212891601f5Smrg  fi
213891601f5Smrgfi
214891601f5Smrg
215d4401354Smrgif test -z "$dir_arg"; then
216986c8b3dSmrg  do_exit='(exit $ret); exit $ret'
217986c8b3dSmrg  trap "ret=129; $do_exit" 1
218986c8b3dSmrg  trap "ret=130; $do_exit" 2
219986c8b3dSmrg  trap "ret=141; $do_exit" 13
220986c8b3dSmrg  trap "ret=143; $do_exit" 15
221d4401354Smrg
222d4401354Smrg  # Set umask so as not to create temps with too-generous modes.
223d4401354Smrg  # However, 'strip' requires both read and write access to temps.
224d4401354Smrg  case $mode in
225d4401354Smrg    # Optimize common cases.
226d4401354Smrg    *644) cp_umask=133;;
227d4401354Smrg    *755) cp_umask=22;;
228d4401354Smrg
229d4401354Smrg    *[0-7])
230d4401354Smrg      if test -z "$stripcmd"; then
231891601f5Smrg        u_plus_rw=
232d4401354Smrg      else
233891601f5Smrg        u_plus_rw='% 200'
234d4401354Smrg      fi
235d4401354Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
236d4401354Smrg    *)
237d4401354Smrg      if test -z "$stripcmd"; then
238891601f5Smrg        u_plus_rw=
239d4401354Smrg      else
240891601f5Smrg        u_plus_rw=,u+rw
241d4401354Smrg      fi
242d4401354Smrg      cp_umask=$mode$u_plus_rw;;
243d4401354Smrg  esac
244d4401354Smrgfi
245d4401354Smrg
246d4401354Smrgfor src
247d4401354Smrgdo
248f591e195Smrg  # Protect names problematic for 'test' and other utilities.
249d4401354Smrg  case $src in
250986c8b3dSmrg    -* | [=\(\)!]) src=./$src;;
251d4401354Smrg  esac
252d4401354Smrg
253d4401354Smrg  if test -n "$dir_arg"; then
254d4401354Smrg    dst=$src
255d4401354Smrg    dstdir=$dst
256d4401354Smrg    test -d "$dstdir"
257d4401354Smrg    dstdir_status=$?
258d4401354Smrg  else
259d4401354Smrg
260d4401354Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
261d4401354Smrg    # might cause directories to be created, which would be especially bad
262d4401354Smrg    # if $src (and thus $dsttmp) contains '*'.
263d4401354Smrg    if test ! -f "$src" && test ! -d "$src"; then
264d4401354Smrg      echo "$0: $src does not exist." >&2
265d4401354Smrg      exit 1
266d4401354Smrg    fi
267d4401354Smrg
268d4401354Smrg    if test -z "$dst_arg"; then
269d4401354Smrg      echo "$0: no destination specified." >&2
270d4401354Smrg      exit 1
271d4401354Smrg    fi
272d4401354Smrg    dst=$dst_arg
273d4401354Smrg
274d4401354Smrg    # If destination is a directory, append the input filename; won't work
275d4401354Smrg    # if double slashes aren't ignored.
276d4401354Smrg    if test -d "$dst"; then
277891601f5Smrg      if test "$is_target_a_directory" = never; then
278891601f5Smrg        echo "$0: $dst_arg: Is a directory" >&2
279891601f5Smrg        exit 1
280d4401354Smrg      fi
281d4401354Smrg      dstdir=$dst
282d4401354Smrg      dst=$dstdir/`basename "$src"`
283d4401354Smrg      dstdir_status=0
284d4401354Smrg    else
285891601f5Smrg      dstdir=`dirname "$dst"`
286d4401354Smrg      test -d "$dstdir"
287d4401354Smrg      dstdir_status=$?
288d4401354Smrg    fi
289d4401354Smrg  fi
290d4401354Smrg
291d4401354Smrg  obsolete_mkdir_used=false
292d4401354Smrg
293d4401354Smrg  if test $dstdir_status != 0; then
294d4401354Smrg    case $posix_mkdir in
295d4401354Smrg      '')
296891601f5Smrg        # Create intermediate dirs using mode 755 as modified by the umask.
297891601f5Smrg        # This is like FreeBSD 'install' as of 1997-10-28.
298891601f5Smrg        umask=`umask`
299891601f5Smrg        case $stripcmd.$umask in
300891601f5Smrg          # Optimize common cases.
301891601f5Smrg          *[2367][2367]) mkdir_umask=$umask;;
302891601f5Smrg          .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
303891601f5Smrg
304891601f5Smrg          *[0-7])
305891601f5Smrg            mkdir_umask=`expr $umask + 22 \
306891601f5Smrg              - $umask % 100 % 40 + $umask % 20 \
307891601f5Smrg              - $umask % 10 % 4 + $umask % 2
308891601f5Smrg            `;;
309891601f5Smrg          *) mkdir_umask=$umask,go-w;;
310891601f5Smrg        esac
311891601f5Smrg
312891601f5Smrg        # With -d, create the new directory with the user-specified mode.
313891601f5Smrg        # Otherwise, rely on $mkdir_umask.
314891601f5Smrg        if test -n "$dir_arg"; then
315891601f5Smrg          mkdir_mode=-m$mode
316891601f5Smrg        else
317891601f5Smrg          mkdir_mode=
318891601f5Smrg        fi
319891601f5Smrg
320891601f5Smrg        posix_mkdir=false
321891601f5Smrg        case $umask in
322891601f5Smrg          *[123567][0-7][0-7])
323891601f5Smrg            # POSIX mkdir -p sets u+wx bits regardless of umask, which
324891601f5Smrg            # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
325891601f5Smrg            ;;
326891601f5Smrg          *)
327891601f5Smrg            # $RANDOM is not portable (e.g. dash);  use it when possible to
328891601f5Smrg            # lower collision chance
329891601f5Smrg            tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
330891601f5Smrg            trap 'ret=$?; rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null; exit $ret' 0
331891601f5Smrg
332891601f5Smrg            # As "mkdir -p" follows symlinks and we work in /tmp possibly;  so
333891601f5Smrg            # create the $tmpdir first (and fail if unsuccessful) to make sure
334891601f5Smrg            # that nobody tries to guess the $tmpdir name.
335891601f5Smrg            if (umask $mkdir_umask &&
336891601f5Smrg                $mkdirprog $mkdir_mode "$tmpdir" &&
337891601f5Smrg                exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
338891601f5Smrg            then
339891601f5Smrg              if test -z "$dir_arg" || {
340891601f5Smrg                   # Check for POSIX incompatibilities with -m.
341891601f5Smrg                   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
342891601f5Smrg                   # other-writable bit of parent directory when it shouldn't.
343891601f5Smrg                   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
344891601f5Smrg                   test_tmpdir="$tmpdir/a"
345891601f5Smrg                   ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
346891601f5Smrg                   case $ls_ld_tmpdir in
347891601f5Smrg                     d????-?r-*) different_mode=700;;
348891601f5Smrg                     d????-?--*) different_mode=755;;
349891601f5Smrg                     *) false;;
350891601f5Smrg                   esac &&
351891601f5Smrg                   $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
352891601f5Smrg                     ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
353891601f5Smrg                     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
354891601f5Smrg                   }
355891601f5Smrg                 }
356891601f5Smrg              then posix_mkdir=:
357891601f5Smrg              fi
358891601f5Smrg              rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
359891601f5Smrg            else
360891601f5Smrg              # Remove any dirs left behind by ancient mkdir implementations.
361891601f5Smrg              rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
362891601f5Smrg            fi
363891601f5Smrg            trap '' 0;;
364891601f5Smrg        esac;;
365d4401354Smrg    esac
366d4401354Smrg
367d4401354Smrg    if
368d4401354Smrg      $posix_mkdir && (
369891601f5Smrg        umask $mkdir_umask &&
370891601f5Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
371d4401354Smrg      )
372d4401354Smrg    then :
373d4401354Smrg    else
374d4401354Smrg
375d4401354Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
376d4401354Smrg      # or it failed possibly due to a race condition.  Create the
377d4401354Smrg      # directory the slow way, step by step, checking for races as we go.
378d4401354Smrg
379d4401354Smrg      case $dstdir in
380891601f5Smrg        /*) prefix='/';;
381891601f5Smrg        [-=\(\)!]*) prefix='./';;
382891601f5Smrg        *)  prefix='';;
383d4401354Smrg      esac
384d4401354Smrg
385d4401354Smrg      oIFS=$IFS
386d4401354Smrg      IFS=/
387891601f5Smrg      set -f
388d4401354Smrg      set fnord $dstdir
389d4401354Smrg      shift
390891601f5Smrg      set +f
391d4401354Smrg      IFS=$oIFS
392d4401354Smrg
393d4401354Smrg      prefixes=
394d4401354Smrg
395d4401354Smrg      for d
396d4401354Smrg      do
397891601f5Smrg        test X"$d" = X && continue
398891601f5Smrg
399891601f5Smrg        prefix=$prefix$d
400891601f5Smrg        if test -d "$prefix"; then
401891601f5Smrg          prefixes=
402891601f5Smrg        else
403891601f5Smrg          if $posix_mkdir; then
404891601f5Smrg            (umask=$mkdir_umask &&
405891601f5Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
406891601f5Smrg            # Don't fail if two instances are running concurrently.
407891601f5Smrg            test -d "$prefix" || exit 1
408891601f5Smrg          else
409891601f5Smrg            case $prefix in
410891601f5Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
411891601f5Smrg              *) qprefix=$prefix;;
412891601f5Smrg            esac
413891601f5Smrg            prefixes="$prefixes '$qprefix'"
414891601f5Smrg          fi
415891601f5Smrg        fi
416891601f5Smrg        prefix=$prefix/
417d4401354Smrg      done
418d4401354Smrg
419d4401354Smrg      if test -n "$prefixes"; then
420891601f5Smrg        # Don't fail if two instances are running concurrently.
421891601f5Smrg        (umask $mkdir_umask &&
422891601f5Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
423891601f5Smrg          test -d "$dstdir" || exit 1
424891601f5Smrg        obsolete_mkdir_used=true
425d4401354Smrg      fi
426d4401354Smrg    fi
427d4401354Smrg  fi
428d4401354Smrg
429d4401354Smrg  if test -n "$dir_arg"; then
430d4401354Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
431d4401354Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
432d4401354Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
433d4401354Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
434d4401354Smrg  else
435d4401354Smrg
436d4401354Smrg    # Make a couple of temp file names in the proper directory.
437d4401354Smrg    dsttmp=$dstdir/_inst.$$_
438d4401354Smrg    rmtmp=$dstdir/_rm.$$_
439d4401354Smrg
440d4401354Smrg    # Trap to clean up those temp files at exit.
441d4401354Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
442d4401354Smrg
443d4401354Smrg    # Copy the file name to the temp name.
444d4401354Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
445d4401354Smrg
446d4401354Smrg    # and set any options; do chmod last to preserve setuid bits.
447d4401354Smrg    #
448d4401354Smrg    # If any of these fail, we abort the whole thing.  If we want to
449d4401354Smrg    # ignore errors from any of these, just make sure not to ignore
450d4401354Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
451d4401354Smrg    #
452d4401354Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
453d4401354Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
454d4401354Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
455d4401354Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
456d4401354Smrg
457d4401354Smrg    # If -C, don't bother to copy if it wouldn't change the file.
458d4401354Smrg    if $copy_on_change &&
459891601f5Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
460891601f5Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
461891601f5Smrg       set -f &&
462d4401354Smrg       set X $old && old=:$2:$4:$5:$6 &&
463d4401354Smrg       set X $new && new=:$2:$4:$5:$6 &&
464891601f5Smrg       set +f &&
465d4401354Smrg       test "$old" = "$new" &&
466d4401354Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
467d4401354Smrg    then
468d4401354Smrg      rm -f "$dsttmp"
469d4401354Smrg    else
470d4401354Smrg      # Rename the file to the real destination.
471d4401354Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
472d4401354Smrg
473d4401354Smrg      # The rename failed, perhaps because mv can't rename something else
474d4401354Smrg      # to itself, or perhaps because mv is so ancient that it does not
475d4401354Smrg      # support -f.
476d4401354Smrg      {
477891601f5Smrg        # Now remove or move aside any old file at destination location.
478891601f5Smrg        # We try this two ways since rm can't unlink itself on some
479891601f5Smrg        # systems and the destination file might be busy for other
480891601f5Smrg        # reasons.  In this case, the final cleanup might fail but the new
481891601f5Smrg        # file should still install successfully.
482891601f5Smrg        {
483891601f5Smrg          test ! -f "$dst" ||
484891601f5Smrg          $doit $rmcmd -f "$dst" 2>/dev/null ||
485891601f5Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
486891601f5Smrg            { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
487891601f5Smrg          } ||
488891601f5Smrg          { echo "$0: cannot unlink or rename $dst" >&2
489891601f5Smrg            (exit 1); exit 1
490891601f5Smrg          }
491891601f5Smrg        } &&
492891601f5Smrg
493891601f5Smrg        # Now rename the file to the real destination.
494891601f5Smrg        $doit $mvcmd "$dsttmp" "$dst"
495d4401354Smrg      }
496d4401354Smrg    fi || exit 1
497d4401354Smrg
498d4401354Smrg    trap '' 0
499d4401354Smrg  fi
500d4401354Smrgdone
501d4401354Smrg
502d4401354Smrg# Local variables:
503d4401354Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
504d4401354Smrg# time-stamp-start: "scriptversion="
505d4401354Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
506d4401354Smrg# time-stamp-time-zone: "UTC"
507d4401354Smrg# time-stamp-end: "; # UTC"
508d4401354Smrg# End:
509