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