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