install-sh revision 1f0ac6a5
11f0ac6a5Smrg#!/bin/sh
21f0ac6a5Smrg# install - install a program, script, or datafile
31f0ac6a5Smrg
41f0ac6a5Smrgscriptversion=2006-10-14.15
51f0ac6a5Smrg
61f0ac6a5Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
71f0ac6a5Smrg# later released in X11R6 (xc/config/util/install.sh) with the
81f0ac6a5Smrg# following copyright and license.
91f0ac6a5Smrg#
101f0ac6a5Smrg# Copyright (C) 1994 X Consortium
111f0ac6a5Smrg#
121f0ac6a5Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
131f0ac6a5Smrg# of this software and associated documentation files (the "Software"), to
141f0ac6a5Smrg# deal in the Software without restriction, including without limitation the
151f0ac6a5Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
161f0ac6a5Smrg# sell copies of the Software, and to permit persons to whom the Software is
171f0ac6a5Smrg# furnished to do so, subject to the following conditions:
181f0ac6a5Smrg#
191f0ac6a5Smrg# The above copyright notice and this permission notice shall be included in
201f0ac6a5Smrg# all copies or substantial portions of the Software.
211f0ac6a5Smrg#
221f0ac6a5Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
231f0ac6a5Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
241f0ac6a5Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
251f0ac6a5Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
261f0ac6a5Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
271f0ac6a5Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
281f0ac6a5Smrg#
291f0ac6a5Smrg# Except as contained in this notice, the name of the X Consortium shall not
301f0ac6a5Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
311f0ac6a5Smrg# ings in this Software without prior written authorization from the X Consor-
321f0ac6a5Smrg# tium.
331f0ac6a5Smrg#
341f0ac6a5Smrg#
351f0ac6a5Smrg# FSF changes to this file are in the public domain.
361f0ac6a5Smrg#
371f0ac6a5Smrg# Calling this script install-sh is preferred over install.sh, to prevent
381f0ac6a5Smrg# `make' implicit rules from creating a file called install from it
391f0ac6a5Smrg# when there is no Makefile.
401f0ac6a5Smrg#
411f0ac6a5Smrg# This script is compatible with the BSD install script, but was written
421f0ac6a5Smrg# from scratch.
431f0ac6a5Smrg
441f0ac6a5Smrgnl='
451f0ac6a5Smrg'
461f0ac6a5SmrgIFS=" ""	$nl"
471f0ac6a5Smrg
481f0ac6a5Smrg# set DOITPROG to echo to test this script
491f0ac6a5Smrg
501f0ac6a5Smrg# Don't use :- since 4.3BSD and earlier shells don't like it.
511f0ac6a5Smrgdoit="${DOITPROG-}"
521f0ac6a5Smrgif test -z "$doit"; then
531f0ac6a5Smrg  doit_exec=exec
541f0ac6a5Smrgelse
551f0ac6a5Smrg  doit_exec=$doit
561f0ac6a5Smrgfi
571f0ac6a5Smrg
581f0ac6a5Smrg# Put in absolute file names if you don't have them in your path;
591f0ac6a5Smrg# or use environment vars.
601f0ac6a5Smrg
611f0ac6a5Smrgmvprog="${MVPROG-mv}"
621f0ac6a5Smrgcpprog="${CPPROG-cp}"
631f0ac6a5Smrgchmodprog="${CHMODPROG-chmod}"
641f0ac6a5Smrgchownprog="${CHOWNPROG-chown}"
651f0ac6a5Smrgchgrpprog="${CHGRPPROG-chgrp}"
661f0ac6a5Smrgstripprog="${STRIPPROG-strip}"
671f0ac6a5Smrgrmprog="${RMPROG-rm}"
681f0ac6a5Smrgmkdirprog="${MKDIRPROG-mkdir}"
691f0ac6a5Smrg
701f0ac6a5Smrgposix_glob=
711f0ac6a5Smrgposix_mkdir=
721f0ac6a5Smrg
731f0ac6a5Smrg# Desired mode of installed file.
741f0ac6a5Smrgmode=0755
751f0ac6a5Smrg
761f0ac6a5Smrgchmodcmd=$chmodprog
771f0ac6a5Smrgchowncmd=
781f0ac6a5Smrgchgrpcmd=
791f0ac6a5Smrgstripcmd=
801f0ac6a5Smrgrmcmd="$rmprog -f"
811f0ac6a5Smrgmvcmd="$mvprog"
821f0ac6a5Smrgsrc=
831f0ac6a5Smrgdst=
841f0ac6a5Smrgdir_arg=
851f0ac6a5Smrgdstarg=
861f0ac6a5Smrgno_target_directory=
871f0ac6a5Smrg
881f0ac6a5Smrgusage="Usage: $0 [OPTION]... [-T] SRCFILE DSTFILE
891f0ac6a5Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
901f0ac6a5Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
911f0ac6a5Smrg   or: $0 [OPTION]... -d DIRECTORIES...
921f0ac6a5Smrg
931f0ac6a5SmrgIn the 1st form, copy SRCFILE to DSTFILE.
941f0ac6a5SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
951f0ac6a5SmrgIn the 4th, create DIRECTORIES.
961f0ac6a5Smrg
971f0ac6a5SmrgOptions:
981f0ac6a5Smrg-c         (ignored)
991f0ac6a5Smrg-d         create directories instead of installing files.
1001f0ac6a5Smrg-g GROUP   $chgrpprog installed files to GROUP.
1011f0ac6a5Smrg-m MODE    $chmodprog installed files to MODE.
1021f0ac6a5Smrg-o USER    $chownprog installed files to USER.
1031f0ac6a5Smrg-s         $stripprog installed files.
1041f0ac6a5Smrg-t DIRECTORY  install into DIRECTORY.
1051f0ac6a5Smrg-T         report an error if DSTFILE is a directory.
1061f0ac6a5Smrg--help     display this help and exit.
1071f0ac6a5Smrg--version  display version info and exit.
1081f0ac6a5Smrg
1091f0ac6a5SmrgEnvironment variables override the default commands:
1101f0ac6a5Smrg  CHGRPPROG CHMODPROG CHOWNPROG CPPROG MKDIRPROG MVPROG RMPROG STRIPPROG
1111f0ac6a5Smrg"
1121f0ac6a5Smrg
1131f0ac6a5Smrgwhile test $# -ne 0; do
1141f0ac6a5Smrg  case $1 in
1151f0ac6a5Smrg    -c) shift
1161f0ac6a5Smrg        continue;;
1171f0ac6a5Smrg
1181f0ac6a5Smrg    -d) dir_arg=true
1191f0ac6a5Smrg        shift
1201f0ac6a5Smrg        continue;;
1211f0ac6a5Smrg
1221f0ac6a5Smrg    -g) chgrpcmd="$chgrpprog $2"
1231f0ac6a5Smrg        shift
1241f0ac6a5Smrg        shift
1251f0ac6a5Smrg        continue;;
1261f0ac6a5Smrg
1271f0ac6a5Smrg    --help) echo "$usage"; exit $?;;
1281f0ac6a5Smrg
1291f0ac6a5Smrg    -m) mode=$2
1301f0ac6a5Smrg        shift
1311f0ac6a5Smrg        shift
1321f0ac6a5Smrg	case $mode in
1331f0ac6a5Smrg	  *' '* | *'	'* | *'
1341f0ac6a5Smrg'*	  | *'*'* | *'?'* | *'['*)
1351f0ac6a5Smrg	    echo "$0: invalid mode: $mode" >&2
1361f0ac6a5Smrg	    exit 1;;
1371f0ac6a5Smrg	esac
1381f0ac6a5Smrg        continue;;
1391f0ac6a5Smrg
1401f0ac6a5Smrg    -o) chowncmd="$chownprog $2"
1411f0ac6a5Smrg        shift
1421f0ac6a5Smrg        shift
1431f0ac6a5Smrg        continue;;
1441f0ac6a5Smrg
1451f0ac6a5Smrg    -s) stripcmd=$stripprog
1461f0ac6a5Smrg        shift
1471f0ac6a5Smrg        continue;;
1481f0ac6a5Smrg
1491f0ac6a5Smrg    -t) dstarg=$2
1501f0ac6a5Smrg	shift
1511f0ac6a5Smrg	shift
1521f0ac6a5Smrg	continue;;
1531f0ac6a5Smrg
1541f0ac6a5Smrg    -T) no_target_directory=true
1551f0ac6a5Smrg	shift
1561f0ac6a5Smrg	continue;;
1571f0ac6a5Smrg
1581f0ac6a5Smrg    --version) echo "$0 $scriptversion"; exit $?;;
1591f0ac6a5Smrg
1601f0ac6a5Smrg    --)	shift
1611f0ac6a5Smrg	break;;
1621f0ac6a5Smrg
1631f0ac6a5Smrg    -*)	echo "$0: invalid option: $1" >&2
1641f0ac6a5Smrg	exit 1;;
1651f0ac6a5Smrg
1661f0ac6a5Smrg    *)  break;;
1671f0ac6a5Smrg  esac
1681f0ac6a5Smrgdone
1691f0ac6a5Smrg
1701f0ac6a5Smrgif test $# -ne 0 && test -z "$dir_arg$dstarg"; then
1711f0ac6a5Smrg  # When -d is used, all remaining arguments are directories to create.
1721f0ac6a5Smrg  # When -t is used, the destination is already specified.
1731f0ac6a5Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
1741f0ac6a5Smrg  for arg
1751f0ac6a5Smrg  do
1761f0ac6a5Smrg    if test -n "$dstarg"; then
1771f0ac6a5Smrg      # $@ is not empty: it contains at least $arg.
1781f0ac6a5Smrg      set fnord "$@" "$dstarg"
1791f0ac6a5Smrg      shift # fnord
1801f0ac6a5Smrg    fi
1811f0ac6a5Smrg    shift # arg
1821f0ac6a5Smrg    dstarg=$arg
1831f0ac6a5Smrg  done
1841f0ac6a5Smrgfi
1851f0ac6a5Smrg
1861f0ac6a5Smrgif test $# -eq 0; then
1871f0ac6a5Smrg  if test -z "$dir_arg"; then
1881f0ac6a5Smrg    echo "$0: no input file specified." >&2
1891f0ac6a5Smrg    exit 1
1901f0ac6a5Smrg  fi
1911f0ac6a5Smrg  # It's OK to call `install-sh -d' without argument.
1921f0ac6a5Smrg  # This can happen when creating conditional directories.
1931f0ac6a5Smrg  exit 0
1941f0ac6a5Smrgfi
1951f0ac6a5Smrg
1961f0ac6a5Smrgif test -z "$dir_arg"; then
1971f0ac6a5Smrg  trap '(exit $?); exit' 1 2 13 15
1981f0ac6a5Smrg
1991f0ac6a5Smrg  # Set umask so as not to create temps with too-generous modes.
2001f0ac6a5Smrg  # However, 'strip' requires both read and write access to temps.
2011f0ac6a5Smrg  case $mode in
2021f0ac6a5Smrg    # Optimize common cases.
2031f0ac6a5Smrg    *644) cp_umask=133;;
2041f0ac6a5Smrg    *755) cp_umask=22;;
2051f0ac6a5Smrg
2061f0ac6a5Smrg    *[0-7])
2071f0ac6a5Smrg      if test -z "$stripcmd"; then
2081f0ac6a5Smrg	u_plus_rw=
2091f0ac6a5Smrg      else
2101f0ac6a5Smrg	u_plus_rw='% 200'
2111f0ac6a5Smrg      fi
2121f0ac6a5Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2131f0ac6a5Smrg    *)
2141f0ac6a5Smrg      if test -z "$stripcmd"; then
2151f0ac6a5Smrg	u_plus_rw=
2161f0ac6a5Smrg      else
2171f0ac6a5Smrg	u_plus_rw=,u+rw
2181f0ac6a5Smrg      fi
2191f0ac6a5Smrg      cp_umask=$mode$u_plus_rw;;
2201f0ac6a5Smrg  esac
2211f0ac6a5Smrgfi
2221f0ac6a5Smrg
2231f0ac6a5Smrgfor src
2241f0ac6a5Smrgdo
2251f0ac6a5Smrg  # Protect names starting with `-'.
2261f0ac6a5Smrg  case $src in
2271f0ac6a5Smrg    -*) src=./$src ;;
2281f0ac6a5Smrg  esac
2291f0ac6a5Smrg
2301f0ac6a5Smrg  if test -n "$dir_arg"; then
2311f0ac6a5Smrg    dst=$src
2321f0ac6a5Smrg    dstdir=$dst
2331f0ac6a5Smrg    test -d "$dstdir"
2341f0ac6a5Smrg    dstdir_status=$?
2351f0ac6a5Smrg  else
2361f0ac6a5Smrg
2371f0ac6a5Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
2381f0ac6a5Smrg    # might cause directories to be created, which would be especially bad
2391f0ac6a5Smrg    # if $src (and thus $dsttmp) contains '*'.
2401f0ac6a5Smrg    if test ! -f "$src" && test ! -d "$src"; then
2411f0ac6a5Smrg      echo "$0: $src does not exist." >&2
2421f0ac6a5Smrg      exit 1
2431f0ac6a5Smrg    fi
2441f0ac6a5Smrg
2451f0ac6a5Smrg    if test -z "$dstarg"; then
2461f0ac6a5Smrg      echo "$0: no destination specified." >&2
2471f0ac6a5Smrg      exit 1
2481f0ac6a5Smrg    fi
2491f0ac6a5Smrg
2501f0ac6a5Smrg    dst=$dstarg
2511f0ac6a5Smrg    # Protect names starting with `-'.
2521f0ac6a5Smrg    case $dst in
2531f0ac6a5Smrg      -*) dst=./$dst ;;
2541f0ac6a5Smrg    esac
2551f0ac6a5Smrg
2561f0ac6a5Smrg    # If destination is a directory, append the input filename; won't work
2571f0ac6a5Smrg    # if double slashes aren't ignored.
2581f0ac6a5Smrg    if test -d "$dst"; then
2591f0ac6a5Smrg      if test -n "$no_target_directory"; then
2601f0ac6a5Smrg	echo "$0: $dstarg: Is a directory" >&2
2611f0ac6a5Smrg	exit 1
2621f0ac6a5Smrg      fi
2631f0ac6a5Smrg      dstdir=$dst
2641f0ac6a5Smrg      dst=$dstdir/`basename "$src"`
2651f0ac6a5Smrg      dstdir_status=0
2661f0ac6a5Smrg    else
2671f0ac6a5Smrg      # Prefer dirname, but fall back on a substitute if dirname fails.
2681f0ac6a5Smrg      dstdir=`
2691f0ac6a5Smrg	(dirname "$dst") 2>/dev/null ||
2701f0ac6a5Smrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
2711f0ac6a5Smrg	     X"$dst" : 'X\(//\)[^/]' \| \
2721f0ac6a5Smrg	     X"$dst" : 'X\(//\)$' \| \
2731f0ac6a5Smrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
2741f0ac6a5Smrg	echo X"$dst" |
2751f0ac6a5Smrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
2761f0ac6a5Smrg		   s//\1/
2771f0ac6a5Smrg		   q
2781f0ac6a5Smrg		 }
2791f0ac6a5Smrg		 /^X\(\/\/\)[^/].*/{
2801f0ac6a5Smrg		   s//\1/
2811f0ac6a5Smrg		   q
2821f0ac6a5Smrg		 }
2831f0ac6a5Smrg		 /^X\(\/\/\)$/{
2841f0ac6a5Smrg		   s//\1/
2851f0ac6a5Smrg		   q
2861f0ac6a5Smrg		 }
2871f0ac6a5Smrg		 /^X\(\/\).*/{
2881f0ac6a5Smrg		   s//\1/
2891f0ac6a5Smrg		   q
2901f0ac6a5Smrg		 }
2911f0ac6a5Smrg		 s/.*/./; q'
2921f0ac6a5Smrg      `
2931f0ac6a5Smrg
2941f0ac6a5Smrg      test -d "$dstdir"
2951f0ac6a5Smrg      dstdir_status=$?
2961f0ac6a5Smrg    fi
2971f0ac6a5Smrg  fi
2981f0ac6a5Smrg
2991f0ac6a5Smrg  obsolete_mkdir_used=false
3001f0ac6a5Smrg
3011f0ac6a5Smrg  if test $dstdir_status != 0; then
3021f0ac6a5Smrg    case $posix_mkdir in
3031f0ac6a5Smrg      '')
3041f0ac6a5Smrg	# Create intermediate dirs using mode 755 as modified by the umask.
3051f0ac6a5Smrg	# This is like FreeBSD 'install' as of 1997-10-28.
3061f0ac6a5Smrg	umask=`umask`
3071f0ac6a5Smrg	case $stripcmd.$umask in
3081f0ac6a5Smrg	  # Optimize common cases.
3091f0ac6a5Smrg	  *[2367][2367]) mkdir_umask=$umask;;
3101f0ac6a5Smrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
3111f0ac6a5Smrg
3121f0ac6a5Smrg	  *[0-7])
3131f0ac6a5Smrg	    mkdir_umask=`expr $umask + 22 \
3141f0ac6a5Smrg	      - $umask % 100 % 40 + $umask % 20 \
3151f0ac6a5Smrg	      - $umask % 10 % 4 + $umask % 2
3161f0ac6a5Smrg	    `;;
3171f0ac6a5Smrg	  *) mkdir_umask=$umask,go-w;;
3181f0ac6a5Smrg	esac
3191f0ac6a5Smrg
3201f0ac6a5Smrg	# With -d, create the new directory with the user-specified mode.
3211f0ac6a5Smrg	# Otherwise, rely on $mkdir_umask.
3221f0ac6a5Smrg	if test -n "$dir_arg"; then
3231f0ac6a5Smrg	  mkdir_mode=-m$mode
3241f0ac6a5Smrg	else
3251f0ac6a5Smrg	  mkdir_mode=
3261f0ac6a5Smrg	fi
3271f0ac6a5Smrg
3281f0ac6a5Smrg	posix_mkdir=false
3291f0ac6a5Smrg	case $umask in
3301f0ac6a5Smrg	  *[123567][0-7][0-7])
3311f0ac6a5Smrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
3321f0ac6a5Smrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
3331f0ac6a5Smrg	    ;;
3341f0ac6a5Smrg	  *)
3351f0ac6a5Smrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
3361f0ac6a5Smrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
3371f0ac6a5Smrg
3381f0ac6a5Smrg	    if (umask $mkdir_umask &&
3391f0ac6a5Smrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
3401f0ac6a5Smrg	    then
3411f0ac6a5Smrg	      if test -z "$dir_arg" || {
3421f0ac6a5Smrg		   # Check for POSIX incompatibilities with -m.
3431f0ac6a5Smrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
3441f0ac6a5Smrg		   # other-writeable bit of parent directory when it shouldn't.
3451f0ac6a5Smrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
3461f0ac6a5Smrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
3471f0ac6a5Smrg		   case $ls_ld_tmpdir in
3481f0ac6a5Smrg		     d????-?r-*) different_mode=700;;
3491f0ac6a5Smrg		     d????-?--*) different_mode=755;;
3501f0ac6a5Smrg		     *) false;;
3511f0ac6a5Smrg		   esac &&
3521f0ac6a5Smrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
3531f0ac6a5Smrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
3541f0ac6a5Smrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
3551f0ac6a5Smrg		   }
3561f0ac6a5Smrg		 }
3571f0ac6a5Smrg	      then posix_mkdir=:
3581f0ac6a5Smrg	      fi
3591f0ac6a5Smrg	      rmdir "$tmpdir/d" "$tmpdir"
3601f0ac6a5Smrg	    else
3611f0ac6a5Smrg	      # Remove any dirs left behind by ancient mkdir implementations.
3621f0ac6a5Smrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
3631f0ac6a5Smrg	    fi
3641f0ac6a5Smrg	    trap '' 0;;
3651f0ac6a5Smrg	esac;;
3661f0ac6a5Smrg    esac
3671f0ac6a5Smrg
3681f0ac6a5Smrg    if
3691f0ac6a5Smrg      $posix_mkdir && (
3701f0ac6a5Smrg	umask $mkdir_umask &&
3711f0ac6a5Smrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3721f0ac6a5Smrg      )
3731f0ac6a5Smrg    then :
3741f0ac6a5Smrg    else
3751f0ac6a5Smrg
3761f0ac6a5Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
3771f0ac6a5Smrg      # or it failed possibly due to a race condition.  Create the
3781f0ac6a5Smrg      # directory the slow way, step by step, checking for races as we go.
3791f0ac6a5Smrg
3801f0ac6a5Smrg      case $dstdir in
3811f0ac6a5Smrg	/*) prefix=/ ;;
3821f0ac6a5Smrg	-*) prefix=./ ;;
3831f0ac6a5Smrg	*)  prefix= ;;
3841f0ac6a5Smrg      esac
3851f0ac6a5Smrg
3861f0ac6a5Smrg      case $posix_glob in
3871f0ac6a5Smrg        '')
3881f0ac6a5Smrg	  if (set -f) 2>/dev/null; then
3891f0ac6a5Smrg	    posix_glob=true
3901f0ac6a5Smrg	  else
3911f0ac6a5Smrg	    posix_glob=false
3921f0ac6a5Smrg	  fi ;;
3931f0ac6a5Smrg      esac
3941f0ac6a5Smrg
3951f0ac6a5Smrg      oIFS=$IFS
3961f0ac6a5Smrg      IFS=/
3971f0ac6a5Smrg      $posix_glob && set -f
3981f0ac6a5Smrg      set fnord $dstdir
3991f0ac6a5Smrg      shift
4001f0ac6a5Smrg      $posix_glob && set +f
4011f0ac6a5Smrg      IFS=$oIFS
4021f0ac6a5Smrg
4031f0ac6a5Smrg      prefixes=
4041f0ac6a5Smrg
4051f0ac6a5Smrg      for d
4061f0ac6a5Smrg      do
4071f0ac6a5Smrg	test -z "$d" && continue
4081f0ac6a5Smrg
4091f0ac6a5Smrg	prefix=$prefix$d
4101f0ac6a5Smrg	if test -d "$prefix"; then
4111f0ac6a5Smrg	  prefixes=
4121f0ac6a5Smrg	else
4131f0ac6a5Smrg	  if $posix_mkdir; then
4141f0ac6a5Smrg	    (umask=$mkdir_umask &&
4151f0ac6a5Smrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
4161f0ac6a5Smrg	    # Don't fail if two instances are running concurrently.
4171f0ac6a5Smrg	    test -d "$prefix" || exit 1
4181f0ac6a5Smrg	  else
4191f0ac6a5Smrg	    case $prefix in
4201f0ac6a5Smrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
4211f0ac6a5Smrg	      *) qprefix=$prefix;;
4221f0ac6a5Smrg	    esac
4231f0ac6a5Smrg	    prefixes="$prefixes '$qprefix'"
4241f0ac6a5Smrg	  fi
4251f0ac6a5Smrg	fi
4261f0ac6a5Smrg	prefix=$prefix/
4271f0ac6a5Smrg      done
4281f0ac6a5Smrg
4291f0ac6a5Smrg      if test -n "$prefixes"; then
4301f0ac6a5Smrg	# Don't fail if two instances are running concurrently.
4311f0ac6a5Smrg	(umask $mkdir_umask &&
4321f0ac6a5Smrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
4331f0ac6a5Smrg	  test -d "$dstdir" || exit 1
4341f0ac6a5Smrg	obsolete_mkdir_used=true
4351f0ac6a5Smrg      fi
4361f0ac6a5Smrg    fi
4371f0ac6a5Smrg  fi
4381f0ac6a5Smrg
4391f0ac6a5Smrg  if test -n "$dir_arg"; then
4401f0ac6a5Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4411f0ac6a5Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4421f0ac6a5Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4431f0ac6a5Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
4441f0ac6a5Smrg  else
4451f0ac6a5Smrg
4461f0ac6a5Smrg    # Make a couple of temp file names in the proper directory.
4471f0ac6a5Smrg    dsttmp=$dstdir/_inst.$$_
4481f0ac6a5Smrg    rmtmp=$dstdir/_rm.$$_
4491f0ac6a5Smrg
4501f0ac6a5Smrg    # Trap to clean up those temp files at exit.
4511f0ac6a5Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
4521f0ac6a5Smrg
4531f0ac6a5Smrg    # Copy the file name to the temp name.
4541f0ac6a5Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
4551f0ac6a5Smrg
4561f0ac6a5Smrg    # and set any options; do chmod last to preserve setuid bits.
4571f0ac6a5Smrg    #
4581f0ac6a5Smrg    # If any of these fail, we abort the whole thing.  If we want to
4591f0ac6a5Smrg    # ignore errors from any of these, just make sure not to ignore
4601f0ac6a5Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
4611f0ac6a5Smrg    #
4621f0ac6a5Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } \
4631f0ac6a5Smrg      && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } \
4641f0ac6a5Smrg      && { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } \
4651f0ac6a5Smrg      && { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4661f0ac6a5Smrg
4671f0ac6a5Smrg    # Now rename the file to the real destination.
4681f0ac6a5Smrg    { $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null \
4691f0ac6a5Smrg      || {
4701f0ac6a5Smrg	   # The rename failed, perhaps because mv can't rename something else
4711f0ac6a5Smrg	   # to itself, or perhaps because mv is so ancient that it does not
4721f0ac6a5Smrg	   # support -f.
4731f0ac6a5Smrg
4741f0ac6a5Smrg	   # Now remove or move aside any old file at destination location.
4751f0ac6a5Smrg	   # We try this two ways since rm can't unlink itself on some
4761f0ac6a5Smrg	   # systems and the destination file might be busy for other
4771f0ac6a5Smrg	   # reasons.  In this case, the final cleanup might fail but the new
4781f0ac6a5Smrg	   # file should still install successfully.
4791f0ac6a5Smrg	   {
4801f0ac6a5Smrg	     if test -f "$dst"; then
4811f0ac6a5Smrg	       $doit $rmcmd -f "$dst" 2>/dev/null \
4821f0ac6a5Smrg	       || { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null \
4831f0ac6a5Smrg		     && { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }; }\
4841f0ac6a5Smrg	       || {
4851f0ac6a5Smrg		 echo "$0: cannot unlink or rename $dst" >&2
4861f0ac6a5Smrg		 (exit 1); exit 1
4871f0ac6a5Smrg	       }
4881f0ac6a5Smrg	     else
4891f0ac6a5Smrg	       :
4901f0ac6a5Smrg	     fi
4911f0ac6a5Smrg	   } &&
4921f0ac6a5Smrg
4931f0ac6a5Smrg	   # Now rename the file to the real destination.
4941f0ac6a5Smrg	   $doit $mvcmd "$dsttmp" "$dst"
4951f0ac6a5Smrg	 }
4961f0ac6a5Smrg    } || exit 1
4971f0ac6a5Smrg
4981f0ac6a5Smrg    trap '' 0
4991f0ac6a5Smrg  fi
5001f0ac6a5Smrgdone
5011f0ac6a5Smrg
5021f0ac6a5Smrg# Local variables:
5031f0ac6a5Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
5041f0ac6a5Smrg# time-stamp-start: "scriptversion="
5051f0ac6a5Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
5061f0ac6a5Smrg# time-stamp-end: "$"
5071f0ac6a5Smrg# End:
508