AC_INIT(main.c) 
PWDD=`pwd`
VER=`cat $PWDD/VERSION`

AC_MSG_RESULT(beginning autoconfiguration process for dnsa-${VER})

AC_CANONICAL_SYSTEM
AC_PREFIX_DEFAULT("/usr/local")
AC_LANG_C

AM_INIT_AUTOMAKE(dnsa, $VER)
AC_PROG_CC
AC_PROG_INSTALL
AM_SANITY_CHECK

dnl Replace `main' with a function in -lnet:
AC_CHECK_LIB(net, main)

# Allow user to specify flags
# copy/paste from OpenSSH's configure.ac
AC_ARG_WITH(cflags,
	[  --with-cflags           Specify additional flags to pass to compiler],
	[
		if test "x$withval" != "xno" ; then
			CFLAGS="$CFLAGS $withval"
		fi
	]	
)
AC_ARG_WITH(cppflags,
	[  --with-cppflags         Specify additional flags to pass to preprocessor] ,
	[
		if test "x$withval" != "xno"; then
			CPPFLAGS="$CPPFLAGS $withval"
		fi
	]
)
AC_ARG_WITH(ldflags,
	[  --with-ldflags          Specify additional flags to pass to linker],
	[
		if test "x$withval" != "xno" ; then
			LDFLAGS="$LDFLAGS $withval"
		fi
	]	
)


dnl DEBUG MODE OR NOT
AC_MSG_CHECKING(if debug is requested)
AC_ARG_ENABLE(debug,
	      [  --enable-debug                 Compile dnsa with debug flag [default : no]],
	      WITH_DEBUG="${enableval}",WITH_DEBUG=no
	      )

if test "x${WITH_DEBUG}" = "xyes"; then
		AC_MSG_RESULT(yes)
		AC_DEFINE(DEBUG,1)
	else
		AC_MSG_RESULT(no)
	fi


	dnl checking for libnet
	AC_MSG_CHECKING(for libnet)
	AC_ARG_WITH(libnet,
		    [  --with-libnet=DIR       Use libnet build directory],
		    [ case "$withval" in
		     yes|no)
	AC_MSG_RESULT(no)
	;;
	*)
     if test -f $withval/include/libnet.h -a -f $withval/src/libnet.a; then
	     owd=$PWDD;
	     if cd $withval; then
		     withval=`pwd`; cd $owd;
	     else
		     AC_ERROR($withval does not exist)
		     fi
		     NETINC="$withval/include"
		     NETLIB="-L$withval/src -lnet"
		     AC_MSG_RESULT($withval)
		     else
			     AC_ERROR(libnet.h or libnet.a not found in $withval)
			     fi
			     ;;
		     esac ],
		     [ if test -f ${prefix}/include/libnet.h; then
		      NETINC="${prefix}/include"
		      NETLIB="-L${prefix}/lib -lnet"
	      elif test -f /usr/include/libnet/libnet.h; then
		      NETINC="/usr/include/libnet"
		      NETLIB="-lnet"
	      elif  test -f /usr/local/include/libnet.h; then
		      NETINC="/usr/local/include"
		      NETLIB="-L/usr/local/lib -lnet"
	      elif test -f /usr/include/libnet.h; then
		      dnl NETINC="/usr/include"
		      NETINC=
		      NETLIB="-lnet"
	      else
		      AC_MSG_RESULT(no)
		      AC_ERROR(libnet not found)
		      fi
		      AC_MSG_RESULT(yes) ]
		     )

dnl Now let's check the version of libnet (1.1.x requirerd)
AC_MSG_CHECKING(for version 1.1.x of libnet)
AC_TRY_RUN([
	    #include "$NETINC/libnet.h"
	    #include <string.h>
	    int main()
	    {
		    return (strncmp(LIBNET_VERSION, "1.1.", 4));
	    }
	    ],
	    [ AC_MSG_RESULT(yes)],
	    [  AC_MSG_RESULT(no)
	    AC_MSG_ERROR(Bad libnet version found in $NETINC/libnet.h:
			 download libnet-1.1.x from http://www.packetfactory.net/projects/libnet/. 
			 If you think this version of libnet is already installed on your system
			 tell dnsa where it is using --with-libnet=DIR)
	    ]
	    )

NETINC="-I$NETINC"
AC_SUBST(NETINC)
AC_SUBST(NETLIB)



dnl checking for libpcap
AC_MSG_CHECKING(for libpcap)
AC_ARG_WITH(libpcap,
	    [  --with-libpcap=DIR      Use libpcap build directory],
	    [ case "$withval" in
	     yes|no)
have_pcap="no"
AC_MSG_RESULT(no)
;;
*) 
if test -f $withval/include/pcap.h -a -f $withval/src/libpcap.a; then
	owd=$PWDD;
	if cd $withval; then
		withval=`pwd`; cd $owd;
	else
		AC_ERROR($withval does not exist)
		fi
		PCAPINC="-I$withval/include"
		PCAPLIB="-L$withval/src -lpcap"
		have_pcap="yes"
		AC_MSG_RESULT($withval)
		else
			AC_ERROR(libpcap.h or libpcap.a not found in $withval)
			fi
			;;
		esac ],
		[if test -f ${prefix}/include/libpcap.h; then
		 PCAPINC="-I${prefix}/include"
		 PCAPLIB="-L${prefix}/lib -lpcap"
	 elif test -f /usr/include/pcap/pcap.h; then
		 PCAPINC="-I/usr/include/pcap"
		 PCAPLIB="-lpcap"
	 elif  test -f /usr/local/include/pcap.h; then
		 PCAPINC="-I/usr/local/include"
		 PCAPLIB="-L/usr/local/lib -lpcap"
	 elif test -f /usr/include/pcap.h; then
		 dnl PCAPINC="-I/usr/include"
		 PCAPINC=
		 PCAPLIB="-lpcap"
	 else
		 have_pcap="no"
		 AC_MSG_RESULT(no)
		 dnl     AC_ERROR(libpcap not found)
		 fi
		 have_pcap="yes"
		 AC_MSG_RESULT(yes) ]
		)
AC_SUBST(PCAPINC)
AC_SUBST(PCAPLIB)



dnl Replace `main' with a function in -lpcap:
AC_CHECK_LIB(pcap, main)

dnl Checks for header files.
AC_HEADER_STDC

AC_CHECK_HEADERS([unistd.h string.h stdlib.h stdarg.h errno.h netinet/in.h arpa/inet.h netinet/if_ether.h netinet/ether.h sys/socket.h sys/types.h sys/stat.h])


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

dnl Checks for library functions.
AM_CONFIG_HEADER(config.h)
AC_OUTPUT(Makefile)

A=`eval echo ${prefix}` ; A=`eval echo ${A}`
C=`eval echo ${sbindir}` ; C=`eval echo ${C}`
F=`eval echo ${mandir}/man1` ; F=`eval echo ${F}`

echo ""
echo "dnsa-${VER} has been configured with the following options:"
echo ""

echo "   System binaries: $C"
echo "      Manual pages: $F"

echo ""

echo "              Host: ${host}"
echo "          Compiler: ${CC}"
echo "    Compiler flags: ${CFLAGS}"
echo "Preprocessor flags: ${CPPFLAGS}"
echo "      Linker flags: ${LDFLAGS}"
echo ""

echo "            libnet: ${NETINC} ${NETLIB}"
if test "${have_pcap}" = yes; then
	echo "           libpcap: ${PCAPINC} ${PCAPLIB}"
else
	echo "           libpcap: no (Download it from http://www.tcpdump.org and install it please)"
fi
echo ""
