install-sh revision 2fd96bc7
11f0ac6a5Smrg#!/bin/sh
21f0ac6a5Smrg# install - install a program, script, or datafile
31f0ac6a5Smrg
42fd96bc7Smrgscriptversion=2011-11-20.07; # UTC
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
3889c04b6cSmrg# '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'
462fd96bc7SmrgIFS=" ""	$nl"
471f0ac6a5Smrg
482fd96bc7Smrg# set DOITPROG to echo to test this script
491f0ac6a5Smrg
502fd96bc7Smrg# Don't use :- since 4.3BSD and earlier shells don't like it.
51e5410a46Smrgdoit=${DOITPROG-}
522fd96bc7Smrgif test -z "$doit"; then
532fd96bc7Smrg  doit_exec=exec
542fd96bc7Smrgelse
552fd96bc7Smrg  doit_exec=$doit
562fd96bc7Smrgfi
571f0ac6a5Smrg
581f0ac6a5Smrg# Put in absolute file names if you don't have them in your path;
591f0ac6a5Smrg# or use environment vars.
601f0ac6a5Smrg
61e5410a46Smrgchgrpprog=${CHGRPPROG-chgrp}
62e5410a46Smrgchmodprog=${CHMODPROG-chmod}
63e5410a46Smrgchownprog=${CHOWNPROG-chown}
64e5410a46Smrgcmpprog=${CMPPROG-cmp}
65e5410a46Smrgcpprog=${CPPROG-cp}
66e5410a46Smrgmkdirprog=${MKDIRPROG-mkdir}
67e5410a46Smrgmvprog=${MVPROG-mv}
68e5410a46Smrgrmprog=${RMPROG-rm}
69e5410a46Smrgstripprog=${STRIPPROG-strip}
70e5410a46Smrg
712fd96bc7Smrgposix_glob='?'
722fd96bc7Smrginitialize_posix_glob='
732fd96bc7Smrg  test "$posix_glob" != "?" || {
742fd96bc7Smrg    if (set -f) 2>/dev/null; then
752fd96bc7Smrg      posix_glob=
762fd96bc7Smrg    else
772fd96bc7Smrg      posix_glob=:
782fd96bc7Smrg    fi
792fd96bc7Smrg  }
802fd96bc7Smrg'
812fd96bc7Smrg
821f0ac6a5Smrgposix_mkdir=
831f0ac6a5Smrg
841f0ac6a5Smrg# Desired mode of installed file.
851f0ac6a5Smrgmode=0755
861f0ac6a5Smrg
87e5410a46Smrgchgrpcmd=
881f0ac6a5Smrgchmodcmd=$chmodprog
891f0ac6a5Smrgchowncmd=
90e5410a46Smrgmvcmd=$mvprog
911f0ac6a5Smrgrmcmd="$rmprog -f"
92e5410a46Smrgstripcmd=
93e5410a46Smrg
941f0ac6a5Smrgsrc=
951f0ac6a5Smrgdst=
961f0ac6a5Smrgdir_arg=
97e5410a46Smrgdst_arg=
98e5410a46Smrg
99e5410a46Smrgcopy_on_change=false
1002fd96bc7Smrgno_target_directory=
1011f0ac6a5Smrg
102e5410a46Smrgusage="\
103e5410a46SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
1041f0ac6a5Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
1051f0ac6a5Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
1061f0ac6a5Smrg   or: $0 [OPTION]... -d DIRECTORIES...
1071f0ac6a5Smrg
1081f0ac6a5SmrgIn the 1st form, copy SRCFILE to DSTFILE.
1091f0ac6a5SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
1101f0ac6a5SmrgIn the 4th, create DIRECTORIES.
1111f0ac6a5Smrg
1121f0ac6a5SmrgOptions:
113e5410a46Smrg     --help     display this help and exit.
114e5410a46Smrg     --version  display version info and exit.
115e5410a46Smrg
116e5410a46Smrg  -c            (ignored)
117e5410a46Smrg  -C            install only if different (preserve the last data modification time)
118e5410a46Smrg  -d            create directories instead of installing files.
119e5410a46Smrg  -g GROUP      $chgrpprog installed files to GROUP.
120e5410a46Smrg  -m MODE       $chmodprog installed files to MODE.
121e5410a46Smrg  -o USER       $chownprog installed files to USER.
122e5410a46Smrg  -s            $stripprog installed files.
123e5410a46Smrg  -t DIRECTORY  install into DIRECTORY.
124e5410a46Smrg  -T            report an error if DSTFILE is a directory.
1251f0ac6a5Smrg
1261f0ac6a5SmrgEnvironment variables override the default commands:
127e5410a46Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
128e5410a46Smrg  RMPROG STRIPPROG
1291f0ac6a5Smrg"
1301f0ac6a5Smrg
1311f0ac6a5Smrgwhile test $# -ne 0; do
1321f0ac6a5Smrg  case $1 in
133e5410a46Smrg    -c) ;;
134e5410a46Smrg
135e5410a46Smrg    -C) copy_on_change=true;;
1361f0ac6a5Smrg
137e5410a46Smrg    -d) dir_arg=true;;
1381f0ac6a5Smrg
1391f0ac6a5Smrg    -g) chgrpcmd="$chgrpprog $2"
1402fd96bc7Smrg	shift;;
1411f0ac6a5Smrg
1421f0ac6a5Smrg    --help) echo "$usage"; exit $?;;
1431f0ac6a5Smrg
1441f0ac6a5Smrg    -m) mode=$2
1452fd96bc7Smrg	case $mode in
1462fd96bc7Smrg	  *' '* | *'	'* | *'
1472fd96bc7Smrg'*	  | *'*'* | *'?'* | *'['*)
1482fd96bc7Smrg	    echo "$0: invalid mode: $mode" >&2
1492fd96bc7Smrg	    exit 1;;
1502fd96bc7Smrg	esac
1512fd96bc7Smrg	shift;;
1521f0ac6a5Smrg
1531f0ac6a5Smrg    -o) chowncmd="$chownprog $2"
1542fd96bc7Smrg	shift;;
1551f0ac6a5Smrg
156e5410a46Smrg    -s) stripcmd=$stripprog;;
1571f0ac6a5Smrg
1582fd96bc7Smrg    -t) dst_arg=$2
1592fd96bc7Smrg	# Protect names problematic for 'test' and other utilities.
1602fd96bc7Smrg	case $dst_arg in
1612fd96bc7Smrg	  -* | [=\(\)!]) dst_arg=./$dst_arg;;
1622fd96bc7Smrg	esac
1632fd96bc7Smrg	shift;;
1641f0ac6a5Smrg
1652fd96bc7Smrg    -T) no_target_directory=true;;
1661f0ac6a5Smrg
1671f0ac6a5Smrg    --version) echo "$0 $scriptversion"; exit $?;;
1681f0ac6a5Smrg
1692fd96bc7Smrg    --)	shift
1702fd96bc7Smrg	break;;
1711f0ac6a5Smrg
1722fd96bc7Smrg    -*)	echo "$0: invalid option: $1" >&2
1732fd96bc7Smrg	exit 1;;
1741f0ac6a5Smrg
1751f0ac6a5Smrg    *)  break;;
1761f0ac6a5Smrg  esac
177e5410a46Smrg  shift
1781f0ac6a5Smrgdone
1791f0ac6a5Smrg
180e5410a46Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1811f0ac6a5Smrg  # When -d is used, all remaining arguments are directories to create.
1821f0ac6a5Smrg  # When -t is used, the destination is already specified.
1831f0ac6a5Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
1841f0ac6a5Smrg  for arg
1851f0ac6a5Smrg  do
186e5410a46Smrg    if test -n "$dst_arg"; then
1871f0ac6a5Smrg      # $@ is not empty: it contains at least $arg.
188e5410a46Smrg      set fnord "$@" "$dst_arg"
1891f0ac6a5Smrg      shift # fnord
1901f0ac6a5Smrg    fi
1911f0ac6a5Smrg    shift # arg
192e5410a46Smrg    dst_arg=$arg
19389c04b6cSmrg    # Protect names problematic for 'test' and other utilities.
19489c04b6cSmrg    case $dst_arg in
19589c04b6cSmrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
19689c04b6cSmrg    esac
1971f0ac6a5Smrg  done
1981f0ac6a5Smrgfi
1991f0ac6a5Smrg
2001f0ac6a5Smrgif test $# -eq 0; then
2011f0ac6a5Smrg  if test -z "$dir_arg"; then
2021f0ac6a5Smrg    echo "$0: no input file specified." >&2
2031f0ac6a5Smrg    exit 1
2041f0ac6a5Smrg  fi
20589c04b6cSmrg  # It's OK to call 'install-sh -d' without argument.
2061f0ac6a5Smrg  # This can happen when creating conditional directories.
2071f0ac6a5Smrg  exit 0
2081f0ac6a5Smrgfi
2091f0ac6a5Smrg
2101f0ac6a5Smrgif test -z "$dir_arg"; then
21189c04b6cSmrg  do_exit='(exit $ret); exit $ret'
21289c04b6cSmrg  trap "ret=129; $do_exit" 1
21389c04b6cSmrg  trap "ret=130; $do_exit" 2
21489c04b6cSmrg  trap "ret=141; $do_exit" 13
21589c04b6cSmrg  trap "ret=143; $do_exit" 15
2161f0ac6a5Smrg
2171f0ac6a5Smrg  # Set umask so as not to create temps with too-generous modes.
2181f0ac6a5Smrg  # However, 'strip' requires both read and write access to temps.
2191f0ac6a5Smrg  case $mode in
2201f0ac6a5Smrg    # Optimize common cases.
2211f0ac6a5Smrg    *644) cp_umask=133;;
2221f0ac6a5Smrg    *755) cp_umask=22;;
2231f0ac6a5Smrg
2241f0ac6a5Smrg    *[0-7])
2251f0ac6a5Smrg      if test -z "$stripcmd"; then
2262fd96bc7Smrg	u_plus_rw=
2271f0ac6a5Smrg      else
2282fd96bc7Smrg	u_plus_rw='% 200'
2291f0ac6a5Smrg      fi
2301f0ac6a5Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2311f0ac6a5Smrg    *)
2321f0ac6a5Smrg      if test -z "$stripcmd"; then
2332fd96bc7Smrg	u_plus_rw=
2341f0ac6a5Smrg      else
2352fd96bc7Smrg	u_plus_rw=,u+rw
2361f0ac6a5Smrg      fi
2371f0ac6a5Smrg      cp_umask=$mode$u_plus_rw;;
2381f0ac6a5Smrg  esac
2391f0ac6a5Smrgfi
2401f0ac6a5Smrg
2411f0ac6a5Smrgfor src
2421f0ac6a5Smrgdo
24389c04b6cSmrg  # Protect names problematic for 'test' and other utilities.
2441f0ac6a5Smrg  case $src in
24589c04b6cSmrg    -* | [=\(\)!]) src=./$src;;
2461f0ac6a5Smrg  esac
2471f0ac6a5Smrg
2481f0ac6a5Smrg  if test -n "$dir_arg"; then
2491f0ac6a5Smrg    dst=$src
2501f0ac6a5Smrg    dstdir=$dst
2511f0ac6a5Smrg    test -d "$dstdir"
2521f0ac6a5Smrg    dstdir_status=$?
2531f0ac6a5Smrg  else
2541f0ac6a5Smrg
2551f0ac6a5Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
2561f0ac6a5Smrg    # might cause directories to be created, which would be especially bad
2571f0ac6a5Smrg    # if $src (and thus $dsttmp) contains '*'.
2581f0ac6a5Smrg    if test ! -f "$src" && test ! -d "$src"; then
2591f0ac6a5Smrg      echo "$0: $src does not exist." >&2
2601f0ac6a5Smrg      exit 1
2611f0ac6a5Smrg    fi
2621f0ac6a5Smrg
263e5410a46Smrg    if test -z "$dst_arg"; then
2641f0ac6a5Smrg      echo "$0: no destination specified." >&2
2651f0ac6a5Smrg      exit 1
2661f0ac6a5Smrg    fi
267e5410a46Smrg    dst=$dst_arg
2681f0ac6a5Smrg
2691f0ac6a5Smrg    # If destination is a directory, append the input filename; won't work
2701f0ac6a5Smrg    # if double slashes aren't ignored.
2711f0ac6a5Smrg    if test -d "$dst"; then
2722fd96bc7Smrg      if test -n "$no_target_directory"; then
2732fd96bc7Smrg	echo "$0: $dst_arg: Is a directory" >&2
2742fd96bc7Smrg	exit 1
2751f0ac6a5Smrg      fi
2761f0ac6a5Smrg      dstdir=$dst
2771f0ac6a5Smrg      dst=$dstdir/`basename "$src"`
2781f0ac6a5Smrg      dstdir_status=0
2791f0ac6a5Smrg    else
2802fd96bc7Smrg      # Prefer dirname, but fall back on a substitute if dirname fails.
2812fd96bc7Smrg      dstdir=`
2822fd96bc7Smrg	(dirname "$dst") 2>/dev/null ||
2832fd96bc7Smrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
2842fd96bc7Smrg	     X"$dst" : 'X\(//\)[^/]' \| \
2852fd96bc7Smrg	     X"$dst" : 'X\(//\)$' \| \
2862fd96bc7Smrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
2872fd96bc7Smrg	echo X"$dst" |
2882fd96bc7Smrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
2892fd96bc7Smrg		   s//\1/
2902fd96bc7Smrg		   q
2912fd96bc7Smrg		 }
2922fd96bc7Smrg		 /^X\(\/\/\)[^/].*/{
2932fd96bc7Smrg		   s//\1/
2942fd96bc7Smrg		   q
2952fd96bc7Smrg		 }
2962fd96bc7Smrg		 /^X\(\/\/\)$/{
2972fd96bc7Smrg		   s//\1/
2982fd96bc7Smrg		   q
2992fd96bc7Smrg		 }
3002fd96bc7Smrg		 /^X\(\/\).*/{
3012fd96bc7Smrg		   s//\1/
3022fd96bc7Smrg		   q
3032fd96bc7Smrg		 }
3042fd96bc7Smrg		 s/.*/./; q'
3052fd96bc7Smrg      `
3062fd96bc7Smrg
3071f0ac6a5Smrg      test -d "$dstdir"
3081f0ac6a5Smrg      dstdir_status=$?
3091f0ac6a5Smrg    fi
3101f0ac6a5Smrg  fi
3111f0ac6a5Smrg
3121f0ac6a5Smrg  obsolete_mkdir_used=false
3131f0ac6a5Smrg
3141f0ac6a5Smrg  if test $dstdir_status != 0; then
3151f0ac6a5Smrg    case $posix_mkdir in
3161f0ac6a5Smrg      '')
3172fd96bc7Smrg	# Create intermediate dirs using mode 755 as modified by the umask.
3182fd96bc7Smrg	# This is like FreeBSD 'install' as of 1997-10-28.
3192fd96bc7Smrg	umask=`umask`
3202fd96bc7Smrg	case $stripcmd.$umask in
3212fd96bc7Smrg	  # Optimize common cases.
3222fd96bc7Smrg	  *[2367][2367]) mkdir_umask=$umask;;
3232fd96bc7Smrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
3242fd96bc7Smrg
3252fd96bc7Smrg	  *[0-7])
3262fd96bc7Smrg	    mkdir_umask=`expr $umask + 22 \
3272fd96bc7Smrg	      - $umask % 100 % 40 + $umask % 20 \
3282fd96bc7Smrg	      - $umask % 10 % 4 + $umask % 2
3292fd96bc7Smrg	    `;;
3302fd96bc7Smrg	  *) mkdir_umask=$umask,go-w;;
3312fd96bc7Smrg	esac
3322fd96bc7Smrg
3332fd96bc7Smrg	# With -d, create the new directory with the user-specified mode.
3342fd96bc7Smrg	# Otherwise, rely on $mkdir_umask.
3352fd96bc7Smrg	if test -n "$dir_arg"; then
3362fd96bc7Smrg	  mkdir_mode=-m$mode
3372fd96bc7Smrg	else
3382fd96bc7Smrg	  mkdir_mode=
3392fd96bc7Smrg	fi
3402fd96bc7Smrg
3412fd96bc7Smrg	posix_mkdir=false
3422fd96bc7Smrg	case $umask in
3432fd96bc7Smrg	  *[123567][0-7][0-7])
3442fd96bc7Smrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
3452fd96bc7Smrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
3462fd96bc7Smrg	    ;;
3472fd96bc7Smrg	  *)
3482fd96bc7Smrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
3492fd96bc7Smrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
3502fd96bc7Smrg
3512fd96bc7Smrg	    if (umask $mkdir_umask &&
3522fd96bc7Smrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
3532fd96bc7Smrg	    then
3542fd96bc7Smrg	      if test -z "$dir_arg" || {
3552fd96bc7Smrg		   # Check for POSIX incompatibilities with -m.
3562fd96bc7Smrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
3572fd96bc7Smrg		   # other-writable bit of parent directory when it shouldn't.
3582fd96bc7Smrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
3592fd96bc7Smrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
3602fd96bc7Smrg		   case $ls_ld_tmpdir in
3612fd96bc7Smrg		     d????-?r-*) different_mode=700;;
3622fd96bc7Smrg		     d????-?--*) different_mode=755;;
3632fd96bc7Smrg		     *) false;;
3642fd96bc7Smrg		   esac &&
3652fd96bc7Smrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
3662fd96bc7Smrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
3672fd96bc7Smrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
3682fd96bc7Smrg		   }
3692fd96bc7Smrg		 }
3702fd96bc7Smrg	      then posix_mkdir=:
3712fd96bc7Smrg	      fi
3722fd96bc7Smrg	      rmdir "$tmpdir/d" "$tmpdir"
3732fd96bc7Smrg	    else
3742fd96bc7Smrg	      # Remove any dirs left behind by ancient mkdir implementations.
3752fd96bc7Smrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
3762fd96bc7Smrg	    fi
3772fd96bc7Smrg	    trap '' 0;;
3782fd96bc7Smrg	esac;;
3791f0ac6a5Smrg    esac
3801f0ac6a5Smrg
3811f0ac6a5Smrg    if
3821f0ac6a5Smrg      $posix_mkdir && (
3832fd96bc7Smrg	umask $mkdir_umask &&
3842fd96bc7Smrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3851f0ac6a5Smrg      )
3861f0ac6a5Smrg    then :
3871f0ac6a5Smrg    else
3881f0ac6a5Smrg
3891f0ac6a5Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
3901f0ac6a5Smrg      # or it failed possibly due to a race condition.  Create the
3911f0ac6a5Smrg      # directory the slow way, step by step, checking for races as we go.
3921f0ac6a5Smrg
3931f0ac6a5Smrg      case $dstdir in
3942fd96bc7Smrg	/*) prefix='/';;
3952fd96bc7Smrg	[-=\(\)!]*) prefix='./';;
3962fd96bc7Smrg	*)  prefix='';;
3971f0ac6a5Smrg      esac
3981f0ac6a5Smrg
3992fd96bc7Smrg      eval "$initialize_posix_glob"
4002fd96bc7Smrg
4011f0ac6a5Smrg      oIFS=$IFS
4021f0ac6a5Smrg      IFS=/
4032fd96bc7Smrg      $posix_glob set -f
4041f0ac6a5Smrg      set fnord $dstdir
4051f0ac6a5Smrg      shift
4062fd96bc7Smrg      $posix_glob set +f
4071f0ac6a5Smrg      IFS=$oIFS
4081f0ac6a5Smrg
4091f0ac6a5Smrg      prefixes=
4101f0ac6a5Smrg
4111f0ac6a5Smrg      for d
4121f0ac6a5Smrg      do
4132fd96bc7Smrg	test X"$d" = X && continue
4142fd96bc7Smrg
4152fd96bc7Smrg	prefix=$prefix$d
4162fd96bc7Smrg	if test -d "$prefix"; then
4172fd96bc7Smrg	  prefixes=
4182fd96bc7Smrg	else
4192fd96bc7Smrg	  if $posix_mkdir; then
4202fd96bc7Smrg	    (umask=$mkdir_umask &&
4212fd96bc7Smrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
4222fd96bc7Smrg	    # Don't fail if two instances are running concurrently.
4232fd96bc7Smrg	    test -d "$prefix" || exit 1
4242fd96bc7Smrg	  else
4252fd96bc7Smrg	    case $prefix in
4262fd96bc7Smrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
4272fd96bc7Smrg	      *) qprefix=$prefix;;
4282fd96bc7Smrg	    esac
4292fd96bc7Smrg	    prefixes="$prefixes '$qprefix'"
4302fd96bc7Smrg	  fi
4312fd96bc7Smrg	fi
4322fd96bc7Smrg	prefix=$prefix/
4331f0ac6a5Smrg      done
4341f0ac6a5Smrg
4351f0ac6a5Smrg      if test -n "$prefixes"; then
4362fd96bc7Smrg	# Don't fail if two instances are running concurrently.
4372fd96bc7Smrg	(umask $mkdir_umask &&
4382fd96bc7Smrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
4392fd96bc7Smrg	  test -d "$dstdir" || exit 1
4402fd96bc7Smrg	obsolete_mkdir_used=true
4411f0ac6a5Smrg      fi
4421f0ac6a5Smrg    fi
4431f0ac6a5Smrg  fi
4441f0ac6a5Smrg
4451f0ac6a5Smrg  if test -n "$dir_arg"; then
4461f0ac6a5Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4471f0ac6a5Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4481f0ac6a5Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4491f0ac6a5Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
4501f0ac6a5Smrg  else
4511f0ac6a5Smrg
4521f0ac6a5Smrg    # Make a couple of temp file names in the proper directory.
4531f0ac6a5Smrg    dsttmp=$dstdir/_inst.$$_
4541f0ac6a5Smrg    rmtmp=$dstdir/_rm.$$_
4551f0ac6a5Smrg
4561f0ac6a5Smrg    # Trap to clean up those temp files at exit.
4571f0ac6a5Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
4581f0ac6a5Smrg
4591f0ac6a5Smrg    # Copy the file name to the temp name.
4601f0ac6a5Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
4611f0ac6a5Smrg
4621f0ac6a5Smrg    # and set any options; do chmod last to preserve setuid bits.
4631f0ac6a5Smrg    #
4641f0ac6a5Smrg    # If any of these fail, we abort the whole thing.  If we want to
4651f0ac6a5Smrg    # ignore errors from any of these, just make sure not to ignore
4661f0ac6a5Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
4671f0ac6a5Smrg    #
468e5410a46Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
469e5410a46Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
470e5410a46Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
471e5410a46Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
472e5410a46Smrg
473e5410a46Smrg    # If -C, don't bother to copy if it wouldn't change the file.
474e5410a46Smrg    if $copy_on_change &&
4752fd96bc7Smrg       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
4762fd96bc7Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
4772fd96bc7Smrg
4782fd96bc7Smrg       eval "$initialize_posix_glob" &&
4792fd96bc7Smrg       $posix_glob set -f &&
480e5410a46Smrg       set X $old && old=:$2:$4:$5:$6 &&
481e5410a46Smrg       set X $new && new=:$2:$4:$5:$6 &&
4822fd96bc7Smrg       $posix_glob set +f &&
4832fd96bc7Smrg
484e5410a46Smrg       test "$old" = "$new" &&
485e5410a46Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
486e5410a46Smrg    then
487e5410a46Smrg      rm -f "$dsttmp"
488e5410a46Smrg    else
489e5410a46Smrg      # Rename the file to the real destination.
490e5410a46Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
491e5410a46Smrg
492e5410a46Smrg      # The rename failed, perhaps because mv can't rename something else
493e5410a46Smrg      # to itself, or perhaps because mv is so ancient that it does not
494e5410a46Smrg      # support -f.
495e5410a46Smrg      {
4962fd96bc7Smrg	# Now remove or move aside any old file at destination location.
4972fd96bc7Smrg	# We try this two ways since rm can't unlink itself on some
4982fd96bc7Smrg	# systems and the destination file might be busy for other
4992fd96bc7Smrg	# reasons.  In this case, the final cleanup might fail but the new
5002fd96bc7Smrg	# file should still install successfully.
5012fd96bc7Smrg	{
5022fd96bc7Smrg	  test ! -f "$dst" ||
5032fd96bc7Smrg	  $doit $rmcmd -f "$dst" 2>/dev/null ||
5042fd96bc7Smrg	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
5052fd96bc7Smrg	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
5062fd96bc7Smrg	  } ||
5072fd96bc7Smrg	  { echo "$0: cannot unlink or rename $dst" >&2
5082fd96bc7Smrg	    (exit 1); exit 1
5092fd96bc7Smrg	  }
5102fd96bc7Smrg	} &&
5112fd96bc7Smrg
5122fd96bc7Smrg	# Now rename the file to the real destination.
5132fd96bc7Smrg	$doit $mvcmd "$dsttmp" "$dst"
514e5410a46Smrg      }
515e5410a46Smrg    fi || exit 1
5161f0ac6a5Smrg
5171f0ac6a5Smrg    trap '' 0
5181f0ac6a5Smrg  fi
5191f0ac6a5Smrgdone
5201f0ac6a5Smrg
5211f0ac6a5Smrg# Local variables:
5221f0ac6a5Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
5231f0ac6a5Smrg# time-stamp-start: "scriptversion="
5241f0ac6a5Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
525e5410a46Smrg# time-stamp-time-zone: "UTC"
526e5410a46Smrg# time-stamp-end: "; # UTC"
5271f0ac6a5Smrg# End:
528