install-sh revision 2f39173d
1209ff23fSmrg#!/bin/sh
2209ff23fSmrg# install - install a program, script, or datafile
3209ff23fSmrg
42f39173dSmrgscriptversion=2009-04-28.21; # UTC
5209ff23fSmrg
6209ff23fSmrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7209ff23fSmrg# later released in X11R6 (xc/config/util/install.sh) with the
8209ff23fSmrg# following copyright and license.
9209ff23fSmrg#
10209ff23fSmrg# Copyright (C) 1994 X Consortium
11209ff23fSmrg#
12209ff23fSmrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13209ff23fSmrg# of this software and associated documentation files (the "Software"), to
14209ff23fSmrg# deal in the Software without restriction, including without limitation the
15209ff23fSmrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16209ff23fSmrg# sell copies of the Software, and to permit persons to whom the Software is
17209ff23fSmrg# furnished to do so, subject to the following conditions:
18209ff23fSmrg#
19209ff23fSmrg# The above copyright notice and this permission notice shall be included in
20209ff23fSmrg# all copies or substantial portions of the Software.
21209ff23fSmrg#
22209ff23fSmrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23209ff23fSmrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24209ff23fSmrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25209ff23fSmrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26209ff23fSmrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27209ff23fSmrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28209ff23fSmrg#
29209ff23fSmrg# Except as contained in this notice, the name of the X Consortium shall not
30209ff23fSmrg# be used in advertising or otherwise to promote the sale, use or other deal-
31209ff23fSmrg# ings in this Software without prior written authorization from the X Consor-
32209ff23fSmrg# tium.
33209ff23fSmrg#
34209ff23fSmrg#
35209ff23fSmrg# FSF changes to this file are in the public domain.
36209ff23fSmrg#
37209ff23fSmrg# Calling this script install-sh is preferred over install.sh, to prevent
38209ff23fSmrg# `make' implicit rules from creating a file called install from it
39209ff23fSmrg# when there is no Makefile.
40209ff23fSmrg#
41209ff23fSmrg# This script is compatible with the BSD install script, but was written
42209ff23fSmrg# from scratch.
43209ff23fSmrg
44209ff23fSmrgnl='
45209ff23fSmrg'
46209ff23fSmrgIFS=" ""	$nl"
47209ff23fSmrg
48209ff23fSmrg# set DOITPROG to echo to test this script
49209ff23fSmrg
50209ff23fSmrg# Don't use :- since 4.3BSD and earlier shells don't like it.
51c503f109Smrgdoit=${DOITPROG-}
52209ff23fSmrgif test -z "$doit"; then
53209ff23fSmrg  doit_exec=exec
54209ff23fSmrgelse
55209ff23fSmrg  doit_exec=$doit
56209ff23fSmrgfi
57209ff23fSmrg
58209ff23fSmrg# Put in absolute file names if you don't have them in your path;
59209ff23fSmrg# or use environment vars.
60209ff23fSmrg
61c503f109Smrgchgrpprog=${CHGRPPROG-chgrp}
62c503f109Smrgchmodprog=${CHMODPROG-chmod}
63c503f109Smrgchownprog=${CHOWNPROG-chown}
64c503f109Smrgcmpprog=${CMPPROG-cmp}
65c503f109Smrgcpprog=${CPPROG-cp}
66c503f109Smrgmkdirprog=${MKDIRPROG-mkdir}
67c503f109Smrgmvprog=${MVPROG-mv}
68c503f109Smrgrmprog=${RMPROG-rm}
69c503f109Smrgstripprog=${STRIPPROG-strip}
70c503f109Smrg
71c503f109Smrgposix_glob='?'
72c503f109Smrginitialize_posix_glob='
73c503f109Smrg  test "$posix_glob" != "?" || {
74c503f109Smrg    if (set -f) 2>/dev/null; then
75c503f109Smrg      posix_glob=
76c503f109Smrg    else
77c503f109Smrg      posix_glob=:
78c503f109Smrg    fi
79c503f109Smrg  }
80c503f109Smrg'
81209ff23fSmrg
82209ff23fSmrgposix_mkdir=
83209ff23fSmrg
84209ff23fSmrg# Desired mode of installed file.
85209ff23fSmrgmode=0755
86209ff23fSmrg
87c503f109Smrgchgrpcmd=
88209ff23fSmrgchmodcmd=$chmodprog
89209ff23fSmrgchowncmd=
90c503f109Smrgmvcmd=$mvprog
91209ff23fSmrgrmcmd="$rmprog -f"
92c503f109Smrgstripcmd=
93c503f109Smrg
94209ff23fSmrgsrc=
95209ff23fSmrgdst=
96209ff23fSmrgdir_arg=
97c503f109Smrgdst_arg=
98c503f109Smrg
99c503f109Smrgcopy_on_change=false
100209ff23fSmrgno_target_directory=
101209ff23fSmrg
102c503f109Smrgusage="\
103c503f109SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
104209ff23fSmrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
105209ff23fSmrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
106209ff23fSmrg   or: $0 [OPTION]... -d DIRECTORIES...
107209ff23fSmrg
108209ff23fSmrgIn the 1st form, copy SRCFILE to DSTFILE.
109209ff23fSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
110209ff23fSmrgIn the 4th, create DIRECTORIES.
111209ff23fSmrg
112209ff23fSmrgOptions:
113c503f109Smrg     --help     display this help and exit.
114c503f109Smrg     --version  display version info and exit.
115c503f109Smrg
116c503f109Smrg  -c            (ignored)
117c503f109Smrg  -C            install only if different (preserve the last data modification time)
118c503f109Smrg  -d            create directories instead of installing files.
119c503f109Smrg  -g GROUP      $chgrpprog installed files to GROUP.
120c503f109Smrg  -m MODE       $chmodprog installed files to MODE.
121c503f109Smrg  -o USER       $chownprog installed files to USER.
122c503f109Smrg  -s            $stripprog installed files.
123c503f109Smrg  -t DIRECTORY  install into DIRECTORY.
124c503f109Smrg  -T            report an error if DSTFILE is a directory.
125209ff23fSmrg
126209ff23fSmrgEnvironment variables override the default commands:
127c503f109Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
128c503f109Smrg  RMPROG STRIPPROG
129209ff23fSmrg"
130209ff23fSmrg
131209ff23fSmrgwhile test $# -ne 0; do
132209ff23fSmrg  case $1 in
133c503f109Smrg    -c) ;;
134c503f109Smrg
135c503f109Smrg    -C) copy_on_change=true;;
136209ff23fSmrg
137c503f109Smrg    -d) dir_arg=true;;
138209ff23fSmrg
139209ff23fSmrg    -g) chgrpcmd="$chgrpprog $2"
140c503f109Smrg	shift;;
141209ff23fSmrg
142209ff23fSmrg    --help) echo "$usage"; exit $?;;
143209ff23fSmrg
144209ff23fSmrg    -m) mode=$2
145209ff23fSmrg	case $mode in
146209ff23fSmrg	  *' '* | *'	'* | *'
147209ff23fSmrg'*	  | *'*'* | *'?'* | *'['*)
148209ff23fSmrg	    echo "$0: invalid mode: $mode" >&2
149209ff23fSmrg	    exit 1;;
150209ff23fSmrg	esac
151c503f109Smrg	shift;;
152209ff23fSmrg
153209ff23fSmrg    -o) chowncmd="$chownprog $2"
154c503f109Smrg	shift;;
155209ff23fSmrg
156c503f109Smrg    -s) stripcmd=$stripprog;;
157209ff23fSmrg
158c503f109Smrg    -t) dst_arg=$2
159c503f109Smrg	shift;;
160209ff23fSmrg
161c503f109Smrg    -T) no_target_directory=true;;
162209ff23fSmrg
163209ff23fSmrg    --version) echo "$0 $scriptversion"; exit $?;;
164209ff23fSmrg
165209ff23fSmrg    --)	shift
166209ff23fSmrg	break;;
167209ff23fSmrg
168209ff23fSmrg    -*)	echo "$0: invalid option: $1" >&2
169209ff23fSmrg	exit 1;;
170209ff23fSmrg
171209ff23fSmrg    *)  break;;
172209ff23fSmrg  esac
173c503f109Smrg  shift
174209ff23fSmrgdone
175209ff23fSmrg
176c503f109Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
177209ff23fSmrg  # When -d is used, all remaining arguments are directories to create.
178209ff23fSmrg  # When -t is used, the destination is already specified.
179209ff23fSmrg  # Otherwise, the last argument is the destination.  Remove it from $@.
180209ff23fSmrg  for arg
181209ff23fSmrg  do
182c503f109Smrg    if test -n "$dst_arg"; then
183209ff23fSmrg      # $@ is not empty: it contains at least $arg.
184c503f109Smrg      set fnord "$@" "$dst_arg"
185209ff23fSmrg      shift # fnord
186209ff23fSmrg    fi
187209ff23fSmrg    shift # arg
188c503f109Smrg    dst_arg=$arg
189209ff23fSmrg  done
190209ff23fSmrgfi
191209ff23fSmrg
192209ff23fSmrgif test $# -eq 0; then
193209ff23fSmrg  if test -z "$dir_arg"; then
194209ff23fSmrg    echo "$0: no input file specified." >&2
195209ff23fSmrg    exit 1
196209ff23fSmrg  fi
197209ff23fSmrg  # It's OK to call `install-sh -d' without argument.
198209ff23fSmrg  # This can happen when creating conditional directories.
199209ff23fSmrg  exit 0
200209ff23fSmrgfi
201209ff23fSmrg
202209ff23fSmrgif test -z "$dir_arg"; then
203209ff23fSmrg  trap '(exit $?); exit' 1 2 13 15
204209ff23fSmrg
205209ff23fSmrg  # Set umask so as not to create temps with too-generous modes.
206209ff23fSmrg  # However, 'strip' requires both read and write access to temps.
207209ff23fSmrg  case $mode in
208209ff23fSmrg    # Optimize common cases.
209209ff23fSmrg    *644) cp_umask=133;;
210209ff23fSmrg    *755) cp_umask=22;;
211209ff23fSmrg
212209ff23fSmrg    *[0-7])
213209ff23fSmrg      if test -z "$stripcmd"; then
214209ff23fSmrg	u_plus_rw=
215209ff23fSmrg      else
216209ff23fSmrg	u_plus_rw='% 200'
217209ff23fSmrg      fi
218209ff23fSmrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
219209ff23fSmrg    *)
220209ff23fSmrg      if test -z "$stripcmd"; then
221209ff23fSmrg	u_plus_rw=
222209ff23fSmrg      else
223209ff23fSmrg	u_plus_rw=,u+rw
224209ff23fSmrg      fi
225209ff23fSmrg      cp_umask=$mode$u_plus_rw;;
226209ff23fSmrg  esac
227209ff23fSmrgfi
228209ff23fSmrg
229209ff23fSmrgfor src
230209ff23fSmrgdo
231209ff23fSmrg  # Protect names starting with `-'.
232209ff23fSmrg  case $src in
233c503f109Smrg    -*) src=./$src;;
234209ff23fSmrg  esac
235209ff23fSmrg
236209ff23fSmrg  if test -n "$dir_arg"; then
237209ff23fSmrg    dst=$src
238209ff23fSmrg    dstdir=$dst
239209ff23fSmrg    test -d "$dstdir"
240209ff23fSmrg    dstdir_status=$?
241209ff23fSmrg  else
242209ff23fSmrg
243209ff23fSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
244209ff23fSmrg    # might cause directories to be created, which would be especially bad
245209ff23fSmrg    # if $src (and thus $dsttmp) contains '*'.
246209ff23fSmrg    if test ! -f "$src" && test ! -d "$src"; then
247209ff23fSmrg      echo "$0: $src does not exist." >&2
248209ff23fSmrg      exit 1
249209ff23fSmrg    fi
250209ff23fSmrg
251c503f109Smrg    if test -z "$dst_arg"; then
252209ff23fSmrg      echo "$0: no destination specified." >&2
253209ff23fSmrg      exit 1
254209ff23fSmrg    fi
255209ff23fSmrg
256c503f109Smrg    dst=$dst_arg
257209ff23fSmrg    # Protect names starting with `-'.
258209ff23fSmrg    case $dst in
259c503f109Smrg      -*) dst=./$dst;;
260209ff23fSmrg    esac
261209ff23fSmrg
262209ff23fSmrg    # If destination is a directory, append the input filename; won't work
263209ff23fSmrg    # if double slashes aren't ignored.
264209ff23fSmrg    if test -d "$dst"; then
265209ff23fSmrg      if test -n "$no_target_directory"; then
266c503f109Smrg	echo "$0: $dst_arg: Is a directory" >&2
267209ff23fSmrg	exit 1
268209ff23fSmrg      fi
269209ff23fSmrg      dstdir=$dst
270209ff23fSmrg      dst=$dstdir/`basename "$src"`
271209ff23fSmrg      dstdir_status=0
272209ff23fSmrg    else
273209ff23fSmrg      # Prefer dirname, but fall back on a substitute if dirname fails.
274209ff23fSmrg      dstdir=`
275209ff23fSmrg	(dirname "$dst") 2>/dev/null ||
276209ff23fSmrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
277209ff23fSmrg	     X"$dst" : 'X\(//\)[^/]' \| \
278209ff23fSmrg	     X"$dst" : 'X\(//\)$' \| \
279209ff23fSmrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
280209ff23fSmrg	echo X"$dst" |
281209ff23fSmrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
282209ff23fSmrg		   s//\1/
283209ff23fSmrg		   q
284209ff23fSmrg		 }
285209ff23fSmrg		 /^X\(\/\/\)[^/].*/{
286209ff23fSmrg		   s//\1/
287209ff23fSmrg		   q
288209ff23fSmrg		 }
289209ff23fSmrg		 /^X\(\/\/\)$/{
290209ff23fSmrg		   s//\1/
291209ff23fSmrg		   q
292209ff23fSmrg		 }
293209ff23fSmrg		 /^X\(\/\).*/{
294209ff23fSmrg		   s//\1/
295209ff23fSmrg		   q
296209ff23fSmrg		 }
297209ff23fSmrg		 s/.*/./; q'
298209ff23fSmrg      `
299209ff23fSmrg
300209ff23fSmrg      test -d "$dstdir"
301209ff23fSmrg      dstdir_status=$?
302209ff23fSmrg    fi
303209ff23fSmrg  fi
304209ff23fSmrg
305209ff23fSmrg  obsolete_mkdir_used=false
306209ff23fSmrg
307209ff23fSmrg  if test $dstdir_status != 0; then
308209ff23fSmrg    case $posix_mkdir in
309209ff23fSmrg      '')
310209ff23fSmrg	# Create intermediate dirs using mode 755 as modified by the umask.
311209ff23fSmrg	# This is like FreeBSD 'install' as of 1997-10-28.
312209ff23fSmrg	umask=`umask`
313209ff23fSmrg	case $stripcmd.$umask in
314209ff23fSmrg	  # Optimize common cases.
315209ff23fSmrg	  *[2367][2367]) mkdir_umask=$umask;;
316209ff23fSmrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
317209ff23fSmrg
318209ff23fSmrg	  *[0-7])
319209ff23fSmrg	    mkdir_umask=`expr $umask + 22 \
320209ff23fSmrg	      - $umask % 100 % 40 + $umask % 20 \
321209ff23fSmrg	      - $umask % 10 % 4 + $umask % 2
322209ff23fSmrg	    `;;
323209ff23fSmrg	  *) mkdir_umask=$umask,go-w;;
324209ff23fSmrg	esac
325209ff23fSmrg
326209ff23fSmrg	# With -d, create the new directory with the user-specified mode.
327209ff23fSmrg	# Otherwise, rely on $mkdir_umask.
328209ff23fSmrg	if test -n "$dir_arg"; then
329209ff23fSmrg	  mkdir_mode=-m$mode
330209ff23fSmrg	else
331209ff23fSmrg	  mkdir_mode=
332209ff23fSmrg	fi
333209ff23fSmrg
334209ff23fSmrg	posix_mkdir=false
335209ff23fSmrg	case $umask in
336209ff23fSmrg	  *[123567][0-7][0-7])
337209ff23fSmrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
338209ff23fSmrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
339209ff23fSmrg	    ;;
340209ff23fSmrg	  *)
341209ff23fSmrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
342209ff23fSmrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
343209ff23fSmrg
344209ff23fSmrg	    if (umask $mkdir_umask &&
345209ff23fSmrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
346209ff23fSmrg	    then
347209ff23fSmrg	      if test -z "$dir_arg" || {
348209ff23fSmrg		   # Check for POSIX incompatibilities with -m.
349209ff23fSmrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
350209ff23fSmrg		   # other-writeable bit of parent directory when it shouldn't.
351209ff23fSmrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
352209ff23fSmrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
353209ff23fSmrg		   case $ls_ld_tmpdir in
354209ff23fSmrg		     d????-?r-*) different_mode=700;;
355209ff23fSmrg		     d????-?--*) different_mode=755;;
356209ff23fSmrg		     *) false;;
357209ff23fSmrg		   esac &&
358209ff23fSmrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
359209ff23fSmrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
360209ff23fSmrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
361209ff23fSmrg		   }
362209ff23fSmrg		 }
363209ff23fSmrg	      then posix_mkdir=:
364209ff23fSmrg	      fi
365209ff23fSmrg	      rmdir "$tmpdir/d" "$tmpdir"
366209ff23fSmrg	    else
367209ff23fSmrg	      # Remove any dirs left behind by ancient mkdir implementations.
368209ff23fSmrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
369209ff23fSmrg	    fi
370209ff23fSmrg	    trap '' 0;;
371209ff23fSmrg	esac;;
372209ff23fSmrg    esac
373209ff23fSmrg
374209ff23fSmrg    if
375209ff23fSmrg      $posix_mkdir && (
376209ff23fSmrg	umask $mkdir_umask &&
377209ff23fSmrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
378209ff23fSmrg      )
379209ff23fSmrg    then :
380209ff23fSmrg    else
381209ff23fSmrg
382209ff23fSmrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
383209ff23fSmrg      # or it failed possibly due to a race condition.  Create the
384209ff23fSmrg      # directory the slow way, step by step, checking for races as we go.
385209ff23fSmrg
386209ff23fSmrg      case $dstdir in
387c503f109Smrg	/*) prefix='/';;
388c503f109Smrg	-*) prefix='./';;
389c503f109Smrg	*)  prefix='';;
390209ff23fSmrg      esac
391209ff23fSmrg
392c503f109Smrg      eval "$initialize_posix_glob"
393209ff23fSmrg
394209ff23fSmrg      oIFS=$IFS
395209ff23fSmrg      IFS=/
396c503f109Smrg      $posix_glob set -f
397209ff23fSmrg      set fnord $dstdir
398209ff23fSmrg      shift
399c503f109Smrg      $posix_glob set +f
400209ff23fSmrg      IFS=$oIFS
401209ff23fSmrg
402209ff23fSmrg      prefixes=
403209ff23fSmrg
404209ff23fSmrg      for d
405209ff23fSmrg      do
406209ff23fSmrg	test -z "$d" && continue
407209ff23fSmrg
408209ff23fSmrg	prefix=$prefix$d
409209ff23fSmrg	if test -d "$prefix"; then
410209ff23fSmrg	  prefixes=
411209ff23fSmrg	else
412209ff23fSmrg	  if $posix_mkdir; then
413209ff23fSmrg	    (umask=$mkdir_umask &&
414209ff23fSmrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
415209ff23fSmrg	    # Don't fail if two instances are running concurrently.
416209ff23fSmrg	    test -d "$prefix" || exit 1
417209ff23fSmrg	  else
418209ff23fSmrg	    case $prefix in
419209ff23fSmrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
420209ff23fSmrg	      *) qprefix=$prefix;;
421209ff23fSmrg	    esac
422209ff23fSmrg	    prefixes="$prefixes '$qprefix'"
423209ff23fSmrg	  fi
424209ff23fSmrg	fi
425209ff23fSmrg	prefix=$prefix/
426209ff23fSmrg      done
427209ff23fSmrg
428209ff23fSmrg      if test -n "$prefixes"; then
429209ff23fSmrg	# Don't fail if two instances are running concurrently.
430209ff23fSmrg	(umask $mkdir_umask &&
431209ff23fSmrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
432209ff23fSmrg	  test -d "$dstdir" || exit 1
433209ff23fSmrg	obsolete_mkdir_used=true
434209ff23fSmrg      fi
435209ff23fSmrg    fi
436209ff23fSmrg  fi
437209ff23fSmrg
438209ff23fSmrg  if test -n "$dir_arg"; then
439209ff23fSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
440209ff23fSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
441209ff23fSmrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
442209ff23fSmrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
443209ff23fSmrg  else
444209ff23fSmrg
445209ff23fSmrg    # Make a couple of temp file names in the proper directory.
446209ff23fSmrg    dsttmp=$dstdir/_inst.$$_
447209ff23fSmrg    rmtmp=$dstdir/_rm.$$_
448209ff23fSmrg
449209ff23fSmrg    # Trap to clean up those temp files at exit.
450209ff23fSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
451209ff23fSmrg
452209ff23fSmrg    # Copy the file name to the temp name.
453209ff23fSmrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
454209ff23fSmrg
455209ff23fSmrg    # and set any options; do chmod last to preserve setuid bits.
456209ff23fSmrg    #
457209ff23fSmrg    # If any of these fail, we abort the whole thing.  If we want to
458209ff23fSmrg    # ignore errors from any of these, just make sure not to ignore
459209ff23fSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
460209ff23fSmrg    #
461c503f109Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
462c503f109Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
463c503f109Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
464c503f109Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
465c503f109Smrg
466c503f109Smrg    # If -C, don't bother to copy if it wouldn't change the file.
467c503f109Smrg    if $copy_on_change &&
468c503f109Smrg       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
469c503f109Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
470c503f109Smrg
471c503f109Smrg       eval "$initialize_posix_glob" &&
472c503f109Smrg       $posix_glob set -f &&
473c503f109Smrg       set X $old && old=:$2:$4:$5:$6 &&
474c503f109Smrg       set X $new && new=:$2:$4:$5:$6 &&
475c503f109Smrg       $posix_glob set +f &&
476c503f109Smrg
477c503f109Smrg       test "$old" = "$new" &&
478c503f109Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
479c503f109Smrg    then
480c503f109Smrg      rm -f "$dsttmp"
481c503f109Smrg    else
482c503f109Smrg      # Rename the file to the real destination.
483c503f109Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
484c503f109Smrg
485c503f109Smrg      # The rename failed, perhaps because mv can't rename something else
486c503f109Smrg      # to itself, or perhaps because mv is so ancient that it does not
487c503f109Smrg      # support -f.
488c503f109Smrg      {
489c503f109Smrg	# Now remove or move aside any old file at destination location.
490c503f109Smrg	# We try this two ways since rm can't unlink itself on some
491c503f109Smrg	# systems and the destination file might be busy for other
492c503f109Smrg	# reasons.  In this case, the final cleanup might fail but the new
493c503f109Smrg	# file should still install successfully.
494c503f109Smrg	{
495c503f109Smrg	  test ! -f "$dst" ||
496c503f109Smrg	  $doit $rmcmd -f "$dst" 2>/dev/null ||
497c503f109Smrg	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
498c503f109Smrg	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
499c503f109Smrg	  } ||
500c503f109Smrg	  { echo "$0: cannot unlink or rename $dst" >&2
501c503f109Smrg	    (exit 1); exit 1
502c503f109Smrg	  }
503c503f109Smrg	} &&
504c503f109Smrg
505c503f109Smrg	# Now rename the file to the real destination.
506c503f109Smrg	$doit $mvcmd "$dsttmp" "$dst"
507c503f109Smrg      }
508c503f109Smrg    fi || exit 1
509209ff23fSmrg
510209ff23fSmrg    trap '' 0
511209ff23fSmrg  fi
512209ff23fSmrgdone
513209ff23fSmrg
514209ff23fSmrg# Local variables:
515209ff23fSmrg# eval: (add-hook 'write-file-hooks 'time-stamp)
516209ff23fSmrg# time-stamp-start: "scriptversion="
517209ff23fSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
5182f39173dSmrg# time-stamp-time-zone: "UTC"
5192f39173dSmrg# time-stamp-end: "; # UTC"
520209ff23fSmrg# End:
521