dnl @(#) $Id: configure.ac,v 1.34 2003/09/04 22:57:29 vern Exp $ (LBL)
dnl
dnl Copyright (c) 1997, 1998, 2001, 2002
dnl	The Regents of the University of California.  All rights reserved.
dnl
dnl Process this file with autoconf to produce a configure script.
dnl

AC_INIT(Active.cc)
AC_CONFIG_HEADER(config.h)
AC_CANONICAL_SYSTEM

AC_LBL_C_INIT(V_CCOPT, V_INCLS)

AC_ARG_ENABLE(brov6,
    [  --enable-brov6          enable IPV6 processing],
    AC_DEFINE(BROv6,,[enable IPV6 processing]))
AC_ARG_ENABLE(activemapping,
    [  --enable-activemapping  enable active mapping processing],
    AC_DEFINE(ACTIVE_MAPPING,,[Enable active mapping processing]))
AC_ARG_ENABLE(expire-dfa-states,
    [  --enable-expire-dfa-states enable DFA state expiration],
    AC_DEFINE(EXPIRE_DFA_STATES,,[Enable DFA state expiration]))

AC_ARG_ENABLE(debug,
    [  --enable-debug          no compiler optimizations],
    V_CCOPT="")

AC_ARG_WITH(mpatrol,
    [  --with-mpatrol[=PATH]   enable mpatrol (for debugging only)],
    if test -x "$withval" ; then
        MPATROL="$withval"
    else
        MPATROL="$prefix"
    fi
    LDFLAGS="-L${MPATROL}/lib -L/usr/local/lib ${LDFLAGS}"
    V_INCLS="${V_INCLS} -I${MPATROL}/include"
    AC_DEFINE(USE_MPATROL,,[enable mpatrol (for debugging only)])
    AC_CHECK_LIB(elf, elf_begin,,AC_MSG_ERROR([Unable to find elf library]))
    AC_CHECK_LIB(mpatrol, __mp_startleaktable,,AC_MSG_ERROR([Unable to find mpatrol library]))
    LDFLAGS="${MPATROL}/lib/libmptools.a ${LDFLAGS}"
	CFLAGS="${CFLAGS} -g"
    )

AC_LBL_ENABLE_CHECK(brov6 activemapping expire-dfa-states mpatrol debug)

case "$target_os" in

linux*)
	AC_MSG_CHECKING(Linux kernel version)
	AC_CACHE_VAL(ac_cv_linux_vers,
	    ac_cv_linux_vers=`uname -r 2>&1 | \
		sed -n -e '$s/.* //' -e '$s/\..*//p'`)
	AC_MSG_RESULT($ac_cv_linux_vers)
	if test $ac_cv_linux_vers -lt 2 ; then
		AC_MSG_ERROR(version 2 or higher required; see the INSTALL doc for more info)
	fi
	;;

esac

dnl Checks for programs.
AC_PROG_YACC
AC_PROG_CXX
AC_PROG_LEX
AC_PROG_INSTALL
AC_PROG_MAKE_SET
AC_CHECK_PROGS(COMPRESS, gzip, compress)


AC_LBL_DEVEL(V_CCOPT)

case "$target_os" in

linux*)
	V_INCLS="$V_INCLS -Ilinux-include"
	;;

osf*)
	dnl Workaround around ip_hl vs. ip_vhl problem in netinet/ip.h
	V_CCOPT="$V_CCOPT -D__STDC__=2"
	;;

esac

dnl Checks for header files.
AC_HEADER_STDC
AC_LBL_TYPE_SIGNAL
AC_LBL_CHECK_TYPE(int32_t, int)
AC_LBL_CHECK_TYPE(u_int32_t, u_int)
AC_LBL_CHECK_TYPE(u_int16_t, u_short)
AC_HEADER_TIME

AC_CHECK_HEADERS(memory.h)

