dnl Process this file with autoconf to produce a configure script.
dnl

dnl provide it a file to simply check for its existence.
AC_INIT([dnssec-tools-validator], [0.3], [dnssec-tools-users@dnssec-tools.org])
AC_PREREQ([2.59])

AC_CONFIG_HEADER(include/validator/validator-config.h)

AC_MSG_CACHE_INIT()
if test "x$cache_file" = "x/dev/null" ; then
  CONFIG_CACHE_FILE=""
else
  CONFIG_CACHE_FILE=$cache_file
fi
AC_SUBST(CONFIG_CACHE_FILE)

AC_PATH_PROG([PERLPROG],perl)
AC_PATH_PROG(AR, ar)
AC_PATH_PROG(RM, rm)
AC_PATH_PROG(CP, cp)

# libtool info
# AC_LIBTOOL_WIN32_DLL
AC_PROG_LIBTOOL

# useful stuff for installation
AC_PROG_INSTALL
AC_PROG_LN_S
AC_PROG_MAKE_SET

# system check
AC_CANONICAL_TARGET

#
# save the configure arguments
#
AC_DEFINE_UNQUOTED(LIBVAL_CONFIGURE_OPTIONS,"$ac_configure_args", [Configure options])
CONFIGURE_OPTIONS="\"$ac_configure_args\""
AC_SUBST(CONFIGURE_OPTIONS)

########################################
# add in -Dsolaris2, -Dlinux, etc as needed

changequote(, )
PARTIALTARGETOS=`echo $target_os | sed 's/[-._].*//'`
changequote([, ])

#
# Check whether we can use -Ux -Dx=x with the expected behaviour
#
AC_MSG_CHECKING([whether to un-define target system token (before redefining)])
OLD_CPPFLAGS="$CPPFLAGS"
CPPFLAGS="-U$PARTIALTARGETOS -D$PARTIALTARGETOS=$PARTIALTARGETOS"

AC_EGREP_CPP(yes,[[
#ifdef $PARTIALTARGETOS
yes
#endif
]],
[PARTIALTARGETFLAGS="-U$PARTIALTARGETOS -D$PARTIALTARGETOS=$PARTIALTARGETOS"
 AC_MSG_RESULT([yes])],
[PARTIALTARGETFLAGS="-D$PARTIALTARGETOS=$PARTIALTARGETOS"
 AC_MSG_RESULT([no])])
CFLAGS="$CFLAGS $PARTIALTARGETFLAGS"
CPPFLAGS="$OLD_CPPFLAGS"
CFLAGS="$CFLAGS $PARTIALTARGETFLAGS"

dnl ----------------------------------------------------------------------
AC_MSG_CHECKING([for developer gcc flags])
DEVFLAGS=""
if test -d $srcdir/.git -o -d $srcdir/.svn; then
  # disable --enable-shared by default in cvs/developer cases.
  # don't do anything if option set via cmdline
  if test "${enable_shared+set}" != set; then
    enable_shared=no
  fi
  if test "x$GCC" = "xyes"; then
    DEVFLAGS="-Wall -Winline -Wstrict-prototypes -Wwrite-strings -Wcast-qual -Wno-char-subscripts"
  fi
fi
AC_MSG_RESULT($DEVFLAGS)AC_SUBST(DEVFLAGS)
AC_MSG_CACHE_ADD(Developer flags                : $DEVFLAGS)

dnl ----------------------------------------------------------------------
# for some reason $prefix is NONE until much later in the script, so
# fix up val_sysconfdir if no prefix or sysconfdir was specified
val_sysconfdir=`eval echo $sysconfdir`
if test "x$val_sysconfdir" = "xNONE/etc" ; then
   val_sysconfdir="${ac_default_prefix}/etc"
fi
val_sysconfdir=`eval echo $sysconfdir`
if test "x$val_sysconfdir" = "xNONE/etc" ; then
   val_sysconfdir="${ac_default_prefix}/etc"
fi
AC_MSG_CACHE_ADD(system configuration directory : $val_sysconfdir)
AC_DEFINE_UNQUOTED(ETC_HOSTS,"$val_sysconfdir/hosts", [validator hosts file])
AC_SUBST(ETC_HOSTS)

