MYDDAS_VERSION=0.9.2
AC_DEFINE_UNQUOTED(MYDDAS_VERSION, $MYDDAS_VERSION, [myddas release])

OLD_LIBS="$LIBS"
OLD_CPPFLAGS="$CPPFLAGS"
MYDDAS_CPPFLAGS=""

AC_ARG_ENABLE(myddas,
	[ --enable-myddas[[=DIR]]      with the MYDDAS library],
	if test "$withval" = yes; then
		yap_cv_myddas=/usr
	elif test "$withval" = no; then
		yap_cv_myddas=no
	else
		yap_cv_myddas=$withval
		LDFLAGS="$LDFLAGS -L${yap_cv_myddas}/lib "
		CPPFLAGS="$CPPFLAGS -I${yap_cv_myddas}/include "
		MYDDAS_CPPFLAGS="$MYDDAS_CPPFLAGS -I${yap_cv_myddas}/include "
fi,
	[yap_cv_myddas=/usr])

AC_ARG_WITH(mysql,
	[ --with-mysql[[=DIR]]      with the MYSQL library],
	if test "$withval" = yes; then
		yap_cv_mysql=/usr
	elif test "$withval" = no; then
		yap_cv_mysql=no
	else
		yap_cv_mysql=$withval
		LDFLAGS="$LDFLAGS -L${yap_cv_mysql}/lib "
		CPPFLAGS="$CPPFLAGS -I${yap_cv_mysql}/include "
		MYDDAS_CPPFLAGS="$MYDDAS_CPPFLAGS -I${yap_cv_mysql}/include "
	fi,
	[yap_cv_mysql=/usr])

AC_ARG_WITH(odbc,
	[ --with-odbc[[=DIR]]      with an ODBC library],
	if test "$withval" = yes; then
		yap_cv_odbc=/usr
	elif test "$withval" = no; then
		yap_cv_odbc=no
	else
		yap_cv_odbc=$withval
		LDFLAGS="$LDFLAGS -L${yap_cv_odbc}/lib "
		CPPFLAGS="$CPPFLAGS -I${yap_cv_odbc}/include "
		MYDDAS_CPPFLAGS="$MYDDAS_CPPFLAGS -I${yap_cv_odbc}/include "
	fi,
	[yap_cv_odbc=/usr])

AC_ARG_WITH(sqlite3,
	[ --with-sqlite3[[=DIR]]      with an SQLITE3 library],
	if test "$withval" = yes; then
		yap_cv_sqlite3=/usr
	elif test "$withval" = no; then
		yap_cv_sqlite3=no
	else
		yap_cv_sqlite3=$withval
		LDFLAGS="$LDFLAGS -L${yap_cv_sqlite3}/lib "
		CPPFLAGS="$CPPFLAGS -I${yap_cv_sqlite3}/include "
		MYDDAS_CPPFLAGS="$MYDDAS_CPPFLAGS -I${yap_cv_sqlite3}/include "
	fi,
	[yap_cv_sqlite3=/usr])

AC_ARG_WITH(postgres,
	[ --with-postgres[[=DIR]]      with an POSTGRES library],
	if test "$withval" = yes; then
		yap_cv_postgres=/usr
	elif test "$withval" = no; then
		yap_cv_postgres=no
	else
		yap_cv_postgres=$withval
		LDFLAGS="$LDFLAGS -L${yap_cv_postgres}/lib "
		CPPFLAGS="$CPPFLAGS -I${yap_cv_postgres}/include "
		MYDDAS_CPPFLAGS="$MYDDAS_CPPFLAGS -I${yap_cv_postgres}/include "
	fi,
	[yap_cv_postgres=/usr])

AC_ARG_ENABLE(myddas-stats,
	[ --enable-myddas-stats      enable the MYDDAS library statistics support],
	if test "$yap_cv_myddas" = no; then
		myddasstats=no
	else
		myddasstats="$enableval"
	fi, myddasstats=no)

AC_ARG_ENABLE(myddas-top-level,
	[ --enable-myddas-top-level  enable the MYDDAS top-level support to MySQL],
	if test "$yap_cv_myddas" = no; then
	   	myddastoplevel=no
	else
		myddastoplevel="$enableval"
	fi, myddastoplevel=no)

