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