install-sh revision d656433a
1126a8a12Smrg#!/bin/sh
2126a8a12Smrg# install - install a program, script, or datafile
3126a8a12Smrg
4d656433aSmrgscriptversion=2009-04-28.21; # UTC
5126a8a12Smrg
6126a8a12Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7126a8a12Smrg# later released in X11R6 (xc/config/util/install.sh) with the
8126a8a12Smrg# following copyright and license.
9126a8a12Smrg#
10126a8a12Smrg# Copyright (C) 1994 X Consortium
11126a8a12Smrg#
12126a8a12Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13126a8a12Smrg# of this software and associated documentation files (the "Software"), to
14126a8a12Smrg# deal in the Software without restriction, including without limitation the
15126a8a12Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16126a8a12Smrg# sell copies of the Software, and to permit persons to whom the Software is
17126a8a12Smrg# furnished to do so, subject to the following conditions:
18126a8a12Smrg#
19126a8a12Smrg# The above copyright notice and this permission notice shall be included in
20126a8a12Smrg# all copies or substantial portions of the Software.
21126a8a12Smrg#
22126a8a12Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23126a8a12Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24126a8a12Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25126a8a12Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26126a8a12Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27126a8a12Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28126a8a12Smrg#
29126a8a12Smrg# Except as contained in this notice, the name of the X Consortium shall not
30126a8a12Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
31126a8a12Smrg# ings in this Software without prior written authorization from the X Consor-
32126a8a12Smrg# tium.
33126a8a12Smrg#
34126a8a12Smrg#
35126a8a12Smrg# FSF changes to this file are in the public domain.
36126a8a12Smrg#
37126a8a12Smrg# Calling this script install-sh is preferred over install.sh, to prevent
38126a8a12Smrg# `make' implicit rules from creating a file called install from it
39126a8a12Smrg# when there is no Makefile.
40126a8a12Smrg#
41126a8a12Smrg# This script is compatible with the BSD install script, but was written
42126a8a12Smrg# from scratch.
43126a8a12Smrg
44126a8a12Smrgnl='
45126a8a12Smrg'
46126a8a12SmrgIFS=" ""	$nl"
47126a8a12Smrg
48126a8a12Smrg# set DOITPROG to echo to test this script
49126a8a12Smrg
50126a8a12Smrg# Don't use :- since 4.3BSD and earlier shells don't like it.
51d656433aSmrgdoit=${DOITPROG-}
52126a8a12Smrgif test -z "$doit"; then
53126a8a12Smrg  doit_exec=exec
54126a8a12Smrgelse
55126a8a12Smrg  doit_exec=$doit
56126a8a12Smrgfi
57126a8a12Smrg
58126a8a12Smrg# Put in absolute file names if you don't have them in your path;
59126a8a12Smrg# or use environment vars.
60126a8a12Smrg
61d656433aSmrgchgrpprog=${CHGRPPROG-chgrp}
62d656433aSmrgchmodprog=${CHMODPROG-chmod}
63d656433aSmrgchownprog=${CHOWNPROG-chown}
64d656433aSmrgcmpprog=${CMPPROG-cmp}
65d656433aSmrgcpprog=${CPPROG-cp}
66d656433aSmrgmkdirprog=${MKDIRPROG-mkdir}
67d656433aSmrgmvprog=${MVPROG-mv}
68d656433aSmrgrmprog=${RMPROG-rm}
69d656433aSmrgstripprog=${STRIPPROG-strip}
70d656433aSmrg
71d656433aSmrgposix_glob='?'
72d656433aSmrginitialize_posix_glob='
73d656433aSmrg  test "$posix_glob" != "?" || {
74d656433aSmrg    if (set -f) 2>/dev/null; then
75d656433aSmrg      posix_glob=
76d656433aSmrg    else
77d656433aSmrg      posix_glob=:
78d656433aSmrg    fi
79d656433aSmrg  }
80d656433aSmrg'
81126a8a12Smrg
82126a8a12Smrgposix_mkdir=
83126a8a12Smrg
84126a8a12Smrg# Desired mode of installed file.
85126a8a12Smrgmode=0755
86126a8a12Smrg
87d656433aSmrgchgrpcmd=
88126a8a12Smrgchmodcmd=$chmodprog
89126a8a12Smrgchowncmd=
90d656433aSmrgmvcmd=$mvprog
91126a8a12Smrgrmcmd="$rmprog -f"
92d656433aSmrgstripcmd=
93d656433aSmrg
94126a8a12Smrgsrc=
95126a8a12Smrgdst=
96126a8a12Smrgdir_arg=
97d656433aSmrgdst_arg=
98d656433aSmrg
99d656433aSmrgcopy_on_change=false
100126a8a12Smrgno_target_directory=
101126a8a12Smrg
102d656433aSmrgusage="\
103d656433aSmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
104126a8a12Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
105126a8a12Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
106126a8a12Smrg   or: $0 [OPTION]... -d DIRECTORIES...
107126a8a12Smrg
108126a8a12SmrgIn the 1st form, copy SRCFILE to DSTFILE.
109126a8a12SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
110126a8a12SmrgIn the 4th, create DIRECTORIES.
111126a8a12Smrg
112126a8a12SmrgOptions:
113d656433aSmrg     --help     display this help and exit.
114d656433aSmrg     --version  display version info and exit.
115d656433aSmrg
116d656433aSmrg  -c            (ignored)
117d656433aSmrg  -C            install only if different (preserve the last data modification time)
118d656433aSmrg  -d            create directories instead of installing files.
119d656433aSmrg  -g GROUP      $chgrpprog installed files to GROUP.
120d656433aSmrg  -m MODE       $chmodprog installed files to MODE.
121d656433aSmrg  -o USER       $chownprog installed files to USER.
122d656433aSmrg  -s            $stripprog installed files.
123d656433aSmrg  -t DIRECTORY  install into DIRECTORY.
124d656433aSmrg  -T            report an error if DSTFILE is a directory.
125126a8a12Smrg
126126a8a12SmrgEnvironment variables override the default commands:
127d656433aSmrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
128d656433aSmrg  RMPROG STRIPPROG
129126a8a12Smrg"
130126a8a12Smrg
131126a8a12Smrgwhile test $# -ne 0; do
132126a8a12Smrg  case $1 in
133d656433aSmrg    -c) ;;
134d656433aSmrg
135d656433aSmrg    -C) copy_on_change=true;;
136126a8a12Smrg
137d656433aSmrg    -d) dir_arg=true;;
138126a8a12Smrg
139126a8a12Smrg    -g) chgrpcmd="$chgrpprog $2"
140d656433aSmrg	shift;;
141126a8a12Smrg
142126a8a12Smrg    --help) echo "$usage"; exit $?;;
143126a8a12Smrg
144126a8a12Smrg    -m) mode=$2
145126a8a12Smrg	case $mode in
146126a8a12Smrg	  *' '* | *'	'* | *'
147126a8a12Smrg'*	  | *'*'* | *'?'* | *'['*)
148126a8a12Smrg	    echo "$0: invalid mode: $mode" >&2
149126a8a12Smrg	    exit 1;;
150126a8a12Smrg	esac
151d656433aSmrg	shift;;
152126a8a12Smrg
153126a8a12Smrg    -o) chowncmd="$chownprog $2"
154d656433aSmrg	shift;;
155126a8a12Smrg
156d656433aSmrg    -s) stripcmd=$stripprog;;
157126a8a12Smrg
158d656433aSmrg    -t) dst_arg=$2
159d656433aSmrg	shift;;
160126a8a12Smrg
161d656433aSmrg    -T) no_target_directory=true;;
162126a8a12Smrg
163126a8a12Smrg    --version) echo "$0 $scriptversion"; exit $?;;
164126a8a12Smrg
165126a8a12Smrg    --)	shift
166126a8a12Smrg	break;;
167126a8a12Smrg
168126a8a12Smrg    -*)	echo "$0: invalid option: $1" >&2
169126a8a12Smrg	exit 1;;
170126a8a12Smrg
171126a8a12Smrg    *)  break;;
172126a8a12Smrg  esac
173d656433aSmrg  shift
174126a8a12Smrgdone
175126a8a12Smrg
176d656433aSmrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
177126a8a12Smrg  # When -d is used, all remaining arguments are directories to create.
178126a8a12Smrg  # When -t is used, the destination is already specified.
179126a8a12Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
180126a8a12Smrg  for arg
181126a8a12Smrg  do
182d656433aSmrg    if test -n "$dst_arg"; then
183126a8a12Smrg      # $@ is not empty: it contains at least $arg.
184d656433aSmrg      set fnord "$@" "$dst_arg"
185126a8a12Smrg      shift # fnord
186126a8a12Smrg    fi
187126a8a12Smrg    shift # arg
188d656433aSmrg    dst_arg=$arg
189126a8a12Smrg  done
190126a8a12Smrgfi
191126a8a12Smrg
192126a8a12Smrgif test $# -eq 0; then
193126a8a12Smrg  if test -z "$dir_arg"; then
194126a8a12Smrg    echo "$0: no input file specified." >&2
195126a8a12Smrg    exit 1
196126a8a12Smrg  fi
197126a8a12Smrg  # It's OK to call `install-sh -d' without argument.
198126a8a12Smrg  # This can happen when creating conditional directories.
199126a8a12Smrg  exit 0
200126a8a12Smrgfi
201126a8a12Smrg
202126a8a12Smrgif test -z "$dir_arg"; then
203126a8a12Smrg  trap '(exit $?); exit' 1 2 13 15
204126a8a12Smrg
205126a8a12Smrg  # Set umask so as not to create temps with too-generous modes.
206126a8a12Smrg  # However, 'strip' requires both read and write access to temps.
207126a8a12Smrg  case $mode in
208126a8a12Smrg    # Optimize common cases.
209126a8a12Smrg    *644) cp_umask=133;;
210126a8a12Smrg    *755) cp_umask=22;;
211126a8a12Smrg
212126a8a12Smrg    *[0-7])
213126a8a12Smrg      if test -z "$stripcmd"; then
214126a8a12Smrg	u_plus_rw=
215126a8a12Smrg      else
216126a8a12Smrg	u_plus_rw='% 200'
217126a8a12Smrg      fi
218126a8a12Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
219126a8a12Smrg    *)
220126a8a12Smrg      if test -z "$stripcmd"; then
221126a8a12Smrg	u_plus_rw=
222126a8a12Smrg      else
223126a8a12Smrg	u_plus_rw=,u+rw
224126a8a12Smrg      fi
225126a8a12Smrg      cp_umask=$mode$u_plus_rw;;
226126a8a12Smrg  esac
227126a8a12Smrgfi
228126a8a12Smrg
229126a8a12Smrgfor src
230126a8a12Smrgdo
231126a8a12Smrg  # Protect names starting with `-'.
232126a8a12Smrg  case $src in
233d656433aSmrg    -*) src=./$src;;
234126a8a12Smrg  esac
235126a8a12Smrg
236126a8a12Smrg  if test -n "$dir_arg"; then
237126a8a12Smrg    dst=$src
238126a8a12Smrg    dstdir=$dst
239126a8a12Smrg    test -d "$dstdir"
240126a8a12Smrg    dstdir_status=$?
241126a8a12Smrg  else
242126a8a12Smrg
243126a8a12Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
244126a8a12Smrg    # might cause directories to be created, which would be especially bad
245126a8a12Smrg    # if $src (and thus $dsttmp) contains '*'.
246126a8a12Smrg    if test ! -f "$src" && test ! -d "$src"; then
247126a8a12Smrg      echo "$0: $src does not exist." >&2
248126a8a12Smrg      exit 1
249126a8a12Smrg    fi
250126a8a12Smrg
251d656433aSmrg    if test -z "$dst_arg"; then
252126a8a12Smrg      echo "$0: no destination specified." >&2
253126a8a12Smrg      exit 1
254126a8a12Smrg    fi
255126a8a12Smrg
256d656433aSmrg    dst=$dst_arg
257126a8a12Smrg    # Protect names starting with `-'.
258126a8a12Smrg    case $dst in
259d656433aSmrg      -*) dst=./$dst;;
260126a8a12Smrg    esac
261126a8a12Smrg
262126a8a12Smrg    # If destination is a directory, append the input filename; won't work
263126a8a12Smrg    # if double slashes aren't ignored.
264126a8a12Smrg    if test -d "$dst"; then
265126a8a12Smrg      if test -n "$no_target_directory"; then
266d656433aSmrg	echo "$0: $dst_arg: Is a directory" >&2
267126a8a12Smrg	exit 1
268126a8a12Smrg      fi
269126a8a12Smrg      dstdir=$dst
270126a8a12Smrg      dst=$dstdir/`basename "$src"`
271126a8a12Smrg      dstdir_status=0
272126a8a12Smrg    else
273126a8a12Smrg      # Prefer dirname, but fall back on a substitute if dirname fails.
274126a8a12Smrg      dstdir=`
275126a8a12Smrg	(dirname "$dst") 2>/dev/null ||
276126a8a12Smrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
277126a8a12Smrg	     X"$dst" : 'X\(//\)[^/]' \| \
278126a8a12Smrg	     X"$dst" : 'X\(//\)$' \| \
279126a8a12Smrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
280126a8a12Smrg	echo X"$dst" |
281126a8a12Smrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
282126a8a12Smrg		   s//\1/
283126a8a12Smrg		   q
284126a8a12Smrg		 }
285126a8a12Smrg		 /^X\(\/\/\)[^/].*/{
286126a8a12Smrg		   s//\1/
287126a8a12Smrg		   q
288126a8a12Smrg		 }
289126a8a12Smrg		 /^X\(\/\/\)$/{
290126a8a12Smrg		   s//\1/
291126a8a12Smrg		   q
292126a8a12Smrg		 }
293126a8a12Smrg		 /^X\(\/\).*/{
294126a8a12Smrg		   s//\1/
295126a8a12Smrg		   q
296126a8a12Smrg		 }
297126a8a12Smrg		 s/.*/./; q'
298126a8a12Smrg      `
299126a8a12Smrg
300126a8a12Smrg      test -d "$dstdir"
301126a8a12Smrg      dstdir_status=$?
302126a8a12Smrg    fi
303126a8a12Smrg  fi
304126a8a12Smrg
305126a8a12Smrg  obsolete_mkdir_used=false
306126a8a12Smrg
307126a8a12Smrg  if test $dstdir_status != 0; then
308126a8a12Smrg    case $posix_mkdir in
309126a8a12Smrg      '')
310126a8a12Smrg	# Create intermediate dirs using mode 755 as modified by the umask.
311126a8a12Smrg	# This is like FreeBSD 'install' as of 1997-10-28.
312126a8a12Smrg	umask=`umask`
313126a8a12Smrg	case $stripcmd.$umask in
314126a8a12Smrg	  # Optimize common cases.
315126a8a12Smrg	  *[2367][2367]) mkdir_umask=$umask;;
316126a8a12Smrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
317126a8a12Smrg
318126a8a12Smrg	  *[0-7])
319126a8a12Smrg	    mkdir_umask=`expr $umask + 22 \
320126a8a12Smrg	      - $umask % 100 % 40 + $umask % 20 \
321126a8a12Smrg	      - $umask % 10 % 4 + $umask % 2
322126a8a12Smrg	    `;;
323126a8a12Smrg	  *) mkdir_umask=$umask,go-w;;
324126a8a12Smrg	esac
325126a8a12Smrg
326126a8a12Smrg	# With -d, create the new directory with the user-specified mode.
327126a8a12Smrg	# Otherwise, rely on $mkdir_umask.
328126a8a12Smrg	if test -n "$dir_arg"; then
329126a8a12Smrg	  mkdir_mode=-m$mode
330126a8a12Smrg	else
331126a8a12Smrg	  mkdir_mode=
332126a8a12Smrg	fi
333126a8a12Smrg
334126a8a12Smrg	posix_mkdir=false
335126a8a12Smrg	case $umask in
336126a8a12Smrg	  *[123567][0-7][0-7])
337126a8a12Smrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
338126a8a12Smrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
339126a8a12Smrg	    ;;
340126a8a12Smrg	  *)
341126a8a12Smrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
342126a8a12Smrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
343126a8a12Smrg
344126a8a12Smrg	    if (umask $mkdir_umask &&
345126a8a12Smrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
346126a8a12Smrg	    then
347126a8a12Smrg	      if test -z "$dir_arg" || {
348126a8a12Smrg		   # Check for POSIX incompatibilities with -m.
349126a8a12Smrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
350126a8a12Smrg		   # other-writeable bit of parent directory when it shouldn't.
351126a8a12Smrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
352126a8a12Smrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
353126a8a12Smrg		   case $ls_ld_tmpdir in
354126a8a12Smrg		     d????-?r-*) different_mode=700;;
355126a8a12Smrg		     d????-?--*) different_mode=755;;
356126a8a12Smrg		     *) false;;
357126a8a12Smrg		   esac &&
358126a8a12Smrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
359126a8a12Smrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
360126a8a12Smrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
361126a8a12Smrg		   }
362126a8a12Smrg		 }
363126a8a12Smrg	      then posix_mkdir=:
364126a8a12Smrg	      fi
365126a8a12Smrg	      rmdir "$tmpdir/d" "$tmpdir"
366126a8a12Smrg	    else
367126a8a12Smrg	      # Remove any dirs left behind by ancient mkdir implementations.
368126a8a12Smrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
369126a8a12Smrg	    fi
370126a8a12Smrg	    trap '' 0;;
371126a8a12Smrg	esac;;
372126a8a12Smrg    esac
373126a8a12Smrg
374126a8a12Smrg    if
375126a8a12Smrg      $posix_mkdir && (
376126a8a12Smrg	umask $mkdir_umask &&
377126a8a12Smrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
378126a8a12Smrg      )
379126a8a12Smrg    then :
380126a8a12Smrg    else
381126a8a12Smrg
382126a8a12Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
383126a8a12Smrg      # or it failed possibly due to a race condition.  Create the
384126a8a12Smrg      # directory the slow way, step by step, checking for races as we go.
385126a8a12Smrg
386126a8a12Smrg      case $dstdir in
387d656433aSmrg	/*) prefix='/';;
388d656433aSmrg	-*) prefix='./';;
389d656433aSmrg	*)  prefix='';;
390126a8a12Smrg      esac
391126a8a12Smrg
392d656433aSmrg      eval "$initialize_posix_glob"
393126a8a12Smrg
394126a8a12Smrg      oIFS=$IFS
395126a8a12Smrg      IFS=/
396d656433aSmrg      $posix_glob set -f
397126a8a12Smrg      set fnord $dstdir
398126a8a12Smrg      shift
399d656433aSmrg      $posix_glob set +f
400126a8a12Smrg      IFS=$oIFS
401126a8a12Smrg
402126a8a12Smrg      prefixes=
403126a8a12Smrg
404126a8a12Smrg      for d
405126a8a12Smrg      do
406126a8a12Smrg	test -z "$d" && continue
407126a8a12Smrg
408126a8a12Smrg	prefix=$prefix$d
409126a8a12Smrg	if test -d "$prefix"; then
410126a8a12Smrg	  prefixes=
411126a8a12Smrg	else
412126a8a12Smrg	  if $posix_mkdir; then
413126a8a12Smrg	    (umask=$mkdir_umask &&
414126a8a12Smrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
415126a8a12Smrg	    # Don't fail if two instances are running concurrently.
416126a8a12Smrg	    test -d "$prefix" || exit 1
417126a8a12Smrg	  else
418126a8a12Smrg	    case $prefix in
419126a8a12Smrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
420126a8a12Smrg	      *) qprefix=$prefix;;
421126a8a12Smrg	    esac
422126a8a12Smrg	    prefixes="$prefixes '$qprefix'"
423126a8a12Smrg	  fi
424126a8a12Smrg	fi
425126a8a12Smrg	prefix=$prefix/
426126a8a12Smrg      done
427126a8a12Smrg
428126a8a12Smrg      if test -n "$prefixes"; then
429126a8a12Smrg	# Don't fail if two instances are running concurrently.
430126a8a12Smrg	(umask $mkdir_umask &&
431126a8a12Smrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
432126a8a12Smrg	  test -d "$dstdir" || exit 1
433126a8a12Smrg	obsolete_mkdir_used=true
434126a8a12Smrg      fi
435126a8a12Smrg    fi
436126a8a12Smrg  fi
437126a8a12Smrg
438126a8a12Smrg  if test -n "$dir_arg"; then
439126a8a12Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
440126a8a12Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
441126a8a12Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
442126a8a12Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
443126a8a12Smrg  else
444126a8a12Smrg
445126a8a12Smrg    # Make a couple of temp file names in the proper directory.
446126a8a12Smrg    dsttmp=$dstdir/_inst.$$_
447126a8a12Smrg    rmtmp=$dstdir/_rm.$$_
448126a8a12Smrg
449126a8a12Smrg    # Trap to clean up those temp files at exit.
450126a8a12Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
451126a8a12Smrg
452126a8a12Smrg    # Copy the file name to the temp name.
453126a8a12Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
454126a8a12Smrg
455126a8a12Smrg    # and set any options; do chmod last to preserve setuid bits.
456126a8a12Smrg    #
457126a8a12Smrg    # If any of these fail, we abort the whole thing.  If we want to
458126a8a12Smrg    # ignore errors from any of these, just make sure not to ignore
459126a8a12Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
460126a8a12Smrg    #
461d656433aSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
462d656433aSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
463d656433aSmrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
464d656433aSmrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
465d656433aSmrg
466d656433aSmrg    # If -C, don't bother to copy if it wouldn't change the file.
467d656433aSmrg    if $copy_on_change &&
468d656433aSmrg       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
469d656433aSmrg       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
470d656433aSmrg
471d656433aSmrg       eval "$initialize_posix_glob" &&
472d656433aSmrg       $posix_glob set -f &&
473d656433aSmrg       set X $old && old=:$2:$4:$5:$6 &&
474d656433aSmrg       set X $new && new=:$2:$4:$5:$6 &&
475d656433aSmrg       $posix_glob set +f &&
476d656433aSmrg
477d656433aSmrg       test "$old" = "$new" &&
478d656433aSmrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
479d656433aSmrg    then
480d656433aSmrg      rm -f "$dsttmp"
481d656433aSmrg    else
482d656433aSmrg      # Rename the file to the real destination.
483d656433aSmrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
484d656433aSmrg
485d656433aSmrg      # The rename failed, perhaps because mv can't rename something else
486d656433aSmrg      # to itself, or perhaps because mv is so ancient that it does not
487d656433aSmrg      # support -f.
488d656433aSmrg      {
489d656433aSmrg	# Now remove or move aside any old file at destination location.
490d656433aSmrg	# We try this two ways since rm can't unlink itself on some
491d656433aSmrg	# systems and the destination file might be busy for other
492d656433aSmrg	# reasons.  In this case, the final cleanup might fail but the new
493d656433aSmrg	# file should still install successfully.
494d656433aSmrg	{
495d656433aSmrg	  test ! -f "$dst" ||
496d656433aSmrg	  $doit $rmcmd -f "$dst" 2>/dev/null ||
497d656433aSmrg	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
498d656433aSmrg	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
499d656433aSmrg	  } ||
500d656433aSmrg	  { echo "$0: cannot unlink or rename $dst" >&2
501d656433aSmrg	    (exit 1); exit 1
502d656433aSmrg	  }
503d656433aSmrg	} &&
504d656433aSmrg
505d656433aSmrg	# Now rename the file to the real destination.
506d656433aSmrg	$doit $mvcmd "$dsttmp" "$dst"
507d656433aSmrg      }
508d656433aSmrg    fi || exit 1
509126a8a12Smrg
510126a8a12Smrg    trap '' 0
511126a8a12Smrg  fi
512126a8a12Smrgdone
513126a8a12Smrg
514126a8a12Smrg# Local variables:
515126a8a12Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
516126a8a12Smrg# time-stamp-start: "scriptversion="
517126a8a12Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
518d656433aSmrg# time-stamp-time-zone: "UTC"
519d656433aSmrg# time-stamp-end: "; # UTC"
520126a8a12Smrg# End:
521