install-sh revision 42a55b46
1c582b7e3Smrg#!/bin/sh
2c582b7e3Smrg# install - install a program, script, or datafile
3c582b7e3Smrg
442a55b46Smrgscriptversion=2011-01-19.21; # UTC
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
15942a55b46Smrg	# Protect names problematic for `test' and other utilities.
16042a55b46Smrg	case $dst_arg in
16142a55b46Smrg	  -* | [=\(\)!]) dst_arg=./$dst_arg;;
16242a55b46Smrg	esac
163c582b7e3Smrg	shift;;
164c582b7e3Smrg
165c582b7e3Smrg    -T) no_target_directory=true;;
166c582b7e3Smrg
167c582b7e3Smrg    --version) echo "$0 $scriptversion"; exit $?;;
168c582b7e3Smrg
169c582b7e3Smrg    --)	shift
170c582b7e3Smrg	break;;
171c582b7e3Smrg
172c582b7e3Smrg    -*)	echo "$0: invalid option: $1" >&2
173c582b7e3Smrg	exit 1;;
174c582b7e3Smrg
175c582b7e3Smrg    *)  break;;
176c582b7e3Smrg  esac
177c582b7e3Smrg  shift
178c582b7e3Smrgdone
179c582b7e3Smrg
180c582b7e3Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
181c582b7e3Smrg  # When -d is used, all remaining arguments are directories to create.
182c582b7e3Smrg  # When -t is used, the destination is already specified.
183c582b7e3Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
184c582b7e3Smrg  for arg
185c582b7e3Smrg  do
186c582b7e3Smrg    if test -n "$dst_arg"; then
187c582b7e3Smrg      # $@ is not empty: it contains at least $arg.
188c582b7e3Smrg      set fnord "$@" "$dst_arg"
189c582b7e3Smrg      shift # fnord
190c582b7e3Smrg    fi
191c582b7e3Smrg    shift # arg
192c582b7e3Smrg    dst_arg=$arg
19342a55b46Smrg    # Protect names problematic for `test' and other utilities.
19442a55b46Smrg    case $dst_arg in
19542a55b46Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
19642a55b46Smrg    esac
197c582b7e3Smrg  done
198c582b7e3Smrgfi
199c582b7e3Smrg
200c582b7e3Smrgif test $# -eq 0; then
201c582b7e3Smrg  if test -z "$dir_arg"; then
202c582b7e3Smrg    echo "$0: no input file specified." >&2
203c582b7e3Smrg    exit 1
204c582b7e3Smrg  fi
205c582b7e3Smrg  # It's OK to call `install-sh -d' without argument.
206c582b7e3Smrg  # This can happen when creating conditional directories.
207c582b7e3Smrg  exit 0
208c582b7e3Smrgfi
209c582b7e3Smrg
210c582b7e3Smrgif test -z "$dir_arg"; then
21142a55b46Smrg  do_exit='(exit $ret); exit $ret'
21242a55b46Smrg  trap "ret=129; $do_exit" 1
21342a55b46Smrg  trap "ret=130; $do_exit" 2
21442a55b46Smrg  trap "ret=141; $do_exit" 13
21542a55b46Smrg  trap "ret=143; $do_exit" 15
216c582b7e3Smrg
217c582b7e3Smrg  # Set umask so as not to create temps with too-generous modes.
218c582b7e3Smrg  # However, 'strip' requires both read and write access to temps.
219c582b7e3Smrg  case $mode in
220c582b7e3Smrg    # Optimize common cases.
221c582b7e3Smrg    *644) cp_umask=133;;
222c582b7e3Smrg    *755) cp_umask=22;;
223c582b7e3Smrg
224c582b7e3Smrg    *[0-7])
225c582b7e3Smrg      if test -z "$stripcmd"; then
226c582b7e3Smrg	u_plus_rw=
227c582b7e3Smrg      else
228c582b7e3Smrg	u_plus_rw='% 200'
229c582b7e3Smrg      fi
230c582b7e3Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
231c582b7e3Smrg    *)
232c582b7e3Smrg      if test -z "$stripcmd"; then
233c582b7e3Smrg	u_plus_rw=
234c582b7e3Smrg      else
235c582b7e3Smrg	u_plus_rw=,u+rw
236c582b7e3Smrg      fi
237c582b7e3Smrg      cp_umask=$mode$u_plus_rw;;
238c582b7e3Smrg  esac
239c582b7e3Smrgfi
240c582b7e3Smrg
241c582b7e3Smrgfor src
242c582b7e3Smrgdo
24342a55b46Smrg  # Protect names problematic for `test' and other utilities.
244c582b7e3Smrg  case $src in
24542a55b46Smrg    -* | [=\(\)!]) src=./$src;;
246c582b7e3Smrg  esac
247c582b7e3Smrg
248c582b7e3Smrg  if test -n "$dir_arg"; then
249c582b7e3Smrg    dst=$src
250c582b7e3Smrg    dstdir=$dst
251c582b7e3Smrg    test -d "$dstdir"
252c582b7e3Smrg    dstdir_status=$?
253c582b7e3Smrg  else
254c582b7e3Smrg
255c582b7e3Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
256c582b7e3Smrg    # might cause directories to be created, which would be especially bad
257c582b7e3Smrg    # if $src (and thus $dsttmp) contains '*'.
258c582b7e3Smrg    if test ! -f "$src" && test ! -d "$src"; then
259c582b7e3Smrg      echo "$0: $src does not exist." >&2
260c582b7e3Smrg      exit 1
261c582b7e3Smrg    fi
262c582b7e3Smrg
263c582b7e3Smrg    if test -z "$dst_arg"; then
264c582b7e3Smrg      echo "$0: no destination specified." >&2
265c582b7e3Smrg      exit 1
266c582b7e3Smrg    fi
267c582b7e3Smrg    dst=$dst_arg
268c582b7e3Smrg
269c582b7e3Smrg    # If destination is a directory, append the input filename; won't work
270c582b7e3Smrg    # if double slashes aren't ignored.
271c582b7e3Smrg    if test -d "$dst"; then
272c582b7e3Smrg      if test -n "$no_target_directory"; then
273c582b7e3Smrg	echo "$0: $dst_arg: Is a directory" >&2
274c582b7e3Smrg	exit 1
275c582b7e3Smrg      fi
276c582b7e3Smrg      dstdir=$dst
277c582b7e3Smrg      dst=$dstdir/`basename "$src"`
278c582b7e3Smrg      dstdir_status=0
279c582b7e3Smrg    else
280c582b7e3Smrg      # Prefer dirname, but fall back on a substitute if dirname fails.
281c582b7e3Smrg      dstdir=`
282c582b7e3Smrg	(dirname "$dst") 2>/dev/null ||
283c582b7e3Smrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
284c582b7e3Smrg	     X"$dst" : 'X\(//\)[^/]' \| \
285c582b7e3Smrg	     X"$dst" : 'X\(//\)$' \| \
286c582b7e3Smrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
287c582b7e3Smrg	echo X"$dst" |
288c582b7e3Smrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
289c582b7e3Smrg		   s//\1/
290c582b7e3Smrg		   q
291c582b7e3Smrg		 }
292c582b7e3Smrg		 /^X\(\/\/\)[^/].*/{
293c582b7e3Smrg		   s//\1/
294c582b7e3Smrg		   q
295c582b7e3Smrg		 }
296c582b7e3Smrg		 /^X\(\/\/\)$/{
297c582b7e3Smrg		   s//\1/
298c582b7e3Smrg		   q
299c582b7e3Smrg		 }
300c582b7e3Smrg		 /^X\(\/\).*/{
301c582b7e3Smrg		   s//\1/
302c582b7e3Smrg		   q
303c582b7e3Smrg		 }
304c582b7e3Smrg		 s/.*/./; q'
305c582b7e3Smrg      `
306c582b7e3Smrg
307c582b7e3Smrg      test -d "$dstdir"
308c582b7e3Smrg      dstdir_status=$?
309c582b7e3Smrg    fi
310c582b7e3Smrg  fi
311c582b7e3Smrg
312c582b7e3Smrg  obsolete_mkdir_used=false
313c582b7e3Smrg
314c582b7e3Smrg  if test $dstdir_status != 0; then
315c582b7e3Smrg    case $posix_mkdir in
316c582b7e3Smrg      '')
317c582b7e3Smrg	# Create intermediate dirs using mode 755 as modified by the umask.
318c582b7e3Smrg	# This is like FreeBSD 'install' as of 1997-10-28.
319c582b7e3Smrg	umask=`umask`
320c582b7e3Smrg	case $stripcmd.$umask in
321c582b7e3Smrg	  # Optimize common cases.
322c582b7e3Smrg	  *[2367][2367]) mkdir_umask=$umask;;
323c582b7e3Smrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
324c582b7e3Smrg
325c582b7e3Smrg	  *[0-7])
326c582b7e3Smrg	    mkdir_umask=`expr $umask + 22 \
327c582b7e3Smrg	      - $umask % 100 % 40 + $umask % 20 \
328c582b7e3Smrg	      - $umask % 10 % 4 + $umask % 2
329c582b7e3Smrg	    `;;
330c582b7e3Smrg	  *) mkdir_umask=$umask,go-w;;
331c582b7e3Smrg	esac
332c582b7e3Smrg
333c582b7e3Smrg	# With -d, create the new directory with the user-specified mode.
334c582b7e3Smrg	# Otherwise, rely on $mkdir_umask.
335c582b7e3Smrg	if test -n "$dir_arg"; then
336c582b7e3Smrg	  mkdir_mode=-m$mode
337c582b7e3Smrg	else
338c582b7e3Smrg	  mkdir_mode=
339c582b7e3Smrg	fi
340c582b7e3Smrg
341c582b7e3Smrg	posix_mkdir=false
342c582b7e3Smrg	case $umask in
343c582b7e3Smrg	  *[123567][0-7][0-7])
344c582b7e3Smrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
345c582b7e3Smrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
346c582b7e3Smrg	    ;;
347c582b7e3Smrg	  *)
348c582b7e3Smrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
349c582b7e3Smrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
350c582b7e3Smrg
351c582b7e3Smrg	    if (umask $mkdir_umask &&
352c582b7e3Smrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
353c582b7e3Smrg	    then
354c582b7e3Smrg	      if test -z "$dir_arg" || {
355c582b7e3Smrg		   # Check for POSIX incompatibilities with -m.
356c582b7e3Smrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
357c582b7e3Smrg		   # other-writeable bit of parent directory when it shouldn't.
358c582b7e3Smrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
359c582b7e3Smrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
360c582b7e3Smrg		   case $ls_ld_tmpdir in
361c582b7e3Smrg		     d????-?r-*) different_mode=700;;
362c582b7e3Smrg		     d????-?--*) different_mode=755;;
363c582b7e3Smrg		     *) false;;
364c582b7e3Smrg		   esac &&
365c582b7e3Smrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
366c582b7e3Smrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
367c582b7e3Smrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
368c582b7e3Smrg		   }
369c582b7e3Smrg		 }
370c582b7e3Smrg	      then posix_mkdir=:
371c582b7e3Smrg	      fi
372c582b7e3Smrg	      rmdir "$tmpdir/d" "$tmpdir"
373c582b7e3Smrg	    else
374c582b7e3Smrg	      # Remove any dirs left behind by ancient mkdir implementations.
375c582b7e3Smrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
376c582b7e3Smrg	    fi
377c582b7e3Smrg	    trap '' 0;;
378c582b7e3Smrg	esac;;
379c582b7e3Smrg    esac
380c582b7e3Smrg
381c582b7e3Smrg    if
382c582b7e3Smrg      $posix_mkdir && (
383c582b7e3Smrg	umask $mkdir_umask &&
384c582b7e3Smrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
385c582b7e3Smrg      )
386c582b7e3Smrg    then :
387c582b7e3Smrg    else
388c582b7e3Smrg
389c582b7e3Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
390c582b7e3Smrg      # or it failed possibly due to a race condition.  Create the
391c582b7e3Smrg      # directory the slow way, step by step, checking for races as we go.
392c582b7e3Smrg
393c582b7e3Smrg      case $dstdir in
394c582b7e3Smrg	/*) prefix='/';;
39542a55b46Smrg	[-=\(\)!]*) prefix='./';;
396c582b7e3Smrg	*)  prefix='';;
397c582b7e3Smrg      esac
398c582b7e3Smrg
399c582b7e3Smrg      eval "$initialize_posix_glob"
400c582b7e3Smrg
401c582b7e3Smrg      oIFS=$IFS
402c582b7e3Smrg      IFS=/
403c582b7e3Smrg      $posix_glob set -f
404c582b7e3Smrg      set fnord $dstdir
405c582b7e3Smrg      shift
406c582b7e3Smrg      $posix_glob set +f
407c582b7e3Smrg      IFS=$oIFS
408c582b7e3Smrg
409c582b7e3Smrg      prefixes=
410c582b7e3Smrg
411c582b7e3Smrg      for d
412c582b7e3Smrg      do
41342a55b46Smrg	test X"$d" = X && continue
414c582b7e3Smrg
415c582b7e3Smrg	prefix=$prefix$d
416c582b7e3Smrg	if test -d "$prefix"; then
417c582b7e3Smrg	  prefixes=
418c582b7e3Smrg	else
419c582b7e3Smrg	  if $posix_mkdir; then
420c582b7e3Smrg	    (umask=$mkdir_umask &&
421c582b7e3Smrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
422c582b7e3Smrg	    # Don't fail if two instances are running concurrently.
423c582b7e3Smrg	    test -d "$prefix" || exit 1
424c582b7e3Smrg	  else
425c582b7e3Smrg	    case $prefix in
426c582b7e3Smrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
427c582b7e3Smrg	      *) qprefix=$prefix;;
428c582b7e3Smrg	    esac
429c582b7e3Smrg	    prefixes="$prefixes '$qprefix'"
430c582b7e3Smrg	  fi
431c582b7e3Smrg	fi
432c582b7e3Smrg	prefix=$prefix/
433c582b7e3Smrg      done
434c582b7e3Smrg
435c582b7e3Smrg      if test -n "$prefixes"; then
436c582b7e3Smrg	# Don't fail if two instances are running concurrently.
437c582b7e3Smrg	(umask $mkdir_umask &&
438c582b7e3Smrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
439c582b7e3Smrg	  test -d "$dstdir" || exit 1
440c582b7e3Smrg	obsolete_mkdir_used=true
441c582b7e3Smrg      fi
442c582b7e3Smrg    fi
443c582b7e3Smrg  fi
444c582b7e3Smrg
445c582b7e3Smrg  if test -n "$dir_arg"; then
446c582b7e3Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
447c582b7e3Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
448c582b7e3Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
449c582b7e3Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
450c582b7e3Smrg  else
451c582b7e3Smrg
452c582b7e3Smrg    # Make a couple of temp file names in the proper directory.
453c582b7e3Smrg    dsttmp=$dstdir/_inst.$$_
454c582b7e3Smrg    rmtmp=$dstdir/_rm.$$_
455c582b7e3Smrg
456c582b7e3Smrg    # Trap to clean up those temp files at exit.
457c582b7e3Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
458c582b7e3Smrg
459c582b7e3Smrg    # Copy the file name to the temp name.
460c582b7e3Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
461c582b7e3Smrg
462c582b7e3Smrg    # and set any options; do chmod last to preserve setuid bits.
463c582b7e3Smrg    #
464c582b7e3Smrg    # If any of these fail, we abort the whole thing.  If we want to
465c582b7e3Smrg    # ignore errors from any of these, just make sure not to ignore
466c582b7e3Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
467c582b7e3Smrg    #
468c582b7e3Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
469c582b7e3Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
470c582b7e3Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
471c582b7e3Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
472c582b7e3Smrg
473c582b7e3Smrg    # If -C, don't bother to copy if it wouldn't change the file.
474c582b7e3Smrg    if $copy_on_change &&
475c582b7e3Smrg       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
476c582b7e3Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
477c582b7e3Smrg
478c582b7e3Smrg       eval "$initialize_posix_glob" &&
479c582b7e3Smrg       $posix_glob set -f &&
480c582b7e3Smrg       set X $old && old=:$2:$4:$5:$6 &&
481c582b7e3Smrg       set X $new && new=:$2:$4:$5:$6 &&
482c582b7e3Smrg       $posix_glob set +f &&
483c582b7e3Smrg
484c582b7e3Smrg       test "$old" = "$new" &&
485c582b7e3Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
486c582b7e3Smrg    then
487c582b7e3Smrg      rm -f "$dsttmp"
488c582b7e3Smrg    else
489c582b7e3Smrg      # Rename the file to the real destination.
490c582b7e3Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
491c582b7e3Smrg
492c582b7e3Smrg      # The rename failed, perhaps because mv can't rename something else
493c582b7e3Smrg      # to itself, or perhaps because mv is so ancient that it does not
494c582b7e3Smrg      # support -f.
495c582b7e3Smrg      {
496c582b7e3Smrg	# Now remove or move aside any old file at destination location.
497c582b7e3Smrg	# We try this two ways since rm can't unlink itself on some
498c582b7e3Smrg	# systems and the destination file might be busy for other
499c582b7e3Smrg	# reasons.  In this case, the final cleanup might fail but the new
500c582b7e3Smrg	# file should still install successfully.
501c582b7e3Smrg	{
502c582b7e3Smrg	  test ! -f "$dst" ||
503c582b7e3Smrg	  $doit $rmcmd -f "$dst" 2>/dev/null ||
504c582b7e3Smrg	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
505c582b7e3Smrg	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
506c582b7e3Smrg	  } ||
507c582b7e3Smrg	  { echo "$0: cannot unlink or rename $dst" >&2
508c582b7e3Smrg	    (exit 1); exit 1
509c582b7e3Smrg	  }
510c582b7e3Smrg	} &&
511c582b7e3Smrg
512c582b7e3Smrg	# Now rename the file to the real destination.
513c582b7e3Smrg	$doit $mvcmd "$dsttmp" "$dst"
514c582b7e3Smrg      }
515c582b7e3Smrg    fi || exit 1
516c582b7e3Smrg
517c582b7e3Smrg    trap '' 0
518c582b7e3Smrg  fi
519c582b7e3Smrgdone
520c582b7e3Smrg
521c582b7e3Smrg# Local variables:
522c582b7e3Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
523c582b7e3Smrg# time-stamp-start: "scriptversion="
524c582b7e3Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
52519019ffeSmrg# time-stamp-time-zone: "UTC"
52619019ffeSmrg# time-stamp-end: "; # UTC"
527c582b7e3Smrg# End:
528