install-sh revision e9554658
1e9554658Smrg#!/bin/sh
2e9554658Smrg# install - install a program, script, or datafile
3e9554658Smrg
4e9554658Smrgscriptversion=2006-10-14.15
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.
51e9554658Smrgdoit="${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
61e9554658Smrgmvprog="${MVPROG-mv}"
62e9554658Smrgcpprog="${CPPROG-cp}"
63e9554658Smrgchmodprog="${CHMODPROG-chmod}"
64e9554658Smrgchownprog="${CHOWNPROG-chown}"
65e9554658Smrgchgrpprog="${CHGRPPROG-chgrp}"
66e9554658Smrgstripprog="${STRIPPROG-strip}"
67e9554658Smrgrmprog="${RMPROG-rm}"
68e9554658Smrgmkdirprog="${MKDIRPROG-mkdir}"
69e9554658Smrg
70e9554658Smrgposix_glob=
71e9554658Smrgposix_mkdir=
72e9554658Smrg
73e9554658Smrg# Desired mode of installed file.
74e9554658Smrgmode=0755
75e9554658Smrg
76e9554658Smrgchmodcmd=$chmodprog
77e9554658Smrgchowncmd=
78e9554658Smrgchgrpcmd=
79e9554658Smrgstripcmd=
80e9554658Smrgrmcmd="$rmprog -f"
81e9554658Smrgmvcmd="$mvprog"
82e9554658Smrgsrc=
83e9554658Smrgdst=
84e9554658Smrgdir_arg=
85e9554658Smrgdstarg=
86e9554658Smrgno_target_directory=
87e9554658Smrg
88e9554658Smrgusage="Usage: $0 [OPTION]... [-T] SRCFILE DSTFILE
89e9554658Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
90e9554658Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
91e9554658Smrg   or: $0 [OPTION]... -d DIRECTORIES...
92e9554658Smrg
93e9554658SmrgIn the 1st form, copy SRCFILE to DSTFILE.
94e9554658SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
95e9554658SmrgIn the 4th, create DIRECTORIES.
96e9554658Smrg
97e9554658SmrgOptions:
98e9554658Smrg-c         (ignored)
99e9554658Smrg-d         create directories instead of installing files.
100e9554658Smrg-g GROUP   $chgrpprog installed files to GROUP.
101e9554658Smrg-m MODE    $chmodprog installed files to MODE.
102e9554658Smrg-o USER    $chownprog installed files to USER.
103e9554658Smrg-s         $stripprog installed files.
104e9554658Smrg-t DIRECTORY  install into DIRECTORY.
105e9554658Smrg-T         report an error if DSTFILE is a directory.
106e9554658Smrg--help     display this help and exit.
107e9554658Smrg--version  display version info and exit.
108e9554658Smrg
109e9554658SmrgEnvironment variables override the default commands:
110e9554658Smrg  CHGRPPROG CHMODPROG CHOWNPROG CPPROG MKDIRPROG MVPROG RMPROG STRIPPROG
111e9554658Smrg"
112e9554658Smrg
113e9554658Smrgwhile test $# -ne 0; do
114e9554658Smrg  case $1 in
115e9554658Smrg    -c) shift
116e9554658Smrg        continue;;
117e9554658Smrg
118e9554658Smrg    -d) dir_arg=true
119e9554658Smrg        shift
120e9554658Smrg        continue;;
121e9554658Smrg
122e9554658Smrg    -g) chgrpcmd="$chgrpprog $2"
123e9554658Smrg        shift
124e9554658Smrg        shift
125e9554658Smrg        continue;;
126e9554658Smrg
127e9554658Smrg    --help) echo "$usage"; exit $?;;
128e9554658Smrg
129e9554658Smrg    -m) mode=$2
130e9554658Smrg        shift
131e9554658Smrg        shift
132e9554658Smrg	case $mode in
133e9554658Smrg	  *' '* | *'	'* | *'
134e9554658Smrg'*	  | *'*'* | *'?'* | *'['*)
135e9554658Smrg	    echo "$0: invalid mode: $mode" >&2
136e9554658Smrg	    exit 1;;
137e9554658Smrg	esac
138e9554658Smrg        continue;;
139e9554658Smrg
140e9554658Smrg    -o) chowncmd="$chownprog $2"
141e9554658Smrg        shift
142e9554658Smrg        shift
143e9554658Smrg        continue;;
144e9554658Smrg
145e9554658Smrg    -s) stripcmd=$stripprog
146e9554658Smrg        shift
147e9554658Smrg        continue;;
148e9554658Smrg
149e9554658Smrg    -t) dstarg=$2
150e9554658Smrg	shift
151e9554658Smrg	shift
152e9554658Smrg	continue;;
153e9554658Smrg
154e9554658Smrg    -T) no_target_directory=true
155e9554658Smrg	shift
156e9554658Smrg	continue;;
157e9554658Smrg
158e9554658Smrg    --version) echo "$0 $scriptversion"; exit $?;;
159e9554658Smrg
160e9554658Smrg    --)	shift
161e9554658Smrg	break;;
162e9554658Smrg
163e9554658Smrg    -*)	echo "$0: invalid option: $1" >&2
164e9554658Smrg	exit 1;;
165e9554658Smrg
166e9554658Smrg    *)  break;;
167e9554658Smrg  esac
168e9554658Smrgdone
169e9554658Smrg
170e9554658Smrgif test $# -ne 0 && test -z "$dir_arg$dstarg"; then
171e9554658Smrg  # When -d is used, all remaining arguments are directories to create.
172e9554658Smrg  # When -t is used, the destination is already specified.
173e9554658Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
174e9554658Smrg  for arg
175e9554658Smrg  do
176e9554658Smrg    if test -n "$dstarg"; then
177e9554658Smrg      # $@ is not empty: it contains at least $arg.
178e9554658Smrg      set fnord "$@" "$dstarg"
179e9554658Smrg      shift # fnord
180e9554658Smrg    fi
181e9554658Smrg    shift # arg
182e9554658Smrg    dstarg=$arg
183e9554658Smrg  done
184e9554658Smrgfi
185e9554658Smrg
186e9554658Smrgif test $# -eq 0; then
187e9554658Smrg  if test -z "$dir_arg"; then
188e9554658Smrg    echo "$0: no input file specified." >&2
189e9554658Smrg    exit 1
190e9554658Smrg  fi
191e9554658Smrg  # It's OK to call `install-sh -d' without argument.
192e9554658Smrg  # This can happen when creating conditional directories.
193e9554658Smrg  exit 0
194e9554658Smrgfi
195e9554658Smrg
196e9554658Smrgif test -z "$dir_arg"; then
197e9554658Smrg  trap '(exit $?); exit' 1 2 13 15
198e9554658Smrg
199e9554658Smrg  # Set umask so as not to create temps with too-generous modes.
200e9554658Smrg  # However, 'strip' requires both read and write access to temps.
201e9554658Smrg  case $mode in
202e9554658Smrg    # Optimize common cases.
203e9554658Smrg    *644) cp_umask=133;;
204e9554658Smrg    *755) cp_umask=22;;
205e9554658Smrg
206e9554658Smrg    *[0-7])
207e9554658Smrg      if test -z "$stripcmd"; then
208e9554658Smrg	u_plus_rw=
209e9554658Smrg      else
210e9554658Smrg	u_plus_rw='% 200'
211e9554658Smrg      fi
212e9554658Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
213e9554658Smrg    *)
214e9554658Smrg      if test -z "$stripcmd"; then
215e9554658Smrg	u_plus_rw=
216e9554658Smrg      else
217e9554658Smrg	u_plus_rw=,u+rw
218e9554658Smrg      fi
219e9554658Smrg      cp_umask=$mode$u_plus_rw;;
220e9554658Smrg  esac
221e9554658Smrgfi
222e9554658Smrg
223e9554658Smrgfor src
224e9554658Smrgdo
225e9554658Smrg  # Protect names starting with `-'.
226e9554658Smrg  case $src in
227e9554658Smrg    -*) src=./$src ;;
228e9554658Smrg  esac
229e9554658Smrg
230e9554658Smrg  if test -n "$dir_arg"; then
231e9554658Smrg    dst=$src
232e9554658Smrg    dstdir=$dst
233e9554658Smrg    test -d "$dstdir"
234e9554658Smrg    dstdir_status=$?
235e9554658Smrg  else
236e9554658Smrg
237e9554658Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
238e9554658Smrg    # might cause directories to be created, which would be especially bad
239e9554658Smrg    # if $src (and thus $dsttmp) contains '*'.
240e9554658Smrg    if test ! -f "$src" && test ! -d "$src"; then
241e9554658Smrg      echo "$0: $src does not exist." >&2
242e9554658Smrg      exit 1
243e9554658Smrg    fi
244e9554658Smrg
245e9554658Smrg    if test -z "$dstarg"; then
246e9554658Smrg      echo "$0: no destination specified." >&2
247e9554658Smrg      exit 1
248e9554658Smrg    fi
249e9554658Smrg
250e9554658Smrg    dst=$dstarg
251e9554658Smrg    # Protect names starting with `-'.
252e9554658Smrg    case $dst in
253e9554658Smrg      -*) dst=./$dst ;;
254e9554658Smrg    esac
255e9554658Smrg
256e9554658Smrg    # If destination is a directory, append the input filename; won't work
257e9554658Smrg    # if double slashes aren't ignored.
258e9554658Smrg    if test -d "$dst"; then
259e9554658Smrg      if test -n "$no_target_directory"; then
260e9554658Smrg	echo "$0: $dstarg: Is a directory" >&2
261e9554658Smrg	exit 1
262e9554658Smrg      fi
263e9554658Smrg      dstdir=$dst
264e9554658Smrg      dst=$dstdir/`basename "$src"`
265e9554658Smrg      dstdir_status=0
266e9554658Smrg    else
267e9554658Smrg      # Prefer dirname, but fall back on a substitute if dirname fails.
268e9554658Smrg      dstdir=`
269e9554658Smrg	(dirname "$dst") 2>/dev/null ||
270e9554658Smrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
271e9554658Smrg	     X"$dst" : 'X\(//\)[^/]' \| \
272e9554658Smrg	     X"$dst" : 'X\(//\)$' \| \
273e9554658Smrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
274e9554658Smrg	echo X"$dst" |
275e9554658Smrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
276e9554658Smrg		   s//\1/
277e9554658Smrg		   q
278e9554658Smrg		 }
279e9554658Smrg		 /^X\(\/\/\)[^/].*/{
280e9554658Smrg		   s//\1/
281e9554658Smrg		   q
282e9554658Smrg		 }
283e9554658Smrg		 /^X\(\/\/\)$/{
284e9554658Smrg		   s//\1/
285e9554658Smrg		   q
286e9554658Smrg		 }
287e9554658Smrg		 /^X\(\/\).*/{
288e9554658Smrg		   s//\1/
289e9554658Smrg		   q
290e9554658Smrg		 }
291e9554658Smrg		 s/.*/./; q'
292e9554658Smrg      `
293e9554658Smrg
294e9554658Smrg      test -d "$dstdir"
295e9554658Smrg      dstdir_status=$?
296e9554658Smrg    fi
297e9554658Smrg  fi
298e9554658Smrg
299e9554658Smrg  obsolete_mkdir_used=false
300e9554658Smrg
301e9554658Smrg  if test $dstdir_status != 0; then
302e9554658Smrg    case $posix_mkdir in
303e9554658Smrg      '')
304e9554658Smrg	# Create intermediate dirs using mode 755 as modified by the umask.
305e9554658Smrg	# This is like FreeBSD 'install' as of 1997-10-28.
306e9554658Smrg	umask=`umask`
307e9554658Smrg	case $stripcmd.$umask in
308e9554658Smrg	  # Optimize common cases.
309e9554658Smrg	  *[2367][2367]) mkdir_umask=$umask;;
310e9554658Smrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
311e9554658Smrg
312e9554658Smrg	  *[0-7])
313e9554658Smrg	    mkdir_umask=`expr $umask + 22 \
314e9554658Smrg	      - $umask % 100 % 40 + $umask % 20 \
315e9554658Smrg	      - $umask % 10 % 4 + $umask % 2
316e9554658Smrg	    `;;
317e9554658Smrg	  *) mkdir_umask=$umask,go-w;;
318e9554658Smrg	esac
319e9554658Smrg
320e9554658Smrg	# With -d, create the new directory with the user-specified mode.
321e9554658Smrg	# Otherwise, rely on $mkdir_umask.
322e9554658Smrg	if test -n "$dir_arg"; then
323e9554658Smrg	  mkdir_mode=-m$mode
324e9554658Smrg	else
325e9554658Smrg	  mkdir_mode=
326e9554658Smrg	fi
327e9554658Smrg
328e9554658Smrg	posix_mkdir=false
329e9554658Smrg	case $umask in
330e9554658Smrg	  *[123567][0-7][0-7])
331e9554658Smrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
332e9554658Smrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
333e9554658Smrg	    ;;
334e9554658Smrg	  *)
335e9554658Smrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
336e9554658Smrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
337e9554658Smrg
338e9554658Smrg	    if (umask $mkdir_umask &&
339e9554658Smrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
340e9554658Smrg	    then
341e9554658Smrg	      if test -z "$dir_arg" || {
342e9554658Smrg		   # Check for POSIX incompatibilities with -m.
343e9554658Smrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
344e9554658Smrg		   # other-writeable bit of parent directory when it shouldn't.
345e9554658Smrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
346e9554658Smrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
347e9554658Smrg		   case $ls_ld_tmpdir in
348e9554658Smrg		     d????-?r-*) different_mode=700;;
349e9554658Smrg		     d????-?--*) different_mode=755;;
350e9554658Smrg		     *) false;;
351e9554658Smrg		   esac &&
352e9554658Smrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
353e9554658Smrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
354e9554658Smrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
355e9554658Smrg		   }
356e9554658Smrg		 }
357e9554658Smrg	      then posix_mkdir=:
358e9554658Smrg	      fi
359e9554658Smrg	      rmdir "$tmpdir/d" "$tmpdir"
360e9554658Smrg	    else
361e9554658Smrg	      # Remove any dirs left behind by ancient mkdir implementations.
362e9554658Smrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
363e9554658Smrg	    fi
364e9554658Smrg	    trap '' 0;;
365e9554658Smrg	esac;;
366e9554658Smrg    esac
367e9554658Smrg
368e9554658Smrg    if
369e9554658Smrg      $posix_mkdir && (
370e9554658Smrg	umask $mkdir_umask &&
371e9554658Smrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
372e9554658Smrg      )
373e9554658Smrg    then :
374e9554658Smrg    else
375e9554658Smrg
376e9554658Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
377e9554658Smrg      # or it failed possibly due to a race condition.  Create the
378e9554658Smrg      # directory the slow way, step by step, checking for races as we go.
379e9554658Smrg
380e9554658Smrg      case $dstdir in
381e9554658Smrg	/*) prefix=/ ;;
382e9554658Smrg	-*) prefix=./ ;;
383e9554658Smrg	*)  prefix= ;;
384e9554658Smrg      esac
385e9554658Smrg
386e9554658Smrg      case $posix_glob in
387e9554658Smrg        '')
388e9554658Smrg	  if (set -f) 2>/dev/null; then
389e9554658Smrg	    posix_glob=true
390e9554658Smrg	  else
391e9554658Smrg	    posix_glob=false
392e9554658Smrg	  fi ;;
393e9554658Smrg      esac
394e9554658Smrg
395e9554658Smrg      oIFS=$IFS
396e9554658Smrg      IFS=/
397e9554658Smrg      $posix_glob && set -f
398e9554658Smrg      set fnord $dstdir
399e9554658Smrg      shift
400e9554658Smrg      $posix_glob && set +f
401e9554658Smrg      IFS=$oIFS
402e9554658Smrg
403e9554658Smrg      prefixes=
404e9554658Smrg
405e9554658Smrg      for d
406e9554658Smrg      do
407e9554658Smrg	test -z "$d" && continue
408e9554658Smrg
409e9554658Smrg	prefix=$prefix$d
410e9554658Smrg	if test -d "$prefix"; then
411e9554658Smrg	  prefixes=
412e9554658Smrg	else
413e9554658Smrg	  if $posix_mkdir; then
414e9554658Smrg	    (umask=$mkdir_umask &&
415e9554658Smrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
416e9554658Smrg	    # Don't fail if two instances are running concurrently.
417e9554658Smrg	    test -d "$prefix" || exit 1
418e9554658Smrg	  else
419e9554658Smrg	    case $prefix in
420e9554658Smrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
421e9554658Smrg	      *) qprefix=$prefix;;
422e9554658Smrg	    esac
423e9554658Smrg	    prefixes="$prefixes '$qprefix'"
424e9554658Smrg	  fi
425e9554658Smrg	fi
426e9554658Smrg	prefix=$prefix/
427e9554658Smrg      done
428e9554658Smrg
429e9554658Smrg      if test -n "$prefixes"; then
430e9554658Smrg	# Don't fail if two instances are running concurrently.
431e9554658Smrg	(umask $mkdir_umask &&
432e9554658Smrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
433e9554658Smrg	  test -d "$dstdir" || exit 1
434e9554658Smrg	obsolete_mkdir_used=true
435e9554658Smrg      fi
436e9554658Smrg    fi
437e9554658Smrg  fi
438e9554658Smrg
439e9554658Smrg  if test -n "$dir_arg"; then
440e9554658Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
441e9554658Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
442e9554658Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
443e9554658Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
444e9554658Smrg  else
445e9554658Smrg
446e9554658Smrg    # Make a couple of temp file names in the proper directory.
447e9554658Smrg    dsttmp=$dstdir/_inst.$$_
448e9554658Smrg    rmtmp=$dstdir/_rm.$$_
449e9554658Smrg
450e9554658Smrg    # Trap to clean up those temp files at exit.
451e9554658Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
452e9554658Smrg
453e9554658Smrg    # Copy the file name to the temp name.
454e9554658Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
455e9554658Smrg
456e9554658Smrg    # and set any options; do chmod last to preserve setuid bits.
457e9554658Smrg    #
458e9554658Smrg    # If any of these fail, we abort the whole thing.  If we want to
459e9554658Smrg    # ignore errors from any of these, just make sure not to ignore
460e9554658Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
461e9554658Smrg    #
462e9554658Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } \
463e9554658Smrg      && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } \
464e9554658Smrg      && { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } \
465e9554658Smrg      && { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
466e9554658Smrg
467e9554658Smrg    # Now rename the file to the real destination.
468e9554658Smrg    { $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null \
469e9554658Smrg      || {
470e9554658Smrg	   # The rename failed, perhaps because mv can't rename something else
471e9554658Smrg	   # to itself, or perhaps because mv is so ancient that it does not
472e9554658Smrg	   # support -f.
473e9554658Smrg
474e9554658Smrg	   # Now remove or move aside any old file at destination location.
475e9554658Smrg	   # We try this two ways since rm can't unlink itself on some
476e9554658Smrg	   # systems and the destination file might be busy for other
477e9554658Smrg	   # reasons.  In this case, the final cleanup might fail but the new
478e9554658Smrg	   # file should still install successfully.
479e9554658Smrg	   {
480e9554658Smrg	     if test -f "$dst"; then
481e9554658Smrg	       $doit $rmcmd -f "$dst" 2>/dev/null \
482e9554658Smrg	       || { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null \
483e9554658Smrg		     && { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }; }\
484e9554658Smrg	       || {
485e9554658Smrg		 echo "$0: cannot unlink or rename $dst" >&2
486e9554658Smrg		 (exit 1); exit 1
487e9554658Smrg	       }
488e9554658Smrg	     else
489e9554658Smrg	       :
490e9554658Smrg	     fi
491e9554658Smrg	   } &&
492e9554658Smrg
493e9554658Smrg	   # Now rename the file to the real destination.
494e9554658Smrg	   $doit $mvcmd "$dsttmp" "$dst"
495e9554658Smrg	 }
496e9554658Smrg    } || exit 1
497e9554658Smrg
498e9554658Smrg    trap '' 0
499e9554658Smrg  fi
500e9554658Smrgdone
501e9554658Smrg
502e9554658Smrg# Local variables:
503e9554658Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
504e9554658Smrg# time-stamp-start: "scriptversion="
505e9554658Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
506e9554658Smrg# time-stamp-end: "$"
507e9554658Smrg# End:
508