# Configure for Makefile

set -eu

Check_DB() {
	Command="$1"
	Database="$2"

	set +eu
	set -- $(type $Command 2>/dev/null)
	if [ "$2" != "is" ] ; then
		HAVE_DB=0
	else
		HAVE_DB=1
	fi

	if [ $HAVE_DB -eq 0 ] ; then
		cat >/dev/tty <<EOF
-------------------------------------------------------------------------------
	WARNING: Unable to locate the $Command command.

	This suggests that you do not have $Database installed.

	Alternatively, you may need to alter your PATH variable to
	include $Database commands. Please check your PATH
	variable if you believe that $Database is installed on
	your system.
-------------------------------------------------------------------------------

EOF
		echo "Is it OK to build APQ without $Database support (Yes/No)?"
		read reply </dev/tty
		case "$reply" in
		Y* | y* )
			echo "Very well then. No $Database support it is."
			echo
			;;
		* )	echo "Configuration aborted. Please check your PATH" >/dev/tty
			echo "variable, and try again." >/dev/tty
			exit 13;;
		esac
	fi
	return $HAVE_DB
}

################################################################################

if [ -e ./GNUmakefile ] ; then
	if [ ! -h GNUmakefile ] ; then
		echo "YIKES! GNUmakefile is NOT a symlink!"
		echo "I was going to delete it!  You better"
		echo "move it somewhere else for safe keeping"
		echo "or delete it, and let me try again later."
		exit 1
	fi
	rm GNUmakefile
fi


#
# Determine platform
#
case "$(uname -s)" in
CYGWIN* )	WIN32=1
		ln -s Makefile.win32 GNUmakefile;;
Linux* )	WIN32=0
		PLATFORM=Linux
		ln -s Makefile GNUmakefile;;
* )		WIN32=0
		PLATFORM=UNIX
		ln -s Makefile GNUmakefile;;
esac

if [ $WIN32 -gt 0 ] ; then
	exec ./win32_config	# Run the win32 configuration instead
fi

echo
echo "THIS IS A $PLATFORM INSTALL:"
echo

#
# TEST FOR MISSING DATABASES
#
HAVE_PG="${HAVE_PG:-NULL}"
HAVE_MY="${HAVE_MY:-NULL}"

if [ "$HAVE_PG" = NULL ] ; then
	Check_DB pg_config PostgreSQL; 	HAVE_PG=$?
fi
if [ "$HAVE_MY" = NULL ] ; then
	Check_DB mysql_config MySQL;	HAVE_MY=$?
fi

set -eu

#
# TEST TO SEE IF WE HAVE ANY DATABASES TO WORK WITH
#
if [ $(expr $HAVE_PG + $HAVE_MY) -eq 0 ] ; then
	cat <<EOF >/dev/tty

------------------------------------------------------------------------------

	Based upon the configure script's findings, and your answers,
	there were no databases left to configure the APQ package
	to build for.

	Also check any exported environment variables like HAVE_PG/HAVE_MY.

	Consequently, the configuration of APQ will be abandoned.

	Please install your database software first, and then
	try building APQ again later.

------------------------------------------------------------------------------

EOF
	exit 13
fi

#
# PostgreSQL Config :
#
if [ $HAVE_PG -gt 0 ] ; then
	PG_INCL="-I`pg_config --includedir`"
	PG_LIBS="-L`pg_config --libdir` -lpg"
	PG_OBJS='apq-postgresql.o apq-postgresql-client.o apq-postgresql-decimal.o numeric.o notices.o'
else
	PG_INCL=
	PG_LIBS=
	PG_OBJS=
fi

#
# MySQL Config :
#
if [ $HAVE_MY -gt 0 ] ; then
	MY_INCL=`mysql_config --cflags`
	MY_LIBS=`mysql_config --libs`
	MY_OBJS='apq-mysql.o apq-mysql-client.o c_mysql.o'
else
	MY_INCL=
	MY_LIBS=
	MY_OBJS=
fi

#
# GENERATE MAKEINCL FILE
#

cat >Makeincl <<EOF
# Makeincl : Generated by ./configure on $(date)

VERSION=$(cat APQ_VERSION)

#
# PostgreSQL Config :
#
HAVE_PG=$HAVE_PG
PG_INCL=$PG_INCL
PG_LIBS=$PG_LIBS
PG_OBJS=$PG_OBJS

#
# MySQL Config :
#
HAVE_MY=$HAVE_MY
MY_INCL=$MY_INCL
MY_LIBS=$MY_LIBS
MY_OBJS=$MY_OBJS

# End

EOF

echo "   You are now ready to 'make'"
echo

# $Source: /home/cvsroot/bush/src/apq-2.1/configure,v $
