install-sh revision 659607e0
1659607e0Smrg#!/bin/sh
2659607e0Smrg# install - install a program, script, or datafile
3659607e0Smrg
4659607e0Smrgscriptversion=2006-10-14.15
5659607e0Smrg
6659607e0Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7659607e0Smrg# later released in X11R6 (xc/config/util/install.sh) with the
8659607e0Smrg# following copyright and license.
9659607e0Smrg#
10659607e0Smrg# Copyright (C) 1994 X Consortium
11659607e0Smrg#
12659607e0Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13659607e0Smrg# of this software and associated documentation files (the "Software"), to
14659607e0Smrg# deal in the Software without restriction, including without limitation the
15659607e0Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16659607e0Smrg# sell copies of the Software, and to permit persons to whom the Software is
17659607e0Smrg# furnished to do so, subject to the following conditions:
18659607e0Smrg#
19659607e0Smrg# The above copyright notice and this permission notice shall be included in
20659607e0Smrg# all copies or substantial portions of the Software.
21659607e0Smrg#
22659607e0Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23659607e0Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24659607e0Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25659607e0Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26659607e0Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27659607e0Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28659607e0Smrg#
29659607e0Smrg# Except as contained in this notice, the name of the X Consortium shall not
30659607e0Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
31659607e0Smrg# ings in this Software without prior written authorization from the X Consor-
32659607e0Smrg# tium.
33659607e0Smrg#
34659607e0Smrg#
35659607e0Smrg# FSF changes to this file are in the public domain.
36659607e0Smrg#
37659607e0Smrg# Calling this script install-sh is preferred over install.sh, to prevent
38659607e0Smrg# `make' implicit rules from creating a file called install from it
39659607e0Smrg# when there is no Makefile.
40659607e0Smrg#
41659607e0Smrg# This script is compatible with the BSD install script, but was written
42659607e0Smrg# from scratch.
43659607e0Smrg
44659607e0Smrgnl='
45659607e0Smrg'
46659607e0SmrgIFS=" ""	$nl"
47659607e0Smrg
48659607e0Smrg# set DOITPROG to echo to test this script
49659607e0Smrg
50659607e0Smrg# Don't use :- since 4.3BSD and earlier shells don't like it.
51659607e0Smrgdoit="${DOITPROG-}"
52659607e0Smrgif test -z "$doit"; then
53659607e0Smrg  doit_exec=exec
54659607e0Smrgelse
55659607e0Smrg  doit_exec=$doit
56659607e0Smrgfi
57659607e0Smrg
58659607e0Smrg# Put in absolute file names if you don't have them in your path;
59659607e0Smrg# or use environment vars.
60659607e0Smrg
61659607e0Smrgmvprog="${MVPROG-mv}"
62659607e0Smrgcpprog="${CPPROG-cp}"
63659607e0Smrgchmodprog="${CHMODPROG-chmod}"
64659607e0Smrgchownprog="${CHOWNPROG-chown}"
65659607e0Smrgchgrpprog="${CHGRPPROG-chgrp}"
66659607e0Smrgstripprog="${STRIPPROG-strip}"
67659607e0Smrgrmprog="${RMPROG-rm}"
68659607e0Smrgmkdirprog="${MKDIRPROG-mkdir}"
69659607e0Smrg
70659607e0Smrgposix_glob=
71659607e0Smrgposix_mkdir=
72659607e0Smrg
73659607e0Smrg# Desired mode of installed file.
74659607e0Smrgmode=0755
75659607e0Smrg
76659607e0Smrgchmodcmd=$chmodprog
77659607e0Smrgchowncmd=
78659607e0Smrgchgrpcmd=
79659607e0Smrgstripcmd=
80659607e0Smrgrmcmd="$rmprog -f"
81659607e0Smrgmvcmd="$mvprog"
82659607e0Smrgsrc=
83659607e0Smrgdst=
84659607e0Smrgdir_arg=
85659607e0Smrgdstarg=
86659607e0Smrgno_target_directory=
87659607e0Smrg
88659607e0Smrgusage="Usage: $0 [OPTION]... [-T] SRCFILE DSTFILE
89659607e0Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
90659607e0Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
91659607e0Smrg   or: $0 [OPTION]... -d DIRECTORIES...
92659607e0Smrg
93659607e0SmrgIn the 1st form, copy SRCFILE to DSTFILE.
94659607e0SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
95659607e0SmrgIn the 4th, create DIRECTORIES.
96659607e0Smrg
97659607e0SmrgOptions:
98659607e0Smrg-c         (ignored)
99659607e0Smrg-d         create directories instead of installing files.
100659607e0Smrg-g GROUP   $chgrpprog installed files to GROUP.
101659607e0Smrg-m MODE    $chmodprog installed files to MODE.
102659607e0Smrg-o USER    $chownprog installed files to USER.
103659607e0Smrg-s         $stripprog installed files.
104659607e0Smrg-t DIRECTORY  install into DIRECTORY.
105659607e0Smrg-T         report an error if DSTFILE is a directory.
106659607e0Smrg--help     display this help and exit.
107659607e0Smrg--version  display version info and exit.
108659607e0Smrg
109659607e0SmrgEnvironment variables override the default commands:
110659607e0Smrg  CHGRPPROG CHMODPROG CHOWNPROG CPPROG MKDIRPROG MVPROG RMPROG STRIPPROG
111659607e0Smrg"
112659607e0Smrg
113659607e0Smrgwhile test $# -ne 0; do
114659607e0Smrg  case $1 in
115659607e0Smrg    -c) shift
116659607e0Smrg        continue;;
117659607e0Smrg
118659607e0Smrg    -d) dir_arg=true
119659607e0Smrg        shift
120659607e0Smrg        continue;;
121659607e0Smrg
122659607e0Smrg    -g) chgrpcmd="$chgrpprog $2"
123659607e0Smrg        shift
124659607e0Smrg        shift
125659607e0Smrg        continue;;
126659607e0Smrg
127659607e0Smrg    --help) echo "$usage"; exit $?;;
128659607e0Smrg
129659607e0Smrg    -m) mode=$2
130659607e0Smrg        shift
131659607e0Smrg        shift
132659607e0Smrg	case $mode in
133659607e0Smrg	  *' '* | *'	'* | *'
134659607e0Smrg'*	  | *'*'* | *'?'* | *'['*)
135659607e0Smrg	    echo "$0: invalid mode: $mode" >&2
136659607e0Smrg	    exit 1;;
137659607e0Smrg	esac
138659607e0Smrg        continue;;
139659607e0Smrg
140659607e0Smrg    -o) chowncmd="$chownprog $2"
141659607e0Smrg        shift
142659607e0Smrg        shift
143659607e0Smrg        continue;;
144659607e0Smrg
145659607e0Smrg    -s) stripcmd=$stripprog
146659607e0Smrg        shift
147659607e0Smrg        continue;;
148659607e0Smrg
149659607e0Smrg    -t) dstarg=$2
150659607e0Smrg	shift
151659607e0Smrg	shift
152659607e0Smrg	continue;;
153659607e0Smrg
154659607e0Smrg    -T) no_target_directory=true
155659607e0Smrg	shift
156659607e0Smrg	continue;;
157659607e0Smrg
158659607e0Smrg    --version) echo "$0 $scriptversion"; exit $?;;
159659607e0Smrg
160659607e0Smrg    --)	shift
161659607e0Smrg	break;;
162659607e0Smrg
163659607e0Smrg    -*)	echo "$0: invalid option: $1" >&2
164659607e0Smrg	exit 1;;
165659607e0Smrg
166659607e0Smrg    *)  break;;
167659607e0Smrg  esac
168659607e0Smrgdone
169659607e0Smrg
170659607e0Smrgif test $# -ne 0 && test -z "$dir_arg$dstarg"; then
171659607e0Smrg  # When -d is used, all remaining arguments are directories to create.
172659607e0Smrg  # When -t is used, the destination is already specified.
173659607e0Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
174659607e0Smrg  for arg
175659607e0Smrg  do
176659607e0Smrg    if test -n "$dstarg"; then
177659607e0Smrg      # $@ is not empty: it contains at least $arg.
178659607e0Smrg      set fnord "$@" "$dstarg"
179659607e0Smrg      shift # fnord
180659607e0Smrg    fi
181659607e0Smrg    shift # arg
182659607e0Smrg    dstarg=$arg
183659607e0Smrg  done
184659607e0Smrgfi
185659607e0Smrg
186659607e0Smrgif test $# -eq 0; then
187659607e0Smrg  if test -z "$dir_arg"; then
188659607e0Smrg    echo "$0: no input file specified." >&2
189659607e0Smrg    exit 1
190659607e0Smrg  fi
191659607e0Smrg  # It's OK to call `install-sh -d' without argument.
192659607e0Smrg  # This can happen when creating conditional directories.
193659607e0Smrg  exit 0
194659607e0Smrgfi
195659607e0Smrg
196659607e0Smrgif test -z "$dir_arg"; then
197659607e0Smrg  trap '(exit $?); exit' 1 2 13 15
198659607e0Smrg
199659607e0Smrg  # Set umask so as not to create temps with too-generous modes.
200659607e0Smrg  # However, 'strip' requires both read and write access to temps.
201659607e0Smrg  case $mode in
202659607e0Smrg    # Optimize common cases.
203659607e0Smrg    *644) cp_umask=133;;
204659607e0Smrg    *755) cp_umask=22;;
205659607e0Smrg
206659607e0Smrg    *[0-7])
207659607e0Smrg      if test -z "$stripcmd"; then
208659607e0Smrg	u_plus_rw=
209659607e0Smrg      else
210659607e0Smrg	u_plus_rw='% 200'
211659607e0Smrg      fi
212659607e0Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
213659607e0Smrg    *)
214659607e0Smrg      if test -z "$stripcmd"; then
215659607e0Smrg	u_plus_rw=
216659607e0Smrg      else
217659607e0Smrg	u_plus_rw=,u+rw
218659607e0Smrg      fi
219659607e0Smrg      cp_umask=$mode$u_plus_rw;;
220659607e0Smrg  esac
221659607e0Smrgfi
222659607e0Smrg
223659607e0Smrgfor src
224659607e0Smrgdo
225659607e0Smrg  # Protect names starting with `-'.
226659607e0Smrg  case $src in
227659607e0Smrg    -*) src=./$src ;;
228659607e0Smrg  esac
229659607e0Smrg
230659607e0Smrg  if test -n "$dir_arg"; then
231659607e0Smrg    dst=$src
232659607e0Smrg    dstdir=$dst
233659607e0Smrg    test -d "$dstdir"
234659607e0Smrg    dstdir_status=$?
235659607e0Smrg  else
236659607e0Smrg
237659607e0Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
238659607e0Smrg    # might cause directories to be created, which would be especially bad
239659607e0Smrg    # if $src (and thus $dsttmp) contains '*'.
240659607e0Smrg    if test ! -f "$src" && test ! -d "$src"; then
241659607e0Smrg      echo "$0: $src does not exist." >&2
242659607e0Smrg      exit 1
243659607e0Smrg    fi
244659607e0Smrg
245659607e0Smrg    if test -z "$dstarg"; then
246659607e0Smrg      echo "$0: no destination specified." >&2
247659607e0Smrg      exit 1
248659607e0Smrg    fi
249659607e0Smrg
250659607e0Smrg    dst=$dstarg
251659607e0Smrg    # Protect names starting with `-'.
252659607e0Smrg    case $dst in
253659607e0Smrg      -*) dst=./$dst ;;
254659607e0Smrg    esac
255659607e0Smrg
256659607e0Smrg    # If destination is a directory, append the input filename; won't work
257659607e0Smrg    # if double slashes aren't ignored.
258659607e0Smrg    if test -d "$dst"; then
259659607e0Smrg      if test -n "$no_target_directory"; then
260659607e0Smrg	echo "$0: $dstarg: Is a directory" >&2
261659607e0Smrg	exit 1
262659607e0Smrg      fi
263659607e0Smrg      dstdir=$dst
264659607e0Smrg      dst=$dstdir/`basename "$src"`
265659607e0Smrg      dstdir_status=0
266659607e0Smrg    else
267659607e0Smrg      # Prefer dirname, but fall back on a substitute if dirname fails.
268659607e0Smrg      dstdir=`
269659607e0Smrg	(dirname "$dst") 2>/dev/null ||
270659607e0Smrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
271659607e0Smrg	     X"$dst" : 'X\(//\)[^/]' \| \
272659607e0Smrg	     X"$dst" : 'X\(//\)$' \| \
273659607e0Smrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
274659607e0Smrg	echo X"$dst" |
275659607e0Smrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
276659607e0Smrg		   s//\1/
277659607e0Smrg		   q
278659607e0Smrg		 }
279659607e0Smrg		 /^X\(\/\/\)[^/].*/{
280659607e0Smrg		   s//\1/
281659607e0Smrg		   q
282659607e0Smrg		 }
283659607e0Smrg		 /^X\(\/\/\)$/{
284659607e0Smrg		   s//\1/
285659607e0Smrg		   q
286659607e0Smrg		 }
287659607e0Smrg		 /^X\(\/\).*/{
288659607e0Smrg		   s//\1/
289659607e0Smrg		   q
290659607e0Smrg		 }
291659607e0Smrg		 s/.*/./; q'
292659607e0Smrg      `
293659607e0Smrg
294659607e0Smrg      test -d "$dstdir"
295659607e0Smrg      dstdir_status=$?
296659607e0Smrg    fi
297659607e0Smrg  fi
298659607e0Smrg
299659607e0Smrg  obsolete_mkdir_used=false
300659607e0Smrg
301659607e0Smrg  if test $dstdir_status != 0; then
302659607e0Smrg    case $posix_mkdir in
303659607e0Smrg      '')
304659607e0Smrg	# Create intermediate dirs using mode 755 as modified by the umask.
305659607e0Smrg	# This is like FreeBSD 'install' as of 1997-10-28.
306659607e0Smrg	umask=`umask`
307659607e0Smrg	case $stripcmd.$umask in
308659607e0Smrg	  # Optimize common cases.
309659607e0Smrg	  *[2367][2367]) mkdir_umask=$umask;;
310659607e0Smrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
311659607e0Smrg
312659607e0Smrg	  *[0-7])
313659607e0Smrg	    mkdir_umask=`expr $umask + 22 \
314659607e0Smrg	      - $umask % 100 % 40 + $umask % 20 \
315659607e0Smrg	      - $umask % 10 % 4 + $umask % 2
316659607e0Smrg	    `;;
317659607e0Smrg	  *) mkdir_umask=$umask,go-w;;
318659607e0Smrg	esac
319659607e0Smrg
320659607e0Smrg	# With -d, create the new directory with the user-specified mode.
321659607e0Smrg	# Otherwise, rely on $mkdir_umask.
322659607e0Smrg	if test -n "$dir_arg"; then
323659607e0Smrg	  mkdir_mode=-m$mode
324659607e0Smrg	else
325659607e0Smrg	  mkdir_mode=
326659607e0Smrg	fi
327659607e0Smrg
328659607e0Smrg	posix_mkdir=false
329659607e0Smrg	case $umask in
330659607e0Smrg	  *[123567][0-7][0-7])
331659607e0Smrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
332659607e0Smrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
333659607e0Smrg	    ;;
334659607e0Smrg	  *)
335659607e0Smrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
336659607e0Smrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
337659607e0Smrg
338659607e0Smrg	    if (umask $mkdir_umask &&
339659607e0Smrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
340659607e0Smrg	    then
341659607e0Smrg	      if test -z "$dir_arg" || {
342659607e0Smrg		   # Check for POSIX incompatibilities with -m.
343659607e0Smrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
344659607e0Smrg		   # other-writeable bit of parent directory when it shouldn't.
345659607e0Smrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
346659607e0Smrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
347659607e0Smrg		   case $ls_ld_tmpdir in
348659607e0Smrg		     d????-?r-*) different_mode=700;;
349659607e0Smrg		     d????-?--*) different_mode=755;;
350659607e0Smrg		     *) false;;
351659607e0Smrg		   esac &&
352659607e0Smrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
353659607e0Smrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
354659607e0Smrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
355659607e0Smrg		   }
356659607e0Smrg		 }
357659607e0Smrg	      then posix_mkdir=:
358659607e0Smrg	      fi
359659607e0Smrg	      rmdir "$tmpdir/d" "$tmpdir"
360659607e0Smrg	    else
361659607e0Smrg	      # Remove any dirs left behind by ancient mkdir implementations.
362659607e0Smrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
363659607e0Smrg	    fi
364659607e0Smrg	    trap '' 0;;
365659607e0Smrg	esac;;
366659607e0Smrg    esac
367659607e0Smrg
368659607e0Smrg    if
369659607e0Smrg      $posix_mkdir && (
370659607e0Smrg	umask $mkdir_umask &&
371659607e0Smrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
372659607e0Smrg      )
373659607e0Smrg    then :
374659607e0Smrg    else
375659607e0Smrg
376659607e0Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
377659607e0Smrg      # or it failed possibly due to a race condition.  Create the
378659607e0Smrg      # directory the slow way, step by step, checking for races as we go.
379659607e0Smrg
380659607e0Smrg      case $dstdir in
381659607e0Smrg	/*) prefix=/ ;;
382659607e0Smrg	-*) prefix=./ ;;
383659607e0Smrg	*)  prefix= ;;
384659607e0Smrg      esac
385659607e0Smrg
386659607e0Smrg      case $posix_glob in
387659607e0Smrg        '')
388659607e0Smrg	  if (set -f) 2>/dev/null; then
389659607e0Smrg	    posix_glob=true
390659607e0Smrg	  else
391659607e0Smrg	    posix_glob=false
392659607e0Smrg	  fi ;;
393659607e0Smrg      esac
394659607e0Smrg
395659607e0Smrg      oIFS=$IFS
396659607e0Smrg      IFS=/
397659607e0Smrg      $posix_glob && set -f
398659607e0Smrg      set fnord $dstdir
399659607e0Smrg      shift
400659607e0Smrg      $posix_glob && set +f
401659607e0Smrg      IFS=$oIFS
402659607e0Smrg
403659607e0Smrg      prefixes=
404659607e0Smrg
405659607e0Smrg      for d
406659607e0Smrg      do
407659607e0Smrg	test -z "$d" && continue
408659607e0Smrg
409659607e0Smrg	prefix=$prefix$d
410659607e0Smrg	if test -d "$prefix"; then
411659607e0Smrg	  prefixes=
412659607e0Smrg	else
413659607e0Smrg	  if $posix_mkdir; then
414659607e0Smrg	    (umask=$mkdir_umask &&
415659607e0Smrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
416659607e0Smrg	    # Don't fail if two instances are running concurrently.
417659607e0Smrg	    test -d "$prefix" || exit 1
418659607e0Smrg	  else
419659607e0Smrg	    case $prefix in
420659607e0Smrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
421659607e0Smrg	      *) qprefix=$prefix;;
422659607e0Smrg	    esac
423659607e0Smrg	    prefixes="$prefixes '$qprefix'"
424659607e0Smrg	  fi
425659607e0Smrg	fi
426659607e0Smrg	prefix=$prefix/
427659607e0Smrg      done
428659607e0Smrg
429659607e0Smrg      if test -n "$prefixes"; then
430659607e0Smrg	# Don't fail if two instances are running concurrently.
431659607e0Smrg	(umask $mkdir_umask &&
432659607e0Smrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
433659607e0Smrg	  test -d "$dstdir" || exit 1
434659607e0Smrg	obsolete_mkdir_used=true
435659607e0Smrg      fi
436659607e0Smrg    fi
437659607e0Smrg  fi
438659607e0Smrg
439659607e0Smrg  if test -n "$dir_arg"; then
440659607e0Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
441659607e0Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
442659607e0Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
443659607e0Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
444659607e0Smrg  else
445659607e0Smrg
446659607e0Smrg    # Make a couple of temp file names in the proper directory.
447659607e0Smrg    dsttmp=$dstdir/_inst.$$_
448659607e0Smrg    rmtmp=$dstdir/_rm.$$_
449659607e0Smrg
450659607e0Smrg    # Trap to clean up those temp files at exit.
451659607e0Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
452659607e0Smrg
453659607e0Smrg    # Copy the file name to the temp name.
454659607e0Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
455659607e0Smrg
456659607e0Smrg    # and set any options; do chmod last to preserve setuid bits.
457659607e0Smrg    #
458659607e0Smrg    # If any of these fail, we abort the whole thing.  If we want to
459659607e0Smrg    # ignore errors from any of these, just make sure not to ignore
460659607e0Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
461659607e0Smrg    #
462659607e0Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } \
463659607e0Smrg      && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } \
464659607e0Smrg      && { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } \
465659607e0Smrg      && { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
466659607e0Smrg
467659607e0Smrg    # Now rename the file to the real destination.
468659607e0Smrg    { $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null \
469659607e0Smrg      || {
470659607e0Smrg	   # The rename failed, perhaps because mv can't rename something else
471659607e0Smrg	   # to itself, or perhaps because mv is so ancient that it does not
472659607e0Smrg	   # support -f.
473659607e0Smrg
474659607e0Smrg	   # Now remove or move aside any old file at destination location.
475659607e0Smrg	   # We try this two ways since rm can't unlink itself on some
476659607e0Smrg	   # systems and the destination file might be busy for other
477659607e0Smrg	   # reasons.  In this case, the final cleanup might fail but the new
478659607e0Smrg	   # file should still install successfully.
479659607e0Smrg	   {
480659607e0Smrg	     if test -f "$dst"; then
481659607e0Smrg	       $doit $rmcmd -f "$dst" 2>/dev/null \
482659607e0Smrg	       || { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null \
483659607e0Smrg		     && { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }; }\
484659607e0Smrg	       || {
485659607e0Smrg		 echo "$0: cannot unlink or rename $dst" >&2
486659607e0Smrg		 (exit 1); exit 1
487659607e0Smrg	       }
488659607e0Smrg	     else
489659607e0Smrg	       :
490659607e0Smrg	     fi
491659607e0Smrg	   } &&
492659607e0Smrg
493659607e0Smrg	   # Now rename the file to the real destination.
494659607e0Smrg	   $doit $mvcmd "$dsttmp" "$dst"
495659607e0Smrg	 }
496659607e0Smrg    } || exit 1
497659607e0Smrg
498659607e0Smrg    trap '' 0
499659607e0Smrg  fi
500659607e0Smrgdone
501659607e0Smrg
502659607e0Smrg# Local variables:
503659607e0Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
504659607e0Smrg# time-stamp-start: "scriptversion="
505659607e0Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
506659607e0Smrg# time-stamp-end: "$"
507659607e0Smrg# End:
508