dnl Process this file with autoconf to produce a configure script.
AC_INIT(sqlplusint/mysql++.hh)

# Don't forget to change this in mysql++.spec, too!
VERSION=1.7.18

AM_INIT_AUTOMAKE(mysql++, $VERSION)

AM_CONFIG_HEADER(config.h sqlplusint/exceptions.hh)

AC_PROG_MAKE_SET

AC_PROG_LIBTOOL

AM_MAINTAINER_MODE

dnl Should the standard C++ exception class be used as a base for our exceptions?
AC_ARG_ENABLE(exception,
[  --enable-exception      use standard C++ exception class as the base class
                          for mysql++ exceptions],
    [if test $enable_exception = yes; then AC_DEFINE(USE_STANDARD_EXCEPTION, 1, [Define if using Standard C++ exception classes]) fi])

#
# Location of MySQL installation
#
AC_ARG_WITH(mysql,
    [  --with-mysql=<path>     root directory path of MySQL installation],
    [MYSQL_lib_check="$with_mysql/lib/mysql $with_mysql/lib"
MYSQL_inc_check="$with_mysql/include/mysql"],
    [MYSQL_lib_check="/usr/local/mysql/lib/mysql /usr/local/lib/mysql /opt/mysql/lib/mysql /usr/lib/mysql /usr/local/mysql/lib /usr/local/lib /opt/mysql/lib /usr/lib"
MYSQL_inc_check="/usr/local/mysql/include/mysql /usr/local/include/mysql /opt/mysql/include/mysql /usr/include/mysql"])

AC_ARG_WITH(mysql-lib,
    [  --with-mysql-lib=<path> directory path of MySQL library installation],
    [MYSQL_lib_check=$with_mysql/lib/mysql])

AC_ARG_WITH(mysql-include,
    [  --with-mysql-include=<path>
                          directory path of MySQL header installation],
    [MYSQL_inc_check=$with_mysql/include/mysql])

AC_MSG_CHECKING([for MySQL library directory])
MYSQL_libdir=
for m in $MYSQL_lib_check; do
        if test -d "$m" && \
	   (test -f "$m/libmysqlclient.so" || test -f "$m/libmysqlclient.a")
        then
                MYSQL_libdir=$m
                break
        fi
done

if test -z "$MYSQL_libdir"; then
        AC_MSG_ERROR([Didn't find the mysql library dir in '$MYSQL_dir_check'])
fi

case "$MYSQL_libdir" in
  /* ) ;;
  * )  AC_MSG_ERROR([The MySQL library directory ($MYSQL_libdir) must be an absolute path.]) ;;
esac

AC_MSG_RESULT([$MYSQL_libdir])

case "$MYSQL_libdir" in
  /usr/lib) ;;
  *) LDFLAGS="$LDFLAGS -L${MYSQL_libdir}" ;;
esac

AC_MSG_CHECKING([for MySQL include directory])
MYSQL_incdir=
for m in $MYSQL_inc_check; do
        if test -d "$m" && test -f "$m/mysql.h"
        then
                MYSQL_incdir=$m
                break
        fi
done

if test -z "$MYSQL_incdir"; then
        AC_MSG_ERROR([Didn't find the mysql include dir in '$MYSQL_inc_check'])
fi

case "$MYSQL_incdir" in
  /* ) ;;
  * )  AC_MSG_ERROR([The MySQL include directory ($MYSQL_incdir) must be an absolute path.]) ;;
esac

AC_MSG_RESULT([$MYSQL_incdir])


#
# Check for zlib
#
AC_CHECK_HEADERS(zlib.h, AC_CHECK_LIB(z, gzread, [],
		[ AC_MSG_ERROR([zlib is required]) ]))


#
# ....simpler checks
#
CPPFLAGS="$CPPFLAGS -I${MYSQL_incdir}"

AC_PROG_CC
AC_PROG_CXX

AC_CHECK_LIB(intl, main)
AC_CHECK_LIB(socket, main)
AC_CHECK_LIB(nsl, main)
AC_CHECK_LIB(mysqlclient, mysql_store_result)
AM_WITH_DMALLOC()

dnl Checks for header files.
AC_HEADER_STDC

dnl Checks for typedefs, structures, and compiler characteristics.
AC_C_CONST

dnl Checks for library functions.
AC_CHECK_FUNCS(strtol)

dnl Check for 
AC_CACHE_CHECK([for level argument to mysql_shutdown()], ac_cv_mysql_shutdown_arg,
[
	AC_TRY_LINK(
		[ #include <mysql.h> ],
		[ mysql_shutdown(0, 0); ],
		ac_cv_mysql_shutdown_arg=yes,
		ac_cv_mysql_shutdown_arg=no)
])

if test x"$ac_cv_mysql_shutdown_arg" = "xyes"
then
	AC_DEFINE(HAVE_MYSQL_SHUTDOWN_LEVEL_ARG, 1, 
		[ Define if mysql_shutdown() has a second 'level' argument ])
fi


dnl Use C++ compiler for remaining tests
AC_LANG_CPLUSPLUS


#
# Look for STL slist extension.
#
AC_CHECK_HEADER([slist],
		[AC_DEFINE(HAVE_STD_SLIST, 1,
		[Define if <slist> exists on your system])], [])
AC_CHECK_HEADER([ext/slist],
		[AC_DEFINE(HAVE_EXT_SLIST, 1,
		[Define if <ext/slist> exists on your system])], [])


#
# Done with checks!
#
AC_OUTPUT(Makefile sqlplusint/Makefile examples/Makefile)
