install-sh revision 2a75d1c4
1f7ec340bSmacallan#!/bin/sh
2f7ec340bSmacallan# install - install a program, script, or datafile
3f7ec340bSmacallan
42a75d1c4Smrgscriptversion=2011-11-20.07; # UTC
5f7ec340bSmacallan
6f7ec340bSmacallan# This originates from X11R5 (mit/util/scripts/install.sh), which was
7f7ec340bSmacallan# later released in X11R6 (xc/config/util/install.sh) with the
8f7ec340bSmacallan# following copyright and license.
9f7ec340bSmacallan#
10f7ec340bSmacallan# Copyright (C) 1994 X Consortium
11f7ec340bSmacallan#
12f7ec340bSmacallan# Permission is hereby granted, free of charge, to any person obtaining a copy
13f7ec340bSmacallan# of this software and associated documentation files (the "Software"), to
14f7ec340bSmacallan# deal in the Software without restriction, including without limitation the
15f7ec340bSmacallan# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16f7ec340bSmacallan# sell copies of the Software, and to permit persons to whom the Software is
17f7ec340bSmacallan# furnished to do so, subject to the following conditions:
18f7ec340bSmacallan#
19f7ec340bSmacallan# The above copyright notice and this permission notice shall be included in
20f7ec340bSmacallan# all copies or substantial portions of the Software.
21f7ec340bSmacallan#
22f7ec340bSmacallan# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23f7ec340bSmacallan# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24f7ec340bSmacallan# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25f7ec340bSmacallan# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26f7ec340bSmacallan# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27f7ec340bSmacallan# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28f7ec340bSmacallan#
29f7ec340bSmacallan# Except as contained in this notice, the name of the X Consortium shall not
30f7ec340bSmacallan# be used in advertising or otherwise to promote the sale, use or other deal-
31f7ec340bSmacallan# ings in this Software without prior written authorization from the X Consor-
32f7ec340bSmacallan# tium.
33f7ec340bSmacallan#
34f7ec340bSmacallan#
35f7ec340bSmacallan# FSF changes to this file are in the public domain.
36f7ec340bSmacallan#
37f7ec340bSmacallan# Calling this script install-sh is preferred over install.sh, to prevent
382a75d1c4Smrg# 'make' implicit rules from creating a file called install from it
39f7ec340bSmacallan# when there is no Makefile.
40f7ec340bSmacallan#
41f7ec340bSmacallan# This script is compatible with the BSD install script, but was written
427ce7e03cSmrg# from scratch.
437ce7e03cSmrg
447ce7e03cSmrgnl='
457ce7e03cSmrg'
467ce7e03cSmrgIFS=" ""	$nl"
47f7ec340bSmacallan
48f7ec340bSmacallan# set DOITPROG to echo to test this script
49f7ec340bSmacallan
50f7ec340bSmacallan# Don't use :- since 4.3BSD and earlier shells don't like it.
517ce7e03cSmrgdoit=${DOITPROG-}
527ce7e03cSmrgif test -z "$doit"; then
537ce7e03cSmrg  doit_exec=exec
547ce7e03cSmrgelse
557ce7e03cSmrg  doit_exec=$doit
567ce7e03cSmrgfi
57f7ec340bSmacallan
587ce7e03cSmrg# Put in absolute file names if you don't have them in your path;
597ce7e03cSmrg# or use environment vars.
607ce7e03cSmrg
617ce7e03cSmrgchgrpprog=${CHGRPPROG-chgrp}
627ce7e03cSmrgchmodprog=${CHMODPROG-chmod}
637ce7e03cSmrgchownprog=${CHOWNPROG-chown}
647ce7e03cSmrgcmpprog=${CMPPROG-cmp}
657ce7e03cSmrgcpprog=${CPPROG-cp}
667ce7e03cSmrgmkdirprog=${MKDIRPROG-mkdir}
677ce7e03cSmrgmvprog=${MVPROG-mv}
687ce7e03cSmrgrmprog=${RMPROG-rm}
697ce7e03cSmrgstripprog=${STRIPPROG-strip}
707ce7e03cSmrg
717ce7e03cSmrgposix_glob='?'
727ce7e03cSmrginitialize_posix_glob='
737ce7e03cSmrg  test "$posix_glob" != "?" || {
747ce7e03cSmrg    if (set -f) 2>/dev/null; then
757ce7e03cSmrg      posix_glob=
767ce7e03cSmrg    else
777ce7e03cSmrg      posix_glob=:
787ce7e03cSmrg    fi
797ce7e03cSmrg  }
807ce7e03cSmrg'
81f7ec340bSmacallan
827ce7e03cSmrgposix_mkdir=
837ce7e03cSmrg
847ce7e03cSmrg# Desired mode of installed file.
857ce7e03cSmrgmode=0755
86f7ec340bSmacallan
87f7ec340bSmacallanchgrpcmd=
887ce7e03cSmrgchmodcmd=$chmodprog
897ce7e03cSmrgchowncmd=
907ce7e03cSmrgmvcmd=$mvprog
91f7ec340bSmacallanrmcmd="$rmprog -f"
927ce7e03cSmrgstripcmd=
937ce7e03cSmrg
94f7ec340bSmacallansrc=
95f7ec340bSmacallandst=
96f7ec340bSmacallandir_arg=
977ce7e03cSmrgdst_arg=
987ce7e03cSmrg
997ce7e03cSmrgcopy_on_change=false
100f7ec340bSmacallanno_target_directory=
101f7ec340bSmacallan
1027ce7e03cSmrgusage="\
1037ce7e03cSmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
104f7ec340bSmacallan   or: $0 [OPTION]... SRCFILES... DIRECTORY
105f7ec340bSmacallan   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
106f7ec340bSmacallan   or: $0 [OPTION]... -d DIRECTORIES...
107f7ec340bSmacallan
108f7ec340bSmacallanIn the 1st form, copy SRCFILE to DSTFILE.
109f7ec340bSmacallanIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
110f7ec340bSmacallanIn the 4th, create DIRECTORIES.
111f7ec340bSmacallan
112f7ec340bSmacallanOptions:
1137ce7e03cSmrg     --help     display this help and exit.
1147ce7e03cSmrg     --version  display version info and exit.
1157ce7e03cSmrg
1167ce7e03cSmrg  -c            (ignored)
1177ce7e03cSmrg  -C            install only if different (preserve the last data modification time)
1187ce7e03cSmrg  -d            create directories instead of installing files.
1197ce7e03cSmrg  -g GROUP      $chgrpprog installed files to GROUP.
1207ce7e03cSmrg  -m MODE       $chmodprog installed files to MODE.
1217ce7e03cSmrg  -o USER       $chownprog installed files to USER.
1227ce7e03cSmrg  -s            $stripprog installed files.
1237ce7e03cSmrg  -t DIRECTORY  install into DIRECTORY.
1247ce7e03cSmrg  -T            report an error if DSTFILE is a directory.
125f7ec340bSmacallan
126f7ec340bSmacallanEnvironment variables override the default commands:
1277ce7e03cSmrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
1287ce7e03cSmrg  RMPROG STRIPPROG
129f7ec340bSmacallan"
130f7ec340bSmacallan
1317ce7e03cSmrgwhile test $# -ne 0; do
132f7ec340bSmacallan  case $1 in
1337ce7e03cSmrg    -c) ;;
1347ce7e03cSmrg
1357ce7e03cSmrg    -C) copy_on_change=true;;
136f7ec340bSmacallan
1377ce7e03cSmrg    -d) dir_arg=true;;
138f7ec340bSmacallan
139f7ec340bSmacallan    -g) chgrpcmd="$chgrpprog $2"
1407ce7e03cSmrg	shift;;
141f7ec340bSmacallan
142f7ec340bSmacallan    --help) echo "$usage"; exit $?;;
143f7ec340bSmacallan
1447ce7e03cSmrg    -m) mode=$2
1457ce7e03cSmrg	case $mode in
1467ce7e03cSmrg	  *' '* | *'	'* | *'
1477ce7e03cSmrg'*	  | *'*'* | *'?'* | *'['*)
1487ce7e03cSmrg	    echo "$0: invalid mode: $mode" >&2
1497ce7e03cSmrg	    exit 1;;
1507ce7e03cSmrg	esac
1517ce7e03cSmrg	shift;;
152f7ec340bSmacallan
153f7ec340bSmacallan    -o) chowncmd="$chownprog $2"
1547ce7e03cSmrg	shift;;
155f7ec340bSmacallan
1567ce7e03cSmrg    -s) stripcmd=$stripprog;;
157f7ec340bSmacallan
1587ce7e03cSmrg    -t) dst_arg=$2
1592a75d1c4Smrg	# Protect names problematic for 'test' and other utilities.
1602a75d1c4Smrg	case $dst_arg in
1612a75d1c4Smrg	  -* | [=\(\)!]) dst_arg=./$dst_arg;;
1622a75d1c4Smrg	esac
1637ce7e03cSmrg	shift;;
164f7ec340bSmacallan
1657ce7e03cSmrg    -T) no_target_directory=true;;
166f7ec340bSmacallan
167f7ec340bSmacallan    --version) echo "$0 $scriptversion"; exit $?;;
168f7ec340bSmacallan
1697ce7e03cSmrg    --)	shift
170f7ec340bSmacallan	break;;
1717ce7e03cSmrg
1727ce7e03cSmrg    -*)	echo "$0: invalid option: $1" >&2
1737ce7e03cSmrg	exit 1;;
1747ce7e03cSmrg
1757ce7e03cSmrg    *)  break;;
176f7ec340bSmacallan  esac
1777ce7e03cSmrg  shift
178f7ec340bSmacallandone
179f7ec340bSmacallan
1807ce7e03cSmrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1817ce7e03cSmrg  # When -d is used, all remaining arguments are directories to create.
1827ce7e03cSmrg  # When -t is used, the destination is already specified.
1837ce7e03cSmrg  # Otherwise, the last argument is the destination.  Remove it from $@.
1847ce7e03cSmrg  for arg
1857ce7e03cSmrg  do
1867ce7e03cSmrg    if test -n "$dst_arg"; then
1877ce7e03cSmrg      # $@ is not empty: it contains at least $arg.
1887ce7e03cSmrg      set fnord "$@" "$dst_arg"
1897ce7e03cSmrg      shift # fnord
1907ce7e03cSmrg    fi
1917ce7e03cSmrg    shift # arg
1927ce7e03cSmrg    dst_arg=$arg
1932a75d1c4Smrg    # Protect names problematic for 'test' and other utilities.
1942a75d1c4Smrg    case $dst_arg in
1952a75d1c4Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
1962a75d1c4Smrg    esac
1977ce7e03cSmrg  done
1987ce7e03cSmrgfi
1997ce7e03cSmrg
2007ce7e03cSmrgif test $# -eq 0; then
201f7ec340bSmacallan  if test -z "$dir_arg"; then
202f7ec340bSmacallan    echo "$0: no input file specified." >&2
203f7ec340bSmacallan    exit 1
204f7ec340bSmacallan  fi
2052a75d1c4Smrg  # It's OK to call 'install-sh -d' without argument.
206f7ec340bSmacallan  # This can happen when creating conditional directories.
207f7ec340bSmacallan  exit 0
208f7ec340bSmacallanfi
209f7ec340bSmacallan
2107ce7e03cSmrgif test -z "$dir_arg"; then
2112a75d1c4Smrg  do_exit='(exit $ret); exit $ret'
2122a75d1c4Smrg  trap "ret=129; $do_exit" 1
2132a75d1c4Smrg  trap "ret=130; $do_exit" 2
2142a75d1c4Smrg  trap "ret=141; $do_exit" 13
2152a75d1c4Smrg  trap "ret=143; $do_exit" 15
2167ce7e03cSmrg
2177ce7e03cSmrg  # Set umask so as not to create temps with too-generous modes.
2187ce7e03cSmrg  # However, 'strip' requires both read and write access to temps.
2197ce7e03cSmrg  case $mode in
2207ce7e03cSmrg    # Optimize common cases.
2217ce7e03cSmrg    *644) cp_umask=133;;
2227ce7e03cSmrg    *755) cp_umask=22;;
2237ce7e03cSmrg
2247ce7e03cSmrg    *[0-7])
2257ce7e03cSmrg      if test -z "$stripcmd"; then
2267ce7e03cSmrg	u_plus_rw=
2277ce7e03cSmrg      else
2287ce7e03cSmrg	u_plus_rw='% 200'
2297ce7e03cSmrg      fi
2307ce7e03cSmrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2317ce7e03cSmrg    *)
2327ce7e03cSmrg      if test -z "$stripcmd"; then
2337ce7e03cSmrg	u_plus_rw=
2347ce7e03cSmrg      else
2357ce7e03cSmrg	u_plus_rw=,u+rw
2367ce7e03cSmrg      fi
2377ce7e03cSmrg      cp_umask=$mode$u_plus_rw;;
2387ce7e03cSmrg  esac
2397ce7e03cSmrgfi
2407ce7e03cSmrg
241f7ec340bSmacallanfor src
242f7ec340bSmacallando
2432a75d1c4Smrg  # Protect names problematic for 'test' and other utilities.
244f7ec340bSmacallan  case $src in
2452a75d1c4Smrg    -* | [=\(\)!]) src=./$src;;
246f7ec340bSmacallan  esac
247f7ec340bSmacallan
248f7ec340bSmacallan  if test -n "$dir_arg"; then
249f7ec340bSmacallan    dst=$src
2507ce7e03cSmrg    dstdir=$dst
2517ce7e03cSmrg    test -d "$dstdir"
2527ce7e03cSmrg    dstdir_status=$?
253f7ec340bSmacallan  else
2547ce7e03cSmrg
255f7ec340bSmacallan    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
256f7ec340bSmacallan    # might cause directories to be created, which would be especially bad
257f7ec340bSmacallan    # if $src (and thus $dsttmp) contains '*'.
258f7ec340bSmacallan    if test ! -f "$src" && test ! -d "$src"; then
259f7ec340bSmacallan      echo "$0: $src does not exist." >&2
260f7ec340bSmacallan      exit 1
261f7ec340bSmacallan    fi
262f7ec340bSmacallan
2637ce7e03cSmrg    if test -z "$dst_arg"; then
264f7ec340bSmacallan      echo "$0: no destination specified." >&2
265f7ec340bSmacallan      exit 1
266f7ec340bSmacallan    fi
2677ce7e03cSmrg    dst=$dst_arg
268f7ec340bSmacallan
269f7ec340bSmacallan    # If destination is a directory, append the input filename; won't work
270f7ec340bSmacallan    # if double slashes aren't ignored.
271f7ec340bSmacallan    if test -d "$dst"; then
272f7ec340bSmacallan      if test -n "$no_target_directory"; then
2737ce7e03cSmrg	echo "$0: $dst_arg: Is a directory" >&2
274f7ec340bSmacallan	exit 1
275f7ec340bSmacallan      fi
2767ce7e03cSmrg      dstdir=$dst
2777ce7e03cSmrg      dst=$dstdir/`basename "$src"`
2787ce7e03cSmrg      dstdir_status=0
2797ce7e03cSmrg    else
2807ce7e03cSmrg      # Prefer dirname, but fall back on a substitute if dirname fails.
2817ce7e03cSmrg      dstdir=`
2827ce7e03cSmrg	(dirname "$dst") 2>/dev/null ||
2837ce7e03cSmrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
2847ce7e03cSmrg	     X"$dst" : 'X\(//\)[^/]' \| \
2857ce7e03cSmrg	     X"$dst" : 'X\(//\)$' \| \
2867ce7e03cSmrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
2877ce7e03cSmrg	echo X"$dst" |
2887ce7e03cSmrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
2897ce7e03cSmrg		   s//\1/
2907ce7e03cSmrg		   q
2917ce7e03cSmrg		 }
2927ce7e03cSmrg		 /^X\(\/\/\)[^/].*/{
2937ce7e03cSmrg		   s//\1/
2947ce7e03cSmrg		   q
2957ce7e03cSmrg		 }
2967ce7e03cSmrg		 /^X\(\/\/\)$/{
2977ce7e03cSmrg		   s//\1/
2987ce7e03cSmrg		   q
2997ce7e03cSmrg		 }
3007ce7e03cSmrg		 /^X\(\/\).*/{
3017ce7e03cSmrg		   s//\1/
3027ce7e03cSmrg		   q
3037ce7e03cSmrg		 }
3047ce7e03cSmrg		 s/.*/./; q'
3057ce7e03cSmrg      `
3067ce7e03cSmrg
3077ce7e03cSmrg      test -d "$dstdir"
3087ce7e03cSmrg      dstdir_status=$?
309f7ec340bSmacallan    fi
310f7ec340bSmacallan  fi
311f7ec340bSmacallan
3127ce7e03cSmrg  obsolete_mkdir_used=false
3137ce7e03cSmrg
3147ce7e03cSmrg  if test $dstdir_status != 0; then
3157ce7e03cSmrg    case $posix_mkdir in
3167ce7e03cSmrg      '')
3177ce7e03cSmrg	# Create intermediate dirs using mode 755 as modified by the umask.
3187ce7e03cSmrg	# This is like FreeBSD 'install' as of 1997-10-28.
3197ce7e03cSmrg	umask=`umask`
3207ce7e03cSmrg	case $stripcmd.$umask in
3217ce7e03cSmrg	  # Optimize common cases.
3227ce7e03cSmrg	  *[2367][2367]) mkdir_umask=$umask;;
3237ce7e03cSmrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
3247ce7e03cSmrg
3257ce7e03cSmrg	  *[0-7])
3267ce7e03cSmrg	    mkdir_umask=`expr $umask + 22 \
3277ce7e03cSmrg	      - $umask % 100 % 40 + $umask % 20 \
3287ce7e03cSmrg	      - $umask % 10 % 4 + $umask % 2
3297ce7e03cSmrg	    `;;
3307ce7e03cSmrg	  *) mkdir_umask=$umask,go-w;;
3317ce7e03cSmrg	esac
3327ce7e03cSmrg
3337ce7e03cSmrg	# With -d, create the new directory with the user-specified mode.
3347ce7e03cSmrg	# Otherwise, rely on $mkdir_umask.
3357ce7e03cSmrg	if test -n "$dir_arg"; then
3367ce7e03cSmrg	  mkdir_mode=-m$mode
3377ce7e03cSmrg	else
3387ce7e03cSmrg	  mkdir_mode=
3397ce7e03cSmrg	fi
3407ce7e03cSmrg
3417ce7e03cSmrg	posix_mkdir=false
3427ce7e03cSmrg	case $umask in
3437ce7e03cSmrg	  *[123567][0-7][0-7])
3447ce7e03cSmrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
3457ce7e03cSmrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
3467ce7e03cSmrg	    ;;
3477ce7e03cSmrg	  *)
3487ce7e03cSmrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
3497ce7e03cSmrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
3507ce7e03cSmrg
3517ce7e03cSmrg	    if (umask $mkdir_umask &&
3527ce7e03cSmrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
3537ce7e03cSmrg	    then
3547ce7e03cSmrg	      if test -z "$dir_arg" || {
3557ce7e03cSmrg		   # Check for POSIX incompatibilities with -m.
3567ce7e03cSmrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
3572a75d1c4Smrg		   # other-writable bit of parent directory when it shouldn't.
3587ce7e03cSmrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
3597ce7e03cSmrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
3607ce7e03cSmrg		   case $ls_ld_tmpdir in
3617ce7e03cSmrg		     d????-?r-*) different_mode=700;;
3627ce7e03cSmrg		     d????-?--*) different_mode=755;;
3637ce7e03cSmrg		     *) false;;
3647ce7e03cSmrg		   esac &&
3657ce7e03cSmrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
3667ce7e03cSmrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
3677ce7e03cSmrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
3687ce7e03cSmrg		   }
3697ce7e03cSmrg		 }
3707ce7e03cSmrg	      then posix_mkdir=:
3717ce7e03cSmrg	      fi
3727ce7e03cSmrg	      rmdir "$tmpdir/d" "$tmpdir"
3737ce7e03cSmrg	    else
3747ce7e03cSmrg	      # Remove any dirs left behind by ancient mkdir implementations.
3757ce7e03cSmrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
3767ce7e03cSmrg	    fi
3777ce7e03cSmrg	    trap '' 0;;
3787ce7e03cSmrg	esac;;
3797ce7e03cSmrg    esac
380f7ec340bSmacallan
3817ce7e03cSmrg    if
3827ce7e03cSmrg      $posix_mkdir && (
3837ce7e03cSmrg	umask $mkdir_umask &&
3847ce7e03cSmrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3857ce7e03cSmrg      )
3867ce7e03cSmrg    then :
3877ce7e03cSmrg    else
388f7ec340bSmacallan
3897ce7e03cSmrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
3907ce7e03cSmrg      # or it failed possibly due to a race condition.  Create the
3917ce7e03cSmrg      # directory the slow way, step by step, checking for races as we go.
392f7ec340bSmacallan
3937ce7e03cSmrg      case $dstdir in
3947ce7e03cSmrg	/*) prefix='/';;
3952a75d1c4Smrg	[-=\(\)!]*) prefix='./';;
3967ce7e03cSmrg	*)  prefix='';;
3977ce7e03cSmrg      esac
398f7ec340bSmacallan
3997ce7e03cSmrg      eval "$initialize_posix_glob"
400f7ec340bSmacallan
4017ce7e03cSmrg      oIFS=$IFS
4027ce7e03cSmrg      IFS=/
4037ce7e03cSmrg      $posix_glob set -f
4047ce7e03cSmrg      set fnord $dstdir
405f7ec340bSmacallan      shift
4067ce7e03cSmrg      $posix_glob set +f
4077ce7e03cSmrg      IFS=$oIFS
4087ce7e03cSmrg
4097ce7e03cSmrg      prefixes=
4107ce7e03cSmrg
4117ce7e03cSmrg      for d
4127ce7e03cSmrg      do
4132a75d1c4Smrg	test X"$d" = X && continue
4147ce7e03cSmrg
4157ce7e03cSmrg	prefix=$prefix$d
4167ce7e03cSmrg	if test -d "$prefix"; then
4177ce7e03cSmrg	  prefixes=
4187ce7e03cSmrg	else
4197ce7e03cSmrg	  if $posix_mkdir; then
4207ce7e03cSmrg	    (umask=$mkdir_umask &&
4217ce7e03cSmrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
4227ce7e03cSmrg	    # Don't fail if two instances are running concurrently.
4237ce7e03cSmrg	    test -d "$prefix" || exit 1
4247ce7e03cSmrg	  else
4257ce7e03cSmrg	    case $prefix in
4267ce7e03cSmrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
4277ce7e03cSmrg	      *) qprefix=$prefix;;
4287ce7e03cSmrg	    esac
4297ce7e03cSmrg	    prefixes="$prefixes '$qprefix'"
4307ce7e03cSmrg	  fi
4317ce7e03cSmrg	fi
4327ce7e03cSmrg	prefix=$prefix/
4337ce7e03cSmrg      done
4347ce7e03cSmrg
4357ce7e03cSmrg      if test -n "$prefixes"; then
4367ce7e03cSmrg	# Don't fail if two instances are running concurrently.
4377ce7e03cSmrg	(umask $mkdir_umask &&
4387ce7e03cSmrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
4397ce7e03cSmrg	  test -d "$dstdir" || exit 1
4407ce7e03cSmrg	obsolete_mkdir_used=true
441f7ec340bSmacallan      fi
4427ce7e03cSmrg    fi
443f7ec340bSmacallan  fi
444f7ec340bSmacallan
445f7ec340bSmacallan  if test -n "$dir_arg"; then
4467ce7e03cSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4477ce7e03cSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4487ce7e03cSmrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4497ce7e03cSmrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
450f7ec340bSmacallan  else
451f7ec340bSmacallan
452f7ec340bSmacallan    # Make a couple of temp file names in the proper directory.
453f7ec340bSmacallan    dsttmp=$dstdir/_inst.$$_
454f7ec340bSmacallan    rmtmp=$dstdir/_rm.$$_
455f7ec340bSmacallan
456f7ec340bSmacallan    # Trap to clean up those temp files at exit.
457f7ec340bSmacallan    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
458f7ec340bSmacallan
459f7ec340bSmacallan    # Copy the file name to the temp name.
4607ce7e03cSmrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
461f7ec340bSmacallan
462f7ec340bSmacallan    # and set any options; do chmod last to preserve setuid bits.
463f7ec340bSmacallan    #
464f7ec340bSmacallan    # If any of these fail, we abort the whole thing.  If we want to
465f7ec340bSmacallan    # ignore errors from any of these, just make sure not to ignore
466f7ec340bSmacallan    # errors from the above "$doit $cpprog $src $dsttmp" command.
467f7ec340bSmacallan    #
4687ce7e03cSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
4697ce7e03cSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
4707ce7e03cSmrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
4717ce7e03cSmrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4727ce7e03cSmrg
4737ce7e03cSmrg    # If -C, don't bother to copy if it wouldn't change the file.
4747ce7e03cSmrg    if $copy_on_change &&
4757ce7e03cSmrg       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
4767ce7e03cSmrg       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
4777ce7e03cSmrg
4787ce7e03cSmrg       eval "$initialize_posix_glob" &&
4797ce7e03cSmrg       $posix_glob set -f &&
4807ce7e03cSmrg       set X $old && old=:$2:$4:$5:$6 &&
4817ce7e03cSmrg       set X $new && new=:$2:$4:$5:$6 &&
4827ce7e03cSmrg       $posix_glob set +f &&
4837ce7e03cSmrg
4847ce7e03cSmrg       test "$old" = "$new" &&
4857ce7e03cSmrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
4867ce7e03cSmrg    then
4877ce7e03cSmrg      rm -f "$dsttmp"
4887ce7e03cSmrg    else
4897ce7e03cSmrg      # Rename the file to the real destination.
4907ce7e03cSmrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
4917ce7e03cSmrg
4927ce7e03cSmrg      # The rename failed, perhaps because mv can't rename something else
4937ce7e03cSmrg      # to itself, or perhaps because mv is so ancient that it does not
4947ce7e03cSmrg      # support -f.
4957ce7e03cSmrg      {
4967ce7e03cSmrg	# Now remove or move aside any old file at destination location.
4977ce7e03cSmrg	# We try this two ways since rm can't unlink itself on some
4987ce7e03cSmrg	# systems and the destination file might be busy for other
4997ce7e03cSmrg	# reasons.  In this case, the final cleanup might fail but the new
5007ce7e03cSmrg	# file should still install successfully.
5017ce7e03cSmrg	{
5027ce7e03cSmrg	  test ! -f "$dst" ||
5037ce7e03cSmrg	  $doit $rmcmd -f "$dst" 2>/dev/null ||
5047ce7e03cSmrg	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
5057ce7e03cSmrg	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
5067ce7e03cSmrg	  } ||
5077ce7e03cSmrg	  { echo "$0: cannot unlink or rename $dst" >&2
5087ce7e03cSmrg	    (exit 1); exit 1
5097ce7e03cSmrg	  }
5107ce7e03cSmrg	} &&
5117ce7e03cSmrg
5127ce7e03cSmrg	# Now rename the file to the real destination.
5137ce7e03cSmrg	$doit $mvcmd "$dsttmp" "$dst"
5147ce7e03cSmrg      }
5157ce7e03cSmrg    fi || exit 1
5167ce7e03cSmrg
5177ce7e03cSmrg    trap '' 0
5187ce7e03cSmrg  fi
519f7ec340bSmacallandone
520f7ec340bSmacallan
521f7ec340bSmacallan# Local variables:
522f7ec340bSmacallan# eval: (add-hook 'write-file-hooks 'time-stamp)
523f7ec340bSmacallan# time-stamp-start: "scriptversion="
524f7ec340bSmacallan# time-stamp-format: "%:y-%02m-%02d.%02H"
5252a75d1c4Smrg# time-stamp-time-zone: "UTC"
5262a75d1c4Smrg# time-stamp-end: "; # UTC"
527f7ec340bSmacallan# End:
528