dnl Process this file with autoconf to produce a configure script.
AC_INIT(lib/bit1024.c)

AC_PREFIX_DEFAULT(/usr/local/netflow)

AM_INIT_AUTOMAKE(flow-tools, 0.68)
AM_CONFIG_HEADER(lib/ftconfig.h:lib/ftconfig.h.in) 

CFLAGS="-g -Wall"

dnl Checks for programs.
AC_PROG_CC
AC_PROG_INSTALL
AC_PROG_MAKE_SET
AC_PROG_YACC
YACC="$YACC -d"
AC_PROG_RANLIB
AM_PROG_LEX

AC_SUBST(YLIB)
AC_SUBST(MYSQLLIB)
AC_SUBST(MYSQLCFLAGS)
AC_SUBST(MYSQLINCLUDE)
AC_SUBST(PGSQLLIB)
AC_SUBST(PGSQLCFLAGS)
AC_SUBST(PGSQLINCLUDE)
AC_SUBST(CRYPTOLIB)
AC_SUBST(OPENSSLINCLUDE)

dnl extra argument: --with-mysql
WITH_MYSQL=
AC_ARG_WITH(mysql,
[  --with-mysql[=PATH]           Compile in MySQL support. (default=no)],
[ if test -x "$withval"; then
    WHERE_MYSQL=$withval
  else
    WHERE_MYSQL="/usr"
  fi
]
)

dnl extra argument: --with-pgsql
WITH_PGSQL=
AC_ARG_WITH(pgsql,
[  --with-pgsql[=PATH]           Compile in PostgreSQL support. (default=no)],
[ if test -x "$withval"; then
    WHERE_PGSQL=$withval
  else
    WHERE_PGSQL="/usr"
  fi
]
)

dnl extra argument: --with-openssl
WITH_OPENSSL=
AC_ARG_WITH(openssl,
[  --with-openssl[=PATH]           Compile in OpenSSL support. (default=no)],
[ if test -x "$withval"; then
    WHERE_OPENSSL=$withval
  else
    WHERE_OPENSSL="/usr"
  fi
]
)

dnl Checks for libraries.

if test "x$WHERE_MYSQL" != "x"; then
  LIBS="-L$WHERE_MYSQL/lib/mysql"
  AC_CHECK_LIB(mysqlclient, my_init,
    [
      MYSQLCFLAGS="-L$WHERE_MYSQL/lib/mysql -I$WHERE_MYSQL/include/mysql"
      MYSQLLIB="-lmysqlclient"
      AC_DEFINE(HAVE_MYSQL)
    ]
  )
fi

if test "x$WHERE_OPENSSL" != "x"; then
  LIBS="-L$WHERE_OPENSSL/lib"
  AC_CHECK_LIB(crypto, EVP_EncryptUpdate,
    [
      OPENSSLCFLAGS="-L$WHERE_OPENSSL/lib -I$WHERE_OPENSSL/include"
      CRYPTOLIB="-lcrypto"
      AC_DEFINE(HAVE_OPENSSL)
    ]
  )
fi


if test "x$WHERE_PGSQL" != "x"; then
  LIBS="-L$WHERE_PGSQL/lib/pgsql"
  AC_CHECK_LIB(pq, PQsetdbLogin,
    [
      PGSQLCFLAGS="-L$WHERE_PGSQL/lib -I$WHERE_PGSQL/include/pgsql"
      PGSQLLIB="-lpq"
      AC_DEFINE(HAVE_PGSQL)
    ]
  )
fi

AC_CHECK_LIB(y, main,YLIB="$YLIB -ly",)
AC_CHECK_LIB(z, zlibVersion)
case "X$LIBS" in
*-lz*)
   ;;
*)
   AC_MSG_ERROR(Link with "-lz" (zlib >= 1.0.2) failed!)
   ;;
esac
AC_CHECK_LIB(wrap,allow_severity)

dnl Checks for header files.
AC_HEADER_DIRENT
AC_HEADER_STDC
AC_CHECK_HEADERS(fcntl.h features.h limits.h malloc.h string.h strings.h sys/time.h syslog.h unistd.h)

# from cvs
echo $ac_n "checking for sin_len in sockaddr_in ... $ac_c"
AC_TRY_COMPILE([#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>],
[struct sockaddr_in sock; sock.sin_len = sizeof(sock);],
echo yes;AC_DEFINE(HAVE_SOCK_SIN_LEN), 
echo no)


AC_DEFINE(_BSD_SOURCE)

dnl Checks for typedefs, structures, and compiler characteristics.
AC_C_CONST
AC_TYPE_OFF_T
AC_TYPE_PID_T
AC_TYPE_SIZE_T
AC_STRUCT_ST_RDEV
AC_HEADER_TIME
AC_STRUCT_TM

dnl Checks for library functions.
AC_FUNC_MMAP
AC_FUNC_ALLOCA
AC_TYPE_SIGNAL
AC_CHECK_LIB(nsl,gethostbyname)
AC_CHECK_LIB(socket,socket)
AC_CHECK_FUNCS(gethostname gettimeofday select socket strdup strtoul)
AC_CHECK_FUNCS(timelocal)
AC_CHECK_FUNCS(sigaction)
AC_REPLACE_FUNCS(strsep strerror strtoull)

test "x$prefix" = xNONE && prefix=$ac_default_prefix
eval localstatedir=`echo $localstatedir`

dnl strtoul on some systems is really strtoull
AC_MSG_CHECKING([strtoul returns 64 bits])
AC_TRY_RUN(
[
#include <stdlib.h>
int main(void) {
  unsigned long long ll;
  ll = strtoul("0x1010101010101010", (char**)0L, 0);
  return (ll != 0x1010101010101010LL);
}
],
[AC_MSG_RESULT(yes)
  AC_DEFINE(HAVE_LL_STRTOUL)
],
[AC_MSG_RESULT(no)],
)

dnl Allow the user to enable large file support
AC_ARG_ENABLE(lfs, AC_HELP_STRING([--enable-lfs],
	[Enable Large File Support. (default=no)]),
	[
		if test "x$enable_lfs" = xyes; then
			AC_MSG_NOTICE([Large File Support Enabled.])
			ifelse(AC_ACVERSION, [2.13],
			[
				CFLAGS="$CFLAGS -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE"
			],
			[
				AC_SYS_LARGEFILE
			])
		fi
	],[])

AC_OUTPUT(lib/Makefile src/Makefile bin/Makefile Makefile docs/Makefile lib/ftpaths.h configs/Makefile docs/flow-capture.1 docs/flow-capture.html docs/flow-nfilter.1 docs/flow-nfilter.html docs/flow-print.1 docs/flow-print.html docs/flow-report.1 docs/flow-report.html docs/flow-receive.1 docs/flow-receive.html docs/flow-tag.1 docs/flow-tag.html docs/flow-mask.1 docs/flow-mask.html docs/flow-fanout.1 docs/flow-fanout.html docs/flow-xlate.1 docs/flow-xlate.html docs/flow-rpt2rrd.1 docs/flow-rpt2rrd.html docs/flow-rptfmt.1 docs/flow-rptfmt.html docs/flow-log2rrd.1 docs/flow-log2rrd.html)

# create build header
$srcdir/src/ftbuild.sh; cd ..
#cd src; ./ftbuild.sh; cd ..

echo
echo "Please subscribe to the flow-tools mailing list by sending a message to"
echo "flow-tools-request@splintered.net"
echo
echo "Now type make to continue the build process"
echo

