install-sh revision c2b339b4
1e9554658Smrg#!/bin/sh
2e9554658Smrg# install - install a program, script, or datafile
3e9554658Smrg
4c2b339b4Smrgscriptversion=2009-04-28.21; # 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
38e9554658Smrg# `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
159c2b339b4Smrg	shift;;
160e9554658Smrg
161c2b339b4Smrg    -T) no_target_directory=true;;
162e9554658Smrg
163e9554658Smrg    --version) echo "$0 $scriptversion"; exit $?;;
164e9554658Smrg
165e9554658Smrg    --)	shift
166e9554658Smrg	break;;
167e9554658Smrg
168e9554658Smrg    -*)	echo "$0: invalid option: $1" >&2
169e9554658Smrg	exit 1;;
170e9554658Smrg
171e9554658Smrg    *)  break;;
172e9554658Smrg  esac
173c2b339b4Smrg  shift
174e9554658Smrgdone
175e9554658Smrg
176c2b339b4Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
177e9554658Smrg  # When -d is used, all remaining arguments are directories to create.
178e9554658Smrg  # When -t is used, the destination is already specified.
179e9554658Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
180e9554658Smrg  for arg
181e9554658Smrg  do
182c2b339b4Smrg    if test -n "$dst_arg"; then
183e9554658Smrg      # $@ is not empty: it contains at least $arg.
184c2b339b4Smrg      set fnord "$@" "$dst_arg"
185e9554658Smrg      shift # fnord
186e9554658Smrg    fi
187e9554658Smrg    shift # arg
188c2b339b4Smrg    dst_arg=$arg
189e9554658Smrg  done
190e9554658Smrgfi
191e9554658Smrg
192e9554658Smrgif test $# -eq 0; then
193e9554658Smrg  if test -z "$dir_arg"; then
194e9554658Smrg    echo "$0: no input file specified." >&2
195e9554658Smrg    exit 1
196e9554658Smrg  fi
197e9554658Smrg  # It's OK to call `install-sh -d' without argument.
198e9554658Smrg  # This can happen when creating conditional directories.
199e9554658Smrg  exit 0
200e9554658Smrgfi
201e9554658Smrg
202e9554658Smrgif test -z "$dir_arg"; then
203e9554658Smrg  trap '(exit $?); exit' 1 2 13 15
204e9554658Smrg
205e9554658Smrg  # Set umask so as not to create temps with too-generous modes.
206e9554658Smrg  # However, 'strip' requires both read and write access to temps.
207e9554658Smrg  case $mode in
208e9554658Smrg    # Optimize common cases.
209e9554658Smrg    *644) cp_umask=133;;
210e9554658Smrg    *755) cp_umask=22;;
211e9554658Smrg
212e9554658Smrg    *[0-7])
213e9554658Smrg      if test -z "$stripcmd"; then
214e9554658Smrg	u_plus_rw=
215e9554658Smrg      else
216e9554658Smrg	u_plus_rw='% 200'
217e9554658Smrg      fi
218e9554658Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
219e9554658Smrg    *)
220e9554658Smrg      if test -z "$stripcmd"; then
221e9554658Smrg	u_plus_rw=
222e9554658Smrg      else
223e9554658Smrg	u_plus_rw=,u+rw
224e9554658Smrg      fi
225e9554658Smrg      cp_umask=$mode$u_plus_rw;;
226e9554658Smrg  esac
227e9554658Smrgfi
228e9554658Smrg
229e9554658Smrgfor src
230e9554658Smrgdo
231e9554658Smrg  # Protect names starting with `-'.
232e9554658Smrg  case $src in
233c2b339b4Smrg    -*) src=./$src;;
234e9554658Smrg  esac
235e9554658Smrg
236e9554658Smrg  if test -n "$dir_arg"; then
237e9554658Smrg    dst=$src
238e9554658Smrg    dstdir=$dst
239e9554658Smrg    test -d "$dstdir"
240e9554658Smrg    dstdir_status=$?
241e9554658Smrg  else
242e9554658Smrg
243e9554658Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
244e9554658Smrg    # might cause directories to be created, which would be especially bad
245e9554658Smrg    # if $src (and thus $dsttmp) contains '*'.
246e9554658Smrg    if test ! -f "$src" && test ! -d "$src"; then
247e9554658Smrg      echo "$0: $src does not exist." >&2
248e9554658Smrg      exit 1
249e9554658Smrg    fi
250e9554658Smrg
251c2b339b4Smrg    if test -z "$dst_arg"; then
252e9554658Smrg      echo "$0: no destination specified." >&2
253e9554658Smrg      exit 1
254e9554658Smrg    fi
255e9554658Smrg
256c2b339b4Smrg    dst=$dst_arg
257e9554658Smrg    # Protect names starting with `-'.
258e9554658Smrg    case $dst in
259c2b339b4Smrg      -*) dst=./$dst;;
260e9554658Smrg    esac
261e9554658Smrg
262e9554658Smrg    # If destination is a directory, append the input filename; won't work
263e9554658Smrg    # if double slashes aren't ignored.
264e9554658Smrg    if test -d "$dst"; then
265e9554658Smrg      if test -n "$no_target_directory"; then
266c2b339b4Smrg	echo "$0: $dst_arg: Is a directory" >&2
267e9554658Smrg	exit 1
268e9554658Smrg      fi
269e9554658Smrg      dstdir=$dst
270e9554658Smrg      dst=$dstdir/`basename "$src"`
271e9554658Smrg      dstdir_status=0
272e9554658Smrg    else
273e9554658Smrg      # Prefer dirname, but fall back on a substitute if dirname fails.
274e9554658Smrg      dstdir=`
275e9554658Smrg	(dirname "$dst") 2>/dev/null ||
276e9554658Smrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
277e9554658Smrg	     X"$dst" : 'X\(//\)[^/]' \| \
278e9554658Smrg	     X"$dst" : 'X\(//\)$' \| \
279e9554658Smrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
280e9554658Smrg	echo X"$dst" |
281e9554658Smrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
282e9554658Smrg		   s//\1/
283e9554658Smrg		   q
284e9554658Smrg		 }
285e9554658Smrg		 /^X\(\/\/\)[^/].*/{
286e9554658Smrg		   s//\1/
287e9554658Smrg		   q
288e9554658Smrg		 }
289e9554658Smrg		 /^X\(\/\/\)$/{
290e9554658Smrg		   s//\1/
291e9554658Smrg		   q
292e9554658Smrg		 }
293e9554658Smrg		 /^X\(\/\).*/{
294e9554658Smrg		   s//\1/
295e9554658Smrg		   q
296e9554658Smrg		 }
297e9554658Smrg		 s/.*/./; q'
298e9554658Smrg      `
299e9554658Smrg
300e9554658Smrg      test -d "$dstdir"
301e9554658Smrg      dstdir_status=$?
302e9554658Smrg    fi
303e9554658Smrg  fi
304e9554658Smrg
305e9554658Smrg  obsolete_mkdir_used=false
306e9554658Smrg
307e9554658Smrg  if test $dstdir_status != 0; then
308e9554658Smrg    case $posix_mkdir in
309e9554658Smrg      '')
310e9554658Smrg	# Create intermediate dirs using mode 755 as modified by the umask.
311e9554658Smrg	# This is like FreeBSD 'install' as of 1997-10-28.
312e9554658Smrg	umask=`umask`
313e9554658Smrg	case $stripcmd.$umask in
314e9554658Smrg	  # Optimize common cases.
315e9554658Smrg	  *[2367][2367]) mkdir_umask=$umask;;
316e9554658Smrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
317e9554658Smrg
318e9554658Smrg	  *[0-7])
319e9554658Smrg	    mkdir_umask=`expr $umask + 22 \
320e9554658Smrg	      - $umask % 100 % 40 + $umask % 20 \
321e9554658Smrg	      - $umask % 10 % 4 + $umask % 2
322e9554658Smrg	    `;;
323e9554658Smrg	  *) mkdir_umask=$umask,go-w;;
324e9554658Smrg	esac
325e9554658Smrg
326e9554658Smrg	# With -d, create the new directory with the user-specified mode.
327e9554658Smrg	# Otherwise, rely on $mkdir_umask.
328e9554658Smrg	if test -n "$dir_arg"; then
329e9554658Smrg	  mkdir_mode=-m$mode
330e9554658Smrg	else
331e9554658Smrg	  mkdir_mode=
332e9554658Smrg	fi
333e9554658Smrg
334e9554658Smrg	posix_mkdir=false
335e9554658Smrg	case $umask in
336e9554658Smrg	  *[123567][0-7][0-7])
337e9554658Smrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
338e9554658Smrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
339e9554658Smrg	    ;;
340e9554658Smrg	  *)
341e9554658Smrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
342e9554658Smrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
343e9554658Smrg
344e9554658Smrg	    if (umask $mkdir_umask &&
345e9554658Smrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
346e9554658Smrg	    then
347e9554658Smrg	      if test -z "$dir_arg" || {
348e9554658Smrg		   # Check for POSIX incompatibilities with -m.
349e9554658Smrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
350e9554658Smrg		   # other-writeable bit of parent directory when it shouldn't.
351e9554658Smrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
352e9554658Smrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
353e9554658Smrg		   case $ls_ld_tmpdir in
354e9554658Smrg		     d????-?r-*) different_mode=700;;
355e9554658Smrg		     d????-?--*) different_mode=755;;
356e9554658Smrg		     *) false;;
357e9554658Smrg		   esac &&
358e9554658Smrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
359e9554658Smrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
360e9554658Smrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
361e9554658Smrg		   }
362e9554658Smrg		 }
363e9554658Smrg	      then posix_mkdir=:
364e9554658Smrg	      fi
365e9554658Smrg	      rmdir "$tmpdir/d" "$tmpdir"
366e9554658Smrg	    else
367e9554658Smrg	      # Remove any dirs left behind by ancient mkdir implementations.
368e9554658Smrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
369e9554658Smrg	    fi
370e9554658Smrg	    trap '' 0;;
371e9554658Smrg	esac;;
372e9554658Smrg    esac
373e9554658Smrg
374e9554658Smrg    if
375e9554658Smrg      $posix_mkdir && (
376e9554658Smrg	umask $mkdir_umask &&
377e9554658Smrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
378e9554658Smrg      )
379e9554658Smrg    then :
380e9554658Smrg    else
381e9554658Smrg
382e9554658Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
383e9554658Smrg      # or it failed possibly due to a race condition.  Create the
384e9554658Smrg      # directory the slow way, step by step, checking for races as we go.
385e9554658Smrg
386e9554658Smrg      case $dstdir in
387c2b339b4Smrg	/*) prefix='/';;
388c2b339b4Smrg	-*) prefix='./';;
389c2b339b4Smrg	*)  prefix='';;
390e9554658Smrg      esac
391e9554658Smrg
392c2b339b4Smrg      eval "$initialize_posix_glob"
393e9554658Smrg
394e9554658Smrg      oIFS=$IFS
395e9554658Smrg      IFS=/
396c2b339b4Smrg      $posix_glob set -f
397e9554658Smrg      set fnord $dstdir
398e9554658Smrg      shift
399c2b339b4Smrg      $posix_glob set +f
400e9554658Smrg      IFS=$oIFS
401e9554658Smrg
402e9554658Smrg      prefixes=
403e9554658Smrg
404e9554658Smrg      for d
405e9554658Smrg      do
406e9554658Smrg	test -z "$d" && continue
407e9554658Smrg
408e9554658Smrg	prefix=$prefix$d
409e9554658Smrg	if test -d "$prefix"; then
410e9554658Smrg	  prefixes=
411e9554658Smrg	else
412e9554658Smrg	  if $posix_mkdir; then
413e9554658Smrg	    (umask=$mkdir_umask &&
414e9554658Smrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
415e9554658Smrg	    # Don't fail if two instances are running concurrently.
416e9554658Smrg	    test -d "$prefix" || exit 1
417e9554658Smrg	  else
418e9554658Smrg	    case $prefix in
419e9554658Smrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
420e9554658Smrg	      *) qprefix=$prefix;;
421e9554658Smrg	    esac
422e9554658Smrg	    prefixes="$prefixes '$qprefix'"
423e9554658Smrg	  fi
424e9554658Smrg	fi
425e9554658Smrg	prefix=$prefix/
426e9554658Smrg      done
427e9554658Smrg
428e9554658Smrg      if test -n "$prefixes"; then
429e9554658Smrg	# Don't fail if two instances are running concurrently.
430e9554658Smrg	(umask $mkdir_umask &&
431e9554658Smrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
432e9554658Smrg	  test -d "$dstdir" || exit 1
433e9554658Smrg	obsolete_mkdir_used=true
434e9554658Smrg      fi
435e9554658Smrg    fi
436e9554658Smrg  fi
437e9554658Smrg
438e9554658Smrg  if test -n "$dir_arg"; then
439e9554658Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
440e9554658Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
441e9554658Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
442e9554658Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
443e9554658Smrg  else
444e9554658Smrg
445e9554658Smrg    # Make a couple of temp file names in the proper directory.
446e9554658Smrg    dsttmp=$dstdir/_inst.$$_
447e9554658Smrg    rmtmp=$dstdir/_rm.$$_
448e9554658Smrg
449e9554658Smrg    # Trap to clean up those temp files at exit.
450e9554658Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
451e9554658Smrg
452e9554658Smrg    # Copy the file name to the temp name.
453e9554658Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
454e9554658Smrg
455e9554658Smrg    # and set any options; do chmod last to preserve setuid bits.
456e9554658Smrg    #
457e9554658Smrg    # If any of these fail, we abort the whole thing.  If we want to
458e9554658Smrg    # ignore errors from any of these, just make sure not to ignore
459e9554658Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
460e9554658Smrg    #
461c2b339b4Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
462c2b339b4Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
463c2b339b4Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
464c2b339b4Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
465c2b339b4Smrg
466c2b339b4Smrg    # If -C, don't bother to copy if it wouldn't change the file.
467c2b339b4Smrg    if $copy_on_change &&
468c2b339b4Smrg       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
469c2b339b4Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
470c2b339b4Smrg
471c2b339b4Smrg       eval "$initialize_posix_glob" &&
472c2b339b4Smrg       $posix_glob set -f &&
473c2b339b4Smrg       set X $old && old=:$2:$4:$5:$6 &&
474c2b339b4Smrg       set X $new && new=:$2:$4:$5:$6 &&
475c2b339b4Smrg       $posix_glob set +f &&
476c2b339b4Smrg
477c2b339b4Smrg       test "$old" = "$new" &&
478c2b339b4Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
479c2b339b4Smrg    then
480c2b339b4Smrg      rm -f "$dsttmp"
481c2b339b4Smrg    else
482c2b339b4Smrg      # Rename the file to the real destination.
483c2b339b4Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
484c2b339b4Smrg
485c2b339b4Smrg      # The rename failed, perhaps because mv can't rename something else
486c2b339b4Smrg      # to itself, or perhaps because mv is so ancient that it does not
487c2b339b4Smrg      # support -f.
488c2b339b4Smrg      {
489c2b339b4Smrg	# Now remove or move aside any old file at destination location.
490c2b339b4Smrg	# We try this two ways since rm can't unlink itself on some
491c2b339b4Smrg	# systems and the destination file might be busy for other
492c2b339b4Smrg	# reasons.  In this case, the final cleanup might fail but the new
493c2b339b4Smrg	# file should still install successfully.
494c2b339b4Smrg	{
495c2b339b4Smrg	  test ! -f "$dst" ||
496c2b339b4Smrg	  $doit $rmcmd -f "$dst" 2>/dev/null ||
497c2b339b4Smrg	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
498c2b339b4Smrg	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
499c2b339b4Smrg	  } ||
500c2b339b4Smrg	  { echo "$0: cannot unlink or rename $dst" >&2
501c2b339b4Smrg	    (exit 1); exit 1
502c2b339b4Smrg	  }
503c2b339b4Smrg	} &&
504c2b339b4Smrg
505c2b339b4Smrg	# Now rename the file to the real destination.
506c2b339b4Smrg	$doit $mvcmd "$dsttmp" "$dst"
507c2b339b4Smrg      }
508c2b339b4Smrg    fi || exit 1
509e9554658Smrg
510e9554658Smrg    trap '' 0
511e9554658Smrg  fi
512e9554658Smrgdone
513e9554658Smrg
514e9554658Smrg# Local variables:
515e9554658Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
516e9554658Smrg# time-stamp-start: "scriptversion="
517e9554658Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
518c2b339b4Smrg# time-stamp-time-zone: "UTC"
519c2b339b4Smrg# time-stamp-end: "; # UTC"
520e9554658Smrg# End:
521