dnl Process this file with autoconf to produce a configure script.
AC_INIT(lib/zorp/zorp.h)
AM_INIT_AUTOMAKE(zorp, 1.4.0rc2)

AM_CONFIG_HEADER(zorpconfig.h)
AC_PREFIX_DEFAULT(/usr/local)

if test "x$prefix" = "xNONE"; then
	prefix=$ac_default_prefix
fi

# We want an absolute path to the source-dir.
case "$srcdir" in
    /*)
        top_srcdir=$srcdir
        ;;
    *)
        oldsrcdir="$srcdir"
        top_srcdir="`cd \"$srcdir\"; pwd`"
        ;;
esac

zorp_srcdir=$top_srcdir
AC_SUBST(zorp_srcdir)

AC_ARG_ENABLE(debug,
              [  --enable-debug       Enable debug information & messages (default: yes)],,
	      enable_debug=yes)

AC_ARG_ENABLE(trace,
              [  --enable-trace       Enable trace information & messages (default: no)],,
	      enable_trace=no)

AC_ARG_ENABLE(packet_trace,
              [  --enable-packet-trace  Enable trace information & messages (default: no)],,
	      enable_packet_trace=no)

AC_ARG_ENABLE(mem_trace,
              [  --enable-mem-trace   Enable memory trace (default: no)],,
	      enable_mem_trace=no)

AC_ARG_ENABLE(caps,
              [  --enable-caps        Enable using POSIX 1003.e capabilities (default: autodetect)],,
	      enable_caps=auto)

AC_ARG_ENABLE(dmalloc,
              [  --enable-dmalloc     Enable dmalloc memory debugger support],,
              enable_dmalloc=no)
              
AC_ARG_ENABLE(ip_options,
              [  --enable-ip-options  Enable handling IP options (EXPERIMENTAL)],,
              enable_ip_options=no)
              
AC_ARG_ENABLE(ipchains,
	      [  --enable-ipchains    Enable IPChains support],,
	      enable_ipchains=auto)

AC_ARG_ENABLE(netfilter,
              [  --enable-netfilter   Enable Linux kernel 2.4 netfilter support],,
              enable_netfilter=yes)

AC_ARG_ENABLE(ipfilter,
              [  --enable-ipfilter    Enable portable ipfilter support],,
              enable_ipfilter=no)
              
AC_ARG_ENABLE(residual-protection,
 	      [  --enable-residual-protection   Enable residual information protection by clearing all allocated memory (not working)],, 
 	      enable_residual_protection=no)

AC_ARG_ENABLE(conntrack,
              [  --enable-conntrack             Enable connection tracking for UDP based protocols],,
	      enable_conntrack=auto)

AC_ARG_WITH(python-headers,
 	      [  --with-python-headers=[path]   Python headers are located at path],
 	      CPPFLAGS="$CPPFLAGS -I$with_python_headers",
 	      CPPFLAGS="$CPPFLAGS -I/usr/include/python1.5")

AC_ARG_WITH(python-libs,
 	      [  --with-python-libs=[path]   Python libraries are located at path],
 	      LDFLAGS="$LDFLAGS -L$with_python_libs")

AC_SUBST(enable_trace)
AC_SUBST(enable_packet_trace)
AC_SUBST(enable_mem_trace)
AC_SUBST(enable_debug)
AC_SUBST(enable_dmalloc)
AC_SUBST(enable_residual_protection)
AC_SUBST(enable_conntrack)
AC_SUBST(enable_ipchains)
AC_SUBST(enable_ipfilter)

sysconfdir="${sysconfdir}/zorp"
#datadir='${prefix}/share/zorp'
libdir='${prefix}/lib'


if test "x$enable_debug" = "xyes"; then
  CFLAGS="-g "
  AC_DEFINE(ENABLE_DEBUG)
else
  CFLAGS="-O2 "
fi

if test "x$enable_trace" = "xyes"; then
  AC_DEFINE(ENABLE_TRACE)
fi

if test "x$enable_packet_trace" = "xyes"; then
  AC_DEFINE(ENABLE_PACKET_TRACE)
fi

if test "x$enable_mem_trace" = "xyes"; then
  AC_DEFINE(ENABLE_MEM_TRACE)
fi

if test "x$enable_ip_options" = "xyes"; then
  AC_DEFINE(ENABLE_IPOPTIONS)
fi

if test "x$enable_netfilter" = "xyes"; then
  AC_DEFINE(ENABLE_NETFILTER)
fi


dnl Checks for programs.
AC_PROG_CC
AC_PROG_CPP
AC_DISABLE_STATIC
AM_PROG_LIBTOOL
AC_PATH_PROG(path_python, python, none)

if test "x$path_python" = "xnone"; then
	AC_ERROR(python interpreter required)
fi

if ! ${path_python} -c 'import ExtensionClass' > /dev/null 2> /dev/null; then
	AC_ERROR(python extension class required)
fi

dnl Checks for libraries.

dnl Checks for header files.
AC_CHECK_HEADERS(sys/capability.h sys/prctl.h getopt.h)

dnl Checks for typedefs, structures, and compiler characteristics.
AC_CACHE_CHECK(for MSG_PROXY, blb_cv_msg_proxy,
  [AC_EGREP_CPP(MSG_PROXY, 
[
#include <sys/types.h>
#include <sys/socket.h>

#ifdef MSG_PROXY
MSG_PROXY
#endif
],
  blb_cv_msg_proxy=yes,
  blb_cv_msg_proxy=no)])
  
if test "x$blb_cv_msg_proxy" = "xyes"; then
	AC_DEFINE(HAVE_MSG_PROXY)
else
	AC_MSG_RESULT([MSG_PROXY is not defined in your header files, CONNTRACK will be disabled])
fi

AC_CACHE_CHECK(for PR_SET_KEEPCAPS, blb_cv_keepcaps,
  [AC_EGREP_CPP(PR_SET_KEEPCAPS, 
[
#include <sys/prctl.h>

PR_SET_KEEPCAPS
],
  blb_cv_keepcaps=no,
  blb_cv_keepcaps=yes)])
  
dnl Checks for library functions.
AC_CHECK_FUNCS(socket)
AC_CHECK_FUNCS(select snprintf vsnprintf strerror inet_aton strncpy)
AC_CHECK_FUNCS(prctl getopt_long)
AC_CHECK_LIB(xnet, inet_addr)
AC_CHECK_LIB(nsl, gethostbyname)
AC_CHECK_LIB(socket, setsockopt)
AC_CHECK_LIB(m, sin)
AC_CHECK_LIB(cap, cap_set_proc)
AC_CHECK_LIB(crypto, des_ecb_encrypt)
AC_CHECK_LIB(ssl, SSL_accept)
AC_CHECK_LIB(pthread, pthread_create)
AC_CHECK_LIB(dl, dlopen)
AC_CHECK_LIB(python2.1, Py_Finalize, ZORP_LIBS="$ZORP_LIBS -lpython2.1",
	AC_CHECK_LIB(python1.5, Py_Finalize, ZORP_LIBS="$ZORP_LIBS -lpython1.5"))

if test "x$ac_cv_lib_python1_5_Py_Finalize" = "xno"; then
	unset ac_cv_lib_python1_5_Py_Finalize
	AC_MSG_CHECKING(for Python libs in /usr/lib/python1.5/config)
	save_LDFLAGS=$LDFLAGS
	LDFLAGS="$LDFLAGS "
	AC_CHECK_LIB(python1.5, Py_Finalize, ZORP_LIBS="$ZORP_LIBS -L/usr/lib/python1.5/config -lpython1.5")
	LDFLAGS=$save_LDFLAGS
fi

if test "x$ac_cv_lib_crypto_des_ecb_encrypt" != "xyes" -o "x$ac_cv_lib_ssl_SSL_accept" != "xyes"; then
        AC_ERROR(openssl 0.9.5a required)
fi

dnl if test "x$ac_cv_lib_curses_initscr" = "xno"; then
dnl 	AC_ERROR(curses or ncurses required)
dnl fi

AC_DEFINE_UNQUOTED(ZORP_SYSCONFDIR, "`eval echo $sysconfdir`")
AC_DEFINE_UNQUOTED(ZORP_DATADIR, "`eval echo $datadir/zorp`")
AC_DEFINE_UNQUOTED(ZORP_LIBDIR, "`eval echo $libdir/zorp`")

ZORP_PROC="`eval uname -m`"
ZORP_OS="`eval uname -s`"

AC_MSG_CHECKING(whether to enable stack dumping (only in i386 Linux))
if test "x$ZORP_PROC" = "xi386" -o "x$ZORP_PROC" = "xi486" -o "x$ZORP_PROC" = "xi586" -o "x$ZORP_PROC" = "xi686"; then
	if test "x$ZORP_OS" = "xLinux"; then
		AC_MSG_RESULT(yes)
		AC_DEFINE(ENABLE_STACKDUMP)
	else
		AC_MSG_RESULT(no)
	fi
else
	AC_MSG_RESULT(no)
fi

AC_SUBST(ZORP_PROC)
AC_SUBST(ZORP_OS)

CFLAGS="$CFLAGS -Wall -D_GNU_SOURCE"

if test "x$enable_dmalloc" = "xyes"; then
	CFLAGS="$CFLAGS -DUSE_DMALLOC"
	LIBS="$LIBS -ldmallocth"
fi

if test "x$enable_debug" = "xyes"; then
	LIBS="$LIBS -L/usr/lib/debug"
fi

if test "x$enable_residual_protection" = "xyes"; then
	CFLAGS="$CFLAGS -DRESIDUAL_PROTECTION"
fi

AC_MSG_CHECKING(whether to enable POSIX 1003.1e capability support)
if test "x$enable_caps" = "xauto" ; then
	if test "x$ac_cv_header_sys_capability_h" = "xyes" -a "x$ac_cv_lib_cap_cap_set_proc" = "xyes"; then
		AC_MSG_RESULT(yes)
		AC_DEFINE(ENABLE_CAPS)
	else
		AC_MSG_RESULT(no)
	fi
elif test "x$enable_caps" = "xyes"; then
	AC_MSG_RESULT(yes)
	AC_DEFINE(ENABLE_CAPS)
else
	AC_MSG_RESULT(no)
fi

AC_MSG_CHECKING(whether to enable connection tracking support)
if test "x$enable_conntrack" = "xauto"; then
	if test "x$blb_cv_msg_proxy" = "xyes"; then
		AC_MSG_RESULT(yes)
		AC_DEFINE(ENABLE_CONNTRACK)
	else
		AC_MSG_RESULT(no)
	fi
elif test "x$enable_conntrack" = "xyes"; then
	AC_MSG_RESULT(yes)
	AC_DEFINE(ENABLE_CONNTRACK)
else
	AC_MSG_RESULT(no)
fi

AC_MSG_CHECKING(whether to enable Linux IPChains support)
if test "x$enable_ipchains" = "xauto" -o "x$enable_ipchains" = "xyes"; then
	AC_MSG_RESULT(yes)
	AC_DEFINE(ENABLE_IPCHAINS)
else
	AC_MSG_RESULT(no)
fi

AC_MSG_CHECKING(whether to enable IPFilter support)
if test "x$enable_ipfilter" = "xyes"; then
	AC_MSG_RESULT(yes)
	AC_DEFINE(ENABLE_IPFILTER)
else
	AC_MSG_RESULT(no)
fi


DEPS_CPPFLAGS="$CPPFLAGS `glib-config --cflags gthread gmodule`"
CPPFLAGS="-I$top_srcdir/lib -I$top_srcdir $DEPS_CPPFLAGS"
DEPS_LIBS="$LIBS `glib-config --libs gthread gmodule`"
LIBS="$ZORP_LIBS $DEPS_LIBS"

AC_SUBST(DEPS_CPPFLAGS)
AC_SUBST(DEPS_LIBS)
AC_SUBST(ZORP_LIBS)

date=`date +%Y-%m-%d`
AC_DEFINE_UNQUOTED(ZORP_CONFIG_DATE, "$date")

if test -r module.list; then
	MODULE_DIRS="`cat module.list`"
else
	MODULE_DIRS=""
fi

AC_SUBST(MODULE_DIRS)

AC_OUTPUT(
zorp-config
scripts/zorpctl

Makefile 
zorp/Makefile 
scripts/Makefile
lib/Makefile 
lib/zorp/Makefile 
pylib/Makefile 
pylib/Zorp/Makefile 
doc/Makefile 
doc/examples/Makefile
doc/man/Makefile
modules/Makefile
tests/Makefile 
tests/core/Makefile
tests/functional/Makefile
tests/functional/env_variables
tests/functional/plug/Makefile
tests/functional/plug/norm/Makefile
tests/python/Makefile
tests/python/runtest.sh
debian/Makefile,

chmod +x zorp-config
chmod +x scripts/zorpctl
chmod +x tests/python/runtest.sh
)
