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