AC_CHECK_HEADERS(netinet/ip6.h,,,
    [#include <sys/types.h>
    #include <netinet/in.h>])

AC_BRO_SYSLOG_INT
AC_BRO_SOCK_DECL

dnl Checks for library functions.
AC_LBL_LIBPCAP(V_PCAPDEP, V_INCLS)
AC_CHECK_FUNCS(bpf_set_bufsize)
AC_FUNC_MEMCMP
AC_FUNC_STRFTIME
AC_CHECK_FUNCS(strerror)
AC_REPLACE_FUNCS(strsep)

dnl Check if ns_initparse and friends do work with -lresolv
AC_CHECK_LIB(resolv, ns_initparse, bro_ns_foo_works=yes)
if test "x$bro_ns_foo_works" = x; then
   dnl Didn't work -- need to check manually if workaround works better
   AC_MSG_CHECKING([for ns_initparse in /usr/lib/libresolv.a])
   saved_libs="${LIBS}"
   LIBS="${LIBS} /usr/lib/libresolv.a"
   AC_LINK_IFELSE(AC_LANG_PROGRAM([[#include <arpa/nameser.h>]], [[ns_initparse(0,0,0);]]), bro_ns_foo_static_works=yes)

   if test "x$bro_ns_foo_static_works" = x; then
      LIBS="${saved_libs}"
      AC_MSG_RESULT(no)
   else
      AC_MSG_RESULT(yes)
   fi
fi


AC_DEFINE(HAVE_READLINE,,[hacked version of libedit])

AC_C_BIGENDIAN

dnl
dnl First see if res_mkquery() is in libc; if not, check -lresolv
dnl If all else fails, try for -lbind
dnl
bro_have_res_mkquery=no
AC_CHECK_FUNCS(res_mkquery)
if test $ac_cv_func_res_mkquery = no ; then
	saved_libs="${LIBS}"
	AC_CHECK_LIB(resolv, res_mkquery)
	if test $ac_cv_lib_resolv_res_mkquery = yes ; then
		bro_have_res_mkquery=yes
	else
		LIBS="${saved_libs}"
		d="/usr/local/bind"
		if test -d ${d} ; then
			AC_MSG_CHECKING(for res_mkquery in ${d}'s -lbind)

			saved_cflags="${CFLAGS}"
			saved_ldflags="${LDFLAGS}"
			CFLAGS="${CFLAGS} -I${d}/include"
			LDFLAGS="${LDFLAGS} -L${d}/lib"
			LIBS="-lbind ${LIBS}"
			AC_TRY_LINK([
#			    include <sys/types.h>
#			    include <sys/socket.h>
#			    include <netinet/in.h>
#			    include <arpa/nameser.h>
#			    include <resolv.h>],
			[int (*p)() = res_mkquery],
			ac_cv_lib_bind_res_mkquery=yes,
			ac_cv_lib_bind_res_mkquery=no)
			AC_MSG_RESULT($ac_cv_lib_bind_res_mkquery)
			if test $ac_cv_lib_bind_res_mkquery = yes ; then
				bro_have_res_mkquery=yes
			else
				LIBS="${saved_libs}"
				CFLAGS="${saved_cflags}"
				LDFLAGS="${saved_ldflags}"

			fi
		fi
		if test $bro_have_res_mkquery = no ; then
			dnl last chance
			AC_LBL_MKQUERY()
			if test $ac_cv_lbl_mkquery = yes ; then
				bro_have_res_mkquery=yes
				LIBS="-lresolv ${LIBS}"
			fi
		fi
	fi
else
	bro_have_res_mkquery=yes
fi
if test $bro_have_res_mkquery = yes ; then
	AC_LIBOBJ(nb_dns)
	AC_DEFINE(HAVE_NB_DNS,,[async dns support])
else
	AC_MSG_ERROR(Missing required support (res_mkquery) for nb_dns.c module)
fi

AC_SUBST(V_CCOPT)
AC_SUBST(V_INCLS)
AC_SUBST(LDFLAGS)
dnl AC_SUBST(V_PCAPDEP) dnl (libpcap dependancies -- not used)
AC_OUTPUT(Makefile aux/cf/Makefile)

exit 0
