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