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