AH_TEMPLATE([VAL_RESOLV_CONF], [Path to resolver configuration])
AC_ARG_WITH(resolv-conf,
[  --with-resolv-conf=FILE resolver config file (SYSCONFDIR/resolv.conf).],
    [ac_cv_user_prompt_VAL_RESOLV_CONF=$with_resolv_conf])

AH_TEMPLATE([VAL_ROOT_HINTS], [Path to root hints config file])
AC_ARG_WITH(root-hints,
[  --with-root-hints=FILE  root.hints config file.],
    [ac_cv_user_prompt_VAL_ROOT_HINTS=$with_root_hints])

VAL_CONFIGURATION_FILE=$val_sysconfdir/dnssec-tools/dnsval.conf
AC_ARG_WITH(dnsval-conf,
[  --with-dnsval-conf=FILE dnsval config file (SYSCONFDIR/dnssec-tools/dnsval.conf).],
    [VAL_CONFIGURATION_FILE=$with_dnsval_conf])
AC_DEFINE_UNQUOTED(VAL_CONFIGURATION_FILE,"$VAL_CONFIGURATION_FILE", [dnsval configuration file])
AC_SUBST(VAL_CONFIGURATION_FILE)
AC_MSG_CACHE_ADD(Validator configuration file   : $VAL_CONFIGURATION_FILE)

VALIDATOR_TESTCASES=$val_sysconfdir/dnssec-tools/validator-testcases
AC_ARG_WITH(validator-testcases-file,
[  --with-validator-testcases-file=FILE dnsval config file (SYSCONFDIR/dnssec-tools/dnsval.conf).],
    [VALIDATOR_TESTCASES=$with_validator_testcases_file])
AC_DEFINE_UNQUOTED(VALIDATOR_TESTCASES,"$VALIDATOR_TESTCASES", [validator testcases configuration file])
AC_SUBST(VALIDATOR_TESTCASES)
dnl ----------------------------------------------------------------------
dnl
#
# otherwise prompted information
#
AC_ARG_WITH(defaults,
[
Miscellaneous:
  --with-defaults         Use defaults for prompted values.],
    defaults="yes",defaults="no")
AC_ARG_ENABLE(defaults,,
        AC_MSG_ERROR([ Invalid option. Use --with-defaults/--without-defaults instead ]) )
if test "$defaults" = "yes" ; then
   if test "${with_resolv_conf+set}" != set; then
      unset ac_cv_user_prompt_VAL_RESOLV_CONF
   fi
   if test "${with_root_hints+set}" != set; then
      unset ac_cv_user_prompt_VAL_ROOT_HINTS
   fi
fi


dnl ----------------------------------------------------------------------
dnl
AC_ARG_WITH(cflags,
[  --with-cflags=CFLAGS    use CFLAGS as compile time arguments.],
    [CFLAGS=$with_cflags; export CFLAGS])

CFLAGS="$CFLAGS -DLIBVAL_CONFIGURED=1"

case $target_os in
    solaris*)
        AC_MSG_CHECKING([whether we need _REENTRANT for str*_r])
        AC_MSG_RESULT([yes])
        CFLAGS="$CFLAGS -D_REENTRANT"
        ;;
    mingw32*)
        _WIN32_MSVC=1
        SKIP_SYSTEM_CHECKS=1
        LDFLAGS="$LDFLAGS -Wl,--enable-stdcall-fixup"
        ;;
    *)
        ;;
esac

dnl ----------------------------------------------------------------------
dnl
AH_TEMPLATE([LIBVAL_NSEC3], [Define if NSEC3 is required.])
AC_ARG_WITH(nsec3,
[  --without-nsec3         Disable nsec3 support.],
   nsec3="$withval", nsec3="yes")
AC_ARG_ENABLE(nsec3,,
        AC_MSG_ERROR([ Invalid option. Use --with-nsec3/--without-nsec3 instead ]) )
if test "$nsec3" != "no"; then
    AC_DEFINE(LIBVAL_NSEC3, 1)
    AC_MSG_CACHE_ADD(NSEC3 support                  : Yes)
else
    AC_MSG_CACHE_ADD(NSEC3 support                  : No)
fi

