AC_PREREQ(2.50)

AC_INIT

AC_ARG_ENABLE(ssl,
	AC_HELP_STRING([--enable-ssl],
		[build spamc with ssl support ('yes'/'no', default is 'no')]),
	[sa_ssl_enabled=${enableval:-no}],
	[sa_ssl_enabled=${enableval:-no}])
AC_CONFIG_SRCDIR([Makefile.PL])
AC_CONFIG_HEADER(config.h)

dnl ensure Makefile.PL has been run too
if test -f Makefile.PL -a ! -f Makefile ; then
  perl Makefile.PL || exit 1
fi

AC_PROG_CC

if test "x$GCC" = "xyes" ; then
  CFLAGS="-Wall $CFLAGS"
fi

AC_EXEEXT

dnl ----------------------------------------------------------------------

AC_HEADER_STDC
AC_CHECK_HEADERS(sys/time.h syslog.h unistd.h errno.h sys/errno.h)
AC_CHECK_HEADERS(time.h sysexits.h sys/socket.h netdb.h netinet/in.h)
AC_CHECK_HEADERS(getopt.h pwd.h signal.h openssl/crypto.h)

AC_C_CONST
AC_TYPE_OFF_T
AC_TYPE_PID_T
AC_TYPE_UID_T
AC_TYPE_SIZE_T

dnl ----------------------------------------------------------------------

AC_CACHE_CHECK([for SHUT_RD],
       shutrd, [
                AC_TRY_COMPILE([#include <sys/types.h>
#include <sys/socket.h>],
                        [printf ("%d", SHUT_RD); return 0;],
                                        [shutrd=yes],
                                        [shutrd=no]),
       ])
if test $shutrd = yes ; then
  AC_DEFINE(HAVE_SHUT_RD)
fi

dnl ----------------------------------------------------------------------

AC_CHECK_LIB(socket, socket)

SSLLIBS=""
SSLCFLAGS=""
if test yes = "$sa_ssl_enabled"; then
	AC_CHECK_LIB(crypto, CRYPTO_lock,[SSLLIBS="-lcrypto $SSLLIBS"])
	AC_CHECK_LIB(ssl, SSL_CTX_free,[SSLLIBS="-lssl $SSLLIBS"],,-lcrypto)

	# before defining SPAMC_SSL check that all its requirements are
	# actually available
	if test yes = "$ac_cv_header_openssl_crypto_h" && \
	   test yes = "$ac_cv_lib_crypto_CRYPTO_lock" && \
	   test yes = "$ac_cv_lib_ssl_SSL_CTX_free"; then
		SSLCFLAGS="-DSPAMC_SSL"
	else
		AC_MSG_ERROR([Cannot use SSL; libraries or header files are missing.
You may try using 'configure --disable-ssl'.])
	fi
fi
AC_CHECK_LIB(inet, connect)
AC_CHECK_LIB(nsl, t_accept)
AC_CHECK_LIB(dl, dlopen)

AC_CHECK_FUNCS(socket strdup strtod strtol snprintf shutdown)

dnl ----------------------------------------------------------------------

AC_CACHE_CHECK([for h_errno],
        herrno, [
                AC_TRY_COMPILE([#include <netdb.h>],
                        [printf ("%d", h_errno); return 0;],
                                        [herrno=yes],
                                        [herrno=no]),
        ])
if test $herrno = yes ; then
  AC_DEFINE(HAVE_H_ERRNO)
fi

dnl ----------------------------------------------------------------------

AC_CACHE_CHECK([for optarg],
        haveoptarg, [
                AC_TRY_COMPILE([#include <getopt.h>],
                        [if (optarg == (char *) 0L) { return 0; } return 1;],
                                        [haveoptarg=yes],
                                        [haveoptarg=no]),
        ])
if test $haveoptarg = yes ; then
  AC_DEFINE(HAVE_OPTARG)
fi

dnl ----------------------------------------------------------------------

AC_CACHE_CHECK([for in_addr_t],
        inaddrt, [
                AC_TRY_COMPILE([#include <sys/types.h>
#include <netinet/in.h>],
                        [in_addr_t foo; return 0;],
                                        [inaddrt=yes],
                                        [inaddrt=no]),
        ])
if test $inaddrt = no ; then
  AC_CHECK_TYPE(in_addr_t, unsigned long)
fi

dnl ----------------------------------------------------------------------

AC_CACHE_CHECK([for INADDR_NONE],
        haveinaddrnone, [
                AC_TRY_COMPILE([#include <sys/types.h>
#include <netinet/in.h>],
                        [in_addr_t foo = INADDR_NONE; return 0;],
                                        [haveinaddrnone=yes],
                                        [haveinaddrnone=no]),
        ])
if test $haveinaddrnone = yes ; then
  AC_DEFINE(HAVE_INADDR_NONE)
fi

dnl ----------------------------------------------------------------------

AC_CACHE_CHECK([for EX__MAX],
        haveexmax, [
                AC_TRY_COMPILE([#ifdef HAVE_SYSEXITS_H
#include <sysexits.h>
#endif
#include <errno.h>],
                        [int foo = EX__MAX; return 0;],
                                        [haveexmax=yes],
                                        [haveexmax=no]),
        ])
if test $haveexmax = yes ; then
  AC_DEFINE(HAVE_EX__MAX)
fi


dnl ----------------------------------------------------------------------

# we cheat, since we *know* we have Perl installed ;)
CCDLFLAGS=`perl -e 'use Config; print $Config::Config{ccdlflags}'`
LDDLFLAGS=`perl -e 'use Config; print $Config::Config{lddlflags}'`
AC_SUBST([SSLLIBS])
AC_SUBST([SSLCFLAGS])
AC_SUBST(CCDLFLAGS)
AC_SUBST(LDDLFLAGS)
AC_SUBST(HAVE_SHUT_RD)

AC_CONFIG_FILES([Makefile])
AC_OUTPUT
