install-sh revision e1670675
1129be1b7Smrg#!/bin/sh
2129be1b7Smrg# install - install a program, script, or datafile
3a9a39c43Smrg
4e1670675Smrgscriptversion=2013-12-25.23; # UTC
5a9a39c43Smrg
6129be1b7Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7129be1b7Smrg# later released in X11R6 (xc/config/util/install.sh) with the
8129be1b7Smrg# following copyright and license.
9129be1b7Smrg#
10129be1b7Smrg# Copyright (C) 1994 X Consortium
11129be1b7Smrg#
12129be1b7Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13129be1b7Smrg# of this software and associated documentation files (the "Software"), to
14129be1b7Smrg# deal in the Software without restriction, including without limitation the
15129be1b7Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16129be1b7Smrg# sell copies of the Software, and to permit persons to whom the Software is
17129be1b7Smrg# furnished to do so, subject to the following conditions:
18129be1b7Smrg#
19129be1b7Smrg# The above copyright notice and this permission notice shall be included in
20129be1b7Smrg# all copies or substantial portions of the Software.
21129be1b7Smrg#
22129be1b7Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23129be1b7Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24129be1b7Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25129be1b7Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26129be1b7Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27129be1b7Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28129be1b7Smrg#
29129be1b7Smrg# Except as contained in this notice, the name of the X Consortium shall not
30129be1b7Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
31129be1b7Smrg# ings in this Software without prior written authorization from the X Consor-
32129be1b7Smrg# tium.
33129be1b7Smrg#
34129be1b7Smrg#
35129be1b7Smrg# FSF changes to this file are in the public domain.
36129be1b7Smrg#
37129be1b7Smrg# Calling this script install-sh is preferred over install.sh, to prevent
38e1670675Smrg# 'make' implicit rules from creating a file called install from it
39129be1b7Smrg# when there is no Makefile.
40129be1b7Smrg#
41129be1b7Smrg# This script is compatible with the BSD install script, but was written
42a9a39c43Smrg# from scratch.
43129be1b7Smrg
44e1670675Smrgtab='	'
45a9a39c43Smrgnl='
46a9a39c43Smrg'
47e1670675SmrgIFS=" $tab$nl"
48129be1b7Smrg
49e1670675Smrg# Set DOITPROG to "echo" to test this script.
50129be1b7Smrg
51a9a39c43Smrgdoit=${DOITPROG-}
52e1670675Smrgdoit_exec=${doit:-exec}
53129be1b7Smrg
54a9a39c43Smrg# Put in absolute file names if you don't have them in your path;
55a9a39c43Smrg# or use environment vars.
56a9a39c43Smrg
57a9a39c43Smrgchgrpprog=${CHGRPPROG-chgrp}
58a9a39c43Smrgchmodprog=${CHMODPROG-chmod}
59a9a39c43Smrgchownprog=${CHOWNPROG-chown}
60a9a39c43Smrgcmpprog=${CMPPROG-cmp}
61a9a39c43Smrgcpprog=${CPPROG-cp}
62a9a39c43Smrgmkdirprog=${MKDIRPROG-mkdir}
63a9a39c43Smrgmvprog=${MVPROG-mv}
64a9a39c43Smrgrmprog=${RMPROG-rm}
65a9a39c43Smrgstripprog=${STRIPPROG-strip}
66a9a39c43Smrg
67a9a39c43Smrgposix_mkdir=
68a9a39c43Smrg
69a9a39c43Smrg# Desired mode of installed file.
70a9a39c43Smrgmode=0755
71a9a39c43Smrg
72a9a39c43Smrgchgrpcmd=
73a9a39c43Smrgchmodcmd=$chmodprog
74a9a39c43Smrgchowncmd=
75a9a39c43Smrgmvcmd=$mvprog
76a9a39c43Smrgrmcmd="$rmprog -f"
77a9a39c43Smrgstripcmd=
78129be1b7Smrg
79a9a39c43Smrgsrc=
80a9a39c43Smrgdst=
81a9a39c43Smrgdir_arg=
82a9a39c43Smrgdst_arg=
83129be1b7Smrg
84a9a39c43Smrgcopy_on_change=false
85e1670675Smrgis_target_a_directory=possibly
86129be1b7Smrg
87a9a39c43Smrgusage="\
88a9a39c43SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
89a9a39c43Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
90a9a39c43Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
91a9a39c43Smrg   or: $0 [OPTION]... -d DIRECTORIES...
92129be1b7Smrg
93a9a39c43SmrgIn the 1st form, copy SRCFILE to DSTFILE.
94a9a39c43SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
95a9a39c43SmrgIn the 4th, create DIRECTORIES.
96129be1b7Smrg
97a9a39c43SmrgOptions:
98a9a39c43Smrg     --help     display this help and exit.
99a9a39c43Smrg     --version  display version info and exit.
100129be1b7Smrg
101a9a39c43Smrg  -c            (ignored)
102a9a39c43Smrg  -C            install only if different (preserve the last data modification time)
103a9a39c43Smrg  -d            create directories instead of installing files.
104a9a39c43Smrg  -g GROUP      $chgrpprog installed files to GROUP.
105a9a39c43Smrg  -m MODE       $chmodprog installed files to MODE.
106a9a39c43Smrg  -o USER       $chownprog installed files to USER.
107a9a39c43Smrg  -s            $stripprog installed files.
108a9a39c43Smrg  -t DIRECTORY  install into DIRECTORY.
109a9a39c43Smrg  -T            report an error if DSTFILE is a directory.
110129be1b7Smrg
111a9a39c43SmrgEnvironment variables override the default commands:
112a9a39c43Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
113a9a39c43Smrg  RMPROG STRIPPROG
114a9a39c43Smrg"
115129be1b7Smrg
116a9a39c43Smrgwhile test $# -ne 0; do
117a9a39c43Smrg  case $1 in
118a9a39c43Smrg    -c) ;;
119129be1b7Smrg
120a9a39c43Smrg    -C) copy_on_change=true;;
121129be1b7Smrg
122a9a39c43Smrg    -d) dir_arg=true;;
123129be1b7Smrg
124a9a39c43Smrg    -g) chgrpcmd="$chgrpprog $2"
125e1670675Smrg        shift;;
126129be1b7Smrg
127a9a39c43Smrg    --help) echo "$usage"; exit $?;;
128129be1b7Smrg
129a9a39c43Smrg    -m) mode=$2
130e1670675Smrg        case $mode in
131e1670675Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
132e1670675Smrg            echo "$0: invalid mode: $mode" >&2
133e1670675Smrg            exit 1;;
134e1670675Smrg        esac
135e1670675Smrg        shift;;
136129be1b7Smrg
137a9a39c43Smrg    -o) chowncmd="$chownprog $2"
138e1670675Smrg        shift;;
139129be1b7Smrg
140a9a39c43Smrg    -s) stripcmd=$stripprog;;
141129be1b7Smrg
142e1670675Smrg    -t)
143e1670675Smrg        is_target_a_directory=always
144e1670675Smrg        dst_arg=$2
145e1670675Smrg        # Protect names problematic for 'test' and other utilities.
146e1670675Smrg        case $dst_arg in
147e1670675Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
148e1670675Smrg        esac
149e1670675Smrg        shift;;
150129be1b7Smrg
151e1670675Smrg    -T) is_target_a_directory=never;;
152129be1b7Smrg
153a9a39c43Smrg    --version) echo "$0 $scriptversion"; exit $?;;
154129be1b7Smrg
155e1670675Smrg    --) shift
156e1670675Smrg        break;;
157129be1b7Smrg
158e1670675Smrg    -*) echo "$0: invalid option: $1" >&2
159e1670675Smrg        exit 1;;
160129be1b7Smrg
161a9a39c43Smrg    *)  break;;
162a9a39c43Smrg  esac
163a9a39c43Smrg  shift
164a9a39c43Smrgdone
165129be1b7Smrg
166e1670675Smrg# We allow the use of options -d and -T together, by making -d
167e1670675Smrg# take the precedence; this is for compatibility with GNU install.
168e1670675Smrg
169e1670675Smrgif test -n "$dir_arg"; then
170e1670675Smrg  if test -n "$dst_arg"; then
171e1670675Smrg    echo "$0: target directory not allowed when installing a directory." >&2
172e1670675Smrg    exit 1
173e1670675Smrg  fi
174e1670675Smrgfi
175e1670675Smrg
176a9a39c43Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
177a9a39c43Smrg  # When -d is used, all remaining arguments are directories to create.
178a9a39c43Smrg  # When -t is used, the destination is already specified.
179a9a39c43Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
180a9a39c43Smrg  for arg
181a9a39c43Smrg  do
182a9a39c43Smrg    if test -n "$dst_arg"; then
183a9a39c43Smrg      # $@ is not empty: it contains at least $arg.
184a9a39c43Smrg      set fnord "$@" "$dst_arg"
185a9a39c43Smrg      shift # fnord
186a9a39c43Smrg    fi
187a9a39c43Smrg    shift # arg
188a9a39c43Smrg    dst_arg=$arg
189e1670675Smrg    # Protect names problematic for 'test' and other utilities.
190e1670675Smrg    case $dst_arg in
191e1670675Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
192e1670675Smrg    esac
193a9a39c43Smrg  done
194a9a39c43Smrgfi
195129be1b7Smrg
196a9a39c43Smrgif test $# -eq 0; then
197a9a39c43Smrg  if test -z "$dir_arg"; then
198a9a39c43Smrg    echo "$0: no input file specified." >&2
199a9a39c43Smrg    exit 1
200a9a39c43Smrg  fi
201e1670675Smrg  # It's OK to call 'install-sh -d' without argument.
202a9a39c43Smrg  # This can happen when creating conditional directories.
203a9a39c43Smrg  exit 0
204a9a39c43Smrgfi
205129be1b7Smrg
206a9a39c43Smrgif test -z "$dir_arg"; then
207e1670675Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
208e1670675Smrg    if test ! -d "$dst_arg"; then
209e1670675Smrg      echo "$0: $dst_arg: Is not a directory." >&2
210e1670675Smrg      exit 1
211e1670675Smrg    fi
212e1670675Smrg  fi
213e1670675Smrgfi
214e1670675Smrg
215e1670675Smrgif test -z "$dir_arg"; then
216e1670675Smrg  do_exit='(exit $ret); exit $ret'
217e1670675Smrg  trap "ret=129; $do_exit" 1
218e1670675Smrg  trap "ret=130; $do_exit" 2
219e1670675Smrg  trap "ret=141; $do_exit" 13
220e1670675Smrg  trap "ret=143; $do_exit" 15
221a9a39c43Smrg
222a9a39c43Smrg  # Set umask so as not to create temps with too-generous modes.
223a9a39c43Smrg  # However, 'strip' requires both read and write access to temps.
224a9a39c43Smrg  case $mode in
225a9a39c43Smrg    # Optimize common cases.
226a9a39c43Smrg    *644) cp_umask=133;;
227a9a39c43Smrg    *755) cp_umask=22;;
228a9a39c43Smrg
229a9a39c43Smrg    *[0-7])
230a9a39c43Smrg      if test -z "$stripcmd"; then
231e1670675Smrg        u_plus_rw=
232a9a39c43Smrg      else
233e1670675Smrg        u_plus_rw='% 200'
234a9a39c43Smrg      fi
235a9a39c43Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
236a9a39c43Smrg    *)
237a9a39c43Smrg      if test -z "$stripcmd"; then
238e1670675Smrg        u_plus_rw=
239a9a39c43Smrg      else
240e1670675Smrg        u_plus_rw=,u+rw
241a9a39c43Smrg      fi
242a9a39c43Smrg      cp_umask=$mode$u_plus_rw;;
243a9a39c43Smrg  esac
244a9a39c43Smrgfi
245129be1b7Smrg
246a9a39c43Smrgfor src
247a9a39c43Smrgdo
248e1670675Smrg  # Protect names problematic for 'test' and other utilities.
249a9a39c43Smrg  case $src in
250e1670675Smrg    -* | [=\(\)!]) src=./$src;;
251a9a39c43Smrg  esac
252a9a39c43Smrg
253a9a39c43Smrg  if test -n "$dir_arg"; then
254a9a39c43Smrg    dst=$src
255a9a39c43Smrg    dstdir=$dst
256a9a39c43Smrg    test -d "$dstdir"
257a9a39c43Smrg    dstdir_status=$?
258a9a39c43Smrg  else
259a9a39c43Smrg
260a9a39c43Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
261a9a39c43Smrg    # might cause directories to be created, which would be especially bad
262a9a39c43Smrg    # if $src (and thus $dsttmp) contains '*'.
263a9a39c43Smrg    if test ! -f "$src" && test ! -d "$src"; then
264a9a39c43Smrg      echo "$0: $src does not exist." >&2
265a9a39c43Smrg      exit 1
266a9a39c43Smrg    fi
267a9a39c43Smrg
268a9a39c43Smrg    if test -z "$dst_arg"; then
269a9a39c43Smrg      echo "$0: no destination specified." >&2
270a9a39c43Smrg      exit 1
271a9a39c43Smrg    fi
272a9a39c43Smrg    dst=$dst_arg
273129be1b7Smrg
274a9a39c43Smrg    # If destination is a directory, append the input filename; won't work
275a9a39c43Smrg    # if double slashes aren't ignored.
276a9a39c43Smrg    if test -d "$dst"; then
277e1670675Smrg      if test "$is_target_a_directory" = never; then
278e1670675Smrg        echo "$0: $dst_arg: Is a directory" >&2
279e1670675Smrg        exit 1
280a9a39c43Smrg      fi
281a9a39c43Smrg      dstdir=$dst
282a9a39c43Smrg      dst=$dstdir/`basename "$src"`
283a9a39c43Smrg      dstdir_status=0
284a9a39c43Smrg    else
285e1670675Smrg      dstdir=`dirname "$dst"`
286a9a39c43Smrg      test -d "$dstdir"
287a9a39c43Smrg      dstdir_status=$?
288a9a39c43Smrg    fi
289a9a39c43Smrg  fi
290a9a39c43Smrg
291a9a39c43Smrg  obsolete_mkdir_used=false
292a9a39c43Smrg
293a9a39c43Smrg  if test $dstdir_status != 0; then
294a9a39c43Smrg    case $posix_mkdir in
295a9a39c43Smrg      '')
296e1670675Smrg        # Create intermediate dirs using mode 755 as modified by the umask.
297e1670675Smrg        # This is like FreeBSD 'install' as of 1997-10-28.
298e1670675Smrg        umask=`umask`
299e1670675Smrg        case $stripcmd.$umask in
300e1670675Smrg          # Optimize common cases.
301e1670675Smrg          *[2367][2367]) mkdir_umask=$umask;;
302e1670675Smrg          .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
303e1670675Smrg
304e1670675Smrg          *[0-7])
305e1670675Smrg            mkdir_umask=`expr $umask + 22 \
306e1670675Smrg              - $umask % 100 % 40 + $umask % 20 \
307e1670675Smrg              - $umask % 10 % 4 + $umask % 2
308e1670675Smrg            `;;
309e1670675Smrg          *) mkdir_umask=$umask,go-w;;
310e1670675Smrg        esac
311e1670675Smrg
312e1670675Smrg        # With -d, create the new directory with the user-specified mode.
313e1670675Smrg        # Otherwise, rely on $mkdir_umask.
314e1670675Smrg        if test -n "$dir_arg"; then
315e1670675Smrg          mkdir_mode=-m$mode
316e1670675Smrg        else
317e1670675Smrg          mkdir_mode=
318e1670675Smrg        fi
319e1670675Smrg
320e1670675Smrg        posix_mkdir=false
321e1670675Smrg        case $umask in
322e1670675Smrg          *[123567][0-7][0-7])
323e1670675Smrg            # POSIX mkdir -p sets u+wx bits regardless of umask, which
324e1670675Smrg            # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
325e1670675Smrg            ;;
326e1670675Smrg          *)
327e1670675Smrg            tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
328e1670675Smrg            trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
329e1670675Smrg
330e1670675Smrg            if (umask $mkdir_umask &&
331e1670675Smrg                exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
332e1670675Smrg            then
333e1670675Smrg              if test -z "$dir_arg" || {
334e1670675Smrg                   # Check for POSIX incompatibilities with -m.
335e1670675Smrg                   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
336e1670675Smrg                   # other-writable bit of parent directory when it shouldn't.
337e1670675Smrg                   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
338e1670675Smrg                   ls_ld_tmpdir=`ls -ld "$tmpdir"`
339e1670675Smrg                   case $ls_ld_tmpdir in
340e1670675Smrg                     d????-?r-*) different_mode=700;;
341e1670675Smrg                     d????-?--*) different_mode=755;;
342e1670675Smrg                     *) false;;
343e1670675Smrg                   esac &&
344e1670675Smrg                   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
345e1670675Smrg                     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
346e1670675Smrg                     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
347e1670675Smrg                   }
348e1670675Smrg                 }
349e1670675Smrg              then posix_mkdir=:
350e1670675Smrg              fi
351e1670675Smrg              rmdir "$tmpdir/d" "$tmpdir"
352e1670675Smrg            else
353e1670675Smrg              # Remove any dirs left behind by ancient mkdir implementations.
354e1670675Smrg              rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
355e1670675Smrg            fi
356e1670675Smrg            trap '' 0;;
357e1670675Smrg        esac;;
358a9a39c43Smrg    esac
359129be1b7Smrg
360a9a39c43Smrg    if
361a9a39c43Smrg      $posix_mkdir && (
362e1670675Smrg        umask $mkdir_umask &&
363e1670675Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
364a9a39c43Smrg      )
365a9a39c43Smrg    then :
366a9a39c43Smrg    else
367a9a39c43Smrg
368a9a39c43Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
369a9a39c43Smrg      # or it failed possibly due to a race condition.  Create the
370a9a39c43Smrg      # directory the slow way, step by step, checking for races as we go.
371a9a39c43Smrg
372a9a39c43Smrg      case $dstdir in
373e1670675Smrg        /*) prefix='/';;
374e1670675Smrg        [-=\(\)!]*) prefix='./';;
375e1670675Smrg        *)  prefix='';;
376a9a39c43Smrg      esac
377a9a39c43Smrg
378a9a39c43Smrg      oIFS=$IFS
379a9a39c43Smrg      IFS=/
380e1670675Smrg      set -f
381a9a39c43Smrg      set fnord $dstdir
382a9a39c43Smrg      shift
383e1670675Smrg      set +f
384a9a39c43Smrg      IFS=$oIFS
385a9a39c43Smrg
386a9a39c43Smrg      prefixes=
387a9a39c43Smrg
388a9a39c43Smrg      for d
389a9a39c43Smrg      do
390e1670675Smrg        test X"$d" = X && continue
391e1670675Smrg
392e1670675Smrg        prefix=$prefix$d
393e1670675Smrg        if test -d "$prefix"; then
394e1670675Smrg          prefixes=
395e1670675Smrg        else
396e1670675Smrg          if $posix_mkdir; then
397e1670675Smrg            (umask=$mkdir_umask &&
398e1670675Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
399e1670675Smrg            # Don't fail if two instances are running concurrently.
400e1670675Smrg            test -d "$prefix" || exit 1
401e1670675Smrg          else
402e1670675Smrg            case $prefix in
403e1670675Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
404e1670675Smrg              *) qprefix=$prefix;;
405e1670675Smrg            esac
406e1670675Smrg            prefixes="$prefixes '$qprefix'"
407e1670675Smrg          fi
408e1670675Smrg        fi
409e1670675Smrg        prefix=$prefix/
410a9a39c43Smrg      done
411a9a39c43Smrg
412a9a39c43Smrg      if test -n "$prefixes"; then
413e1670675Smrg        # Don't fail if two instances are running concurrently.
414e1670675Smrg        (umask $mkdir_umask &&
415e1670675Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
416e1670675Smrg          test -d "$dstdir" || exit 1
417e1670675Smrg        obsolete_mkdir_used=true
418a9a39c43Smrg      fi
419a9a39c43Smrg    fi
420a9a39c43Smrg  fi
421a9a39c43Smrg
422a9a39c43Smrg  if test -n "$dir_arg"; then
423a9a39c43Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
424a9a39c43Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
425a9a39c43Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
426a9a39c43Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
427a9a39c43Smrg  else
428a9a39c43Smrg
429a9a39c43Smrg    # Make a couple of temp file names in the proper directory.
430a9a39c43Smrg    dsttmp=$dstdir/_inst.$$_
431a9a39c43Smrg    rmtmp=$dstdir/_rm.$$_
432a9a39c43Smrg
433a9a39c43Smrg    # Trap to clean up those temp files at exit.
434a9a39c43Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
435a9a39c43Smrg
436a9a39c43Smrg    # Copy the file name to the temp name.
437a9a39c43Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
438a9a39c43Smrg
439a9a39c43Smrg    # and set any options; do chmod last to preserve setuid bits.
440a9a39c43Smrg    #
441a9a39c43Smrg    # If any of these fail, we abort the whole thing.  If we want to
442a9a39c43Smrg    # ignore errors from any of these, just make sure not to ignore
443a9a39c43Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
444a9a39c43Smrg    #
445a9a39c43Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
446a9a39c43Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
447a9a39c43Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
448a9a39c43Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
449a9a39c43Smrg
450a9a39c43Smrg    # If -C, don't bother to copy if it wouldn't change the file.
451a9a39c43Smrg    if $copy_on_change &&
452e1670675Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
453e1670675Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
454e1670675Smrg       set -f &&
455a9a39c43Smrg       set X $old && old=:$2:$4:$5:$6 &&
456a9a39c43Smrg       set X $new && new=:$2:$4:$5:$6 &&
457e1670675Smrg       set +f &&
458a9a39c43Smrg       test "$old" = "$new" &&
459a9a39c43Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
460a9a39c43Smrg    then
461a9a39c43Smrg      rm -f "$dsttmp"
462a9a39c43Smrg    else
463a9a39c43Smrg      # Rename the file to the real destination.
464a9a39c43Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
465a9a39c43Smrg
466a9a39c43Smrg      # The rename failed, perhaps because mv can't rename something else
467a9a39c43Smrg      # to itself, or perhaps because mv is so ancient that it does not
468a9a39c43Smrg      # support -f.
469a9a39c43Smrg      {
470e1670675Smrg        # Now remove or move aside any old file at destination location.
471e1670675Smrg        # We try this two ways since rm can't unlink itself on some
472e1670675Smrg        # systems and the destination file might be busy for other
473e1670675Smrg        # reasons.  In this case, the final cleanup might fail but the new
474e1670675Smrg        # file should still install successfully.
475e1670675Smrg        {
476e1670675Smrg          test ! -f "$dst" ||
477e1670675Smrg          $doit $rmcmd -f "$dst" 2>/dev/null ||
478e1670675Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
479e1670675Smrg            { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
480e1670675Smrg          } ||
481e1670675Smrg          { echo "$0: cannot unlink or rename $dst" >&2
482e1670675Smrg            (exit 1); exit 1
483e1670675Smrg          }
484e1670675Smrg        } &&
485e1670675Smrg
486e1670675Smrg        # Now rename the file to the real destination.
487e1670675Smrg        $doit $mvcmd "$dsttmp" "$dst"
488a9a39c43Smrg      }
489a9a39c43Smrg    fi || exit 1
490a9a39c43Smrg
491a9a39c43Smrg    trap '' 0
492a9a39c43Smrg  fi
493a9a39c43Smrgdone
494129be1b7Smrg
495a9a39c43Smrg# Local variables:
496a9a39c43Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
497a9a39c43Smrg# time-stamp-start: "scriptversion="
498a9a39c43Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
499a9a39c43Smrg# time-stamp-time-zone: "UTC"
500a9a39c43Smrg# time-stamp-end: "; # UTC"
501a9a39c43Smrg# End:
502