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