#!/bin/bash

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 -Wextra -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

# Find location of Bakefile stuff.
bakefilize=bakefilize
for d in /usr/share/aclocal /usr/local/share/aclocal \
	'/c/Program Files (x86)/Bakefile/autoconf'
do
	BAKEFILE_M4="$d"
	if [ -e "$BAKEFILE_M4/bakefile.m4" ]
	then
		if [ -x "$d/bakefilize" ]
		then
			bakefilize="$d/bakefilize"
		fi
		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
if [ ! -x "$bakefilize" ] && ! type -p "$bakefilize" > /dev/null
then
	echo
	echo "Failed to find the bakefilize script.  Is Bakefile installed?"
	echo
	exit 1
fi

# Check for existence of needed tools, so we can give a better error
# message than the shell will.
tools="make"														# POSIX
tools="$tools aclocal autoconf autoheader autoreconf libtoolize"	# Autotools
tools="$tools bakefile bakefile_gen"								# Bakefile
for tool in $tools
do
	if ! type -p $tool > /dev/null
	then
		echo "FAILED to find build tool '$tool'!"
		echo
		echo BOOTSTRAP FAILED!
		echo
		exit 1
	fi
done

# Do Bakefile stuff first.  Autoconf can't succeed without
# autoconf_in.m4, which Bakefile creates.
success=
set -x &&
	for d in 3 5 8 ; do mkdir -p vc200$d ; done &&
	"$bakefilize" &&
	rm -f INSTALL &&
	bakefile_gen $BF_OPTIONS &&
	bakefile -f gnu -o Makefile.simple -DBUILDLIBRARY=no mysql++.bkl &&
	set +x &&
	success=shonuff

# Do the autotools stuff if Bakefile steps succeeded
#
# We're calling automake only because autoconf depends on install-sh or
# similar, and it *ass*umes you're using automake as well, so it
# delegates installation of that to automake!  This is why we ignore its
# errors: it fails because configure.ac doesn't refer to AM_* and there
# is no Makefile.am file, but it does still copy over the missing files.
if [ -n "$success" ]
then
    # autoreconf from old Autoconf doesn't pass -I to aclocal properly.
    export ACLOCAL="aclocal -I config -I '${BAKEFILE_M4}'"
	rm -f config.cache
	mv autoconf_inc.m4 config > /dev/null 2>&1	# don't care if it fails
	set -x &&
        automake --add-missing --force-missing > /dev/null 2>&1 ;
		autoreconf -i &&
		./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

