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