dnl ###########################################################################
dnl ## configure.in
dnl ##
dnl ## This file gives all necessary information to Autoconf to build the
dnl ## DioNiSio configuration script.
dnl ##
dnl ## ------------------------------------------------------------------------
dnl ## dosis - DoS: Internet Sodomizer
dnl ##   (C) 2008 Gerardo Garc�a Pe�a <gerardo@kung-foo.net>
dnl ##
dnl ##   This program is free software; you can redistribute it and/or modify
dnl ##   it under the terms of the GNU General Public License as published by
dnl ##   the Free Software Foundation; either version 2 of the License, or (at
dnl ##   your option) any later version.
dnl ##
dnl ##   This program is distributed in the hope that it will be useful, but
dnl ##   WITHOUT ANY WARRANTY; without even the implied warranty of
dnl ##   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
dnl ##   General Public License for more details.
dnl ##
dnl ##   You should have received a copy of the GNU General Public License
dnl ##   along with this program; if not, write to the Free Software
dnl ##   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
dnl ##   02110-1301  USA
dnl ##
dnl ###########################################################################

AC_PREREQ(2.13)

dnl ---------------------------------------------------------------------------
dnl Set DioNiSio version
dnl ---------------------------------------------------------------------------

define(DOS_AC_VERSION_MAJOR,   1)
define(DOS_AC_VERSION_MINOR,   0)
define(DOS_AC_VERSION_RELEASE, 0)
define(DOS_AC_VERSION, DOS_AC_VERSION_MAJOR.DOS_AC_VERSION_MINOR.DOS_AC_VERSION_RELEASE)

dnl ---------------------------------------------------------------------------
dnl Basic configuration of autoconf+automake
dnl ---------------------------------------------------------------------------

AC_INIT([Dosis],DOS_AC_VERSION,[gerardo@kung-foo.net],[dosis])
AM_INIT_AUTOMAKE
AM_CONFIG_HEADER(include/autoconfig.h)

dnl ---------------------------------------------------------------------------
dnl Check essential tools and libraries
dnl ---------------------------------------------------------------------------

AC_PROG_INSTALL
AC_PROG_CC
AM_PROG_AS
AC_PROG_RANLIB
AC_PROG_YACC
AC_C_INLINE
AC_C_STRINGIZE
AC_HEADER_STDC
AC_FUNC_STAT
AC_FUNC_VPRINTF

dnl ---------------------------------------------------------------------------
dnl Identify host platform and operating system
dnl ---------------------------------------------------------------------------

AC_C_BIGENDIAN
AC_CANONICAL_HOST
case "$host_os" in
*linux*)
  AC_DEFINE([LINUX], [1], [Target OS is Linux (glibc)])
  ;;
*)
  dnl oh yeah! no windows
  AC_MSG_ERROR([Only for Linux.])
esac

case "$host_cpu" in
sparc)
  # this CPU does not allow unaligned accesses
  ;;
i386|i486|i586|i686)
  ALLOW_UNALIGNED_16=1
  ALLOW_UNALIGNED_32=1
  ;;
*)
  AC_MSG_WARN([Unknown CPU '$host_cpu'.])
  ALLOW_UNALIGNED_16=1
  ALLOW_UNALIGNED_32=1
esac
if test "$ALLOW_UNALIGNED_16" ; then
  AC_DEFINE([ALLOW_UNALIGNED_16], [1], [Allows word unaligned memory accesses.])
fi
if test "$ALLOW_UNALIGNED_32" ; then
  AC_DEFINE([ALLOW_UNALIGNED_32], [1], [Allows dword unaligned memory accesses.])
fi

dnl ---------------------------------------------------------------------------
dnl Set compiler flags
dnl ---------------------------------------------------------------------------

dnl Try to detect compiler
if test "$GCC" = "yes"; then
  dnl Active warnings
  CFLAGS="$CFLAGS -Wall"
else
  case `$CC -version 2>&1` in
    *MIPSpro*)
      dnl This is a SGI MIPSpro compiler!
      CFLAGS="$CFLAGS -fullwarn"
      PIC_FLAG=-KPIC
      ;;
    *)
      dnl WTF!?
      AC_MSG_WARN("Unknown compiler.");
  esac
fi

dnl ---------------------------------------------------------------------------
dnl Basic configuration
dnl ---------------------------------------------------------------------------

