install-sh revision b7d26471
1145b7b3cSmrg#!/bin/sh
2145b7b3cSmrg# install - install a program, script, or datafile
3145b7b3cSmrg
4b7d26471Smrgscriptversion=2013-12-25.23; # UTC
5145b7b3cSmrg
6145b7b3cSmrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7145b7b3cSmrg# later released in X11R6 (xc/config/util/install.sh) with the
8145b7b3cSmrg# following copyright and license.
9145b7b3cSmrg#
10145b7b3cSmrg# Copyright (C) 1994 X Consortium
11145b7b3cSmrg#
12145b7b3cSmrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13145b7b3cSmrg# of this software and associated documentation files (the "Software"), to
14145b7b3cSmrg# deal in the Software without restriction, including without limitation the
15145b7b3cSmrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16145b7b3cSmrg# sell copies of the Software, and to permit persons to whom the Software is
17145b7b3cSmrg# furnished to do so, subject to the following conditions:
18145b7b3cSmrg#
19145b7b3cSmrg# The above copyright notice and this permission notice shall be included in
20145b7b3cSmrg# all copies or substantial portions of the Software.
21145b7b3cSmrg#
22145b7b3cSmrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23145b7b3cSmrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24145b7b3cSmrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25145b7b3cSmrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26145b7b3cSmrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27145b7b3cSmrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28145b7b3cSmrg#
29145b7b3cSmrg# Except as contained in this notice, the name of the X Consortium shall not
30145b7b3cSmrg# be used in advertising or otherwise to promote the sale, use or other deal-
31145b7b3cSmrg# ings in this Software without prior written authorization from the X Consor-
32145b7b3cSmrg# tium.
33145b7b3cSmrg#
34145b7b3cSmrg#
35145b7b3cSmrg# FSF changes to this file are in the public domain.
36145b7b3cSmrg#
37145b7b3cSmrg# Calling this script install-sh is preferred over install.sh, to prevent
38b7d26471Smrg# 'make' implicit rules from creating a file called install from it
39145b7b3cSmrg# when there is no Makefile.
40145b7b3cSmrg#
41145b7b3cSmrg# This script is compatible with the BSD install script, but was written
42145b7b3cSmrg# from scratch.
43145b7b3cSmrg
44b7d26471Smrgtab='	'
45145b7b3cSmrgnl='
46145b7b3cSmrg'
47b7d26471SmrgIFS=" $tab$nl"
48145b7b3cSmrg
49b7d26471Smrg# Set DOITPROG to "echo" to test this script.
50145b7b3cSmrg
51578741aaSmrgdoit=${DOITPROG-}
52b7d26471Smrgdoit_exec=${doit:-exec}
53145b7b3cSmrg
54145b7b3cSmrg# Put in absolute file names if you don't have them in your path;
55145b7b3cSmrg# or use environment vars.
56145b7b3cSmrg
57578741aaSmrgchgrpprog=${CHGRPPROG-chgrp}
58578741aaSmrgchmodprog=${CHMODPROG-chmod}
59578741aaSmrgchownprog=${CHOWNPROG-chown}
60578741aaSmrgcmpprog=${CMPPROG-cmp}
61578741aaSmrgcpprog=${CPPROG-cp}
62578741aaSmrgmkdirprog=${MKDIRPROG-mkdir}
63578741aaSmrgmvprog=${MVPROG-mv}
64578741aaSmrgrmprog=${RMPROG-rm}
65578741aaSmrgstripprog=${STRIPPROG-strip}
66578741aaSmrg
67145b7b3cSmrgposix_mkdir=
68145b7b3cSmrg
69145b7b3cSmrg# Desired mode of installed file.
70145b7b3cSmrgmode=0755
71145b7b3cSmrg
72578741aaSmrgchgrpcmd=
73145b7b3cSmrgchmodcmd=$chmodprog
74145b7b3cSmrgchowncmd=
75578741aaSmrgmvcmd=$mvprog
76145b7b3cSmrgrmcmd="$rmprog -f"
77578741aaSmrgstripcmd=
78578741aaSmrg
79145b7b3cSmrgsrc=
80145b7b3cSmrgdst=
81145b7b3cSmrgdir_arg=
82578741aaSmrgdst_arg=
83578741aaSmrg
84578741aaSmrgcopy_on_change=false
85b7d26471Smrgis_target_a_directory=possibly
86145b7b3cSmrg
87578741aaSmrgusage="\
88578741aaSmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
89145b7b3cSmrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
90145b7b3cSmrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
91145b7b3cSmrg   or: $0 [OPTION]... -d DIRECTORIES...
92145b7b3cSmrg
93145b7b3cSmrgIn the 1st form, copy SRCFILE to DSTFILE.
94145b7b3cSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
95145b7b3cSmrgIn the 4th, create DIRECTORIES.
96145b7b3cSmrg
97145b7b3cSmrgOptions:
98578741aaSmrg     --help     display this help and exit.
99578741aaSmrg     --version  display version info and exit.
100578741aaSmrg
101578741aaSmrg  -c            (ignored)
102578741aaSmrg  -C            install only if different (preserve the last data modification time)
103578741aaSmrg  -d            create directories instead of installing files.
104578741aaSmrg  -g GROUP      $chgrpprog installed files to GROUP.
105578741aaSmrg  -m MODE       $chmodprog installed files to MODE.
106578741aaSmrg  -o USER       $chownprog installed files to USER.
107578741aaSmrg  -s            $stripprog installed files.
108578741aaSmrg  -t DIRECTORY  install into DIRECTORY.
109578741aaSmrg  -T            report an error if DSTFILE is a directory.
110145b7b3cSmrg
111145b7b3cSmrgEnvironment variables override the default commands:
112578741aaSmrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
113578741aaSmrg  RMPROG STRIPPROG
114145b7b3cSmrg"
115145b7b3cSmrg
116145b7b3cSmrgwhile test $# -ne 0; do
117145b7b3cSmrg  case $1 in
118578741aaSmrg    -c) ;;
119578741aaSmrg
120578741aaSmrg    -C) copy_on_change=true;;
121145b7b3cSmrg
122578741aaSmrg    -d) dir_arg=true;;
123145b7b3cSmrg
124145b7b3cSmrg    -g) chgrpcmd="$chgrpprog $2"
125b7d26471Smrg        shift;;
126145b7b3cSmrg
127145b7b3cSmrg    --help) echo "$usage"; exit $?;;
128145b7b3cSmrg
129145b7b3cSmrg    -m) mode=$2
130b7d26471Smrg        case $mode in
131b7d26471Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
132b7d26471Smrg            echo "$0: invalid mode: $mode" >&2
133b7d26471Smrg            exit 1;;
134b7d26471Smrg        esac
135b7d26471Smrg        shift;;
136145b7b3cSmrg
137145b7b3cSmrg    -o) chowncmd="$chownprog $2"
138b7d26471Smrg        shift;;
139145b7b3cSmrg
140578741aaSmrg    -s) stripcmd=$stripprog;;
141145b7b3cSmrg
142b7d26471Smrg    -t)
143b7d26471Smrg        is_target_a_directory=always
144b7d26471Smrg        dst_arg=$2
145b7d26471Smrg        # Protect names problematic for 'test' and other utilities.
146b7d26471Smrg        case $dst_arg in
147b7d26471Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
148b7d26471Smrg        esac
149b7d26471Smrg        shift;;
150145b7b3cSmrg
151b7d26471Smrg    -T) is_target_a_directory=never;;
152145b7b3cSmrg
153145b7b3cSmrg    --version) echo "$0 $scriptversion"; exit $?;;
154145b7b3cSmrg
155b7d26471Smrg    --) shift
156b7d26471Smrg        break;;
157145b7b3cSmrg
158b7d26471Smrg    -*) echo "$0: invalid option: $1" >&2
159b7d26471Smrg        exit 1;;
160145b7b3cSmrg
161145b7b3cSmrg    *)  break;;
162145b7b3cSmrg  esac
163578741aaSmrg  shift
164145b7b3cSmrgdone
165145b7b3cSmrg
166b7d26471Smrg# We allow the use of options -d and -T together, by making -d
167b7d26471Smrg# take the precedence; this is for compatibility with GNU install.
168b7d26471Smrg
169b7d26471Smrgif test -n "$dir_arg"; then
170b7d26471Smrg  if test -n "$dst_arg"; then
171b7d26471Smrg    echo "$0: target directory not allowed when installing a directory." >&2
172b7d26471Smrg    exit 1
173b7d26471Smrg  fi
174b7d26471Smrgfi
175b7d26471Smrg
176578741aaSmrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
177145b7b3cSmrg  # When -d is used, all remaining arguments are directories to create.
178145b7b3cSmrg  # When -t is used, the destination is already specified.
179145b7b3cSmrg  # Otherwise, the last argument is the destination.  Remove it from $@.
180145b7b3cSmrg  for arg
181145b7b3cSmrg  do
182578741aaSmrg    if test -n "$dst_arg"; then
183145b7b3cSmrg      # $@ is not empty: it contains at least $arg.
184578741aaSmrg      set fnord "$@" "$dst_arg"
185145b7b3cSmrg      shift # fnord
186145b7b3cSmrg    fi
187145b7b3cSmrg    shift # arg
188578741aaSmrg    dst_arg=$arg
189b7d26471Smrg    # Protect names problematic for 'test' and other utilities.
190b7d26471Smrg    case $dst_arg in
191b7d26471Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
192b7d26471Smrg    esac
193145b7b3cSmrg  done
194145b7b3cSmrgfi
195145b7b3cSmrg
196145b7b3cSmrgif test $# -eq 0; then
197145b7b3cSmrg  if test -z "$dir_arg"; then
198145b7b3cSmrg    echo "$0: no input file specified." >&2
199145b7b3cSmrg    exit 1
200145b7b3cSmrg  fi
201b7d26471Smrg  # It's OK to call 'install-sh -d' without argument.
202145b7b3cSmrg  # This can happen when creating conditional directories.
203145b7b3cSmrg  exit 0
204145b7b3cSmrgfi
205145b7b3cSmrg
206145b7b3cSmrgif test -z "$dir_arg"; then
207b7d26471Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
208b7d26471Smrg    if test ! -d "$dst_arg"; then
209b7d26471Smrg      echo "$0: $dst_arg: Is not a directory." >&2
210b7d26471Smrg      exit 1
211b7d26471Smrg    fi
212b7d26471Smrg  fi
213b7d26471Smrgfi
214b7d26471Smrg
215b7d26471Smrgif test -z "$dir_arg"; then
216b7d26471Smrg  do_exit='(exit $ret); exit $ret'
217b7d26471Smrg  trap "ret=129; $do_exit" 1
218b7d26471Smrg  trap "ret=130; $do_exit" 2
219b7d26471Smrg  trap "ret=141; $do_exit" 13
220b7d26471Smrg  trap "ret=143; $do_exit" 15
221145b7b3cSmrg
222145b7b3cSmrg  # Set umask so as not to create temps with too-generous modes.
223145b7b3cSmrg  # However, 'strip' requires both read and write access to temps.
224145b7b3cSmrg  case $mode in
225145b7b3cSmrg    # Optimize common cases.
226145b7b3cSmrg    *644) cp_umask=133;;
227145b7b3cSmrg    *755) cp_umask=22;;
228145b7b3cSmrg
229145b7b3cSmrg    *[0-7])
230145b7b3cSmrg      if test -z "$stripcmd"; then
231b7d26471Smrg        u_plus_rw=
232145b7b3cSmrg      else
233b7d26471Smrg        u_plus_rw='% 200'
234145b7b3cSmrg      fi
235145b7b3cSmrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
236145b7b3cSmrg    *)
237145b7b3cSmrg      if test -z "$stripcmd"; then
238b7d26471Smrg        u_plus_rw=
239145b7b3cSmrg      else
240b7d26471Smrg        u_plus_rw=,u+rw
241145b7b3cSmrg      fi
242145b7b3cSmrg      cp_umask=$mode$u_plus_rw;;
243145b7b3cSmrg  esac
244145b7b3cSmrgfi
245145b7b3cSmrg
246145b7b3cSmrgfor src
247145b7b3cSmrgdo
248b7d26471Smrg  # Protect names problematic for 'test' and other utilities.
249145b7b3cSmrg  case $src in
250b7d26471Smrg    -* | [=\(\)!]) src=./$src;;
251145b7b3cSmrg  esac
252145b7b3cSmrg
253145b7b3cSmrg  if test -n "$dir_arg"; then
254145b7b3cSmrg    dst=$src
255145b7b3cSmrg    dstdir=$dst
256145b7b3cSmrg    test -d "$dstdir"
257145b7b3cSmrg    dstdir_status=$?
258145b7b3cSmrg  else
259145b7b3cSmrg
260145b7b3cSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
261145b7b3cSmrg    # might cause directories to be created, which would be especially bad
262145b7b3cSmrg    # if $src (and thus $dsttmp) contains '*'.
263145b7b3cSmrg    if test ! -f "$src" && test ! -d "$src"; then
264145b7b3cSmrg      echo "$0: $src does not exist." >&2
265145b7b3cSmrg      exit 1
266145b7b3cSmrg    fi
267145b7b3cSmrg
268578741aaSmrg    if test -z "$dst_arg"; then
269145b7b3cSmrg      echo "$0: no destination specified." >&2
270145b7b3cSmrg      exit 1
271145b7b3cSmrg    fi
272578741aaSmrg    dst=$dst_arg
273145b7b3cSmrg
274145b7b3cSmrg    # If destination is a directory, append the input filename; won't work
275145b7b3cSmrg    # if double slashes aren't ignored.
276145b7b3cSmrg    if test -d "$dst"; then
277b7d26471Smrg      if test "$is_target_a_directory" = never; then
278b7d26471Smrg        echo "$0: $dst_arg: Is a directory" >&2
279b7d26471Smrg        exit 1
280145b7b3cSmrg      fi
281145b7b3cSmrg      dstdir=$dst
282145b7b3cSmrg      dst=$dstdir/`basename "$src"`
283145b7b3cSmrg      dstdir_status=0
284145b7b3cSmrg    else
285b7d26471Smrg      dstdir=`dirname "$dst"`
286145b7b3cSmrg      test -d "$dstdir"
287145b7b3cSmrg      dstdir_status=$?
288145b7b3cSmrg    fi
289145b7b3cSmrg  fi
290145b7b3cSmrg
291145b7b3cSmrg  obsolete_mkdir_used=false
292145b7b3cSmrg
293145b7b3cSmrg  if test $dstdir_status != 0; then
294145b7b3cSmrg    case $posix_mkdir in
295145b7b3cSmrg      '')
296b7d26471Smrg        # Create intermediate dirs using mode 755 as modified by the umask.
297b7d26471Smrg        # This is like FreeBSD 'install' as of 1997-10-28.
298b7d26471Smrg        umask=`umask`
299b7d26471Smrg        case $stripcmd.$umask in
300b7d26471Smrg          # Optimize common cases.
301b7d26471Smrg          *[2367][2367]) mkdir_umask=$umask;;
302b7d26471Smrg          .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
303b7d26471Smrg
304b7d26471Smrg          *[0-7])
305b7d26471Smrg            mkdir_umask=`expr $umask + 22 \
306b7d26471Smrg              - $umask % 100 % 40 + $umask % 20 \
307b7d26471Smrg              - $umask % 10 % 4 + $umask % 2
308b7d26471Smrg            `;;
309b7d26471Smrg          *) mkdir_umask=$umask,go-w;;
310b7d26471Smrg        esac
311b7d26471Smrg
312b7d26471Smrg        # With -d, create the new directory with the user-specified mode.
313b7d26471Smrg        # Otherwise, rely on $mkdir_umask.
314b7d26471Smrg        if test -n "$dir_arg"; then
315b7d26471Smrg          mkdir_mode=-m$mode
316b7d26471Smrg        else
317b7d26471Smrg          mkdir_mode=
318b7d26471Smrg        fi
319b7d26471Smrg
320b7d26471Smrg        posix_mkdir=false
321b7d26471Smrg        case $umask in
322b7d26471Smrg          *[123567][0-7][0-7])
323b7d26471Smrg            # POSIX mkdir -p sets u+wx bits regardless of umask, which
324b7d26471Smrg            # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
325b7d26471Smrg            ;;
326b7d26471Smrg          *)
327b7d26471Smrg            tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
328b7d26471Smrg            trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
329b7d26471Smrg
330b7d26471Smrg            if (umask $mkdir_umask &&
331b7d26471Smrg                exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
332b7d26471Smrg            then
333b7d26471Smrg              if test -z "$dir_arg" || {
334b7d26471Smrg                   # Check for POSIX incompatibilities with -m.
335b7d26471Smrg                   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
336b7d26471Smrg                   # other-writable bit of parent directory when it shouldn't.
337b7d26471Smrg                   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
338b7d26471Smrg                   ls_ld_tmpdir=`ls -ld "$tmpdir"`
339b7d26471Smrg                   case $ls_ld_tmpdir in
340b7d26471Smrg                     d????-?r-*) different_mode=700;;
341b7d26471Smrg                     d????-?--*) different_mode=755;;
342b7d26471Smrg                     *) false;;
343b7d26471Smrg                   esac &&
344b7d26471Smrg                   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
345b7d26471Smrg                     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
346b7d26471Smrg                     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
347b7d26471Smrg                   }
348b7d26471Smrg                 }
349b7d26471Smrg              then posix_mkdir=:
350b7d26471Smrg              fi
351b7d26471Smrg              rmdir "$tmpdir/d" "$tmpdir"
352b7d26471Smrg            else
353b7d26471Smrg              # Remove any dirs left behind by ancient mkdir implementations.
354b7d26471Smrg              rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
355b7d26471Smrg            fi
356b7d26471Smrg            trap '' 0;;
357b7d26471Smrg        esac;;
358145b7b3cSmrg    esac
359145b7b3cSmrg
360145b7b3cSmrg    if
361145b7b3cSmrg      $posix_mkdir && (
362b7d26471Smrg        umask $mkdir_umask &&
363b7d26471Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
364145b7b3cSmrg      )
365145b7b3cSmrg    then :
366145b7b3cSmrg    else
367145b7b3cSmrg
368145b7b3cSmrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
369145b7b3cSmrg      # or it failed possibly due to a race condition.  Create the
370145b7b3cSmrg      # directory the slow way, step by step, checking for races as we go.
371145b7b3cSmrg
372145b7b3cSmrg      case $dstdir in
373b7d26471Smrg        /*) prefix='/';;
374b7d26471Smrg        [-=\(\)!]*) prefix='./';;
375b7d26471Smrg        *)  prefix='';;
376145b7b3cSmrg      esac
377145b7b3cSmrg
378145b7b3cSmrg      oIFS=$IFS
379145b7b3cSmrg      IFS=/
380b7d26471Smrg      set -f
381145b7b3cSmrg      set fnord $dstdir
382145b7b3cSmrg      shift
383b7d26471Smrg      set +f
384145b7b3cSmrg      IFS=$oIFS
385145b7b3cSmrg
386145b7b3cSmrg      prefixes=
387145b7b3cSmrg
388145b7b3cSmrg      for d
389145b7b3cSmrg      do
390b7d26471Smrg        test X"$d" = X && continue
391b7d26471Smrg
392b7d26471Smrg        prefix=$prefix$d
393b7d26471Smrg        if test -d "$prefix"; then
394b7d26471Smrg          prefixes=
395b7d26471Smrg        else
396b7d26471Smrg          if $posix_mkdir; then
397b7d26471Smrg            (umask=$mkdir_umask &&
398b7d26471Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
399b7d26471Smrg            # Don't fail if two instances are running concurrently.
400b7d26471Smrg            test -d "$prefix" || exit 1
401b7d26471Smrg          else
402b7d26471Smrg            case $prefix in
403b7d26471Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
404b7d26471Smrg              *) qprefix=$prefix;;
405b7d26471Smrg            esac
406b7d26471Smrg            prefixes="$prefixes '$qprefix'"
407b7d26471Smrg          fi
408b7d26471Smrg        fi
409b7d26471Smrg        prefix=$prefix/
410145b7b3cSmrg      done
411145b7b3cSmrg
412145b7b3cSmrg      if test -n "$prefixes"; then
413b7d26471Smrg        # Don't fail if two instances are running concurrently.
414b7d26471Smrg        (umask $mkdir_umask &&
415b7d26471Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
416b7d26471Smrg          test -d "$dstdir" || exit 1
417b7d26471Smrg        obsolete_mkdir_used=true
418145b7b3cSmrg      fi
419145b7b3cSmrg    fi
420145b7b3cSmrg  fi
421145b7b3cSmrg
422145b7b3cSmrg  if test -n "$dir_arg"; then
423145b7b3cSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
424145b7b3cSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
425145b7b3cSmrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
426145b7b3cSmrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
427145b7b3cSmrg  else
428145b7b3cSmrg
429145b7b3cSmrg    # Make a couple of temp file names in the proper directory.
430145b7b3cSmrg    dsttmp=$dstdir/_inst.$$_
431145b7b3cSmrg    rmtmp=$dstdir/_rm.$$_
432145b7b3cSmrg
433145b7b3cSmrg    # Trap to clean up those temp files at exit.
434145b7b3cSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
435145b7b3cSmrg
436145b7b3cSmrg    # Copy the file name to the temp name.
437145b7b3cSmrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
438145b7b3cSmrg
439145b7b3cSmrg    # and set any options; do chmod last to preserve setuid bits.
440145b7b3cSmrg    #
441145b7b3cSmrg    # If any of these fail, we abort the whole thing.  If we want to
442145b7b3cSmrg    # ignore errors from any of these, just make sure not to ignore
443145b7b3cSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
444145b7b3cSmrg    #
445578741aaSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
446578741aaSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
447578741aaSmrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
448578741aaSmrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
449578741aaSmrg
450578741aaSmrg    # If -C, don't bother to copy if it wouldn't change the file.
451578741aaSmrg    if $copy_on_change &&
452b7d26471Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
453b7d26471Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
454b7d26471Smrg       set -f &&
455578741aaSmrg       set X $old && old=:$2:$4:$5:$6 &&
456578741aaSmrg       set X $new && new=:$2:$4:$5:$6 &&
457b7d26471Smrg       set +f &&
458578741aaSmrg       test "$old" = "$new" &&
459578741aaSmrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
460578741aaSmrg    then
461578741aaSmrg      rm -f "$dsttmp"
462578741aaSmrg    else
463578741aaSmrg      # Rename the file to the real destination.
464578741aaSmrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
465578741aaSmrg
466578741aaSmrg      # The rename failed, perhaps because mv can't rename something else
467578741aaSmrg      # to itself, or perhaps because mv is so ancient that it does not
468578741aaSmrg      # support -f.
469578741aaSmrg      {
470b7d26471Smrg        # Now remove or move aside any old file at destination location.
471b7d26471Smrg        # We try this two ways since rm can't unlink itself on some
472b7d26471Smrg        # systems and the destination file might be busy for other
473b7d26471Smrg        # reasons.  In this case, the final cleanup might fail but the new
474b7d26471Smrg        # file should still install successfully.
475b7d26471Smrg        {
476b7d26471Smrg          test ! -f "$dst" ||
477b7d26471Smrg          $doit $rmcmd -f "$dst" 2>/dev/null ||
478b7d26471Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
479b7d26471Smrg            { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
480b7d26471Smrg          } ||
481b7d26471Smrg          { echo "$0: cannot unlink or rename $dst" >&2
482b7d26471Smrg            (exit 1); exit 1
483b7d26471Smrg          }
484b7d26471Smrg        } &&
485b7d26471Smrg
486b7d26471Smrg        # Now rename the file to the real destination.
487b7d26471Smrg        $doit $mvcmd "$dsttmp" "$dst"
488578741aaSmrg      }
489578741aaSmrg    fi || exit 1
490145b7b3cSmrg
491145b7b3cSmrg    trap '' 0
492145b7b3cSmrg  fi
493145b7b3cSmrgdone
494145b7b3cSmrg
495145b7b3cSmrg# Local variables:
496145b7b3cSmrg# eval: (add-hook 'write-file-hooks 'time-stamp)
497145b7b3cSmrg# time-stamp-start: "scriptversion="
498145b7b3cSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
499578741aaSmrg# time-stamp-time-zone: "UTC"
500578741aaSmrg# time-stamp-end: "; # UTC"
501145b7b3cSmrg# End:
502