#!/bin/sh

ARGS=1
BF_OPTIONS=
MAINT_FLAGS="--cache-file=config.cache"
while [ $ARGS != 0 ]
do
	case "$1" in
		bat)
			cmd /c bootstrap.bat $BF_OPTIONS
			exit 0
			;;

		nodoc)
			BF_OPTIONS="-DBUILDDOCS=no $BF_OPTIONS"
			shift
			;;

		noex)
			BF_OPTIONS="-DBUILDEXAMPLES=no $BF_OPTIONS"
			shift
			;;

		nolib)
			BF_OPTIONS="-DBUILDLIBRARY=no $BF_OPTIONS"
			shift
			;;

		nomaint)
			MAINT_FLAGS=
			shift
			;;

		noopt)
			export CXXFLAGS="-g -O0"
			shift
			;;

		pedantic)
			export CXXFLAGS="-g -O2 -ansi -pedantic -Wall -W -Wold-style-cast -Wfloat-equal -Wwrite-strings -Wno-overloaded-virtual -Wno-long-long -Wno-variadic-macros -D_GLIBCXX_DEBUG -D_GLIBCXX_DEBUG_PEDANTIC"
			shift
			;;

		*)
			ARGS=0
			;;
	esac
done

rm -f config.cache

# Do Bakefile stuff first
set -x &&
	for d in 3 5 8 ; do mkdir -p vc200$d ; done &&
	bakefilize &&
	bakefile_gen $BF_OPTIONS &&
	bakefile -f gnu -o Makefile.simple -DBUILDLIBRARY=no mysql++.bkl &&
	set +x &&
	success=shonuff

# Get rid of INSTALL symlink added by bakefilize.  We already have
# INSTALL.txt.
rm -f INSTALL

# If that succeeded, move on to autotools stuff, etc.
set +x
if [ -n "$success" ]
then
	mv autoconf_inc.m4 config > /dev/null 2>&1	# don't care if it fails

	# Find location of Bakefile's stock M4 autoconf macros
	for d in /usr /usr/local
	do
		BAKEFILE_M4=$d/share/aclocal
		if [ -e $BAKEFILE_M4/bakefile.m4 ] ; then break ; fi
	done
	if [ ! -e $BAKEFILE_M4/bakefile.m4 ]
	then
		echo
		echo "Failed to find bakefile.m4.  Add the directory containing"
		echo "this to the bootstrap script."
		echo
		exit 1
	fi

	success=
	set -x &&
		aclocal -I config -I $BAKEFILE_M4 &&
		autoheader &&
		autoconf &&
		./configure $MAINT_FLAGS $* &&
		make lib/querydef.h lib/ssqls.h &&
		set +x &&
		success=awyeah
fi

# Detect failure in any part of above
if [ -z "$success" ]
then
	echo
    echo BOOTSTRAP FAILED!
    echo
    exit 1
fi

