install-sh revision 706f2543
1706f2543Smrg#!/bin/sh
2706f2543Smrg# install - install a program, script, or datafile
3706f2543Smrg
4706f2543Smrgscriptversion=2006-10-14.15
5706f2543Smrg
6706f2543Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7706f2543Smrg# later released in X11R6 (xc/config/util/install.sh) with the
8706f2543Smrg# following copyright and license.
9706f2543Smrg#
10706f2543Smrg# Copyright (C) 1994 X Consortium
11706f2543Smrg#
12706f2543Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13706f2543Smrg# of this software and associated documentation files (the "Software"), to
14706f2543Smrg# deal in the Software without restriction, including without limitation the
15706f2543Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16706f2543Smrg# sell copies of the Software, and to permit persons to whom the Software is
17706f2543Smrg# furnished to do so, subject to the following conditions:
18706f2543Smrg#
19706f2543Smrg# The above copyright notice and this permission notice shall be included in
20706f2543Smrg# all copies or substantial portions of the Software.
21706f2543Smrg#
22706f2543Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23706f2543Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24706f2543Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25706f2543Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26706f2543Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27706f2543Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28706f2543Smrg#
29706f2543Smrg# Except as contained in this notice, the name of the X Consortium shall not
30706f2543Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
31706f2543Smrg# ings in this Software without prior written authorization from the X Consor-
32706f2543Smrg# tium.
33706f2543Smrg#
34706f2543Smrg#
35706f2543Smrg# FSF changes to this file are in the public domain.
36706f2543Smrg#
37706f2543Smrg# Calling this script install-sh is preferred over install.sh, to prevent
38706f2543Smrg# `make' implicit rules from creating a file called install from it
39706f2543Smrg# when there is no Makefile.
40706f2543Smrg#
41706f2543Smrg# This script is compatible with the BSD install script, but was written
42706f2543Smrg# from scratch.
43706f2543Smrg
44706f2543Smrgnl='
45706f2543Smrg'
46706f2543SmrgIFS=" ""	$nl"
47706f2543Smrg
48706f2543Smrg# set DOITPROG to echo to test this script
49706f2543Smrg
50706f2543Smrg# Don't use :- since 4.3BSD and earlier shells don't like it.
51706f2543Smrgdoit="${DOITPROG-}"
52706f2543Smrgif test -z "$doit"; then
53706f2543Smrg  doit_exec=exec
54706f2543Smrgelse
55706f2543Smrg  doit_exec=$doit
56706f2543Smrgfi
57706f2543Smrg
58706f2543Smrg# Put in absolute file names if you don't have them in your path;
59706f2543Smrg# or use environment vars.
60706f2543Smrg
61706f2543Smrgmvprog="${MVPROG-mv}"
62706f2543Smrgcpprog="${CPPROG-cp}"
63706f2543Smrgchmodprog="${CHMODPROG-chmod}"
64706f2543Smrgchownprog="${CHOWNPROG-chown}"
65706f2543Smrgchgrpprog="${CHGRPPROG-chgrp}"
66706f2543Smrgstripprog="${STRIPPROG-strip}"
67706f2543Smrgrmprog="${RMPROG-rm}"
68706f2543Smrgmkdirprog="${MKDIRPROG-mkdir}"
69706f2543Smrg
70706f2543Smrgposix_glob=
71706f2543Smrgposix_mkdir=
72706f2543Smrg
73706f2543Smrg# Desired mode of installed file.
74706f2543Smrgmode=0755
75706f2543Smrg
76706f2543Smrgchmodcmd=$chmodprog
77706f2543Smrgchowncmd=
78706f2543Smrgchgrpcmd=
79706f2543Smrgstripcmd=
80706f2543Smrgrmcmd="$rmprog -f"
81706f2543Smrgmvcmd="$mvprog"
82706f2543Smrgsrc=
83706f2543Smrgdst=
84706f2543Smrgdir_arg=
85706f2543Smrgdstarg=
86706f2543Smrgno_target_directory=
87706f2543Smrg
88706f2543Smrgusage="Usage: $0 [OPTION]... [-T] SRCFILE DSTFILE
89706f2543Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
90706f2543Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
91706f2543Smrg   or: $0 [OPTION]... -d DIRECTORIES...
92706f2543Smrg
93706f2543SmrgIn the 1st form, copy SRCFILE to DSTFILE.
94706f2543SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
95706f2543SmrgIn the 4th, create DIRECTORIES.
96706f2543Smrg
97706f2543SmrgOptions:
98706f2543Smrg-c         (ignored)
99706f2543Smrg-d         create directories instead of installing files.
100706f2543Smrg-g GROUP   $chgrpprog installed files to GROUP.
101706f2543Smrg-m MODE    $chmodprog installed files to MODE.
102706f2543Smrg-o USER    $chownprog installed files to USER.
103706f2543Smrg-s         $stripprog installed files.
104706f2543Smrg-t DIRECTORY  install into DIRECTORY.
105706f2543Smrg-T         report an error if DSTFILE is a directory.
106706f2543Smrg--help     display this help and exit.
107706f2543Smrg--version  display version info and exit.
108706f2543Smrg
109706f2543SmrgEnvironment variables override the default commands:
110706f2543Smrg  CHGRPPROG CHMODPROG CHOWNPROG CPPROG MKDIRPROG MVPROG RMPROG STRIPPROG
111706f2543Smrg"
112706f2543Smrg
113706f2543Smrgwhile test $# -ne 0; do
114706f2543Smrg  case $1 in
115706f2543Smrg    -c) shift
116706f2543Smrg        continue;;
117706f2543Smrg
118706f2543Smrg    -d) dir_arg=true
119706f2543Smrg        shift
120706f2543Smrg        continue;;
121706f2543Smrg
122706f2543Smrg    -g) chgrpcmd="$chgrpprog $2"
123706f2543Smrg        shift
124706f2543Smrg        shift
125706f2543Smrg        continue;;
126706f2543Smrg
127706f2543Smrg    --help) echo "$usage"; exit $?;;
128706f2543Smrg
129706f2543Smrg    -m) mode=$2
130706f2543Smrg        shift
131706f2543Smrg        shift
132706f2543Smrg	case $mode in
133706f2543Smrg	  *' '* | *'	'* | *'
134706f2543Smrg'*	  | *'*'* | *'?'* | *'['*)
135706f2543Smrg	    echo "$0: invalid mode: $mode" >&2
136706f2543Smrg	    exit 1;;
137706f2543Smrg	esac
138706f2543Smrg        continue;;
139706f2543Smrg
140706f2543Smrg    -o) chowncmd="$chownprog $2"
141706f2543Smrg        shift
142706f2543Smrg        shift
143706f2543Smrg        continue;;
144706f2543Smrg
145706f2543Smrg    -s) stripcmd=$stripprog
146706f2543Smrg        shift
147706f2543Smrg        continue;;
148706f2543Smrg
149706f2543Smrg    -t) dstarg=$2
150706f2543Smrg	shift
151706f2543Smrg	shift
152706f2543Smrg	continue;;
153706f2543Smrg
154706f2543Smrg    -T) no_target_directory=true
155706f2543Smrg	shift
156706f2543Smrg	continue;;
157706f2543Smrg
158706f2543Smrg    --version) echo "$0 $scriptversion"; exit $?;;
159706f2543Smrg
160706f2543Smrg    --)	shift
161706f2543Smrg	break;;
162706f2543Smrg
163706f2543Smrg    -*)	echo "$0: invalid option: $1" >&2
164706f2543Smrg	exit 1;;
165706f2543Smrg
166706f2543Smrg    *)  break;;
167706f2543Smrg  esac
168706f2543Smrgdone
169706f2543Smrg
170706f2543Smrgif test $# -ne 0 && test -z "$dir_arg$dstarg"; then
171706f2543Smrg  # When -d is used, all remaining arguments are directories to create.
172706f2543Smrg  # When -t is used, the destination is already specified.
173706f2543Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
174706f2543Smrg  for arg
175706f2543Smrg  do
176706f2543Smrg    if test -n "$dstarg"; then
177706f2543Smrg      # $@ is not empty: it contains at least $arg.
178706f2543Smrg      set fnord "$@" "$dstarg"
179706f2543Smrg      shift # fnord
180706f2543Smrg    fi
181706f2543Smrg    shift # arg
182706f2543Smrg    dstarg=$arg
183706f2543Smrg  done
184706f2543Smrgfi
185706f2543Smrg
186706f2543Smrgif test $# -eq 0; then
187706f2543Smrg  if test -z "$dir_arg"; then
188706f2543Smrg    echo "$0: no input file specified." >&2
189706f2543Smrg    exit 1
190706f2543Smrg  fi
191706f2543Smrg  # It's OK to call `install-sh -d' without argument.
192706f2543Smrg  # This can happen when creating conditional directories.
193706f2543Smrg  exit 0
194706f2543Smrgfi
195706f2543Smrg
196706f2543Smrgif test -z "$dir_arg"; then
197706f2543Smrg  trap '(exit $?); exit' 1 2 13 15
198706f2543Smrg
199706f2543Smrg  # Set umask so as not to create temps with too-generous modes.
200706f2543Smrg  # However, 'strip' requires both read and write access to temps.
201706f2543Smrg  case $mode in
202706f2543Smrg    # Optimize common cases.
203706f2543Smrg    *644) cp_umask=133;;
204706f2543Smrg    *755) cp_umask=22;;
205706f2543Smrg
206706f2543Smrg    *[0-7])
207706f2543Smrg      if test -z "$stripcmd"; then
208706f2543Smrg	u_plus_rw=
209706f2543Smrg      else
210706f2543Smrg	u_plus_rw='% 200'
211706f2543Smrg      fi
212706f2543Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
213706f2543Smrg    *)
214706f2543Smrg      if test -z "$stripcmd"; then
215706f2543Smrg	u_plus_rw=
216706f2543Smrg      else
217706f2543Smrg	u_plus_rw=,u+rw
218706f2543Smrg      fi
219706f2543Smrg      cp_umask=$mode$u_plus_rw;;
220706f2543Smrg  esac
221706f2543Smrgfi
222706f2543Smrg
223706f2543Smrgfor src
224706f2543Smrgdo
225706f2543Smrg  # Protect names starting with `-'.
226706f2543Smrg  case $src in
227706f2543Smrg    -*) src=./$src ;;
228706f2543Smrg  esac
229706f2543Smrg
230706f2543Smrg  if test -n "$dir_arg"; then
231706f2543Smrg    dst=$src
232706f2543Smrg    dstdir=$dst
233706f2543Smrg    test -d "$dstdir"
234706f2543Smrg    dstdir_status=$?
235706f2543Smrg  else
236706f2543Smrg
237706f2543Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
238706f2543Smrg    # might cause directories to be created, which would be especially bad
239706f2543Smrg    # if $src (and thus $dsttmp) contains '*'.
240706f2543Smrg    if test ! -f "$src" && test ! -d "$src"; then
241706f2543Smrg      echo "$0: $src does not exist." >&2
242706f2543Smrg      exit 1
243706f2543Smrg    fi
244706f2543Smrg
245706f2543Smrg    if test -z "$dstarg"; then
246706f2543Smrg      echo "$0: no destination specified." >&2
247706f2543Smrg      exit 1
248706f2543Smrg    fi
249706f2543Smrg
250706f2543Smrg    dst=$dstarg
251706f2543Smrg    # Protect names starting with `-'.
252706f2543Smrg    case $dst in
253706f2543Smrg      -*) dst=./$dst ;;
254706f2543Smrg    esac
255706f2543Smrg
256706f2543Smrg    # If destination is a directory, append the input filename; won't work
257706f2543Smrg    # if double slashes aren't ignored.
258706f2543Smrg    if test -d "$dst"; then
259706f2543Smrg      if test -n "$no_target_directory"; then
260706f2543Smrg	echo "$0: $dstarg: Is a directory" >&2
261706f2543Smrg	exit 1
262706f2543Smrg      fi
263706f2543Smrg      dstdir=$dst
264706f2543Smrg      dst=$dstdir/`basename "$src"`
265706f2543Smrg      dstdir_status=0
266706f2543Smrg    else
267706f2543Smrg      # Prefer dirname, but fall back on a substitute if dirname fails.
268706f2543Smrg      dstdir=`
269706f2543Smrg	(dirname "$dst") 2>/dev/null ||
270706f2543Smrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
271706f2543Smrg	     X"$dst" : 'X\(//\)[^/]' \| \
272706f2543Smrg	     X"$dst" : 'X\(//\)$' \| \
273706f2543Smrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
274706f2543Smrg	echo X"$dst" |
275706f2543Smrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
276706f2543Smrg		   s//\1/
277706f2543Smrg		   q
278706f2543Smrg		 }
279706f2543Smrg		 /^X\(\/\/\)[^/].*/{
280706f2543Smrg		   s//\1/
281706f2543Smrg		   q
282706f2543Smrg		 }
283706f2543Smrg		 /^X\(\/\/\)$/{
284706f2543Smrg		   s//\1/
285706f2543Smrg		   q
286706f2543Smrg		 }
287706f2543Smrg		 /^X\(\/\).*/{
288706f2543Smrg		   s//\1/
289706f2543Smrg		   q
290706f2543Smrg		 }
291706f2543Smrg		 s/.*/./; q'
292706f2543Smrg      `
293706f2543Smrg
294706f2543Smrg      test -d "$dstdir"
295706f2543Smrg      dstdir_status=$?
296706f2543Smrg    fi
297706f2543Smrg  fi
298706f2543Smrg
299706f2543Smrg  obsolete_mkdir_used=false
300706f2543Smrg
301706f2543Smrg  if test $dstdir_status != 0; then
302706f2543Smrg    case $posix_mkdir in
303706f2543Smrg      '')
304706f2543Smrg	# Create intermediate dirs using mode 755 as modified by the umask.
305706f2543Smrg	# This is like FreeBSD 'install' as of 1997-10-28.
306706f2543Smrg	umask=`umask`
307706f2543Smrg	case $stripcmd.$umask in
308706f2543Smrg	  # Optimize common cases.
309706f2543Smrg	  *[2367][2367]) mkdir_umask=$umask;;
310706f2543Smrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
311706f2543Smrg
312706f2543Smrg	  *[0-7])
313706f2543Smrg	    mkdir_umask=`expr $umask + 22 \
314706f2543Smrg	      - $umask % 100 % 40 + $umask % 20 \
315706f2543Smrg	      - $umask % 10 % 4 + $umask % 2
316706f2543Smrg	    `;;
317706f2543Smrg	  *) mkdir_umask=$umask,go-w;;
318706f2543Smrg	esac
319706f2543Smrg
320706f2543Smrg	# With -d, create the new directory with the user-specified mode.
321706f2543Smrg	# Otherwise, rely on $mkdir_umask.
322706f2543Smrg	if test -n "$dir_arg"; then
323706f2543Smrg	  mkdir_mode=-m$mode
324706f2543Smrg	else
325706f2543Smrg	  mkdir_mode=
326706f2543Smrg	fi
327706f2543Smrg
328706f2543Smrg	posix_mkdir=false
329706f2543Smrg	case $umask in
330706f2543Smrg	  *[123567][0-7][0-7])
331706f2543Smrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
332706f2543Smrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
333706f2543Smrg	    ;;
334706f2543Smrg	  *)
335706f2543Smrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
336706f2543Smrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
337706f2543Smrg
338706f2543Smrg	    if (umask $mkdir_umask &&
339706f2543Smrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
340706f2543Smrg	    then
341706f2543Smrg	      if test -z "$dir_arg" || {
342706f2543Smrg		   # Check for POSIX incompatibilities with -m.
343706f2543Smrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
344706f2543Smrg		   # other-writeable bit of parent directory when it shouldn't.
345706f2543Smrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
346706f2543Smrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
347706f2543Smrg		   case $ls_ld_tmpdir in
348706f2543Smrg		     d????-?r-*) different_mode=700;;
349706f2543Smrg		     d????-?--*) different_mode=755;;
350706f2543Smrg		     *) false;;
351706f2543Smrg		   esac &&
352706f2543Smrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
353706f2543Smrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
354706f2543Smrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
355706f2543Smrg		   }
356706f2543Smrg		 }
357706f2543Smrg	      then posix_mkdir=:
358706f2543Smrg	      fi
359706f2543Smrg	      rmdir "$tmpdir/d" "$tmpdir"
360706f2543Smrg	    else
361706f2543Smrg	      # Remove any dirs left behind by ancient mkdir implementations.
362706f2543Smrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
363706f2543Smrg	    fi
364706f2543Smrg	    trap '' 0;;
365706f2543Smrg	esac;;
366706f2543Smrg    esac
367706f2543Smrg
368706f2543Smrg    if
369706f2543Smrg      $posix_mkdir && (
370706f2543Smrg	umask $mkdir_umask &&
371706f2543Smrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
372706f2543Smrg      )
373706f2543Smrg    then :
374706f2543Smrg    else
375706f2543Smrg
376706f2543Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
377706f2543Smrg      # or it failed possibly due to a race condition.  Create the
378706f2543Smrg      # directory the slow way, step by step, checking for races as we go.
379706f2543Smrg
380706f2543Smrg      case $dstdir in
381706f2543Smrg	/*) prefix=/ ;;
382706f2543Smrg	-*) prefix=./ ;;
383706f2543Smrg	*)  prefix= ;;
384706f2543Smrg      esac
385706f2543Smrg
386706f2543Smrg      case $posix_glob in
387706f2543Smrg        '')
388706f2543Smrg	  if (set -f) 2>/dev/null; then
389706f2543Smrg	    posix_glob=true
390706f2543Smrg	  else
391706f2543Smrg	    posix_glob=false
392706f2543Smrg	  fi ;;
393706f2543Smrg      esac
394706f2543Smrg
395706f2543Smrg      oIFS=$IFS
396706f2543Smrg      IFS=/
397706f2543Smrg      $posix_glob && set -f
398706f2543Smrg      set fnord $dstdir
399706f2543Smrg      shift
400706f2543Smrg      $posix_glob && set +f
401706f2543Smrg      IFS=$oIFS
402706f2543Smrg
403706f2543Smrg      prefixes=
404706f2543Smrg
405706f2543Smrg      for d
406706f2543Smrg      do
407706f2543Smrg	test -z "$d" && continue
408706f2543Smrg
409706f2543Smrg	prefix=$prefix$d
410706f2543Smrg	if test -d "$prefix"; then
411706f2543Smrg	  prefixes=
412706f2543Smrg	else
413706f2543Smrg	  if $posix_mkdir; then
414706f2543Smrg	    (umask=$mkdir_umask &&
415706f2543Smrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
416706f2543Smrg	    # Don't fail if two instances are running concurrently.
417706f2543Smrg	    test -d "$prefix" || exit 1
418706f2543Smrg	  else
419706f2543Smrg	    case $prefix in
420706f2543Smrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
421706f2543Smrg	      *) qprefix=$prefix;;
422706f2543Smrg	    esac
423706f2543Smrg	    prefixes="$prefixes '$qprefix'"
424706f2543Smrg	  fi
425706f2543Smrg	fi
426706f2543Smrg	prefix=$prefix/
427706f2543Smrg      done
428706f2543Smrg
429706f2543Smrg      if test -n "$prefixes"; then
430706f2543Smrg	# Don't fail if two instances are running concurrently.
431706f2543Smrg	(umask $mkdir_umask &&
432706f2543Smrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
433706f2543Smrg	  test -d "$dstdir" || exit 1
434706f2543Smrg	obsolete_mkdir_used=true
435706f2543Smrg      fi
436706f2543Smrg    fi
437706f2543Smrg  fi
438706f2543Smrg
439706f2543Smrg  if test -n "$dir_arg"; then
440706f2543Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
441706f2543Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
442706f2543Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
443706f2543Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
444706f2543Smrg  else
445706f2543Smrg
446706f2543Smrg    # Make a couple of temp file names in the proper directory.
447706f2543Smrg    dsttmp=$dstdir/_inst.$$_
448706f2543Smrg    rmtmp=$dstdir/_rm.$$_
449706f2543Smrg
450706f2543Smrg    # Trap to clean up those temp files at exit.
451706f2543Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
452706f2543Smrg
453706f2543Smrg    # Copy the file name to the temp name.
454706f2543Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
455706f2543Smrg
456706f2543Smrg    # and set any options; do chmod last to preserve setuid bits.
457706f2543Smrg    #
458706f2543Smrg    # If any of these fail, we abort the whole thing.  If we want to
459706f2543Smrg    # ignore errors from any of these, just make sure not to ignore
460706f2543Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
461706f2543Smrg    #
462706f2543Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } \
463706f2543Smrg      && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } \
464706f2543Smrg      && { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } \
465706f2543Smrg      && { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
466706f2543Smrg
467706f2543Smrg    # Now rename the file to the real destination.
468706f2543Smrg    { $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null \
469706f2543Smrg      || {
470706f2543Smrg	   # The rename failed, perhaps because mv can't rename something else
471706f2543Smrg	   # to itself, or perhaps because mv is so ancient that it does not
472706f2543Smrg	   # support -f.
473706f2543Smrg
474706f2543Smrg	   # Now remove or move aside any old file at destination location.
475706f2543Smrg	   # We try this two ways since rm can't unlink itself on some
476706f2543Smrg	   # systems and the destination file might be busy for other
477706f2543Smrg	   # reasons.  In this case, the final cleanup might fail but the new
478706f2543Smrg	   # file should still install successfully.
479706f2543Smrg	   {
480706f2543Smrg	     if test -f "$dst"; then
481706f2543Smrg	       $doit $rmcmd -f "$dst" 2>/dev/null \
482706f2543Smrg	       || { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null \
483706f2543Smrg		     && { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }; }\
484706f2543Smrg	       || {
485706f2543Smrg		 echo "$0: cannot unlink or rename $dst" >&2
486706f2543Smrg		 (exit 1); exit 1
487706f2543Smrg	       }
488706f2543Smrg	     else
489706f2543Smrg	       :
490706f2543Smrg	     fi
491706f2543Smrg	   } &&
492706f2543Smrg
493706f2543Smrg	   # Now rename the file to the real destination.
494706f2543Smrg	   $doit $mvcmd "$dsttmp" "$dst"
495706f2543Smrg	 }
496706f2543Smrg    } || exit 1
497706f2543Smrg
498706f2543Smrg    trap '' 0
499706f2543Smrg  fi
500706f2543Smrgdone
501706f2543Smrg
502706f2543Smrg# Local variables:
503706f2543Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
504706f2543Smrg# time-stamp-start: "scriptversion="
505706f2543Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
506706f2543Smrg# time-stamp-end: "$"
507706f2543Smrg# End:
508