install-sh revision 533545b5
1264fa531Smrg#!/bin/sh
2264fa531Smrg# install - install a program, script, or datafile
3c37a63b8Smrg
4533545b5Smrgscriptversion=2013-12-25.23; # UTC
5c37a63b8Smrg
6264fa531Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7264fa531Smrg# later released in X11R6 (xc/config/util/install.sh) with the
8264fa531Smrg# following copyright and license.
9264fa531Smrg#
10264fa531Smrg# Copyright (C) 1994 X Consortium
11264fa531Smrg#
12264fa531Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13264fa531Smrg# of this software and associated documentation files (the "Software"), to
14264fa531Smrg# deal in the Software without restriction, including without limitation the
15264fa531Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16264fa531Smrg# sell copies of the Software, and to permit persons to whom the Software is
17264fa531Smrg# furnished to do so, subject to the following conditions:
18264fa531Smrg#
19264fa531Smrg# The above copyright notice and this permission notice shall be included in
20264fa531Smrg# all copies or substantial portions of the Software.
21264fa531Smrg#
22264fa531Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23264fa531Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24264fa531Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25264fa531Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26264fa531Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27264fa531Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28264fa531Smrg#
29264fa531Smrg# Except as contained in this notice, the name of the X Consortium shall not
30264fa531Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
31264fa531Smrg# ings in this Software without prior written authorization from the X Consor-
32264fa531Smrg# tium.
33264fa531Smrg#
34264fa531Smrg#
35264fa531Smrg# FSF changes to this file are in the public domain.
36264fa531Smrg#
37264fa531Smrg# Calling this script install-sh is preferred over install.sh, to prevent
38533545b5Smrg# 'make' implicit rules from creating a file called install from it
39264fa531Smrg# when there is no Makefile.
40264fa531Smrg#
41264fa531Smrg# This script is compatible with the BSD install script, but was written
42c37a63b8Smrg# from scratch.
43264fa531Smrg
44533545b5Smrgtab='	'
45c37a63b8Smrgnl='
46c37a63b8Smrg'
47533545b5SmrgIFS=" $tab$nl"
48264fa531Smrg
49533545b5Smrg# Set DOITPROG to "echo" to test this script.
50264fa531Smrg
51c37a63b8Smrgdoit=${DOITPROG-}
52533545b5Smrgdoit_exec=${doit:-exec}
53264fa531Smrg
54c37a63b8Smrg# Put in absolute file names if you don't have them in your path;
55c37a63b8Smrg# or use environment vars.
56c37a63b8Smrg
57c37a63b8Smrgchgrpprog=${CHGRPPROG-chgrp}
58c37a63b8Smrgchmodprog=${CHMODPROG-chmod}
59c37a63b8Smrgchownprog=${CHOWNPROG-chown}
60c37a63b8Smrgcmpprog=${CMPPROG-cmp}
61c37a63b8Smrgcpprog=${CPPROG-cp}
62c37a63b8Smrgmkdirprog=${MKDIRPROG-mkdir}
63c37a63b8Smrgmvprog=${MVPROG-mv}
64c37a63b8Smrgrmprog=${RMPROG-rm}
65c37a63b8Smrgstripprog=${STRIPPROG-strip}
66c37a63b8Smrg
67c37a63b8Smrgposix_mkdir=
68c37a63b8Smrg
69c37a63b8Smrg# Desired mode of installed file.
70c37a63b8Smrgmode=0755
71c37a63b8Smrg
72c37a63b8Smrgchgrpcmd=
73c37a63b8Smrgchmodcmd=$chmodprog
74c37a63b8Smrgchowncmd=
75c37a63b8Smrgmvcmd=$mvprog
76c37a63b8Smrgrmcmd="$rmprog -f"
77c37a63b8Smrgstripcmd=
78264fa531Smrg
79c37a63b8Smrgsrc=
80c37a63b8Smrgdst=
81c37a63b8Smrgdir_arg=
82c37a63b8Smrgdst_arg=
83264fa531Smrg
84c37a63b8Smrgcopy_on_change=false
85533545b5Smrgis_target_a_directory=possibly
86264fa531Smrg
87c37a63b8Smrgusage="\
88c37a63b8SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
89c37a63b8Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
90c37a63b8Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
91c37a63b8Smrg   or: $0 [OPTION]... -d DIRECTORIES...
92264fa531Smrg
93c37a63b8SmrgIn the 1st form, copy SRCFILE to DSTFILE.
94c37a63b8SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
95c37a63b8SmrgIn the 4th, create DIRECTORIES.
96264fa531Smrg
97c37a63b8SmrgOptions:
98c37a63b8Smrg     --help     display this help and exit.
99c37a63b8Smrg     --version  display version info and exit.
100264fa531Smrg
101c37a63b8Smrg  -c            (ignored)
102c37a63b8Smrg  -C            install only if different (preserve the last data modification time)
103c37a63b8Smrg  -d            create directories instead of installing files.
104c37a63b8Smrg  -g GROUP      $chgrpprog installed files to GROUP.
105c37a63b8Smrg  -m MODE       $chmodprog installed files to MODE.
106c37a63b8Smrg  -o USER       $chownprog installed files to USER.
107c37a63b8Smrg  -s            $stripprog installed files.
108c37a63b8Smrg  -t DIRECTORY  install into DIRECTORY.
109c37a63b8Smrg  -T            report an error if DSTFILE is a directory.
110264fa531Smrg
111c37a63b8SmrgEnvironment variables override the default commands:
112c37a63b8Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
113c37a63b8Smrg  RMPROG STRIPPROG
114c37a63b8Smrg"
115264fa531Smrg
116c37a63b8Smrgwhile test $# -ne 0; do
117c37a63b8Smrg  case $1 in
118c37a63b8Smrg    -c) ;;
119264fa531Smrg
120c37a63b8Smrg    -C) copy_on_change=true;;
121264fa531Smrg
122c37a63b8Smrg    -d) dir_arg=true;;
123264fa531Smrg
124c37a63b8Smrg    -g) chgrpcmd="$chgrpprog $2"
125533545b5Smrg        shift;;
126264fa531Smrg
127c37a63b8Smrg    --help) echo "$usage"; exit $?;;
128264fa531Smrg
129c37a63b8Smrg    -m) mode=$2
130533545b5Smrg        case $mode in
131533545b5Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
132533545b5Smrg            echo "$0: invalid mode: $mode" >&2
133533545b5Smrg            exit 1;;
134533545b5Smrg        esac
135533545b5Smrg        shift;;
136264fa531Smrg
137c37a63b8Smrg    -o) chowncmd="$chownprog $2"
138533545b5Smrg        shift;;
139264fa531Smrg
140c37a63b8Smrg    -s) stripcmd=$stripprog;;
141264fa531Smrg
142533545b5Smrg    -t)
143533545b5Smrg        is_target_a_directory=always
144533545b5Smrg        dst_arg=$2
145533545b5Smrg        # Protect names problematic for 'test' and other utilities.
146533545b5Smrg        case $dst_arg in
147533545b5Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
148533545b5Smrg        esac
149533545b5Smrg        shift;;
150264fa531Smrg
151533545b5Smrg    -T) is_target_a_directory=never;;
152264fa531Smrg
153c37a63b8Smrg    --version) echo "$0 $scriptversion"; exit $?;;
154264fa531Smrg
155533545b5Smrg    --) shift
156533545b5Smrg        break;;
157264fa531Smrg
158533545b5Smrg    -*) echo "$0: invalid option: $1" >&2
159533545b5Smrg        exit 1;;
160264fa531Smrg
161c37a63b8Smrg    *)  break;;
162c37a63b8Smrg  esac
163c37a63b8Smrg  shift
164c37a63b8Smrgdone
165264fa531Smrg
166533545b5Smrg# We allow the use of options -d and -T together, by making -d
167533545b5Smrg# take the precedence; this is for compatibility with GNU install.
168533545b5Smrg
169533545b5Smrgif test -n "$dir_arg"; then
170533545b5Smrg  if test -n "$dst_arg"; then
171533545b5Smrg    echo "$0: target directory not allowed when installing a directory." >&2
172533545b5Smrg    exit 1
173533545b5Smrg  fi
174533545b5Smrgfi
175533545b5Smrg
176c37a63b8Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
177c37a63b8Smrg  # When -d is used, all remaining arguments are directories to create.
178c37a63b8Smrg  # When -t is used, the destination is already specified.
179c37a63b8Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
180c37a63b8Smrg  for arg
181c37a63b8Smrg  do
182c37a63b8Smrg    if test -n "$dst_arg"; then
183c37a63b8Smrg      # $@ is not empty: it contains at least $arg.
184c37a63b8Smrg      set fnord "$@" "$dst_arg"
185c37a63b8Smrg      shift # fnord
186c37a63b8Smrg    fi
187c37a63b8Smrg    shift # arg
188c37a63b8Smrg    dst_arg=$arg
189533545b5Smrg    # Protect names problematic for 'test' and other utilities.
190533545b5Smrg    case $dst_arg in
191533545b5Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
192533545b5Smrg    esac
193c37a63b8Smrg  done
194c37a63b8Smrgfi
195264fa531Smrg
196c37a63b8Smrgif test $# -eq 0; then
197c37a63b8Smrg  if test -z "$dir_arg"; then
198c37a63b8Smrg    echo "$0: no input file specified." >&2
199c37a63b8Smrg    exit 1
200c37a63b8Smrg  fi
201533545b5Smrg  # It's OK to call 'install-sh -d' without argument.
202c37a63b8Smrg  # This can happen when creating conditional directories.
203c37a63b8Smrg  exit 0
204c37a63b8Smrgfi
205264fa531Smrg
206c37a63b8Smrgif test -z "$dir_arg"; then
207533545b5Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
208533545b5Smrg    if test ! -d "$dst_arg"; then
209533545b5Smrg      echo "$0: $dst_arg: Is not a directory." >&2
210533545b5Smrg      exit 1
211533545b5Smrg    fi
212533545b5Smrg  fi
213533545b5Smrgfi
214533545b5Smrg
215533545b5Smrgif test -z "$dir_arg"; then
216533545b5Smrg  do_exit='(exit $ret); exit $ret'
217533545b5Smrg  trap "ret=129; $do_exit" 1
218533545b5Smrg  trap "ret=130; $do_exit" 2
219533545b5Smrg  trap "ret=141; $do_exit" 13
220533545b5Smrg  trap "ret=143; $do_exit" 15
221c37a63b8Smrg
222c37a63b8Smrg  # Set umask so as not to create temps with too-generous modes.
223c37a63b8Smrg  # However, 'strip' requires both read and write access to temps.
224c37a63b8Smrg  case $mode in
225c37a63b8Smrg    # Optimize common cases.
226c37a63b8Smrg    *644) cp_umask=133;;
227c37a63b8Smrg    *755) cp_umask=22;;
228c37a63b8Smrg
229c37a63b8Smrg    *[0-7])
230c37a63b8Smrg      if test -z "$stripcmd"; then
231533545b5Smrg        u_plus_rw=
232c37a63b8Smrg      else
233533545b5Smrg        u_plus_rw='% 200'
234c37a63b8Smrg      fi
235c37a63b8Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
236c37a63b8Smrg    *)
237c37a63b8Smrg      if test -z "$stripcmd"; then
238533545b5Smrg        u_plus_rw=
239c37a63b8Smrg      else
240533545b5Smrg        u_plus_rw=,u+rw
241c37a63b8Smrg      fi
242c37a63b8Smrg      cp_umask=$mode$u_plus_rw;;
243c37a63b8Smrg  esac
244c37a63b8Smrgfi
245264fa531Smrg
246c37a63b8Smrgfor src
247c37a63b8Smrgdo
248533545b5Smrg  # Protect names problematic for 'test' and other utilities.
249c37a63b8Smrg  case $src in
250533545b5Smrg    -* | [=\(\)!]) src=./$src;;
251c37a63b8Smrg  esac
252c37a63b8Smrg
253c37a63b8Smrg  if test -n "$dir_arg"; then
254c37a63b8Smrg    dst=$src
255c37a63b8Smrg    dstdir=$dst
256c37a63b8Smrg    test -d "$dstdir"
257c37a63b8Smrg    dstdir_status=$?
258c37a63b8Smrg  else
259c37a63b8Smrg
260c37a63b8Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
261c37a63b8Smrg    # might cause directories to be created, which would be especially bad
262c37a63b8Smrg    # if $src (and thus $dsttmp) contains '*'.
263c37a63b8Smrg    if test ! -f "$src" && test ! -d "$src"; then
264c37a63b8Smrg      echo "$0: $src does not exist." >&2
265c37a63b8Smrg      exit 1
266c37a63b8Smrg    fi
267c37a63b8Smrg
268c37a63b8Smrg    if test -z "$dst_arg"; then
269c37a63b8Smrg      echo "$0: no destination specified." >&2
270c37a63b8Smrg      exit 1
271c37a63b8Smrg    fi
272c37a63b8Smrg    dst=$dst_arg
273264fa531Smrg
274c37a63b8Smrg    # If destination is a directory, append the input filename; won't work
275c37a63b8Smrg    # if double slashes aren't ignored.
276c37a63b8Smrg    if test -d "$dst"; then
277533545b5Smrg      if test "$is_target_a_directory" = never; then
278533545b5Smrg        echo "$0: $dst_arg: Is a directory" >&2
279533545b5Smrg        exit 1
280c37a63b8Smrg      fi
281c37a63b8Smrg      dstdir=$dst
282c37a63b8Smrg      dst=$dstdir/`basename "$src"`
283c37a63b8Smrg      dstdir_status=0
284c37a63b8Smrg    else
285533545b5Smrg      dstdir=`dirname "$dst"`
286c37a63b8Smrg      test -d "$dstdir"
287c37a63b8Smrg      dstdir_status=$?
288c37a63b8Smrg    fi
289c37a63b8Smrg  fi
290c37a63b8Smrg
291c37a63b8Smrg  obsolete_mkdir_used=false
292c37a63b8Smrg
293c37a63b8Smrg  if test $dstdir_status != 0; then
294c37a63b8Smrg    case $posix_mkdir in
295c37a63b8Smrg      '')
296533545b5Smrg        # Create intermediate dirs using mode 755 as modified by the umask.
297533545b5Smrg        # This is like FreeBSD 'install' as of 1997-10-28.
298533545b5Smrg        umask=`umask`
299533545b5Smrg        case $stripcmd.$umask in
300533545b5Smrg          # Optimize common cases.
301533545b5Smrg          *[2367][2367]) mkdir_umask=$umask;;
302533545b5Smrg          .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
303533545b5Smrg
304533545b5Smrg          *[0-7])
305533545b5Smrg            mkdir_umask=`expr $umask + 22 \
306533545b5Smrg              - $umask % 100 % 40 + $umask % 20 \
307533545b5Smrg              - $umask % 10 % 4 + $umask % 2
308533545b5Smrg            `;;
309533545b5Smrg          *) mkdir_umask=$umask,go-w;;
310533545b5Smrg        esac
311533545b5Smrg
312533545b5Smrg        # With -d, create the new directory with the user-specified mode.
313533545b5Smrg        # Otherwise, rely on $mkdir_umask.
314533545b5Smrg        if test -n "$dir_arg"; then
315533545b5Smrg          mkdir_mode=-m$mode
316533545b5Smrg        else
317533545b5Smrg          mkdir_mode=
318533545b5Smrg        fi
319533545b5Smrg
320533545b5Smrg        posix_mkdir=false
321533545b5Smrg        case $umask in
322533545b5Smrg          *[123567][0-7][0-7])
323533545b5Smrg            # POSIX mkdir -p sets u+wx bits regardless of umask, which
324533545b5Smrg            # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
325533545b5Smrg            ;;
326533545b5Smrg          *)
327533545b5Smrg            tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
328533545b5Smrg            trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
329533545b5Smrg
330533545b5Smrg            if (umask $mkdir_umask &&
331533545b5Smrg                exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
332533545b5Smrg            then
333533545b5Smrg              if test -z "$dir_arg" || {
334533545b5Smrg                   # Check for POSIX incompatibilities with -m.
335533545b5Smrg                   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
336533545b5Smrg                   # other-writable bit of parent directory when it shouldn't.
337533545b5Smrg                   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
338533545b5Smrg                   ls_ld_tmpdir=`ls -ld "$tmpdir"`
339533545b5Smrg                   case $ls_ld_tmpdir in
340533545b5Smrg                     d????-?r-*) different_mode=700;;
341533545b5Smrg                     d????-?--*) different_mode=755;;
342533545b5Smrg                     *) false;;
343533545b5Smrg                   esac &&
344533545b5Smrg                   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
345533545b5Smrg                     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
346533545b5Smrg                     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
347533545b5Smrg                   }
348533545b5Smrg                 }
349533545b5Smrg              then posix_mkdir=:
350533545b5Smrg              fi
351533545b5Smrg              rmdir "$tmpdir/d" "$tmpdir"
352533545b5Smrg            else
353533545b5Smrg              # Remove any dirs left behind by ancient mkdir implementations.
354533545b5Smrg              rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
355533545b5Smrg            fi
356533545b5Smrg            trap '' 0;;
357533545b5Smrg        esac;;
358c37a63b8Smrg    esac
359264fa531Smrg
360c37a63b8Smrg    if
361c37a63b8Smrg      $posix_mkdir && (
362533545b5Smrg        umask $mkdir_umask &&
363533545b5Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
364c37a63b8Smrg      )
365c37a63b8Smrg    then :
366c37a63b8Smrg    else
367c37a63b8Smrg
368c37a63b8Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
369c37a63b8Smrg      # or it failed possibly due to a race condition.  Create the
370c37a63b8Smrg      # directory the slow way, step by step, checking for races as we go.
371c37a63b8Smrg
372c37a63b8Smrg      case $dstdir in
373533545b5Smrg        /*) prefix='/';;
374533545b5Smrg        [-=\(\)!]*) prefix='./';;
375533545b5Smrg        *)  prefix='';;
376c37a63b8Smrg      esac
377c37a63b8Smrg
378c37a63b8Smrg      oIFS=$IFS
379c37a63b8Smrg      IFS=/
380533545b5Smrg      set -f
381c37a63b8Smrg      set fnord $dstdir
382c37a63b8Smrg      shift
383533545b5Smrg      set +f
384c37a63b8Smrg      IFS=$oIFS
385c37a63b8Smrg
386c37a63b8Smrg      prefixes=
387c37a63b8Smrg
388c37a63b8Smrg      for d
389c37a63b8Smrg      do
390533545b5Smrg        test X"$d" = X && continue
391533545b5Smrg
392533545b5Smrg        prefix=$prefix$d
393533545b5Smrg        if test -d "$prefix"; then
394533545b5Smrg          prefixes=
395533545b5Smrg        else
396533545b5Smrg          if $posix_mkdir; then
397533545b5Smrg            (umask=$mkdir_umask &&
398533545b5Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
399533545b5Smrg            # Don't fail if two instances are running concurrently.
400533545b5Smrg            test -d "$prefix" || exit 1
401533545b5Smrg          else
402533545b5Smrg            case $prefix in
403533545b5Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
404533545b5Smrg              *) qprefix=$prefix;;
405533545b5Smrg            esac
406533545b5Smrg            prefixes="$prefixes '$qprefix'"
407533545b5Smrg          fi
408533545b5Smrg        fi
409533545b5Smrg        prefix=$prefix/
410c37a63b8Smrg      done
411c37a63b8Smrg
412c37a63b8Smrg      if test -n "$prefixes"; then
413533545b5Smrg        # Don't fail if two instances are running concurrently.
414533545b5Smrg        (umask $mkdir_umask &&
415533545b5Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
416533545b5Smrg          test -d "$dstdir" || exit 1
417533545b5Smrg        obsolete_mkdir_used=true
418c37a63b8Smrg      fi
419c37a63b8Smrg    fi
420c37a63b8Smrg  fi
421c37a63b8Smrg
422c37a63b8Smrg  if test -n "$dir_arg"; then
423c37a63b8Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
424c37a63b8Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
425c37a63b8Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
426c37a63b8Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
427c37a63b8Smrg  else
428c37a63b8Smrg
429c37a63b8Smrg    # Make a couple of temp file names in the proper directory.
430c37a63b8Smrg    dsttmp=$dstdir/_inst.$$_
431c37a63b8Smrg    rmtmp=$dstdir/_rm.$$_
432c37a63b8Smrg
433c37a63b8Smrg    # Trap to clean up those temp files at exit.
434c37a63b8Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
435c37a63b8Smrg
436c37a63b8Smrg    # Copy the file name to the temp name.
437c37a63b8Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
438c37a63b8Smrg
439c37a63b8Smrg    # and set any options; do chmod last to preserve setuid bits.
440c37a63b8Smrg    #
441c37a63b8Smrg    # If any of these fail, we abort the whole thing.  If we want to
442c37a63b8Smrg    # ignore errors from any of these, just make sure not to ignore
443c37a63b8Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
444c37a63b8Smrg    #
445c37a63b8Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
446c37a63b8Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
447c37a63b8Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
448c37a63b8Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
449c37a63b8Smrg
450c37a63b8Smrg    # If -C, don't bother to copy if it wouldn't change the file.
451c37a63b8Smrg    if $copy_on_change &&
452533545b5Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
453533545b5Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
454533545b5Smrg       set -f &&
455c37a63b8Smrg       set X $old && old=:$2:$4:$5:$6 &&
456c37a63b8Smrg       set X $new && new=:$2:$4:$5:$6 &&
457533545b5Smrg       set +f &&
458c37a63b8Smrg       test "$old" = "$new" &&
459c37a63b8Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
460c37a63b8Smrg    then
461c37a63b8Smrg      rm -f "$dsttmp"
462c37a63b8Smrg    else
463c37a63b8Smrg      # Rename the file to the real destination.
464c37a63b8Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
465c37a63b8Smrg
466c37a63b8Smrg      # The rename failed, perhaps because mv can't rename something else
467c37a63b8Smrg      # to itself, or perhaps because mv is so ancient that it does not
468c37a63b8Smrg      # support -f.
469c37a63b8Smrg      {
470533545b5Smrg        # Now remove or move aside any old file at destination location.
471533545b5Smrg        # We try this two ways since rm can't unlink itself on some
472533545b5Smrg        # systems and the destination file might be busy for other
473533545b5Smrg        # reasons.  In this case, the final cleanup might fail but the new
474533545b5Smrg        # file should still install successfully.
475533545b5Smrg        {
476533545b5Smrg          test ! -f "$dst" ||
477533545b5Smrg          $doit $rmcmd -f "$dst" 2>/dev/null ||
478533545b5Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
479533545b5Smrg            { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
480533545b5Smrg          } ||
481533545b5Smrg          { echo "$0: cannot unlink or rename $dst" >&2
482533545b5Smrg            (exit 1); exit 1
483533545b5Smrg          }
484533545b5Smrg        } &&
485533545b5Smrg
486533545b5Smrg        # Now rename the file to the real destination.
487533545b5Smrg        $doit $mvcmd "$dsttmp" "$dst"
488c37a63b8Smrg      }
489c37a63b8Smrg    fi || exit 1
490c37a63b8Smrg
491c37a63b8Smrg    trap '' 0
492c37a63b8Smrg  fi
493c37a63b8Smrgdone
494264fa531Smrg
495c37a63b8Smrg# Local variables:
496c37a63b8Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
497c37a63b8Smrg# time-stamp-start: "scriptversion="
498c37a63b8Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
499c37a63b8Smrg# time-stamp-time-zone: "UTC"
500c37a63b8Smrg# time-stamp-end: "; # UTC"
501c37a63b8Smrg# End:
502