install-sh revision 7e6533d5
1067610f1Smrg#!/bin/sh
2067610f1Smrg# install - install a program, script, or datafile
3067610f1Smrg
47e6533d5Smrgscriptversion=2009-04-28.21; # UTC
5067610f1Smrg
6067610f1Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7067610f1Smrg# later released in X11R6 (xc/config/util/install.sh) with the
8067610f1Smrg# following copyright and license.
9067610f1Smrg#
10067610f1Smrg# Copyright (C) 1994 X Consortium
11067610f1Smrg#
12067610f1Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13067610f1Smrg# of this software and associated documentation files (the "Software"), to
14067610f1Smrg# deal in the Software without restriction, including without limitation the
15067610f1Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16067610f1Smrg# sell copies of the Software, and to permit persons to whom the Software is
17067610f1Smrg# furnished to do so, subject to the following conditions:
18067610f1Smrg#
19067610f1Smrg# The above copyright notice and this permission notice shall be included in
20067610f1Smrg# all copies or substantial portions of the Software.
21067610f1Smrg#
22067610f1Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23067610f1Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24067610f1Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25067610f1Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26067610f1Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27067610f1Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28067610f1Smrg#
29067610f1Smrg# Except as contained in this notice, the name of the X Consortium shall not
30067610f1Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
31067610f1Smrg# ings in this Software without prior written authorization from the X Consor-
32067610f1Smrg# tium.
33067610f1Smrg#
34067610f1Smrg#
35067610f1Smrg# FSF changes to this file are in the public domain.
36067610f1Smrg#
37067610f1Smrg# Calling this script install-sh is preferred over install.sh, to prevent
38067610f1Smrg# `make' implicit rules from creating a file called install from it
39067610f1Smrg# when there is no Makefile.
40067610f1Smrg#
41067610f1Smrg# This script is compatible with the BSD install script, but was written
42067610f1Smrg# from scratch.
43067610f1Smrg
44067610f1Smrgnl='
45067610f1Smrg'
46067610f1SmrgIFS=" ""	$nl"
47067610f1Smrg
48067610f1Smrg# set DOITPROG to echo to test this script
49067610f1Smrg
50067610f1Smrg# Don't use :- since 4.3BSD and earlier shells don't like it.
517e6533d5Smrgdoit=${DOITPROG-}
52067610f1Smrgif test -z "$doit"; then
53067610f1Smrg  doit_exec=exec
54067610f1Smrgelse
55067610f1Smrg  doit_exec=$doit
56067610f1Smrgfi
57067610f1Smrg
58067610f1Smrg# Put in absolute file names if you don't have them in your path;
59067610f1Smrg# or use environment vars.
60067610f1Smrg
617e6533d5Smrgchgrpprog=${CHGRPPROG-chgrp}
627e6533d5Smrgchmodprog=${CHMODPROG-chmod}
637e6533d5Smrgchownprog=${CHOWNPROG-chown}
647e6533d5Smrgcmpprog=${CMPPROG-cmp}
657e6533d5Smrgcpprog=${CPPROG-cp}
667e6533d5Smrgmkdirprog=${MKDIRPROG-mkdir}
677e6533d5Smrgmvprog=${MVPROG-mv}
687e6533d5Smrgrmprog=${RMPROG-rm}
697e6533d5Smrgstripprog=${STRIPPROG-strip}
707e6533d5Smrg
717e6533d5Smrgposix_glob='?'
727e6533d5Smrginitialize_posix_glob='
737e6533d5Smrg  test "$posix_glob" != "?" || {
747e6533d5Smrg    if (set -f) 2>/dev/null; then
757e6533d5Smrg      posix_glob=
767e6533d5Smrg    else
777e6533d5Smrg      posix_glob=:
787e6533d5Smrg    fi
797e6533d5Smrg  }
807e6533d5Smrg'
81067610f1Smrg
82067610f1Smrgposix_mkdir=
83067610f1Smrg
84067610f1Smrg# Desired mode of installed file.
85067610f1Smrgmode=0755
86067610f1Smrg
877e6533d5Smrgchgrpcmd=
88067610f1Smrgchmodcmd=$chmodprog
89067610f1Smrgchowncmd=
907e6533d5Smrgmvcmd=$mvprog
91067610f1Smrgrmcmd="$rmprog -f"
927e6533d5Smrgstripcmd=
937e6533d5Smrg
94067610f1Smrgsrc=
95067610f1Smrgdst=
96067610f1Smrgdir_arg=
977e6533d5Smrgdst_arg=
987e6533d5Smrg
997e6533d5Smrgcopy_on_change=false
100067610f1Smrgno_target_directory=
101067610f1Smrg
1027e6533d5Smrgusage="\
1037e6533d5SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
104067610f1Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
105067610f1Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
106067610f1Smrg   or: $0 [OPTION]... -d DIRECTORIES...
107067610f1Smrg
108067610f1SmrgIn the 1st form, copy SRCFILE to DSTFILE.
109067610f1SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
110067610f1SmrgIn the 4th, create DIRECTORIES.
111067610f1Smrg
112067610f1SmrgOptions:
1137e6533d5Smrg     --help     display this help and exit.
1147e6533d5Smrg     --version  display version info and exit.
1157e6533d5Smrg
1167e6533d5Smrg  -c            (ignored)
1177e6533d5Smrg  -C            install only if different (preserve the last data modification time)
1187e6533d5Smrg  -d            create directories instead of installing files.
1197e6533d5Smrg  -g GROUP      $chgrpprog installed files to GROUP.
1207e6533d5Smrg  -m MODE       $chmodprog installed files to MODE.
1217e6533d5Smrg  -o USER       $chownprog installed files to USER.
1227e6533d5Smrg  -s            $stripprog installed files.
1237e6533d5Smrg  -t DIRECTORY  install into DIRECTORY.
1247e6533d5Smrg  -T            report an error if DSTFILE is a directory.
125067610f1Smrg
126067610f1SmrgEnvironment variables override the default commands:
1277e6533d5Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
1287e6533d5Smrg  RMPROG STRIPPROG
129067610f1Smrg"
130067610f1Smrg
131067610f1Smrgwhile test $# -ne 0; do
132067610f1Smrg  case $1 in
1337e6533d5Smrg    -c) ;;
1347e6533d5Smrg
1357e6533d5Smrg    -C) copy_on_change=true;;
136067610f1Smrg
1377e6533d5Smrg    -d) dir_arg=true;;
138067610f1Smrg
139067610f1Smrg    -g) chgrpcmd="$chgrpprog $2"
1407e6533d5Smrg	shift;;
141067610f1Smrg
142067610f1Smrg    --help) echo "$usage"; exit $?;;
143067610f1Smrg
144067610f1Smrg    -m) mode=$2
145067610f1Smrg	case $mode in
146067610f1Smrg	  *' '* | *'	'* | *'
147067610f1Smrg'*	  | *'*'* | *'?'* | *'['*)
148067610f1Smrg	    echo "$0: invalid mode: $mode" >&2
149067610f1Smrg	    exit 1;;
150067610f1Smrg	esac
1517e6533d5Smrg	shift;;
152067610f1Smrg
153067610f1Smrg    -o) chowncmd="$chownprog $2"
1547e6533d5Smrg	shift;;
155067610f1Smrg
1567e6533d5Smrg    -s) stripcmd=$stripprog;;
157067610f1Smrg
1587e6533d5Smrg    -t) dst_arg=$2
1597e6533d5Smrg	shift;;
160067610f1Smrg
1617e6533d5Smrg    -T) no_target_directory=true;;
162067610f1Smrg
163067610f1Smrg    --version) echo "$0 $scriptversion"; exit $?;;
164067610f1Smrg
165067610f1Smrg    --)	shift
166067610f1Smrg	break;;
167067610f1Smrg
168067610f1Smrg    -*)	echo "$0: invalid option: $1" >&2
169067610f1Smrg	exit 1;;
170067610f1Smrg
171067610f1Smrg    *)  break;;
172067610f1Smrg  esac
1737e6533d5Smrg  shift
174067610f1Smrgdone
175067610f1Smrg
1767e6533d5Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
177067610f1Smrg  # When -d is used, all remaining arguments are directories to create.
178067610f1Smrg  # When -t is used, the destination is already specified.
179067610f1Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
180067610f1Smrg  for arg
181067610f1Smrg  do
1827e6533d5Smrg    if test -n "$dst_arg"; then
183067610f1Smrg      # $@ is not empty: it contains at least $arg.
1847e6533d5Smrg      set fnord "$@" "$dst_arg"
185067610f1Smrg      shift # fnord
186067610f1Smrg    fi
187067610f1Smrg    shift # arg
1887e6533d5Smrg    dst_arg=$arg
189067610f1Smrg  done
190067610f1Smrgfi
191067610f1Smrg
192067610f1Smrgif test $# -eq 0; then
193067610f1Smrg  if test -z "$dir_arg"; then
194067610f1Smrg    echo "$0: no input file specified." >&2
195067610f1Smrg    exit 1
196067610f1Smrg  fi
197067610f1Smrg  # It's OK to call `install-sh -d' without argument.
198067610f1Smrg  # This can happen when creating conditional directories.
199067610f1Smrg  exit 0
200067610f1Smrgfi
201067610f1Smrg
202067610f1Smrgif test -z "$dir_arg"; then
203067610f1Smrg  trap '(exit $?); exit' 1 2 13 15
204067610f1Smrg
205067610f1Smrg  # Set umask so as not to create temps with too-generous modes.
206067610f1Smrg  # However, 'strip' requires both read and write access to temps.
207067610f1Smrg  case $mode in
208067610f1Smrg    # Optimize common cases.
209067610f1Smrg    *644) cp_umask=133;;
210067610f1Smrg    *755) cp_umask=22;;
211067610f1Smrg
212067610f1Smrg    *[0-7])
213067610f1Smrg      if test -z "$stripcmd"; then
214067610f1Smrg	u_plus_rw=
215067610f1Smrg      else
216067610f1Smrg	u_plus_rw='% 200'
217067610f1Smrg      fi
218067610f1Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
219067610f1Smrg    *)
220067610f1Smrg      if test -z "$stripcmd"; then
221067610f1Smrg	u_plus_rw=
222067610f1Smrg      else
223067610f1Smrg	u_plus_rw=,u+rw
224067610f1Smrg      fi
225067610f1Smrg      cp_umask=$mode$u_plus_rw;;
226067610f1Smrg  esac
227067610f1Smrgfi
228067610f1Smrg
229067610f1Smrgfor src
230067610f1Smrgdo
231067610f1Smrg  # Protect names starting with `-'.
232067610f1Smrg  case $src in
2337e6533d5Smrg    -*) src=./$src;;
234067610f1Smrg  esac
235067610f1Smrg
236067610f1Smrg  if test -n "$dir_arg"; then
237067610f1Smrg    dst=$src
238067610f1Smrg    dstdir=$dst
239067610f1Smrg    test -d "$dstdir"
240067610f1Smrg    dstdir_status=$?
241067610f1Smrg  else
242067610f1Smrg
243067610f1Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
244067610f1Smrg    # might cause directories to be created, which would be especially bad
245067610f1Smrg    # if $src (and thus $dsttmp) contains '*'.
246067610f1Smrg    if test ! -f "$src" && test ! -d "$src"; then
247067610f1Smrg      echo "$0: $src does not exist." >&2
248067610f1Smrg      exit 1
249067610f1Smrg    fi
250067610f1Smrg
2517e6533d5Smrg    if test -z "$dst_arg"; then
252067610f1Smrg      echo "$0: no destination specified." >&2
253067610f1Smrg      exit 1
254067610f1Smrg    fi
255067610f1Smrg
2567e6533d5Smrg    dst=$dst_arg
257067610f1Smrg    # Protect names starting with `-'.
258067610f1Smrg    case $dst in
2597e6533d5Smrg      -*) dst=./$dst;;
260067610f1Smrg    esac
261067610f1Smrg
262067610f1Smrg    # If destination is a directory, append the input filename; won't work
263067610f1Smrg    # if double slashes aren't ignored.
264067610f1Smrg    if test -d "$dst"; then
265067610f1Smrg      if test -n "$no_target_directory"; then
2667e6533d5Smrg	echo "$0: $dst_arg: Is a directory" >&2
267067610f1Smrg	exit 1
268067610f1Smrg      fi
269067610f1Smrg      dstdir=$dst
270067610f1Smrg      dst=$dstdir/`basename "$src"`
271067610f1Smrg      dstdir_status=0
272067610f1Smrg    else
273067610f1Smrg      # Prefer dirname, but fall back on a substitute if dirname fails.
274067610f1Smrg      dstdir=`
275067610f1Smrg	(dirname "$dst") 2>/dev/null ||
276067610f1Smrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
277067610f1Smrg	     X"$dst" : 'X\(//\)[^/]' \| \
278067610f1Smrg	     X"$dst" : 'X\(//\)$' \| \
279067610f1Smrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
280067610f1Smrg	echo X"$dst" |
281067610f1Smrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
282067610f1Smrg		   s//\1/
283067610f1Smrg		   q
284067610f1Smrg		 }
285067610f1Smrg		 /^X\(\/\/\)[^/].*/{
286067610f1Smrg		   s//\1/
287067610f1Smrg		   q
288067610f1Smrg		 }
289067610f1Smrg		 /^X\(\/\/\)$/{
290067610f1Smrg		   s//\1/
291067610f1Smrg		   q
292067610f1Smrg		 }
293067610f1Smrg		 /^X\(\/\).*/{
294067610f1Smrg		   s//\1/
295067610f1Smrg		   q
296067610f1Smrg		 }
297067610f1Smrg		 s/.*/./; q'
298067610f1Smrg      `
299067610f1Smrg
300067610f1Smrg      test -d "$dstdir"
301067610f1Smrg      dstdir_status=$?
302067610f1Smrg    fi
303067610f1Smrg  fi
304067610f1Smrg
305067610f1Smrg  obsolete_mkdir_used=false
306067610f1Smrg
307067610f1Smrg  if test $dstdir_status != 0; then
308067610f1Smrg    case $posix_mkdir in
309067610f1Smrg      '')
310067610f1Smrg	# Create intermediate dirs using mode 755 as modified by the umask.
311067610f1Smrg	# This is like FreeBSD 'install' as of 1997-10-28.
312067610f1Smrg	umask=`umask`
313067610f1Smrg	case $stripcmd.$umask in
314067610f1Smrg	  # Optimize common cases.
315067610f1Smrg	  *[2367][2367]) mkdir_umask=$umask;;
316067610f1Smrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
317067610f1Smrg
318067610f1Smrg	  *[0-7])
319067610f1Smrg	    mkdir_umask=`expr $umask + 22 \
320067610f1Smrg	      - $umask % 100 % 40 + $umask % 20 \
321067610f1Smrg	      - $umask % 10 % 4 + $umask % 2
322067610f1Smrg	    `;;
323067610f1Smrg	  *) mkdir_umask=$umask,go-w;;
324067610f1Smrg	esac
325067610f1Smrg
326067610f1Smrg	# With -d, create the new directory with the user-specified mode.
327067610f1Smrg	# Otherwise, rely on $mkdir_umask.
328067610f1Smrg	if test -n "$dir_arg"; then
329067610f1Smrg	  mkdir_mode=-m$mode
330067610f1Smrg	else
331067610f1Smrg	  mkdir_mode=
332067610f1Smrg	fi
333067610f1Smrg
334067610f1Smrg	posix_mkdir=false
335067610f1Smrg	case $umask in
336067610f1Smrg	  *[123567][0-7][0-7])
337067610f1Smrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
338067610f1Smrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
339067610f1Smrg	    ;;
340067610f1Smrg	  *)
341067610f1Smrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
342067610f1Smrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
343067610f1Smrg
344067610f1Smrg	    if (umask $mkdir_umask &&
345067610f1Smrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
346067610f1Smrg	    then
347067610f1Smrg	      if test -z "$dir_arg" || {
348067610f1Smrg		   # Check for POSIX incompatibilities with -m.
349067610f1Smrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
350067610f1Smrg		   # other-writeable bit of parent directory when it shouldn't.
351067610f1Smrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
352067610f1Smrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
353067610f1Smrg		   case $ls_ld_tmpdir in
354067610f1Smrg		     d????-?r-*) different_mode=700;;
355067610f1Smrg		     d????-?--*) different_mode=755;;
356067610f1Smrg		     *) false;;
357067610f1Smrg		   esac &&
358067610f1Smrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
359067610f1Smrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
360067610f1Smrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
361067610f1Smrg		   }
362067610f1Smrg		 }
363067610f1Smrg	      then posix_mkdir=:
364067610f1Smrg	      fi
365067610f1Smrg	      rmdir "$tmpdir/d" "$tmpdir"
366067610f1Smrg	    else
367067610f1Smrg	      # Remove any dirs left behind by ancient mkdir implementations.
368067610f1Smrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
369067610f1Smrg	    fi
370067610f1Smrg	    trap '' 0;;
371067610f1Smrg	esac;;
372067610f1Smrg    esac
373067610f1Smrg
374067610f1Smrg    if
375067610f1Smrg      $posix_mkdir && (
376067610f1Smrg	umask $mkdir_umask &&
377067610f1Smrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
378067610f1Smrg      )
379067610f1Smrg    then :
380067610f1Smrg    else
381067610f1Smrg
382067610f1Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
383067610f1Smrg      # or it failed possibly due to a race condition.  Create the
384067610f1Smrg      # directory the slow way, step by step, checking for races as we go.
385067610f1Smrg
386067610f1Smrg      case $dstdir in
3877e6533d5Smrg	/*) prefix='/';;
3887e6533d5Smrg	-*) prefix='./';;
3897e6533d5Smrg	*)  prefix='';;
390067610f1Smrg      esac
391067610f1Smrg
3927e6533d5Smrg      eval "$initialize_posix_glob"
393067610f1Smrg
394067610f1Smrg      oIFS=$IFS
395067610f1Smrg      IFS=/
3967e6533d5Smrg      $posix_glob set -f
397067610f1Smrg      set fnord $dstdir
398067610f1Smrg      shift
3997e6533d5Smrg      $posix_glob set +f
400067610f1Smrg      IFS=$oIFS
401067610f1Smrg
402067610f1Smrg      prefixes=
403067610f1Smrg
404067610f1Smrg      for d
405067610f1Smrg      do
406067610f1Smrg	test -z "$d" && continue
407067610f1Smrg
408067610f1Smrg	prefix=$prefix$d
409067610f1Smrg	if test -d "$prefix"; then
410067610f1Smrg	  prefixes=
411067610f1Smrg	else
412067610f1Smrg	  if $posix_mkdir; then
413067610f1Smrg	    (umask=$mkdir_umask &&
414067610f1Smrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
415067610f1Smrg	    # Don't fail if two instances are running concurrently.
416067610f1Smrg	    test -d "$prefix" || exit 1
417067610f1Smrg	  else
418067610f1Smrg	    case $prefix in
419067610f1Smrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
420067610f1Smrg	      *) qprefix=$prefix;;
421067610f1Smrg	    esac
422067610f1Smrg	    prefixes="$prefixes '$qprefix'"
423067610f1Smrg	  fi
424067610f1Smrg	fi
425067610f1Smrg	prefix=$prefix/
426067610f1Smrg      done
427067610f1Smrg
428067610f1Smrg      if test -n "$prefixes"; then
429067610f1Smrg	# Don't fail if two instances are running concurrently.
430067610f1Smrg	(umask $mkdir_umask &&
431067610f1Smrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
432067610f1Smrg	  test -d "$dstdir" || exit 1
433067610f1Smrg	obsolete_mkdir_used=true
434067610f1Smrg      fi
435067610f1Smrg    fi
436067610f1Smrg  fi
437067610f1Smrg
438067610f1Smrg  if test -n "$dir_arg"; then
439067610f1Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
440067610f1Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
441067610f1Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
442067610f1Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
443067610f1Smrg  else
444067610f1Smrg
445067610f1Smrg    # Make a couple of temp file names in the proper directory.
446067610f1Smrg    dsttmp=$dstdir/_inst.$$_
447067610f1Smrg    rmtmp=$dstdir/_rm.$$_
448067610f1Smrg
449067610f1Smrg    # Trap to clean up those temp files at exit.
450067610f1Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
451067610f1Smrg
452067610f1Smrg    # Copy the file name to the temp name.
453067610f1Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
454067610f1Smrg
455067610f1Smrg    # and set any options; do chmod last to preserve setuid bits.
456067610f1Smrg    #
457067610f1Smrg    # If any of these fail, we abort the whole thing.  If we want to
458067610f1Smrg    # ignore errors from any of these, just make sure not to ignore
459067610f1Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
460067610f1Smrg    #
4617e6533d5Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
4627e6533d5Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
4637e6533d5Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
4647e6533d5Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4657e6533d5Smrg
4667e6533d5Smrg    # If -C, don't bother to copy if it wouldn't change the file.
4677e6533d5Smrg    if $copy_on_change &&
4687e6533d5Smrg       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
4697e6533d5Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
4707e6533d5Smrg
4717e6533d5Smrg       eval "$initialize_posix_glob" &&
4727e6533d5Smrg       $posix_glob set -f &&
4737e6533d5Smrg       set X $old && old=:$2:$4:$5:$6 &&
4747e6533d5Smrg       set X $new && new=:$2:$4:$5:$6 &&
4757e6533d5Smrg       $posix_glob set +f &&
4767e6533d5Smrg
4777e6533d5Smrg       test "$old" = "$new" &&
4787e6533d5Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
4797e6533d5Smrg    then
4807e6533d5Smrg      rm -f "$dsttmp"
4817e6533d5Smrg    else
4827e6533d5Smrg      # Rename the file to the real destination.
4837e6533d5Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
4847e6533d5Smrg
4857e6533d5Smrg      # The rename failed, perhaps because mv can't rename something else
4867e6533d5Smrg      # to itself, or perhaps because mv is so ancient that it does not
4877e6533d5Smrg      # support -f.
4887e6533d5Smrg      {
4897e6533d5Smrg	# Now remove or move aside any old file at destination location.
4907e6533d5Smrg	# We try this two ways since rm can't unlink itself on some
4917e6533d5Smrg	# systems and the destination file might be busy for other
4927e6533d5Smrg	# reasons.  In this case, the final cleanup might fail but the new
4937e6533d5Smrg	# file should still install successfully.
4947e6533d5Smrg	{
4957e6533d5Smrg	  test ! -f "$dst" ||
4967e6533d5Smrg	  $doit $rmcmd -f "$dst" 2>/dev/null ||
4977e6533d5Smrg	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
4987e6533d5Smrg	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
4997e6533d5Smrg	  } ||
5007e6533d5Smrg	  { echo "$0: cannot unlink or rename $dst" >&2
5017e6533d5Smrg	    (exit 1); exit 1
5027e6533d5Smrg	  }
5037e6533d5Smrg	} &&
5047e6533d5Smrg
5057e6533d5Smrg	# Now rename the file to the real destination.
5067e6533d5Smrg	$doit $mvcmd "$dsttmp" "$dst"
5077e6533d5Smrg      }
5087e6533d5Smrg    fi || exit 1
509067610f1Smrg
510067610f1Smrg    trap '' 0
511067610f1Smrg  fi
512067610f1Smrgdone
513067610f1Smrg
514067610f1Smrg# Local variables:
515067610f1Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
516067610f1Smrg# time-stamp-start: "scriptversion="
517067610f1Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
5187e6533d5Smrg# time-stamp-time-zone: "UTC"
5197e6533d5Smrg# time-stamp-end: "; # UTC"
520067610f1Smrg# End:
521