dnl ----------------------------------------------------------------------
dnl
AH_TEMPLATE([LIBVAL_DLV], [Define if DLV is required.])
AC_ARG_WITH(dlv,
[  --without-dlv            Disable DLV support.],
   dlv="$withval", dlv="yes")
AC_ARG_ENABLE(dlv,,
        AC_MSG_ERROR([ Invalid option. Use --with-dlv/--without-dlv instead ]) )
if test "$dlv" != "no"; then
    AC_DEFINE(LIBVAL_DLV, 1)
    AC_MSG_CACHE_ADD(DLV support                    : Yes)
else 
    AC_MSG_CACHE_ADD(DLV support                    : No)
fi

dnl ----------------------------------------------------------------------
dnl
AH_TEMPLATE([VAL_IPV6], [Define if IPV6 is required.])
AC_ARG_WITH(ipv6,
[  --without-ipv6             Disable IPv6 support.],
   ipv6="$withval", ipv6="yes")
AC_ARG_ENABLE(ipv6,,
        AC_MSG_ERROR([ Invalid option. Use --with-ipv6/--without-ipv6 instead ]) )
if test "$ipv6" != "no"; then
    AC_DEFINE(VAL_IPV6, 1)
    AC_MSG_CACHE_ADD(IPv6 support                   : Yes)
else
    AC_MSG_CACHE_ADD(IPv6 support                   : No)
fi


dnl ----------------------------------------------------------------------
dnl openssl
dnl
AC_ARG_WITH(openssl,
[  --with-openssl=PATH     Look for openssl in PATH/{lib,include}.],
    if test "x$withval" != "xyes"; then
      opensslpathset=1
      AC_ADD_SEARCH_PATH($withval)
    fi
)
dnl openssl defaults to /usr/local/ssl, so add it to the search path.
if test "x$opensslpathset" != "x1"; then
    case $build_os in
      mingw32*)
          AC_ADD_SEARCH_PATH(/c/OpenSSL-Win32)
          ;;
      *)
          AC_ADD_SEARCH_PATH(/usr/local/ssl)
          ;;
    esac
fi

AC_ARG_ENABLE(openssl,,
        AC_MSG_ERROR([ Invalid option. Use --with-openssl/--without-openssl instead ]) )
AC_ARG_WITH(ssl,,
        AC_MSG_ERROR([ Invalid option. Use --with-openssl/--without-openssl instead ]) )
AC_ARG_ENABLE(ssl,,
        AC_MSG_ERROR([ Invalid option. Use --with-openssl/--without-openssl instead ]) )

AC_SEARCH_LIBS(EVP_md5, [crypto eay32 libeay32 crypt32],,
        AC_MSG_ERROR("OpenSSL crypto library not found. get it from http://www.openssl.org."))
AH_TEMPLATE([HAVE_SHA_2],
            [Define if libcrypto implements the SHA-2 suite of algorithms.])
AH_TEMPLATE([HAVE_ECDSA],
            [Define if libcrypto implements the ECDSA algorithm.])
AC_ARG_ENABLE([sha2-check],
    AS_HELP_STRING([--disable-sha2-check],
         [Make missing SHA-2 support a warning instead of an error.]))
AC_ARG_ENABLE([ecdsa-check],
    AS_HELP_STRING([--disable-ecdsa-check],
         [Make missing ECDSA support a warning instead of an error.]))
AC_SEARCH_LIBS(SHA384_Init, [crypto eay32 libeay32 crypt32], AC_DEFINE(HAVE_SHA_2),
        AS_IF([test "x$enable_sha2_check" != "xno"],
             [AC_MSG_ERROR(Need openssl version > 0.9.8 for SHA-2 support. Use --disable-sha2-check to bypass this error.)],
             [AC_MSG_WARN(Need openssl version > 0.9.8 for SHA-2 support.)]))
AC_SEARCH_LIBS(EC_KEY_set_public_key_affine_coordinates, [crypto eay32 libeay32 crypt32], AC_DEFINE(HAVE_ECDSA),
        AS_IF([test "x$enable_ecdsa_check" != "xno"],
             [AC_MSG_ERROR(Need recent openssl version for ECDSA support. Use --disable-ecdsa-check to bypass this error.)],
             [AC_MSG_WARN(Need recent openssl version for ECDSA support.)]))
AC_SUBST(LIBS)

