dnl ========================================
dnl checks for MySQL
dnl taken form KDEDB
dnl ========================================

AC_ARG_ENABLE(mysql,
	AC_HELP_STRING([--enable-mysql],[build MySQL-plugin [default=yes]]),
              mysql_plugin=$enableval, mysql_plugin=yes)

AC_ARG_WITH(mysql_includes,
AC_HELP_STRING([--with-mysql-includes=DIR],[use MySQL-includes installed in this directory]),
[
  ac_mysql_incdir=$withval
], ac_mysql_incdir=/usr/local/include
)

AC_ARG_WITH(mysql_libraries,
AC_HELP_STRING([--with-mysql-libraries=DIR],[use MySQL-libs installed in this directory ]),
[
  ac_mysql_libdir=$withval
], ac_mysql_libdir=/usr/local/lib
)

dnl ==============================================
dnl check for MySQL should be compiled
dnl and where headers and libraries are installed
dnl if present compile mysql-plugin
dnl ==============================================

AC_MSG_CHECKING([for MySQL])

compile_mysql_plugin="yes"

if test "x$mysql_plugin" != "xyes"; then
  compile_mysql_plugin="no"
fi

KDE_FIND_PATH(mysql_config, MYSQL_CONFIG, [${prefix}/bin ${exec_prefix}/bin /usr/local/bin /opt/local/b\
in /usr/bin /opt/mysql/bin /usr/local/mysql/bin ], [
  AC_MSG_WARN([Could not find MySQL anywhere, check http://www.mysql.com/])
])

if test -n "$MYSQL_CONFIG"; then
	mysql_incdir=`$MYSQL_CONFIG --cflags| $SED -e "s,-I,,g" | cut -d " " -f 1`
	mysql_libdir=`$MYSQL_CONFIG --libs| $SED -e "s,',,g"`
	MYSQL_INC=$mysql_incdir
	MYSQL_LIBS=$mysql_libdir
	AC_SUBST(MYSQL_INC)
	AC_SUBST(MYSQL_LIBS)
else
	compile_mysql_plugin="no"
fi

if test "$compile_mysql_plugin" = "yes"; then
  AC_MSG_RESULT([libraries $mysql_libdir, headers $mysql_incdir])
else
  AC_MSG_RESULT(not found)
fi

AM_CONDITIONAL(compile_mysql_plugin, test "$compile_mysql_plugin" = "yes")

dnl ========================================
dnl checks for PostgreSQL
dnl ========================================

dnl add configure-args
dnl ========================================

AC_ARG_ENABLE(pgsql,
AC_HELP_STRING([--enable-pgsql],[build PostgreSQL-plugin [default=yes]]),
 pgsql_plugin=$enableval, pgsql_plugin=yes)

dnl ========================================
dnl Libpq header/library directories
dnl ========================================

AC_ARG_WITH(pgsqlincdir,
AC_HELP_STRING([--with-pgsqlincdir=DIR],[use PostgreSQL(libpq)-includes installed in this directory ]),
[
  ac_pgsql_incdir=$withval
], ac_pgsql_incdir=/usr/local/include
)

AC_ARG_WITH(pgsqllibdir,
AC_HELP_STRING([  --with-pgsqllibdir=DIR],[use PostgreSQL(libpq)-libraries installed in this directory ]),
[
  ac_pgsql_libdir=$withval
], ac_pgsql_libdir=/usr/local/lib
)

dnl ========================================
dnl Libpqxx header/library directories
dnl ========================================

AC_ARG_WITH(pqxxincdir,
AC_HELP_STRING([--with-pqxxincdir=DIR],[use PostgreSQL(libpqxx)-includes installed in this directory ]),
[
  ac_pqxx_incdir=$withval
], ac_pqxx_incdir=/usr/local/pqxx/include/pqxx
)

AC_ARG_WITH(pqxxlibdir,
AC_HELP_STRING([--with-pqxxlibdir=DIR],[use PostgreSQL(libpqxx)-libraries installed in this directory ]),
[
  ac_pqxx_libdir=$withval
], ac_pqxx_libdir=/usr/local/lib
)

dnl ========================================
dnl Check if PostgreSQL should be compiled
dnl and where headers and libraries are installed
dnl if present compile postgresql-plugin
dnl ========================================


compile_pgsql_plugin="yes"

if test "x$pgsql_plugin" != "xyes"; then
  compile_pgsql_plugin="no"
fi

dnl ========================================
dnl First check for libpq headers
dnl ========================================
if test "$compile_pgsql_plugin" = "yes"; then
  AC_MSG_CHECKING([for PostgreSQL... Step 1 of 4])
  pgsql_incdirs="$ac_pgsql_incdir /usr/include /usr/include/pgsql /usr/include/postgresql /usr/local/include /usr/local/pgsql/include /opt/postgres/include"
  AC_FIND_FILE(libpq-fe.h, $pgsql_incdirs, pgsql_incdir)
  if test ! -r $pgsql_incdir/libpq-fe.h; then
    compile_pgsql_plugin="no"
    AC_MSG_RESULT([not found])
  else
    PG_INCDIR=$pgsql_incdir
    AC_MSG_RESULT([$PG_INCDIR])
    AC_SUBST(PG_INCDIR)
  fi
fi

dnl ========================================
dnl Next check for libpq libraries
dnl ========================================
if test "$compile_pgsql_plugin" = "yes"; then
  AC_MSG_CHECKING([for PostgreSQL... Step 2 of 4])
  pgsql_libdirs="$ac_pgsql_libdir /usr/lib /usr/local/lib /usr/local/pgsql/lib /opt/postgres/lib"
  AC_FIND_FILE(libpq.so, $pgsql_libdirs, pgsql_libdir)
  if test ! -r $pgsql_libdir/libpq.so; then
    compile_pgsql_plugin="no"
    AC_MSG_RESULT([not found])
  else
    PG_LIBDIR=$pgsql_libdir
    AC_MSG_RESULT([$PG_LIBDIR])
    AC_SUBST(PG_LIBDIR)
  fi
fi


dnl ========================================
dnl Next check for libpqxx headers
dnl ========================================
if test "$compile_pgsql_plugin" = "yes"; then
  AC_MSG_CHECKING([for PostgreSQL... Step 3 of 4])
  pqxx_incdirs="$ac_pqxx_incdir /usr/include /usr/local/pqxx/include /usr/local/include"
  AC_FIND_FILE(pqxx/pqxx, $pqxx_incdirs, pqxx_incdir)
  if test ! -r $pqxx_incdir/pqxx/pqxx; then
    compile_pgsql_plugin="no"
    AC_MSG_RESULT([not found])
  else
    PQXX_INCDIR=$pqxx_incdir
    AC_MSG_RESULT([$PQXX_INCDIR])
    AC_SUBST(PQXX_INCDIR)
  fi
fi

dnl ========================================
dnl Finally check for libpqxx libraries
dnl ========================================
if test "$compile_pgsql_plugin" = "yes"; then
  AC_MSG_CHECKING([for PostgreSQL... Step 4 of 4])
  pqxx_libdirs="$ac_pqxx_libdir /usr/lib /usr/local/lib /usr/local/pqxx/lib"
  AC_FIND_FILE(libpqxx.so, $pqxx_libdirs, pqxx_libdir)
  if test ! -r $pqxx_libdir/libpqxx.so; then
    compile_pgsql_plugin="no"
    AC_MSG_RESULT([not found])
  else
    PQXX_LIBDIR=$pqxx_libdir
    AC_MSG_RESULT([$PQXX_LIBDIR])
    AC_SUBST(PQXX_LIBDIR)
  fi
fi

AM_CONDITIONAL(compile_pgsql_plugin, test "$compile_pgsql_plugin" = "yes")
