install-sh revision 91ec45ce
1fc89c0fbSmrg#!/bin/sh
2fc89c0fbSmrg# install - install a program, script, or datafile
3fc89c0fbSmrg
491ec45ceSmrgscriptversion=2009-04-28.21; # UTC
5fc89c0fbSmrg
6fc89c0fbSmrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7fc89c0fbSmrg# later released in X11R6 (xc/config/util/install.sh) with the
8fc89c0fbSmrg# following copyright and license.
9fc89c0fbSmrg#
10fc89c0fbSmrg# Copyright (C) 1994 X Consortium
11fc89c0fbSmrg#
12fc89c0fbSmrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13fc89c0fbSmrg# of this software and associated documentation files (the "Software"), to
14fc89c0fbSmrg# deal in the Software without restriction, including without limitation the
15fc89c0fbSmrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16fc89c0fbSmrg# sell copies of the Software, and to permit persons to whom the Software is
17fc89c0fbSmrg# furnished to do so, subject to the following conditions:
18fc89c0fbSmrg#
19fc89c0fbSmrg# The above copyright notice and this permission notice shall be included in
20fc89c0fbSmrg# all copies or substantial portions of the Software.
21fc89c0fbSmrg#
22fc89c0fbSmrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23fc89c0fbSmrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24fc89c0fbSmrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25fc89c0fbSmrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26fc89c0fbSmrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27fc89c0fbSmrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28fc89c0fbSmrg#
29fc89c0fbSmrg# Except as contained in this notice, the name of the X Consortium shall not
30fc89c0fbSmrg# be used in advertising or otherwise to promote the sale, use or other deal-
31fc89c0fbSmrg# ings in this Software without prior written authorization from the X Consor-
32fc89c0fbSmrg# tium.
33fc89c0fbSmrg#
34fc89c0fbSmrg#
35fc89c0fbSmrg# FSF changes to this file are in the public domain.
36fc89c0fbSmrg#
37fc89c0fbSmrg# Calling this script install-sh is preferred over install.sh, to prevent
38fc89c0fbSmrg# `make' implicit rules from creating a file called install from it
39fc89c0fbSmrg# when there is no Makefile.
40fc89c0fbSmrg#
41fc89c0fbSmrg# This script is compatible with the BSD install script, but was written
42fc89c0fbSmrg# from scratch.
43fc89c0fbSmrg
44fc89c0fbSmrgnl='
45fc89c0fbSmrg'
46fc89c0fbSmrgIFS=" ""	$nl"
47fc89c0fbSmrg
48fc89c0fbSmrg# set DOITPROG to echo to test this script
49fc89c0fbSmrg
50fc89c0fbSmrg# Don't use :- since 4.3BSD and earlier shells don't like it.
5191ec45ceSmrgdoit=${DOITPROG-}
52fc89c0fbSmrgif test -z "$doit"; then
53fc89c0fbSmrg  doit_exec=exec
54fc89c0fbSmrgelse
55fc89c0fbSmrg  doit_exec=$doit
56fc89c0fbSmrgfi
57fc89c0fbSmrg
58fc89c0fbSmrg# Put in absolute file names if you don't have them in your path;
59fc89c0fbSmrg# or use environment vars.
60fc89c0fbSmrg
6191ec45ceSmrgchgrpprog=${CHGRPPROG-chgrp}
6291ec45ceSmrgchmodprog=${CHMODPROG-chmod}
6391ec45ceSmrgchownprog=${CHOWNPROG-chown}
6491ec45ceSmrgcmpprog=${CMPPROG-cmp}
6591ec45ceSmrgcpprog=${CPPROG-cp}
6691ec45ceSmrgmkdirprog=${MKDIRPROG-mkdir}
6791ec45ceSmrgmvprog=${MVPROG-mv}
6891ec45ceSmrgrmprog=${RMPROG-rm}
6991ec45ceSmrgstripprog=${STRIPPROG-strip}
7091ec45ceSmrg
7191ec45ceSmrgposix_glob='?'
7291ec45ceSmrginitialize_posix_glob='
7391ec45ceSmrg  test "$posix_glob" != "?" || {
7491ec45ceSmrg    if (set -f) 2>/dev/null; then
7591ec45ceSmrg      posix_glob=
7691ec45ceSmrg    else
7791ec45ceSmrg      posix_glob=:
7891ec45ceSmrg    fi
7991ec45ceSmrg  }
8091ec45ceSmrg'
81fc89c0fbSmrg
82fc89c0fbSmrgposix_mkdir=
83fc89c0fbSmrg
84fc89c0fbSmrg# Desired mode of installed file.
85fc89c0fbSmrgmode=0755
86fc89c0fbSmrg
8791ec45ceSmrgchgrpcmd=
88fc89c0fbSmrgchmodcmd=$chmodprog
89fc89c0fbSmrgchowncmd=
9091ec45ceSmrgmvcmd=$mvprog
91fc89c0fbSmrgrmcmd="$rmprog -f"
9291ec45ceSmrgstripcmd=
9391ec45ceSmrg
94fc89c0fbSmrgsrc=
95fc89c0fbSmrgdst=
96fc89c0fbSmrgdir_arg=
9791ec45ceSmrgdst_arg=
9891ec45ceSmrg
9991ec45ceSmrgcopy_on_change=false
100fc89c0fbSmrgno_target_directory=
101fc89c0fbSmrg
10291ec45ceSmrgusage="\
10391ec45ceSmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
104fc89c0fbSmrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
105fc89c0fbSmrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
106fc89c0fbSmrg   or: $0 [OPTION]... -d DIRECTORIES...
107fc89c0fbSmrg
108fc89c0fbSmrgIn the 1st form, copy SRCFILE to DSTFILE.
109fc89c0fbSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
110fc89c0fbSmrgIn the 4th, create DIRECTORIES.
111fc89c0fbSmrg
112fc89c0fbSmrgOptions:
11391ec45ceSmrg     --help     display this help and exit.
11491ec45ceSmrg     --version  display version info and exit.
11591ec45ceSmrg
11691ec45ceSmrg  -c            (ignored)
11791ec45ceSmrg  -C            install only if different (preserve the last data modification time)
11891ec45ceSmrg  -d            create directories instead of installing files.
11991ec45ceSmrg  -g GROUP      $chgrpprog installed files to GROUP.
12091ec45ceSmrg  -m MODE       $chmodprog installed files to MODE.
12191ec45ceSmrg  -o USER       $chownprog installed files to USER.
12291ec45ceSmrg  -s            $stripprog installed files.
12391ec45ceSmrg  -t DIRECTORY  install into DIRECTORY.
12491ec45ceSmrg  -T            report an error if DSTFILE is a directory.
125fc89c0fbSmrg
126fc89c0fbSmrgEnvironment variables override the default commands:
12791ec45ceSmrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
12891ec45ceSmrg  RMPROG STRIPPROG
129fc89c0fbSmrg"
130fc89c0fbSmrg
131fc89c0fbSmrgwhile test $# -ne 0; do
132fc89c0fbSmrg  case $1 in
13391ec45ceSmrg    -c) ;;
13491ec45ceSmrg
13591ec45ceSmrg    -C) copy_on_change=true;;
136fc89c0fbSmrg
13791ec45ceSmrg    -d) dir_arg=true;;
138fc89c0fbSmrg
139fc89c0fbSmrg    -g) chgrpcmd="$chgrpprog $2"
14091ec45ceSmrg	shift;;
141fc89c0fbSmrg
142fc89c0fbSmrg    --help) echo "$usage"; exit $?;;
143fc89c0fbSmrg
144fc89c0fbSmrg    -m) mode=$2
145fc89c0fbSmrg	case $mode in
146fc89c0fbSmrg	  *' '* | *'	'* | *'
147fc89c0fbSmrg'*	  | *'*'* | *'?'* | *'['*)
148fc89c0fbSmrg	    echo "$0: invalid mode: $mode" >&2
149fc89c0fbSmrg	    exit 1;;
150fc89c0fbSmrg	esac
15191ec45ceSmrg	shift;;
152fc89c0fbSmrg
153fc89c0fbSmrg    -o) chowncmd="$chownprog $2"
15491ec45ceSmrg	shift;;
155fc89c0fbSmrg
15691ec45ceSmrg    -s) stripcmd=$stripprog;;
157fc89c0fbSmrg
15891ec45ceSmrg    -t) dst_arg=$2
15991ec45ceSmrg	shift;;
160fc89c0fbSmrg
16191ec45ceSmrg    -T) no_target_directory=true;;
162fc89c0fbSmrg
163fc89c0fbSmrg    --version) echo "$0 $scriptversion"; exit $?;;
164fc89c0fbSmrg
165fc89c0fbSmrg    --)	shift
166fc89c0fbSmrg	break;;
167fc89c0fbSmrg
168fc89c0fbSmrg    -*)	echo "$0: invalid option: $1" >&2
169fc89c0fbSmrg	exit 1;;
170fc89c0fbSmrg
171fc89c0fbSmrg    *)  break;;
172fc89c0fbSmrg  esac
17391ec45ceSmrg  shift
174fc89c0fbSmrgdone
175fc89c0fbSmrg
17691ec45ceSmrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
177fc89c0fbSmrg  # When -d is used, all remaining arguments are directories to create.
178fc89c0fbSmrg  # When -t is used, the destination is already specified.
179fc89c0fbSmrg  # Otherwise, the last argument is the destination.  Remove it from $@.
180fc89c0fbSmrg  for arg
181fc89c0fbSmrg  do
18291ec45ceSmrg    if test -n "$dst_arg"; then
183fc89c0fbSmrg      # $@ is not empty: it contains at least $arg.
18491ec45ceSmrg      set fnord "$@" "$dst_arg"
185fc89c0fbSmrg      shift # fnord
186fc89c0fbSmrg    fi
187fc89c0fbSmrg    shift # arg
18891ec45ceSmrg    dst_arg=$arg
189fc89c0fbSmrg  done
190fc89c0fbSmrgfi
191fc89c0fbSmrg
192fc89c0fbSmrgif test $# -eq 0; then
193fc89c0fbSmrg  if test -z "$dir_arg"; then
194fc89c0fbSmrg    echo "$0: no input file specified." >&2
195fc89c0fbSmrg    exit 1
196fc89c0fbSmrg  fi
197fc89c0fbSmrg  # It's OK to call `install-sh -d' without argument.
198fc89c0fbSmrg  # This can happen when creating conditional directories.
199fc89c0fbSmrg  exit 0
200fc89c0fbSmrgfi
201fc89c0fbSmrg
202fc89c0fbSmrgif test -z "$dir_arg"; then
203fc89c0fbSmrg  trap '(exit $?); exit' 1 2 13 15
204fc89c0fbSmrg
205fc89c0fbSmrg  # Set umask so as not to create temps with too-generous modes.
206fc89c0fbSmrg  # However, 'strip' requires both read and write access to temps.
207fc89c0fbSmrg  case $mode in
208fc89c0fbSmrg    # Optimize common cases.
209fc89c0fbSmrg    *644) cp_umask=133;;
210fc89c0fbSmrg    *755) cp_umask=22;;
211fc89c0fbSmrg
212fc89c0fbSmrg    *[0-7])
213fc89c0fbSmrg      if test -z "$stripcmd"; then
214fc89c0fbSmrg	u_plus_rw=
215fc89c0fbSmrg      else
216fc89c0fbSmrg	u_plus_rw='% 200'
217fc89c0fbSmrg      fi
218fc89c0fbSmrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
219fc89c0fbSmrg    *)
220fc89c0fbSmrg      if test -z "$stripcmd"; then
221fc89c0fbSmrg	u_plus_rw=
222fc89c0fbSmrg      else
223fc89c0fbSmrg	u_plus_rw=,u+rw
224fc89c0fbSmrg      fi
225fc89c0fbSmrg      cp_umask=$mode$u_plus_rw;;
226fc89c0fbSmrg  esac
227fc89c0fbSmrgfi
228fc89c0fbSmrg
229fc89c0fbSmrgfor src
230fc89c0fbSmrgdo
231fc89c0fbSmrg  # Protect names starting with `-'.
232fc89c0fbSmrg  case $src in
23391ec45ceSmrg    -*) src=./$src;;
234fc89c0fbSmrg  esac
235fc89c0fbSmrg
236fc89c0fbSmrg  if test -n "$dir_arg"; then
237fc89c0fbSmrg    dst=$src
238fc89c0fbSmrg    dstdir=$dst
239fc89c0fbSmrg    test -d "$dstdir"
240fc89c0fbSmrg    dstdir_status=$?
241fc89c0fbSmrg  else
242fc89c0fbSmrg
243fc89c0fbSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
244fc89c0fbSmrg    # might cause directories to be created, which would be especially bad
245fc89c0fbSmrg    # if $src (and thus $dsttmp) contains '*'.
246fc89c0fbSmrg    if test ! -f "$src" && test ! -d "$src"; then
247fc89c0fbSmrg      echo "$0: $src does not exist." >&2
248fc89c0fbSmrg      exit 1
249fc89c0fbSmrg    fi
250fc89c0fbSmrg
25191ec45ceSmrg    if test -z "$dst_arg"; then
252fc89c0fbSmrg      echo "$0: no destination specified." >&2
253fc89c0fbSmrg      exit 1
254fc89c0fbSmrg    fi
255fc89c0fbSmrg
25691ec45ceSmrg    dst=$dst_arg
257fc89c0fbSmrg    # Protect names starting with `-'.
258fc89c0fbSmrg    case $dst in
25991ec45ceSmrg      -*) dst=./$dst;;
260fc89c0fbSmrg    esac
261fc89c0fbSmrg
262fc89c0fbSmrg    # If destination is a directory, append the input filename; won't work
263fc89c0fbSmrg    # if double slashes aren't ignored.
264fc89c0fbSmrg    if test -d "$dst"; then
265fc89c0fbSmrg      if test -n "$no_target_directory"; then
26691ec45ceSmrg	echo "$0: $dst_arg: Is a directory" >&2
267fc89c0fbSmrg	exit 1
268fc89c0fbSmrg      fi
269fc89c0fbSmrg      dstdir=$dst
270fc89c0fbSmrg      dst=$dstdir/`basename "$src"`
271fc89c0fbSmrg      dstdir_status=0
272fc89c0fbSmrg    else
273fc89c0fbSmrg      # Prefer dirname, but fall back on a substitute if dirname fails.
274fc89c0fbSmrg      dstdir=`
275fc89c0fbSmrg	(dirname "$dst") 2>/dev/null ||
276fc89c0fbSmrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
277fc89c0fbSmrg	     X"$dst" : 'X\(//\)[^/]' \| \
278fc89c0fbSmrg	     X"$dst" : 'X\(//\)$' \| \
279fc89c0fbSmrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
280fc89c0fbSmrg	echo X"$dst" |
281fc89c0fbSmrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
282fc89c0fbSmrg		   s//\1/
283fc89c0fbSmrg		   q
284fc89c0fbSmrg		 }
285fc89c0fbSmrg		 /^X\(\/\/\)[^/].*/{
286fc89c0fbSmrg		   s//\1/
287fc89c0fbSmrg		   q
288fc89c0fbSmrg		 }
289fc89c0fbSmrg		 /^X\(\/\/\)$/{
290fc89c0fbSmrg		   s//\1/
291fc89c0fbSmrg		   q
292fc89c0fbSmrg		 }
293fc89c0fbSmrg		 /^X\(\/\).*/{
294fc89c0fbSmrg		   s//\1/
295fc89c0fbSmrg		   q
296fc89c0fbSmrg		 }
297fc89c0fbSmrg		 s/.*/./; q'
298fc89c0fbSmrg      `
299fc89c0fbSmrg
300fc89c0fbSmrg      test -d "$dstdir"
301fc89c0fbSmrg      dstdir_status=$?
302fc89c0fbSmrg    fi
303fc89c0fbSmrg  fi
304fc89c0fbSmrg
305fc89c0fbSmrg  obsolete_mkdir_used=false
306fc89c0fbSmrg
307fc89c0fbSmrg  if test $dstdir_status != 0; then
308fc89c0fbSmrg    case $posix_mkdir in
309fc89c0fbSmrg      '')
310fc89c0fbSmrg	# Create intermediate dirs using mode 755 as modified by the umask.
311fc89c0fbSmrg	# This is like FreeBSD 'install' as of 1997-10-28.
312fc89c0fbSmrg	umask=`umask`
313fc89c0fbSmrg	case $stripcmd.$umask in
314fc89c0fbSmrg	  # Optimize common cases.
315fc89c0fbSmrg	  *[2367][2367]) mkdir_umask=$umask;;
316fc89c0fbSmrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
317fc89c0fbSmrg
318fc89c0fbSmrg	  *[0-7])
319fc89c0fbSmrg	    mkdir_umask=`expr $umask + 22 \
320fc89c0fbSmrg	      - $umask % 100 % 40 + $umask % 20 \
321fc89c0fbSmrg	      - $umask % 10 % 4 + $umask % 2
322fc89c0fbSmrg	    `;;
323fc89c0fbSmrg	  *) mkdir_umask=$umask,go-w;;
324fc89c0fbSmrg	esac
325fc89c0fbSmrg
326fc89c0fbSmrg	# With -d, create the new directory with the user-specified mode.
327fc89c0fbSmrg	# Otherwise, rely on $mkdir_umask.
328fc89c0fbSmrg	if test -n "$dir_arg"; then
329fc89c0fbSmrg	  mkdir_mode=-m$mode
330fc89c0fbSmrg	else
331fc89c0fbSmrg	  mkdir_mode=
332fc89c0fbSmrg	fi
333fc89c0fbSmrg
334fc89c0fbSmrg	posix_mkdir=false
335fc89c0fbSmrg	case $umask in
336fc89c0fbSmrg	  *[123567][0-7][0-7])
337fc89c0fbSmrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
338fc89c0fbSmrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
339fc89c0fbSmrg	    ;;
340fc89c0fbSmrg	  *)
341fc89c0fbSmrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
342fc89c0fbSmrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
343fc89c0fbSmrg
344fc89c0fbSmrg	    if (umask $mkdir_umask &&
345fc89c0fbSmrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
346fc89c0fbSmrg	    then
347fc89c0fbSmrg	      if test -z "$dir_arg" || {
348fc89c0fbSmrg		   # Check for POSIX incompatibilities with -m.
349fc89c0fbSmrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
350fc89c0fbSmrg		   # other-writeable bit of parent directory when it shouldn't.
351fc89c0fbSmrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
352fc89c0fbSmrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
353fc89c0fbSmrg		   case $ls_ld_tmpdir in
354fc89c0fbSmrg		     d????-?r-*) different_mode=700;;
355fc89c0fbSmrg		     d????-?--*) different_mode=755;;
356fc89c0fbSmrg		     *) false;;
357fc89c0fbSmrg		   esac &&
358fc89c0fbSmrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
359fc89c0fbSmrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
360fc89c0fbSmrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
361fc89c0fbSmrg		   }
362fc89c0fbSmrg		 }
363fc89c0fbSmrg	      then posix_mkdir=:
364fc89c0fbSmrg	      fi
365fc89c0fbSmrg	      rmdir "$tmpdir/d" "$tmpdir"
366fc89c0fbSmrg	    else
367fc89c0fbSmrg	      # Remove any dirs left behind by ancient mkdir implementations.
368fc89c0fbSmrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
369fc89c0fbSmrg	    fi
370fc89c0fbSmrg	    trap '' 0;;
371fc89c0fbSmrg	esac;;
372fc89c0fbSmrg    esac
373fc89c0fbSmrg
374fc89c0fbSmrg    if
375fc89c0fbSmrg      $posix_mkdir && (
376fc89c0fbSmrg	umask $mkdir_umask &&
377fc89c0fbSmrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
378fc89c0fbSmrg      )
379fc89c0fbSmrg    then :
380fc89c0fbSmrg    else
381fc89c0fbSmrg
382fc89c0fbSmrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
383fc89c0fbSmrg      # or it failed possibly due to a race condition.  Create the
384fc89c0fbSmrg      # directory the slow way, step by step, checking for races as we go.
385fc89c0fbSmrg
386fc89c0fbSmrg      case $dstdir in
38791ec45ceSmrg	/*) prefix='/';;
38891ec45ceSmrg	-*) prefix='./';;
38991ec45ceSmrg	*)  prefix='';;
390fc89c0fbSmrg      esac
391fc89c0fbSmrg
39291ec45ceSmrg      eval "$initialize_posix_glob"
393fc89c0fbSmrg
394fc89c0fbSmrg      oIFS=$IFS
395fc89c0fbSmrg      IFS=/
39691ec45ceSmrg      $posix_glob set -f
397fc89c0fbSmrg      set fnord $dstdir
398fc89c0fbSmrg      shift
39991ec45ceSmrg      $posix_glob set +f
400fc89c0fbSmrg      IFS=$oIFS
401fc89c0fbSmrg
402fc89c0fbSmrg      prefixes=
403fc89c0fbSmrg
404fc89c0fbSmrg      for d
405fc89c0fbSmrg      do
406fc89c0fbSmrg	test -z "$d" && continue
407fc89c0fbSmrg
408fc89c0fbSmrg	prefix=$prefix$d
409fc89c0fbSmrg	if test -d "$prefix"; then
410fc89c0fbSmrg	  prefixes=
411fc89c0fbSmrg	else
412fc89c0fbSmrg	  if $posix_mkdir; then
413fc89c0fbSmrg	    (umask=$mkdir_umask &&
414fc89c0fbSmrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
415fc89c0fbSmrg	    # Don't fail if two instances are running concurrently.
416fc89c0fbSmrg	    test -d "$prefix" || exit 1
417fc89c0fbSmrg	  else
418fc89c0fbSmrg	    case $prefix in
419fc89c0fbSmrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
420fc89c0fbSmrg	      *) qprefix=$prefix;;
421fc89c0fbSmrg	    esac
422fc89c0fbSmrg	    prefixes="$prefixes '$qprefix'"
423fc89c0fbSmrg	  fi
424fc89c0fbSmrg	fi
425fc89c0fbSmrg	prefix=$prefix/
426fc89c0fbSmrg      done
427fc89c0fbSmrg
428fc89c0fbSmrg      if test -n "$prefixes"; then
429fc89c0fbSmrg	# Don't fail if two instances are running concurrently.
430fc89c0fbSmrg	(umask $mkdir_umask &&
431fc89c0fbSmrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
432fc89c0fbSmrg	  test -d "$dstdir" || exit 1
433fc89c0fbSmrg	obsolete_mkdir_used=true
434fc89c0fbSmrg      fi
435fc89c0fbSmrg    fi
436fc89c0fbSmrg  fi
437fc89c0fbSmrg
438fc89c0fbSmrg  if test -n "$dir_arg"; then
439fc89c0fbSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
440fc89c0fbSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
441fc89c0fbSmrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
442fc89c0fbSmrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
443fc89c0fbSmrg  else
444fc89c0fbSmrg
445fc89c0fbSmrg    # Make a couple of temp file names in the proper directory.
446fc89c0fbSmrg    dsttmp=$dstdir/_inst.$$_
447fc89c0fbSmrg    rmtmp=$dstdir/_rm.$$_
448fc89c0fbSmrg
449fc89c0fbSmrg    # Trap to clean up those temp files at exit.
450fc89c0fbSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
451fc89c0fbSmrg
452fc89c0fbSmrg    # Copy the file name to the temp name.
453fc89c0fbSmrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
454fc89c0fbSmrg
455fc89c0fbSmrg    # and set any options; do chmod last to preserve setuid bits.
456fc89c0fbSmrg    #
457fc89c0fbSmrg    # If any of these fail, we abort the whole thing.  If we want to
458fc89c0fbSmrg    # ignore errors from any of these, just make sure not to ignore
459fc89c0fbSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
460fc89c0fbSmrg    #
46191ec45ceSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
46291ec45ceSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
46391ec45ceSmrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
46491ec45ceSmrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
46591ec45ceSmrg
46691ec45ceSmrg    # If -C, don't bother to copy if it wouldn't change the file.
46791ec45ceSmrg    if $copy_on_change &&
46891ec45ceSmrg       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
46991ec45ceSmrg       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
47091ec45ceSmrg
47191ec45ceSmrg       eval "$initialize_posix_glob" &&
47291ec45ceSmrg       $posix_glob set -f &&
47391ec45ceSmrg       set X $old && old=:$2:$4:$5:$6 &&
47491ec45ceSmrg       set X $new && new=:$2:$4:$5:$6 &&
47591ec45ceSmrg       $posix_glob set +f &&
47691ec45ceSmrg
47791ec45ceSmrg       test "$old" = "$new" &&
47891ec45ceSmrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
47991ec45ceSmrg    then
48091ec45ceSmrg      rm -f "$dsttmp"
48191ec45ceSmrg    else
48291ec45ceSmrg      # Rename the file to the real destination.
48391ec45ceSmrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
48491ec45ceSmrg
48591ec45ceSmrg      # The rename failed, perhaps because mv can't rename something else
48691ec45ceSmrg      # to itself, or perhaps because mv is so ancient that it does not
48791ec45ceSmrg      # support -f.
48891ec45ceSmrg      {
48991ec45ceSmrg	# Now remove or move aside any old file at destination location.
49091ec45ceSmrg	# We try this two ways since rm can't unlink itself on some
49191ec45ceSmrg	# systems and the destination file might be busy for other
49291ec45ceSmrg	# reasons.  In this case, the final cleanup might fail but the new
49391ec45ceSmrg	# file should still install successfully.
49491ec45ceSmrg	{
49591ec45ceSmrg	  test ! -f "$dst" ||
49691ec45ceSmrg	  $doit $rmcmd -f "$dst" 2>/dev/null ||
49791ec45ceSmrg	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
49891ec45ceSmrg	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
49991ec45ceSmrg	  } ||
50091ec45ceSmrg	  { echo "$0: cannot unlink or rename $dst" >&2
50191ec45ceSmrg	    (exit 1); exit 1
50291ec45ceSmrg	  }
50391ec45ceSmrg	} &&
50491ec45ceSmrg
50591ec45ceSmrg	# Now rename the file to the real destination.
50691ec45ceSmrg	$doit $mvcmd "$dsttmp" "$dst"
50791ec45ceSmrg      }
50891ec45ceSmrg    fi || exit 1
509fc89c0fbSmrg
510fc89c0fbSmrg    trap '' 0
511fc89c0fbSmrg  fi
512fc89c0fbSmrgdone
513fc89c0fbSmrg
514fc89c0fbSmrg# Local variables:
515fc89c0fbSmrg# eval: (add-hook 'write-file-hooks 'time-stamp)
516fc89c0fbSmrg# time-stamp-start: "scriptversion="
517fc89c0fbSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
51891ec45ceSmrg# time-stamp-time-zone: "UTC"
51991ec45ceSmrg# time-stamp-end: "; # UTC"
520fc89c0fbSmrg# End:
521