AC_CHECK_SIZEOF(char)
AC_CHECK_SIZEOF(short int)
AC_CHECK_SIZEOF(int)
AC_CHECK_SIZEOF(long int)
AC_CHECK_SIZEOF(long long)
AC_CHECK_SIZEOF(void *)

dnl ---------------------------------------------------------------------------
dnl Check that libraries are ok
dnl ---------------------------------------------------------------------------

AC_CHECK_HEADERS([getopt.h          \
                  sys/socket.h      \
                  arpa/inet.h       \
                  libnet.h          \
                  libipq.h          \
                  linux/netfilter.h \
                  pthread.h         \
                  netinet/in.h],
                 [],
                 AC_MSG_ERROR([Essential header file not found.]))

AC_CHECK_TYPES([struct sockaddr_in],
               [], [I need at least a 'struct sockaddr_in'],
[
#include <sys/socket.h>
#include <netinet/in.h>
])
AC_CHECK_TYPES([struct sockaddr_in6], [], [],
[
#include <sys/socket.h>
#include <netinet/in.h>
])
AC_CHECK_TYPES([int8_t,   int16_t,   int32_t,   int64_t],   [], [], [#include <sys/types.h>])
AC_CHECK_TYPES([uint8_t,  uint16_t,  uint32_t,  uint64_t],  [], [], [#include <sys/types.h>])
AC_CHECK_TYPES([u_int8_t, u_int16_t, u_int32_t, u_int64_t], [], [], [#include <sys/types.h>])

AC_CHECK_FUNCS([getopt_long], [], AC_MSG_ERROR([This version of getopt doesn't support long options.]))

AC_ARG_WITH([ssl],
            AC_HELP_STRING([--with-ssl], [use ssl (default is YES)]),
            [ac_cv_use_ssl=$withval],
            [ac_cv_use_ssl=yes])

AC_CHECK_LIB(ipq,     ipq_read,       [], AC_MSG_ERROR("Missing library."))
AC_CHECK_LIB(m,       floor,          [], AC_MSG_ERROR("Missing math library."))
AC_CHECK_LIB(net,     libnet_init,    [], AC_MSG_ERROR("Missing library."))
AC_CHECK_LIB(pthread, pthread_create, [], AC_MSG_ERROR("Missing library."))
AC_CHECK_LIB(rt,      clock_gettime,  [], AC_MSG_ERROR("Missing library."))

dnl ---------------------------------------------------------------------------
dnl SSL configuration
dnl ---------------------------------------------------------------------------

OPENSSL="no"
if test "$ac_cv_use_ssl" = "yes"; then
  AC_CHECK_HEADERS([openssl/e_os.h   \
                    openssl/e_os2.h])
  AC_CHECK_HEADERS([openssl/lhash.h  \
                    openssl/crypto.h \
                    openssl/buffer.h \
                    openssl/x509.h   \
                    openssl/ssl.h    \
                    openssl/err.h    \
                    openssl/rand.h   \
                    openssl/pem.h],
                   [OPENSSL="yes"])
  if test "$OPENSSL" = "yes"; then
    AC_CHECK_LIB(ssl, SSL_library_init, [], AC_MSG_ERROR("Missing library."))
    AC_DEFINE(HAVE_SSL, 1, [ssl available])
    AC_DEFINE(SSL_ENABLED, 1, [enable ssl])
  else
    AC_MSG_WARN("SSL is disabled")
    AC_DEFINE(SSL_ENABLED, 0, [disable ssl])
  fi
fi
AM_CONDITIONAL(SSL_ENABLED, test "$OPENSSL" = "yes")

dnl ---------------------------------------------------------------------------
dnl Set directories
dnl ---------------------------------------------------------------------------

dnl Create a definition with the default directory
define(DOS_AC_DEFAULT_PREFIX, /usr/local/AC_PACKAGE_TARNAME-AC_PACKAGE_VERSION)

dnl Set default prefix directory now because we need to write some dirs based
dnl on this value.
AC_PREFIX_DEFAULT(DOS_AC_DEFAULT_PREFIX)
test "$prefix" = "NONE" && prefix='DOS_AC_DEFAULT_PREFIX'
test "$exec_prefix" = "NONE" && exec_prefix='${prefix}'

dnl ---------------------------------------------------------------------------
dnl Tell Autoconf/Automake which files must be generated
dnl ---------------------------------------------------------------------------

AC_CONFIG_FILES([Makefile
                 doc/man/dosis.1
                 include/Makefile
                 src/Makefile
                 src/dosis/Makefile])
AC_OUTPUT