if test ! -z "$_WIN32_MSVC"; then
AC_SEARCH_LIBS(SSL_get_SSL_CTX, [ssl],,
        AC_MSG_ERROR("OpenSSL ssl library not found. get it from http://www.openssl.org."),
        [-lcrypto -lws2_32])
else
AC_SEARCH_LIBS(SSL_get_SSL_CTX, [ssl],,
        AC_MSG_ERROR("OpenSSL ssl library not found. get it from http://www.openssl.org."),
        [-lcrypto])
fi
AC_SUBST(LIBS)

AC_CHECK_HEADERS(crypto/sha2.h openssl/ecdsa.h)

dnl ----------------------------------------------------------------------
AC_ARG_WITH(threads,
[  --without-threads       Don't use threads.])

LIBVAL_SUFFIX=""
AH_TEMPLATE([VAL_NO_THREADS],
            [Define if you do not want to use threads.])
if test "x$with_threads" = "xno"; then
    AC_DEFINE(VAL_NO_THREADS, 1)
    AC_MSG_CACHE_ADD(Thread support                 : No)
else
    AC_CHECK_LIB(pthread, pthread_rwlock_init,
            [LIBS="$LIBS -lpthread"
             LIBVAL_SUFFIX="-threads"
             AC_MSG_CACHE_ADD(Thread support                 : Yes)],
            [AC_DEFINE(VAL_NO_THREADS, 1)
             AC_MSG_CACHE_ADD(Thread support                 : No)])
fi

AC_SUBST(LIBS)
AC_SUBST(LIBVAL_SUFFIX)


dnl ----------------------------------------------------------------------
dnl
if test ! -z "$_WIN32_MSVC"; then
LIBS="$LIBS -lgdi32 -lws2_32" 
AC_SUBST(LIBS)
fi

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

AC_CHECK_HEADERS(sys/param.h sys/types.h sys/stat.h sys/ioctl.h sys/socket.h sys/filio.h sys/file.h sys/fcntl.h sys/select.h netinet/in.h sys/time.h ctype.h getopt.h libgen.h limits.h pthread.h syslog.h sys/resource.h)
AC_CHECK_HEADERS(net/if.h ifaddrs.h,,, [
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif])
AC_CHECK_DECLS([getopt_long_only, getopt_long],,,[
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_GETOPT_H
#include <getopt.h>
#endif
])

dnl ----------------------------------------------------------------------
AC_CHECK_HEADERS(arpa/nameser.h arpa/nameser_compat.h arpa/inet.h endian.h resolv.h netdb.h errno.h)

AC_CACHE_CHECK(for struct HEADER in arpa/nameser.h and arpa/nameser_compat.h, 
               ac_cv_nameser_has_header,
[AC_EGREP_CPP(HEADER, [
#ifdef HAVE_ARPA_NAMESER_H
#include <arpa/nameser.h>
#endif
#ifdef HAVE_ARPA_NAMESER_COMPAT_H
#include <arpa/nameser_compat.h>
#endif
],  ac_cv_nameser_has_header="yes", ac_cv_nameser_has_header="no")])
if test "x$ac_cv_nameser_has_header" = "xyes" ; then
  AC_DEFINE([NAMESER_HAS_HEADER], 1, [Define if <arpa/nameser.h> has HEADER. ])
fi

AC_CHECK_STRUCT_FOR([
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_ARPA_NAMESER_H
#include <arpa/nameser.h>
#endif
], __ns_msg, _msg_ptr, no)
AH_TEMPLATE([STRUCT___NS_MSG_HAS__MSG_PTR],
            [Define if struct __ns_msg had _msg_ptr.])

AC_CHECK_STRUCT_FOR([
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_ARPA_NAMESER_H
#include <arpa/nameser.h>
#endif
], __ns_msg, _ptr, no)
AH_TEMPLATE([STRUCT___NS_MSG_HAS__PTR],
            [Define if struct __ns_msg had _ptr.])


AC_CHECK_TYPES([ns_cert_types],,,[
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_ARPA_NAMESER_H
#include <arpa/nameser.h>
#endif
])

dnl check for varios ns_* decls (record types or functions)
AC_CHECK_DECLS([ns_t_a,ns_t_kx,ns_r_badsig,ns_parse_ttl,ns_name_pton,ns_name_unpack,ns_name_ntop,ns_samename,ns_t_aaaa,ns_t_dnskey,ns_t_rrsig,ns_t_nsec,ns_t_nsec3,ns_t_dlv,ns_t_ds,ns_t_tlsa],,,[
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_ARPA_NAMESER_H
#include <arpa/nameser.h>
#endif
])
AC_CHECK_DECLS([p_rcode,p_section],,,[
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#if HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_RESOLV_H
#include <resolv.h>
#endif
])

AC_CHECK_DECLS([struct addrinfo])

dnl ---------------------------------------------------------------------
found_gethostbyaddr=0
AH_TEMPLATE([GETHOSTBYADDR_USES_CHAR_INT], [Define if gethostbyaddr expects a char, int, int])
AC_MSG_CHECKING([for the proper gethostbyaddr prototype])
AC_TRY_LINK([
#ifdef HAVE_NETDB_H
#include <netdb.h>
#endif
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
],
[
struct hostent *gethostbyaddr(const char *addr, int len, int type);
/* or: gethostbyaddr(__const void *addr, socklen_t len, int type) */

int i = 1;

/* struct hostent *testit = gethostbyaddr("127.0.0.1", int len, int type) */
],[
  AC_MSG_RESULT([char and int])
  AC_DEFINE(GETHOSTBYADDR_USES_CHAR_INT, 1)
  found_gethostbyaddr=1
], [
  AC_MSG_RESULT([unknown])
]
)

if test $found_gethostbyaddr = 0 ; then
dnl ---------------------------------------------------------------------
AH_TEMPLATE([GETHOSTBYADDR_USES_VOID_SOCKLEN], [Define if gethostbyaddr expects void, socklen_t, int]
)
AC_MSG_CHECKING([for the proper gethostbyaddr prototype])
AC_TRY_LINK([
#ifdef HAVE_NETDB_H
#include <netdb.h>
#endif
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
],
[
struct hostent *gethostbyaddr(const void *addr, socklen_t len, int type);

int i = 1;

/* struct hostent *testit = gethostbyaddr("127.0.0.1", int len, int type) */
],[
  AC_MSG_RESULT([void and socklen_t])
  AC_DEFINE(GETHOSTBYADDR_USES_VOID_SOCKLEN, 1)
  found_gethostbyaddr=1
], [
  AC_MSG_RESULT([unknown])
]
)
fi

if test $found_gethostbyaddr = 0 ; then
dnl ---------------------------------------------------------------------
AH_TEMPLATE([GETHOSTBYADDR_USES_VOID_INT], [Define if gethostbyaddr expects void, int, int])
AC_MSG_CHECKING([for the proper gethostbyaddr prototype])
AC_TRY_LINK([
#ifdef HAVE_NETDB_H
#include <netdb.h>
#endif
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
],
[
struct hostent *gethostbyaddr(const void *addr, int len, int type);

int i = 1;

/* struct hostent *testit = gethostbyaddr("127.0.0.1", int len, int type) */
],[
  AC_MSG_RESULT([void and int])
  AC_DEFINE(GETHOSTBYADDR_USES_VOID_INT, 1)
  found_gethostbyaddr=1
], [
  AC_MSG_RESULT([unknown])
] 
)
fi

dnl ---------------------------------------------------------------------
found_getnameinfo=0
AH_TEMPLATE([GETNAMEINFO_USES_SOCKLEN_AND_UINT], [Define if getnameinfo expects a socklen_t and a unsigned int])
AC_MSG_CHECKING([for the proper getnameinfo prototype])
AC_TRY_LINK([
#ifdef HAVE_NETDB_H
#include <netdb.h>
#endif
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
],
[
int getnameinfo(const struct sockaddr *sa,
                            socklen_t len, char *host,
                            socklen_t hostlen, char *serv, socklen_t servlen,
                            unsigned int flags);
int i = 1;

],[
  AC_MSG_RESULT([socklen_t and uint])
  AC_DEFINE(GETNAMEINFO_USES_SOCKLEN_AND_UINT, 1)
  found_getnameinfo=1
], [
  AC_MSG_RESULT([unknown])
]
)

if test $found_getnameinfo = 0 ; then

AH_TEMPLATE([GETNAMEINFO_USES_SOCKLEN_AND_INT], [Define if getnameinfo expects a socklen_t and an int])
AC_MSG_CHECKING([for the proper getnameinfo prototype])
AC_TRY_LINK([
#ifdef HAVE_NETDB_H
#include <netdb.h>
#endif
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
],
[
int getnameinfo(const struct sockaddr *sa, socklen_t len, char *host,
                            socklen_t hostlen, char *serv, socklen_t servlen,
                            int flags);
int i = 1;

],[
  AC_MSG_RESULT([socklen_t and int])
  AC_DEFINE(GETNAMEINFO_USES_SOCKLEN_AND_INT, 1)
  found_getnameinfo=1
], [
  AC_MSG_RESULT([unknown])
]
)

fi

if test $found_getnameinfo = 0 ; then
AH_TEMPLATE([GETNAMEINFO_USES_SIZET_AND_INT], [Define if getnameinfo expects a size_t and an int])
AC_MSG_CHECKING([for the proper getnameinfo prototype])
AC_TRY_LINK([
#ifdef HAVE_NETDB_H
#include <netdb.h>
#endif
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
],
[
int getnameinfo(const struct sockaddr *sa, socklen_t len, char *host,
                            size_t hostlen, char *serv, size_t servlen,
                            int flags);
int i = 1;

],[
  AC_MSG_RESULT([size_t and int])
  AC_DEFINE(GETNAMEINFO_USES_SIZET_AND_INT, 1) 
  found_getnameinfo=1
], [
  AC_MSG_RESULT([unknown])
]
)

fi
AH_TEMPLATE([R_FUNCS_RETURN_STRUCT],
            [Define if gethostbyname_r returns a struct hostent])
AC_MSG_CHECKING([whether gethostbyaddr_r returns an int or a struct hostent])
AC_TRY_LINK([
#ifdef HAVE_NETDB_H
#include <netdb.h>
#endif
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
],
[
struct hostent *            
gethostbyname_r(const char * name,struct hostent * result_buf, char * buf,
        int buflen, int * h_errnop);
int i = 1;
],[
  AC_MSG_RESULT([hostent]) 
  AC_DEFINE(R_FUNCS_RETURN_STRUCT, 1)
], [
  AC_MSG_RESULT([int])
] 
)

dnl ----------------------------------------------------------------------
dnl check for missing functions 
dnl
AC_CHECK_FUNCS(strerror_r)
AC_CHECK_FUNCS(pselect)
AC_CHECK_FUNCS(gmtime_r)
AC_CHECK_FUNCS(strtok_r)
AC_CHECK_FUNCS(localtime_r)
AC_CHECK_FUNCS(flock)
AC_CHECK_FUNCS(inet_nsap_ntoa)
AC_CHECK_FUNCS(gethostbyname2)
AC_CHECK_FUNCS(hstrerror)
AC_CHECK_FUNCS(freeaddrinfo)
AC_CHECK_FUNCS(getifaddrs)
AC_CHECK_FUNCS(getrlimit)
AC_CHECK_FUNCS(setrlimit)


dnl ----------------------------------------------------------------------
case "$host" in
     *-*-sunos4*)
         AC_DEFINE(SPRINTF_CHAR, 1, [*s*printf() functions are char*])    ;;
         esac

dnl ----------------------------------------------------------------------
dnl add any libraries that may be dependencies
AC_CHECK_LIB(nsl, inet_ntop)
dnl check for the socket library 
AC_CHECK_LIB(socket, gai_strerror)
AC_SEARCH_LIBS(socket, socket)

dnl ----------------------------------------------------------------------
# Don't test following on windows

if test -z "$SKIP_SYSTEM_CHECKS"; then
    AC_CHECK_FUNCS(snprintf)
    AC_CHECK_FUNCS(strncasecmp)
    AC_CHECK_FUNCS(gettimeofday)
fi # SKIP_SYSTEM_CHECKS


dnl ----------------------------------------------------------------------
AC_SUBST(LIBS)

dnl ----------------------------------------------------------------------
AC_CHECK_SIZEOF(short)
AC_CHECK_SIZEOF(int)
AC_CHECK_SIZEOF(long)
AC_CHECK_TYPES([int8_t, uint8_t, u_int8_t])
AC_CHECK_TYPES([int16_t, uint16_t, u_int16_t])
AC_CHECK_TYPES([int32_t, uint32_t, u_int32_t])
AC_CHECK_TYPES([int64_t, uint64_t, u_int64_t])
AC_CHECK_TYPES([u_char, u_short, u_long])
AC_CHECK_TYPES([size_t, ssize_t])

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

AH_BOTTOM([
#include "validator/validator-compat.h"
])

dnl ----------------------------------------------------------------------
if test "x$ac_cv_user_prompt_VAL_RESOLV_CONF" = "x" -o "x$ac_cv_user_prompt_VAL_ROOT_HINTS" = "x" ; then
   AC_CACHE_CHECK(if you have run configure before, ac_cv_have_warned,
   [
   if test "x$defaults" = "xno"; then
cat << EOF

         ************** Configuration Section **************

        You are about to be prompted with a series of questions.  Answer
them carefully, as they determine how the validator library and related
applications are to function.

        After the configure script finishes, you can browse the newly
created include/validator/validator-config.h file for further - less
important - parameters to modify.  Be careful if you re-run configure
though, since the file will be overwritten.

-Press return to continue-
EOF

      read tmpinput
      ac_cv_have_warned="yes"
      echo $ECHO_N "disabling above prompt for future runs...  $ECHO_C"
   else
     ac_cv_have_warned="no"
   fi
   ])
else
  ac_cv_have_warned="yes"
fi

AC_PROMPT_USER(VAL_RESOLV_CONF,[

***  Resolver configuration location:

    Enter the location for the resolver configuration file. If your
existing resolver configuration (typically /etc/resolv.conf) contains
DNSSEC-aware resolvers, you may specify that file. This has the benefit
of only having to remember to update a single file if your resolver
configuration changes.

    If you do not wish to modify your default resolver configuration
for any reason (e.g. you are using fast/close non-dnssec aware resolvers
and don't want to impact existing applications), you may use the default,
DNSSEC-Tools specific, location.

    Providing a file via the --with-resolv-conf argument to configure will
avoid this prompt.

Location to read resolver configuration],$val_sysconfdir/dnssec-tools/resolv.conf,quoted)
VAL_RESOLV_CONF=$ac_cv_user_prompt_VAL_RESOLV_CONF
AC_SUBST(VAL_RESOLV_CONF)
AC_MSG_CACHE_ADD(libval resolver configuration  : $VAL_RESOLV_CONF)

default_hints="$val_sysconfdir/dnssec-tools/root.hints"
if test "x$defaults" != "xyes" ; then
   search_dirs="$val_sysconfdir/dnssec-tools $prefix/var/named $prefix/var/named/etc/named $val_sysconfdir/named $val_sysconfdir"
   if test "x$prefix" != "x" ; then
      search_dirs="$search_dirs /var/named /var/named/etc/named /etc/named /etc"
   fi
   for dir in $search_dirs ;
   do
      for file in named.ca named.root named.cache db.cache root.hints ;
      do
         if test -r "$dir/$file" ; then
            default_hints="$dir/$file"
            break 2
         fi
      done
   done
fi
AC_PROMPT_USER(VAL_ROOT_HINTS,[

*** Root hints location:

    Enter the location of the root hints file, or 'none' to install
and use the copy provided with DNSSEC-Tool (see validator/etc/root.hints).

    Providing a file via the --with-root-hints argument to configure will
avoid this prompt.

Location to read root hints configuration],$val_sysconfdir/dnssec-tools/root.hints,quoted)
VAL_ROOT_HINTS=$ac_cv_user_prompt_VAL_ROOT_HINTS
#AC_DEFINE_UNQUOTED(VAL_ROOT_HINTS,"$VAL_ROOT_HINTS")
AC_SUBST(VAL_ROOT_HINTS)
AC_MSG_CACHE_ADD(libval root hints              : $VAL_ROOT_HINTS)


AH_TEMPLATE([LIBVAL_INLINE_POLICY], [Define if inline configuration files are to be used.])

dnl inline_val_conf_file=$VAL_CONFIGURATION_FILE
AC_ARG_WITH(inline-dnsval-conf,
        [  --with-inline-dnsval-conf=FILE dnsval config file.],
        [inline_val_conf_file=$with_inline_dnsval_conf],
        [inline_val_conf_file=etc/dnsval.conf])

dnl inline_root_conf_file=$VAL_ROOT_HINTS
AC_ARG_WITH(inline-root-hints,
        [  --with-inline-root-hints=FILE root.hints file.],
        [inline_root_conf_file=$with_inline_root_hints],
        [inline_root_conf_file=etc/root.hints])
    
dnl inline_resolv_conf_file=$VAL_RESOLV_CONF
AC_ARG_WITH(inline-resolv-conf,
        [  --with-inline-resolv-conf=FILE resolv.conf file.],
        [inline_resolv_conf_file=$with_inline_resolv_conf],
        [inline_resolv_conf_file=etc/resolv.conf])
    
AC_ARG_WITH(inline-confs,
        [  --without-inline-confs         Disable use of inline (pre-configured) configuration data.],
        [inline_confs=$with_inline_confs],
        [inline_confs="yes"])

if test "$inline_confs" = "yes"; then
    AC_DEFINE(LIBVAL_INLINE_POLICY, 1)
else
    inline_val_conf_file=
    inline_root_conf_file=
    inline_resolv_conf_file=
fi

VAL_CONF_INLINE_DATA=
if test "$cross_compiling" = yes ; then
  # ac_check_file dosn't work when cross compiling, so assume it's there
  AC_MSG_CACHE_ADD(Validator inline configuration : $inline_val_conf_file)
else
  AC_CHECK_FILE($inline_val_conf_file,
                  [VAL_CONF_INLINE_DATA=`sed -e 's/"/\\\"/g ; s/^/"/ ; s/$/\\\n"/;' $inline_val_conf_file`
                AC_MSG_CACHE_ADD(Validator inline configuration : $inline_val_conf_file)])
fi
AC_SUBST(VAL_CONF_INLINE_DATA)

ROOT_HINTS_INLINE_DATA=
if test "$cross_compiling" = yes ; then
  # ac_check_file dosn't work when cross compiling, so assume it's there
  AC_MSG_CACHE_ADD(Root hints inline file         : $inline_root_conf_file)
else
  AC_CHECK_FILE($inline_root_conf_file,
                [ ROOT_HINTS_INLINE_DATA=`sed -e 's/"/\\\"/g ; s/^/"/ ; s/$/\\\n"/;' $inline_root_conf_file` 
                AC_MSG_CACHE_ADD(Root hints inline file         : $inline_root_conf_file)])
fi
AC_SUBST(ROOT_HINTS_INLINE_DATA)

RESOLV_CONF_INLINE_DATA=
if test "$cross_compiling" = yes ; then
  # ac_check_file dosn't work when cross compiling, so assume it's there
  AC_MSG_CACHE_ADD(Resolver inline configuration  : $inline_resolv_conf_file)
else
  AC_CHECK_FILE($inline_resolv_conf_file,
                [ RESOLV_CONF_INLINE_DATA=`sed -e 's/"/\\\"/g ; s/^/"/ ; s/$/\\\n"/;' $inline_resolv_conf_file`
                AC_MSG_CACHE_ADD(Resolver inline configuration  : $inline_resolv_conf_file)])
fi
AC_SUBST(RESOLV_CONF_INLINE_DATA)


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

AC_CONFIG_FILES([
 Makefile:Makefile.top:Makefile.in
 libsres/Makefile:Makefile.top:libsres/Makefile.in:Makefile.bot
 libval/Makefile:Makefile.top:libval/Makefile.in:Makefile.bot
 libval_shim/Makefile:Makefile.top:libval_shim/Makefile.in:Makefile.bot
 doc/Makefile:Makefile.top:doc/Makefile.in:Makefile.bot
 apps/Makefile:Makefile.top:apps/Makefile.in:Makefile.bot
])
AC_CONFIG_FILES([libval-config:libval-config.in], [chmod +x libval-config])
AC_CONFIG_FILES([libval/val_inline_conf.h:libval/val_inline_conf.h.in])
AC_CONFIG_COMMANDS([default], echo timestamp > stamp-h)
AC_OUTPUT
AC_MSG_CACHE_DISPLAY()
