install-sh revision 5dd5f640
1e9554658Smrg#!/bin/sh
2e9554658Smrg# install - install a program, script, or datafile
3e9554658Smrg
45dd5f640Smrgscriptversion=2011-11-20.07; # UTC
5e9554658Smrg
6e9554658Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7e9554658Smrg# later released in X11R6 (xc/config/util/install.sh) with the
8e9554658Smrg# following copyright and license.
9e9554658Smrg#
10e9554658Smrg# Copyright (C) 1994 X Consortium
11e9554658Smrg#
12e9554658Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13e9554658Smrg# of this software and associated documentation files (the "Software"), to
14e9554658Smrg# deal in the Software without restriction, including without limitation the
15e9554658Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16e9554658Smrg# sell copies of the Software, and to permit persons to whom the Software is
17e9554658Smrg# furnished to do so, subject to the following conditions:
18e9554658Smrg#
19e9554658Smrg# The above copyright notice and this permission notice shall be included in
20e9554658Smrg# all copies or substantial portions of the Software.
21e9554658Smrg#
22e9554658Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23e9554658Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24e9554658Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25e9554658Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26e9554658Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27e9554658Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28e9554658Smrg#
29e9554658Smrg# Except as contained in this notice, the name of the X Consortium shall not
30e9554658Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
31e9554658Smrg# ings in this Software without prior written authorization from the X Consor-
32e9554658Smrg# tium.
33e9554658Smrg#
34e9554658Smrg#
35e9554658Smrg# FSF changes to this file are in the public domain.
36e9554658Smrg#
37e9554658Smrg# Calling this script install-sh is preferred over install.sh, to prevent
385dd5f640Smrg# 'make' implicit rules from creating a file called install from it
39e9554658Smrg# when there is no Makefile.
40e9554658Smrg#
41e9554658Smrg# This script is compatible with the BSD install script, but was written
42e9554658Smrg# from scratch.
43e9554658Smrg
44e9554658Smrgnl='
45e9554658Smrg'
46e9554658SmrgIFS=" ""	$nl"
47e9554658Smrg
48e9554658Smrg# set DOITPROG to echo to test this script
49e9554658Smrg
50e9554658Smrg# Don't use :- since 4.3BSD and earlier shells don't like it.
51c2b339b4Smrgdoit=${DOITPROG-}
52e9554658Smrgif test -z "$doit"; then
53e9554658Smrg  doit_exec=exec
54e9554658Smrgelse
55e9554658Smrg  doit_exec=$doit
56e9554658Smrgfi
57e9554658Smrg
58e9554658Smrg# Put in absolute file names if you don't have them in your path;
59e9554658Smrg# or use environment vars.
60e9554658Smrg
61c2b339b4Smrgchgrpprog=${CHGRPPROG-chgrp}
62c2b339b4Smrgchmodprog=${CHMODPROG-chmod}
63c2b339b4Smrgchownprog=${CHOWNPROG-chown}
64c2b339b4Smrgcmpprog=${CMPPROG-cmp}
65c2b339b4Smrgcpprog=${CPPROG-cp}
66c2b339b4Smrgmkdirprog=${MKDIRPROG-mkdir}
67c2b339b4Smrgmvprog=${MVPROG-mv}
68c2b339b4Smrgrmprog=${RMPROG-rm}
69c2b339b4Smrgstripprog=${STRIPPROG-strip}
70c2b339b4Smrg
71c2b339b4Smrgposix_glob='?'
72c2b339b4Smrginitialize_posix_glob='
73c2b339b4Smrg  test "$posix_glob" != "?" || {
74c2b339b4Smrg    if (set -f) 2>/dev/null; then
75c2b339b4Smrg      posix_glob=
76c2b339b4Smrg    else
77c2b339b4Smrg      posix_glob=:
78c2b339b4Smrg    fi
79c2b339b4Smrg  }
80c2b339b4Smrg'
81e9554658Smrg
82e9554658Smrgposix_mkdir=
83e9554658Smrg
84e9554658Smrg# Desired mode of installed file.
85e9554658Smrgmode=0755
86e9554658Smrg
87c2b339b4Smrgchgrpcmd=
88e9554658Smrgchmodcmd=$chmodprog
89e9554658Smrgchowncmd=
90c2b339b4Smrgmvcmd=$mvprog
91e9554658Smrgrmcmd="$rmprog -f"
92c2b339b4Smrgstripcmd=
93c2b339b4Smrg
94e9554658Smrgsrc=
95e9554658Smrgdst=
96e9554658Smrgdir_arg=
97c2b339b4Smrgdst_arg=
98c2b339b4Smrg
99c2b339b4Smrgcopy_on_change=false
100e9554658Smrgno_target_directory=
101e9554658Smrg
102c2b339b4Smrgusage="\
103c2b339b4SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
104e9554658Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
105e9554658Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
106e9554658Smrg   or: $0 [OPTION]... -d DIRECTORIES...
107e9554658Smrg
108e9554658SmrgIn the 1st form, copy SRCFILE to DSTFILE.
109e9554658SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
110e9554658SmrgIn the 4th, create DIRECTORIES.
111e9554658Smrg
112e9554658SmrgOptions:
113c2b339b4Smrg     --help     display this help and exit.
114c2b339b4Smrg     --version  display version info and exit.
115c2b339b4Smrg
116c2b339b4Smrg  -c            (ignored)
117c2b339b4Smrg  -C            install only if different (preserve the last data modification time)
118c2b339b4Smrg  -d            create directories instead of installing files.
119c2b339b4Smrg  -g GROUP      $chgrpprog installed files to GROUP.
120c2b339b4Smrg  -m MODE       $chmodprog installed files to MODE.
121c2b339b4Smrg  -o USER       $chownprog installed files to USER.
122c2b339b4Smrg  -s            $stripprog installed files.
123c2b339b4Smrg  -t DIRECTORY  install into DIRECTORY.
124c2b339b4Smrg  -T            report an error if DSTFILE is a directory.
125e9554658Smrg
126e9554658SmrgEnvironment variables override the default commands:
127c2b339b4Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
128c2b339b4Smrg  RMPROG STRIPPROG
129e9554658Smrg"
130e9554658Smrg
131e9554658Smrgwhile test $# -ne 0; do
132e9554658Smrg  case $1 in
133c2b339b4Smrg    -c) ;;
134c2b339b4Smrg
135c2b339b4Smrg    -C) copy_on_change=true;;
136e9554658Smrg
137c2b339b4Smrg    -d) dir_arg=true;;
138e9554658Smrg
139e9554658Smrg    -g) chgrpcmd="$chgrpprog $2"
140c2b339b4Smrg	shift;;
141e9554658Smrg
142e9554658Smrg    --help) echo "$usage"; exit $?;;
143e9554658Smrg
144e9554658Smrg    -m) mode=$2
145e9554658Smrg	case $mode in
146e9554658Smrg	  *' '* | *'	'* | *'
147e9554658Smrg'*	  | *'*'* | *'?'* | *'['*)
148e9554658Smrg	    echo "$0: invalid mode: $mode" >&2
149e9554658Smrg	    exit 1;;
150e9554658Smrg	esac
151c2b339b4Smrg	shift;;
152e9554658Smrg
153e9554658Smrg    -o) chowncmd="$chownprog $2"
154c2b339b4Smrg	shift;;
155e9554658Smrg
156c2b339b4Smrg    -s) stripcmd=$stripprog;;
157e9554658Smrg
158c2b339b4Smrg    -t) dst_arg=$2
1595dd5f640Smrg	# Protect names problematic for 'test' and other utilities.
1605dd5f640Smrg	case $dst_arg in
1615dd5f640Smrg	  -* | [=\(\)!]) dst_arg=./$dst_arg;;
1625dd5f640Smrg	esac
163c2b339b4Smrg	shift;;
164e9554658Smrg
165c2b339b4Smrg    -T) no_target_directory=true;;
166e9554658Smrg
167e9554658Smrg    --version) echo "$0 $scriptversion"; exit $?;;
168e9554658Smrg
169e9554658Smrg    --)	shift
170e9554658Smrg	break;;
171e9554658Smrg
172e9554658Smrg    -*)	echo "$0: invalid option: $1" >&2
173e9554658Smrg	exit 1;;
174e9554658Smrg
175e9554658Smrg    *)  break;;
176e9554658Smrg  esac
177c2b339b4Smrg  shift
178e9554658Smrgdone
179e9554658Smrg
180c2b339b4Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
181e9554658Smrg  # When -d is used, all remaining arguments are directories to create.
182e9554658Smrg  # When -t is used, the destination is already specified.
183e9554658Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
184e9554658Smrg  for arg
185e9554658Smrg  do
186c2b339b4Smrg    if test -n "$dst_arg"; then
187e9554658Smrg      # $@ is not empty: it contains at least $arg.
188c2b339b4Smrg      set fnord "$@" "$dst_arg"
189e9554658Smrg      shift # fnord
190e9554658Smrg    fi
191e9554658Smrg    shift # arg
192c2b339b4Smrg    dst_arg=$arg
1935dd5f640Smrg    # Protect names problematic for 'test' and other utilities.
1945dd5f640Smrg    case $dst_arg in
1955dd5f640Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
1965dd5f640Smrg    esac
197e9554658Smrg  done
198e9554658Smrgfi
199e9554658Smrg
200e9554658Smrgif test $# -eq 0; then
201e9554658Smrg  if test -z "$dir_arg"; then
202e9554658Smrg    echo "$0: no input file specified." >&2
203e9554658Smrg    exit 1
204e9554658Smrg  fi
2055dd5f640Smrg  # It's OK to call 'install-sh -d' without argument.
206e9554658Smrg  # This can happen when creating conditional directories.
207e9554658Smrg  exit 0
208e9554658Smrgfi
209e9554658Smrg
210e9554658Smrgif test -z "$dir_arg"; then
2115dd5f640Smrg  do_exit='(exit $ret); exit $ret'
2125dd5f640Smrg  trap "ret=129; $do_exit" 1
2135dd5f640Smrg  trap "ret=130; $do_exit" 2
2145dd5f640Smrg  trap "ret=141; $do_exit" 13
2155dd5f640Smrg  trap "ret=143; $do_exit" 15
216e9554658Smrg
217e9554658Smrg  # Set umask so as not to create temps with too-generous modes.
218e9554658Smrg  # However, 'strip' requires both read and write access to temps.
219e9554658Smrg  case $mode in
220e9554658Smrg    # Optimize common cases.
221e9554658Smrg    *644) cp_umask=133;;
222e9554658Smrg    *755) cp_umask=22;;
223e9554658Smrg
224e9554658Smrg    *[0-7])
225e9554658Smrg      if test -z "$stripcmd"; then
226e9554658Smrg	u_plus_rw=
227e9554658Smrg      else
228e9554658Smrg	u_plus_rw='% 200'
229e9554658Smrg      fi
230e9554658Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
231e9554658Smrg    *)
232e9554658Smrg      if test -z "$stripcmd"; then
233e9554658Smrg	u_plus_rw=
234e9554658Smrg      else
235e9554658Smrg	u_plus_rw=,u+rw
236e9554658Smrg      fi
237e9554658Smrg      cp_umask=$mode$u_plus_rw;;
238e9554658Smrg  esac
239e9554658Smrgfi
240e9554658Smrg
241e9554658Smrgfor src
242e9554658Smrgdo
2435dd5f640Smrg  # Protect names problematic for 'test' and other utilities.
244e9554658Smrg  case $src in
2455dd5f640Smrg    -* | [=\(\)!]) src=./$src;;
246e9554658Smrg  esac
247e9554658Smrg
248e9554658Smrg  if test -n "$dir_arg"; then
249e9554658Smrg    dst=$src
250e9554658Smrg    dstdir=$dst
251e9554658Smrg    test -d "$dstdir"
252e9554658Smrg    dstdir_status=$?
253e9554658Smrg  else
254e9554658Smrg
255e9554658Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
256e9554658Smrg    # might cause directories to be created, which would be especially bad
257e9554658Smrg    # if $src (and thus $dsttmp) contains '*'.
258e9554658Smrg    if test ! -f "$src" && test ! -d "$src"; then
259e9554658Smrg      echo "$0: $src does not exist." >&2
260e9554658Smrg      exit 1
261e9554658Smrg    fi
262e9554658Smrg
263c2b339b4Smrg    if test -z "$dst_arg"; then
264e9554658Smrg      echo "$0: no destination specified." >&2
265e9554658Smrg      exit 1
266e9554658Smrg    fi
267c2b339b4Smrg    dst=$dst_arg
268e9554658Smrg
269e9554658Smrg    # If destination is a directory, append the input filename; won't work
270e9554658Smrg    # if double slashes aren't ignored.
271e9554658Smrg    if test -d "$dst"; then
272e9554658Smrg      if test -n "$no_target_directory"; then
273c2b339b4Smrg	echo "$0: $dst_arg: Is a directory" >&2
274e9554658Smrg	exit 1
275e9554658Smrg      fi
276e9554658Smrg      dstdir=$dst
277e9554658Smrg      dst=$dstdir/`basename "$src"`
278e9554658Smrg      dstdir_status=0
279e9554658Smrg    else
280e9554658Smrg      # Prefer dirname, but fall back on a substitute if dirname fails.
281e9554658Smrg      dstdir=`
282e9554658Smrg	(dirname "$dst") 2>/dev/null ||
283e9554658Smrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
284e9554658Smrg	     X"$dst" : 'X\(//\)[^/]' \| \
285e9554658Smrg	     X"$dst" : 'X\(//\)$' \| \
286e9554658Smrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
287e9554658Smrg	echo X"$dst" |
288e9554658Smrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
289e9554658Smrg		   s//\1/
290e9554658Smrg		   q
291e9554658Smrg		 }
292e9554658Smrg		 /^X\(\/\/\)[^/].*/{
293e9554658Smrg		   s//\1/
294e9554658Smrg		   q
295e9554658Smrg		 }
296e9554658Smrg		 /^X\(\/\/\)$/{
297e9554658Smrg		   s//\1/
298e9554658Smrg		   q
299e9554658Smrg		 }
300e9554658Smrg		 /^X\(\/\).*/{
301e9554658Smrg		   s//\1/
302e9554658Smrg		   q
303e9554658Smrg		 }
304e9554658Smrg		 s/.*/./; q'
305e9554658Smrg      `
306e9554658Smrg
307e9554658Smrg      test -d "$dstdir"
308e9554658Smrg      dstdir_status=$?
309e9554658Smrg    fi
310e9554658Smrg  fi
311e9554658Smrg
312e9554658Smrg  obsolete_mkdir_used=false
313e9554658Smrg
314e9554658Smrg  if test $dstdir_status != 0; then
315e9554658Smrg    case $posix_mkdir in
316e9554658Smrg      '')
317e9554658Smrg	# Create intermediate dirs using mode 755 as modified by the umask.
318e9554658Smrg	# This is like FreeBSD 'install' as of 1997-10-28.
319e9554658Smrg	umask=`umask`
320e9554658Smrg	case $stripcmd.$umask in
321e9554658Smrg	  # Optimize common cases.
322e9554658Smrg	  *[2367][2367]) mkdir_umask=$umask;;
323e9554658Smrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
324e9554658Smrg
325e9554658Smrg	  *[0-7])
326e9554658Smrg	    mkdir_umask=`expr $umask + 22 \
327e9554658Smrg	      - $umask % 100 % 40 + $umask % 20 \
328e9554658Smrg	      - $umask % 10 % 4 + $umask % 2
329e9554658Smrg	    `;;
330e9554658Smrg	  *) mkdir_umask=$umask,go-w;;
331e9554658Smrg	esac
332e9554658Smrg
333e9554658Smrg	# With -d, create the new directory with the user-specified mode.
334e9554658Smrg	# Otherwise, rely on $mkdir_umask.
335e9554658Smrg	if test -n "$dir_arg"; then
336e9554658Smrg	  mkdir_mode=-m$mode
337e9554658Smrg	else
338e9554658Smrg	  mkdir_mode=
339e9554658Smrg	fi
340e9554658Smrg
341e9554658Smrg	posix_mkdir=false
342e9554658Smrg	case $umask in
343e9554658Smrg	  *[123567][0-7][0-7])
344e9554658Smrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
345e9554658Smrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
346e9554658Smrg	    ;;
347e9554658Smrg	  *)
348e9554658Smrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
349e9554658Smrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
350e9554658Smrg
351e9554658Smrg	    if (umask $mkdir_umask &&
352e9554658Smrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
353e9554658Smrg	    then
354e9554658Smrg	      if test -z "$dir_arg" || {
355e9554658Smrg		   # Check for POSIX incompatibilities with -m.
356e9554658Smrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
3575dd5f640Smrg		   # other-writable bit of parent directory when it shouldn't.
358e9554658Smrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
359e9554658Smrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
360e9554658Smrg		   case $ls_ld_tmpdir in
361e9554658Smrg		     d????-?r-*) different_mode=700;;
362e9554658Smrg		     d????-?--*) different_mode=755;;
363e9554658Smrg		     *) false;;
364e9554658Smrg		   esac &&
365e9554658Smrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
366e9554658Smrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
367e9554658Smrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
368e9554658Smrg		   }
369e9554658Smrg		 }
370e9554658Smrg	      then posix_mkdir=:
371e9554658Smrg	      fi
372e9554658Smrg	      rmdir "$tmpdir/d" "$tmpdir"
373e9554658Smrg	    else
374e9554658Smrg	      # Remove any dirs left behind by ancient mkdir implementations.
375e9554658Smrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
376e9554658Smrg	    fi
377e9554658Smrg	    trap '' 0;;
378e9554658Smrg	esac;;
379e9554658Smrg    esac
380e9554658Smrg
381e9554658Smrg    if
382e9554658Smrg      $posix_mkdir && (
383e9554658Smrg	umask $mkdir_umask &&
384e9554658Smrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
385e9554658Smrg      )
386e9554658Smrg    then :
387e9554658Smrg    else
388e9554658Smrg
389e9554658Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
390e9554658Smrg      # or it failed possibly due to a race condition.  Create the
391e9554658Smrg      # directory the slow way, step by step, checking for races as we go.
392e9554658Smrg
393e9554658Smrg      case $dstdir in
394c2b339b4Smrg	/*) prefix='/';;
3955dd5f640Smrg	[-=\(\)!]*) prefix='./';;
396c2b339b4Smrg	*)  prefix='';;
397e9554658Smrg      esac
398e9554658Smrg
399c2b339b4Smrg      eval "$initialize_posix_glob"
400e9554658Smrg
401e9554658Smrg      oIFS=$IFS
402e9554658Smrg      IFS=/
403c2b339b4Smrg      $posix_glob set -f
404e9554658Smrg      set fnord $dstdir
405e9554658Smrg      shift
406c2b339b4Smrg      $posix_glob set +f
407e9554658Smrg      IFS=$oIFS
408e9554658Smrg
409e9554658Smrg      prefixes=
410e9554658Smrg
411e9554658Smrg      for d
412e9554658Smrg      do
4135dd5f640Smrg	test X"$d" = X && continue
414e9554658Smrg
415e9554658Smrg	prefix=$prefix$d
416e9554658Smrg	if test -d "$prefix"; then
417e9554658Smrg	  prefixes=
418e9554658Smrg	else
419e9554658Smrg	  if $posix_mkdir; then
420e9554658Smrg	    (umask=$mkdir_umask &&
421e9554658Smrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
422e9554658Smrg	    # Don't fail if two instances are running concurrently.
423e9554658Smrg	    test -d "$prefix" || exit 1
424e9554658Smrg	  else
425e9554658Smrg	    case $prefix in
426e9554658Smrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
427e9554658Smrg	      *) qprefix=$prefix;;
428e9554658Smrg	    esac
429e9554658Smrg	    prefixes="$prefixes '$qprefix'"
430e9554658Smrg	  fi
431e9554658Smrg	fi
432e9554658Smrg	prefix=$prefix/
433e9554658Smrg      done
434e9554658Smrg
435e9554658Smrg      if test -n "$prefixes"; then
436e9554658Smrg	# Don't fail if two instances are running concurrently.
437e9554658Smrg	(umask $mkdir_umask &&
438e9554658Smrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
439e9554658Smrg	  test -d "$dstdir" || exit 1
440e9554658Smrg	obsolete_mkdir_used=true
441e9554658Smrg      fi
442e9554658Smrg    fi
443e9554658Smrg  fi
444e9554658Smrg
445e9554658Smrg  if test -n "$dir_arg"; then
446e9554658Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
447e9554658Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
448e9554658Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
449e9554658Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
450e9554658Smrg  else
451e9554658Smrg
452e9554658Smrg    # Make a couple of temp file names in the proper directory.
453e9554658Smrg    dsttmp=$dstdir/_inst.$$_
454e9554658Smrg    rmtmp=$dstdir/_rm.$$_
455e9554658Smrg
456e9554658Smrg    # Trap to clean up those temp files at exit.
457e9554658Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
458e9554658Smrg
459e9554658Smrg    # Copy the file name to the temp name.
460e9554658Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
461e9554658Smrg
462e9554658Smrg    # and set any options; do chmod last to preserve setuid bits.
463e9554658Smrg    #
464e9554658Smrg    # If any of these fail, we abort the whole thing.  If we want to
465e9554658Smrg    # ignore errors from any of these, just make sure not to ignore
466e9554658Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
467e9554658Smrg    #
468c2b339b4Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
469c2b339b4Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
470c2b339b4Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
471c2b339b4Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
472c2b339b4Smrg
473c2b339b4Smrg    # If -C, don't bother to copy if it wouldn't change the file.
474c2b339b4Smrg    if $copy_on_change &&
475c2b339b4Smrg       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
476c2b339b4Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
477c2b339b4Smrg
478c2b339b4Smrg       eval "$initialize_posix_glob" &&
479c2b339b4Smrg       $posix_glob set -f &&
480c2b339b4Smrg       set X $old && old=:$2:$4:$5:$6 &&
481c2b339b4Smrg       set X $new && new=:$2:$4:$5:$6 &&
482c2b339b4Smrg       $posix_glob set +f &&
483c2b339b4Smrg
484c2b339b4Smrg       test "$old" = "$new" &&
485c2b339b4Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
486c2b339b4Smrg    then
487c2b339b4Smrg      rm -f "$dsttmp"
488c2b339b4Smrg    else
489c2b339b4Smrg      # Rename the file to the real destination.
490c2b339b4Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
491c2b339b4Smrg
492c2b339b4Smrg      # The rename failed, perhaps because mv can't rename something else
493c2b339b4Smrg      # to itself, or perhaps because mv is so ancient that it does not
494c2b339b4Smrg      # support -f.
495c2b339b4Smrg      {
496c2b339b4Smrg	# Now remove or move aside any old file at destination location.
497c2b339b4Smrg	# We try this two ways since rm can't unlink itself on some
498c2b339b4Smrg	# systems and the destination file might be busy for other
499c2b339b4Smrg	# reasons.  In this case, the final cleanup might fail but the new
500c2b339b4Smrg	# file should still install successfully.
501c2b339b4Smrg	{
502c2b339b4Smrg	  test ! -f "$dst" ||
503c2b339b4Smrg	  $doit $rmcmd -f "$dst" 2>/dev/null ||
504c2b339b4Smrg	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
505c2b339b4Smrg	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
506c2b339b4Smrg	  } ||
507c2b339b4Smrg	  { echo "$0: cannot unlink or rename $dst" >&2
508c2b339b4Smrg	    (exit 1); exit 1
509c2b339b4Smrg	  }
510c2b339b4Smrg	} &&
511c2b339b4Smrg
512c2b339b4Smrg	# Now rename the file to the real destination.
513c2b339b4Smrg	$doit $mvcmd "$dsttmp" "$dst"
514c2b339b4Smrg      }
515c2b339b4Smrg    fi || exit 1
516e9554658Smrg
517e9554658Smrg    trap '' 0
518e9554658Smrg  fi
519e9554658Smrgdone
520e9554658Smrg
521e9554658Smrg# Local variables:
522e9554658Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
523e9554658Smrg# time-stamp-start: "scriptversion="
524e9554658Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
525c2b339b4Smrg# time-stamp-time-zone: "UTC"
526c2b339b4Smrg# time-stamp-end: "; # UTC"
527e9554658Smrg# End:
528