if test "$yap_cv_myddas" != "no"
then
	PKG_MYDDAS="packages/myddas"
	dnl check for mysql
	AC_SEARCH_LIBS(mysql_init, [mysqlclient], [yap_cv_mysql="yes" MYDDAS_LIBS="-lmysqlclient $MYDDAS_LIBS"], [yap_cv_mysql="no"])
	AC_CHECK_HEADERS(mysql/mysql.h, [], [yap_cv_mysql="no"])
	if test "$yap_cv_mysql" = yes
	then
 	    use_myddas=true
        YAP_EXTRAS="$YAP_EXTRAS -DMYDDAS_MYSQL"
	fi

	dnl check for odbc
        case "$target_os" in
	  *darwin*)
	    LIBS="$LIBS -framework CoreFoundation"
	;;
	  **)
	;;
	esac
	AC_SEARCH_LIBS(SQLAllocHandle, [odbc], [yap_cv_odbc="yes"], [yap_cv_odbc="no"])
	if test "$yap_cv_odbc" != no
	then
	   LIBS="$LIBS -lodbc"
	else
	  AC_SEARCH_LIBS(SQLAllocHandle, [iodbc], [yap_cv_odbc="yes"], [yap_cv_odbc="no"])
	  if test "$yap_cv_odbc" != no
	  then
	     LIBS="$LIBS -liodbc"
  	  else
	    AC_SEARCH_LIBS(SQLAllocHandle, [odbc32], [yap_cv_odbc="yes" ], [yap_cv_odbc="no"])
	    if test "$yap_cv_odbc" != no
	    then
	      LIBS="$LIBS -lodbc32"
	    fi
	  fi
        fi
	#from SWI ODBC library.
	AC_CHECK_HEADERS(windef.h)
	AC_CHECK_HEADERS(sql.h, [yap_cv_odbc="yes" ], [yap_cv_odbc="no"])
	if test "$yap_cv_odbc" != no
	then
	    use_myddas=true
		YAP_EXTRAS="$YAP_EXTRAS -DMYDDAS_ODBC"
	fi

	#from SWI PROSQLITE library.
	havelib=no
	AC_CHECK_LIB(sqlite3, sqlite3_open,
                       LIBS="-lsqlite3 $LIBS"
                       havelib=yes)
	if test "$havelib" = no
	then
	AC_CHECK_LIB(sqliteX, sqlite3_open, # android
                       LIBS="-lsqliteX $LIBS"
                       havelib=yes)
	fi
	if test "$havelib" = no
	then
		yap_cv_sqlite3=no
	else
		AC_CHECK_HEADERS(sqlite3.h, [], [yap_cv_sqlite3="no"])
	fi
	if test "$yap_cv_sqlite3" != no
	then
	    use_myddas=true
		YAP_EXTRAS="$YAP_EXTRAS -DMYDDAS_SQLITE3"
	fi

	havelib=no
	AC_CHECK_LIB(pq, PQconnectdbParams,
                       LIBS="-lpq $LIBS"
                       havelib=yes)

        AC_CHECK_HEADERS(libpq-fe.h, [], [yap_cv_postgres="no"])
	if test "$yap_cv_postgres" != no
	then
	    use_myddas=true
		YAP_EXTRAS="$YAP_EXTRAS -DMYDDAS_POSTGRES"
	fi


	if test "$myddasstats" = "yes"
	  then
	    YAP_EXTRAS="$YAP_EXTRAS -DMYDDAS_STATS"
	fi

	if test "$myddastoplevel" = "yes"
	  then
	    YAP_EXTRAS="$YAP_EXTRAS -DMYDDAS_TOP_LEVEL"
	fi

	if test "$use_myddas" = true; then
		YAP_EXTRAS="$YAP_EXTRAS -DUSE_MYDDAS=1"
	fi


	MYDDAS_LIBS="$LIBS"
	LIBS="$OLD_LIBS"
	CPPFLAGS="$OLD_CPPFLAGS"
else
	ENABLE_MYDDAS=""
fi

OBJECTS_MYDDAS="#"
if test x"$ENABLE_MYDDAS" = x -a x"$STARTUP_ANDROID" = x
then
#in Android we have to actually include myddas in the binary
	OBJECTS_MYDDAS="$ALL_MYDDAS_OBJECTS"
fi

AC_SUBST(PKG_MYDDAS)
AC_SUBST(OBJECTS_MYDDAS)
AC_SUBST(MYDDAS_CPPFLAGS)
AC_SUBST(MYDDAS_LIBS)