:
# NAME:
#	boot-strap
#
# SYNOPSIS:
#	boot-strap [--"configure_arg" ... ][-s "srcdir"][-m "mksrc"]\\
#		["prefix" ["bmakesrc" ["mksrc"]]]
#
# DESCRIPTION:
#	This script is used to configure/build bmake it builds for
#	each OS in a subdir to keep the src clean. 
#	On successful completion it echos commands to put the new
#	bmake binary into the /configs tree (if it exists)
#	(http://www.crufty.net/FreeWare/configs.html), $prefix/bin
#	and a suitable ~/*bin directory.
#
#	Options:
#
#	-m "mksrc"
#		Indicate where the mk files can be found.
#		Default is ./mk or ../mk, set to 'none' to force
#		building without "mksrc" but in that case a sys.mk
#		needs to exist in the default syspath ($prefix/share/mk)
#
#	Possibly useful configure_args:
#
#	--with-machine="machine"
#		set "machine" to override that determined by
#		machine.sh
#	
#	--with-force-machine="machine"
#		force "machine" even if uname(3) provides a value.
#
#	--with-machine_arch="machine_arch"
#		set "machine_arch" to override that determined by
#		machine.sh
#
#	--with-default-sys-path="syspath"
#		set an explicit default "syspath" which is where bmake
#		will look for sys.mk and friends.
#	
#	--with-prefix-sys-path="syspath"
#		prefix the built in default syspath with "syspath"
#
# AUTHOR:
#	Simon J. Gerraty <sjg@crufty.net>

# RCSid:
#	$Id: boot-strap,v 1.16 2006/02/22 21:18:53 sjg Exp $
#
#	@(#) Copyright (c) 2001 Simon J. Gerraty
#
#	This file is provided in the hope that it will
#	be of use.  There is absolutely NO WARRANTY.
#	Permission to copy, redistribute or otherwise
#	use this file is hereby granted provided that 
#	the above copyright notice and this notice are
#	left intact. 
#      
#	Please send copies of changes and bug-fixes to:
#	sjg@crufty.net
#

Mydir=`dirname $0`
. $Mydir/os.sh
case $Mydir in
/*) ;;
*) Mydir=`cd $Mydir; /bin/pwd`;;
esac


Usage() {
	[ "$1" ] && echo "ERROR: $@" >&2
	echo "Usage:" >&2
	echo "$0 [--<configure_arg> ...][-s <srcdir>][-m <mksrc>][<prefix> [[<srcdir>] [<mksrc>]]]" >&2
	exit 1
}

Error() {
	echo "ERROR: $@" >&2
	exit 1
}


CONFIGURE_ARGS=
prefix=
srcdir=
mksrc=
objdir=
quiet=:

while :
do
	case "$1" in
	--) shift; break;;
	--prefix) prefix=$2; shift 2;;
	--prefix=*) prefix=`IFS="="; set -- $1; echo $2`; shift;;
	--src=*) srcdir=`IFS="="; set -- $1; echo $2`; shift;;
	--with-mksrc=*) mksrc=`IFS="="; set -- $1; echo $2`; shift;;
	-s|--src) srcdir=$2; shift 2;;
	-m|--mksrc) mksrc=$2; shift 2;;
	-o|--objdir) objdir=$2; shift 2;;
	-q) quiet=; shift;;
	--*) CONFIGURE_ARGS="$CONFIGURE_ARGS $1"; shift;;
	*=*) eval "$1"; export `IFS="="; set -- $1; echo $1`; shift;;
	*) break;;
	esac
done

AddConfigure() {
	case " $CONFIGURE_ARGS " in
	*" $1"*) ;;
	*) CONFIGURE_ARGS="$CONFIGURE_ARGS $1$2";;
	esac
}

GetDir() {
	match=$1
	shift
	for dir in $*
	do
		[ -d $dir ] || continue
		case "/$dir/" in
		*$match*) ;;
		*) continue;;
		esac
		case "$dir/" in
		*./*) cd $dir && /bin/pwd;;
		/*) echo $dir;;
		*) cd $dir && /bin/pwd;;
		esac
		break
	done
}
	
srcdir=`GetDir /bmake $srcdir $2 $Mydir ./bmake* $Mydir/../bmake*`
[ -d ${srcdir:-/dev/null} ] || Usage
case "$mksrc" in
none|-) # we don't want it
	mksrc=
	;;
*)	# guess we want mksrc...
	mksrc=`GetDir /mk $mksrc $3 ./mk* $srcdir/mk* $srcdir/../mk*`
	[ -d ${mksrc:-/dev/null} ] || Usage "Use '-m none' to build without mksrc"
	;;
esac

# Ok, get to work...
objdir=${objdir:-$OS}
[ -d $objdir ] || mkdir -p $objdir
[ -d $objdir ] || mkdir $objdir
cd $objdir || exit 1

AddConfigure --prefix= $prefix
case "$CONFIGURE_ARGS" in
*--with-prefix-sys-path*) ;;	# skip
*) AddConfigure --with-default-sys-path= $prefix/share/mk;;
esac
[ "$mksrc" ] && AddConfigure --with-mksrc= $mksrc
$srcdir/configure $CONFIGURE_ARGS || exit 1
${MAKE:-make} -f makefile.boot clean
${MAKE:-make} -f makefile.boot bootstrap || exit 1

# If -q given, we don't want all the install instructions
$quiet exit 0

make_version=`./bmake -m ./mk -m /usr/share/mk -f ./Makefile -V MAKE_VERSION | ( read one two; echo $one )`

install_prefix() {
       bdir=$1/bin
       echo
       echo Commands to install into $1/
       echo
       echo mkdir -p $bdir
       echo cp $objdir/bmake $bdir/$make_version
       echo rm -f $bdir/bmake
       echo ln -s $make_version $bdir/bmake
       [ "$mksrc" ] && echo $mksrc/install-mk $1/share/mk
}

case $prefix/ in
$HOME/*) ;;
*)	CONFIGS=${CONFIGS:-/configs}
	[ -d $CONFIGS ] &&
	install_prefix $CONFIGS/$OS/$OSMAJOR.X/$MACHINE_ARCH$prefix
	# I like to keep a copy here...
	install_prefix $HOME/$HOST_TARGET
	;;
esac

install_prefix $prefix
