dnl Process this file with autoconf to produce a configure script.
AC_INIT(main.c)
AC_CONFIG_HEADER(config.h)

dnl Checks for programs.
AC_PROG_CC

dnl Checks for header files.
AC_HEADER_STDC
AC_CHECK_HEADERS(limits.h syslog.h unistd.h)

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

AC_CANONICAL_SYSTEM
dnl AC_CANONICAL_HOST

dnl This is to set the port dir for bind
case "$target_os" in
   linux*)
	port_type=linux;;
   freebsd*)
	port_type=freebsd;;
   netbsd*)
	port_type=netbsd;;
   openbsd*)
	port_type=openbsd;;
   solaris*)
	port_type=solaris;;
esac   

dnl Thanks to the php guys for this bit of detection (www.php.net)
AC_MSG_CHECKING(for MySQL support)
AC_ARG_WITH(mysql,
[  --with-mysql[=DIR]	include Mysql support.],
[
   if test "$withval" != "no"; then
	if test "$withval" = "yes"; then
	   if test -f /usr/include/mysql/mysql.h; then
		MYSQL_INCDIR=/usr/include/mysql
		MYSQL_LIBDIR=/usr/lib/mysql
	   elif test -f /usr/include/mysql.h; then
		MYSQL_INCDIR=/usr/include
		MYSQL_LIBDIR=/usr/lib
	   elif test -f /usr/local/include/mysql/mysql.h; then
		MYSQL_INCDIR=/usr/local/include/mysql
		MYSQL_LIBDIR=/usr/local/lib/mysql
	   elif test -f /usr/local/include/mysql.h; then
		MYSQL_INCDIR=/usr/local/include
		MYSQL_LIBDIR=/usr/local/lib
	   else
	   AC_MSG_RESULT(no)
	   AC_MSG_ERROR(Can't find MySQL headers)
	   fi
	else 
	   if test -f $withval/include/mysql/mysql.h; then
		MYSQL_INCDIR=$withval/include/mysql
		MYSQL_LIBDIR=$withval/lib/mysql
	   elif test -f $withval/include/mysql.h; then
		MYSQL_INCDIR=$withval/include
		MYSQL_LIBDIR=$withval/lib
	   else
	   AC_MSG_RESULT(no)
	   AC_MSG_ERROR(Can't find MySQL headers in $withval)
	   fi
	fi
   MYSQL_INCLUDE=-I$MYSQL_INCDIR
   MYSQL_LFLAGS="-L$MYSQL_LIBDIR"
   MYSQL_LIBS=-lmysqlclient

   AC_DEFINE(HAVE_MYSQL)
   AC_MSG_RESULT(yes)

   old_CPPFLAGS="$CPPFLAGS"
   CPPFLAGS="$CPPFLAGS $MYSQL_INCLUDE"
   AC_CHECK_HEADERS(errmsg.h mysql.h)
   CPPFLAGS="$old_CPPFLAGS"
   else
	AC_MSG_RESULT(no)
   fi
],[
   AC_MSG_RESULT(no)
])
AC_SUBST(MYSQL_LIBS)
AC_SUBST(MYSQL_LFLAGS)
AC_SUBST(MYSQL_INCLUDE)

AC_MSG_CHECKING(for bind)
AC_ARG_WITH(bind,
[  --with-bind[=DIR]	include bind support.],
[
   if test "$withval" != "no"; then
	if test "$withval" = "yes"; then
	   if test -f /usr/lib/bind/libbind.a; then
		BIND_INCDIR=/usr/include/bind
		BIND_LIBDIR=/usr/lib/bind
	   elif test -f /usr/local/lib/libbind.a; then
		BIND_INCDIR=/usr/local/include/bind
		BIND_LIBDIR=/usr/local/lib/bind
	   elif test -f /usr/local/bind/lib/libbind.a; then
		BIND_INCDIR=/usr/local/bind/include
		BIND_LIBDIR=/usr/local/bind/lib
	   else
	   AC_MSG_RESULT(no)
	   AC_MSG_ERROR(Can't find bind libraries)
	   fi
	else 
	   if test -f $withval/lib/libbind.a; then
		BIND_INCDIR=$withval/include
		BIND_LIBDIR=$withval/lib
		PORT_DIR=$withval/port/$port_type/include
	   else
	   AC_MSG_RESULT(no)
	   AC_MSG_ERROR(Can't find bind libraries in $withval)
	   fi
	fi
   BIND_INCLUDE=-I$BIND_INCDIR
   BIND_LIBS=-lbind
   BIND_PORT=-I$PORT_DIR

   AC_DEFINE(HAVE_BIND)
   AC_MSG_RESULT(yes)

   AC_CHECK_HEADERS(resolv.h)
   BIND_LIBDIR="-L$BIND_LIBDIR"
   fi
],[
   AC_MSG_RESULT(no)
])
AC_SUBST(BIND_LIBS)
AC_SUBST(BIND_LIBDIR)
AC_SUBST(BIND_INCLUDE)
AC_SUBST(BIND_PORT)

AC_OUTPUT(Makefile)
