# -*- autoconf -*-
#########################################
##
# Checks for libraries that are run
# after the agent module tests.
##
#########################################

#   HAVE_LIB Definitions used in the Net-SNMP code base:
#
#       HAVE_LIBCRYPTO:	apps
#       HAVE_LIBRPM:	agent
#

##
#   Simple checks:
##

#   ELF libraries
#       Typically used for 'nlist'  (needed for uptime)
#
if test "x$with_elf" != "xno"; then
case $target_os in
	linux*) # add hosts which don't use nlist here
                ;;
	irix*) # Check for nlist in mld (irix)
		AC_CHECK_LIB(elf, nlist)
		AC_CHECK_LIB(elf, nlist64)
		AC_CHECK_LIB(mld, nlist)
		;;
	*) # default
		AC_CHECK_LIB(elf, nlist)
		;;
esac
fi
 

#   libsocket
#       Needed for 'socket(2)'                          (Solaris)
#       Possibly also for 'gethostname(3)'              (non-Solaris)
#           (alternatively in libnsl                    (Solaris)
#            or in libws2_32)                           (MinGW)
#
case x$with_socklib in
xwinsock2)
  #   libws2_w32                                        (MinGW)
  #
  netsnmp_save_LIBS="$LIBS"
  LIBS="$LIBS -lws2_32"
  AC_LINK_IFELSE([AC_LANG_PROGRAM([
    #include <winsock2.h>
  ], [
    socket(0, 0, 0);
    gethostbyname(0);
  ])], [
    # Keep LIBS.
    true
  ], [
    # Restore LIBS and complain.
    LIBS="$netsnmp_save_LIBS"
    AC_MSG_ERROR([libws2_32 not found.])
  ])
  ;;
*)
  AC_SEARCH_LIBS(socket, socket,,AC_MSG_ERROR([socket(2) not found.]))
  AC_SEARCH_LIBS(gethostbyname, [nsl socket])
  AC_CHECK_FUNCS(gethostbyname)
  AC_SEARCH_LIBS(gethostbyaddr, [nsl socket])
  AC_CHECK_FUNCS(gethostbyaddr)
  ;;
esac


#   libiphlpapi                                         (Cygwin and MinGW)
#
netsnmp_save_LIBS="$LIBS"
LIBS="$LIBS -liphlpapi"
AC_LINK_IFELSE([AC_LANG_PROGRAM([
  #include <winsock2.h>
  #include <iphlpapi.h>
], [
  IpRenewAddress(0);
])], [
  # Keep LIBS.
  true
], [
  # Restore LIBS and complain.
  LIBS="$netsnmp_save_LIBS"
  case x$with_socklib in
  xwinsock2)
    AC_MSG_ERROR([libiphlpapi not found.])
    ;;
  esac
])

AC_MSG_CHECKING([for MIB_IF_TABLE2])
AC_LINK_IFELSE([AC_LANG_PROGRAM([
  #define _WIN32_WINNT 0xffff
  #include <winsock2.h>
  #include <ws2tcpip.h>
  #include <iphlpapi.h>
],[
  return sizeof(MIB_IF_TABLE2)
])],[
  AC_MSG_RESULT([yes])
  AC_DEFINE([HAVE_MIB_IF_TABLE2], 1,
    [Define if MIB_IF_TABLE2 is available])],
  [AC_MSG_RESULT([no])])


#   libregex                                            (MinGW)
#
netsnmp_save_LIBS="$LIBS"
LIBS="$LIBS -lregex"
AC_LINK_IFELSE([AC_LANG_PROGRAM([
  #include <sys/types.h>
  #include <regex.h>
], [
  regcomp(0, 0, 0);
])], [
  # Keep LIBS.
  true
], [
  # Restore LIBS.
  LIBS="$netsnmp_save_LIBS"
])


#   libsnmpapi                                          (MinGW)
#
netsnmp_save_LIBS="$LIBS"
LIBS="$LIBS -lsnmpapi"
AC_LINK_IFELSE([AC_LANG_PROGRAM([
  #include <snmp.h>
], [
  SnmpUtilMemAlloc(0);
])], [
  # Keep LIBS.
  true
], [
  # Restore LIBS.
  LIBS="$netsnmp_save_LIBS"
])


#   kstat library                                       (Solaris)
#
AC_CHECK_LIB(kstat, kstat_lookup, [LNETSNMPLIBS="$LNETSNMPLIBS -lkstat"])


#   socket library                                      (Solaris)
#
AC_CHECK_LIB(socket, in6addr_any, [LNETSNMPLIBS="$LNETSNMPLIBS -lsocket"])


#
#   librt
#

NETSNMP_SEARCH_LIBS(clock_gettime, rt,
        AC_DEFINE(HAVE_CLOCK_GETTIME, 1,
                [Define to 1 if you have the `clock_gettime' library]),,,
        LNETSNMPLIBS)


##
#   MIB-module-specific checks
##

#   libdevstat
#        (for diskIO MIB)
#
#   Not-Used: HAVE_GETDEVS/HAVE_DEVSTAT_GETDEVS
#
echo " $module_list " | $GREP " ucd-snmp/diskio " >/dev/null
if test $? -eq 0 ; then
	NETSNMP_SEARCH_LIBS(getdevs, devstat,
                AC_DEFINE(HAVE_GETDEVS, 1,
                        [define if you have getdevs()]),,,LMIBLIBS)
	NETSNMP_SEARCH_LIBS(devstat_getdevs, devstat,
                AC_DEFINE(HAVE_DEVSTAT_GETDEVS, 1,
                        [define if you have devstat_getdevs()]),,,LMIBLIBS)
fi

#   libpci
#        (for if-mib description)
#
NETSNMP_SEARCH_LIBS(pci_lookup_name, pci,
	AC_DEFINE(HAVE_PCI_LOOKUP_NAME, 1,
		[define if you have pci_lookup_name()]),,,LMIBLIBS)

#   LM-SENSORS-MIB support
#
echo " $module_list " | $GREP -i "ucd-snmp/lmsensor" > /dev/null
if test $? -eq 0 ; then
        AC_MSG_CHECKING([for sensors support])
        case $target_os in
        solaris*)
            #  Agent:  HAVE_PICL_H
            #
            AC_CHECK_HEADER(picl.h, LMIBLIBS="${LMIBLIBS} -lpicl",)
            AC_CHECK_HEADERS(picl.h)
            ;;
        *)
            #  Not-Used:  HAVE_SENSORS_SENSORS_H
            #
            AC_CHECK_HEADER(sensors/sensors.h,,
                [AC_MSG_ERROR([asked to use lm_sensors but I couldn't find sensors/sensors.h])])
            NETSNMP_SEARCH_LIBS(sensors_get_detected_chips, sensors,,
                [AC_MSG_ERROR([asked to use lm_sensors but I couldn't find -lsensors])],,
                LMIBLIBS)
            ;;
        esac
fi

##
#   More complex checks:
##

#   Security related functions
#
NONAGENTLIBS=$LIBS	 # save old libraries
SSHPROG=no
TLSPROG=no
if test "x$tryopenssl" != "xno" -a "x$tryopenssl" != "xinternal"; then
    # check for libpkcs11 if asked
    if test "x$askedopenssl" != "xyes" -a "x$askedpkcs" = "xyes"; then
        AC_CHECK_LIB(pkcs11, C_Initialize,
            AC_DEFINE(HAVE_LIBPKCS11, 1,
                [Define to 1 if you have the `pkcs11' library (-lpkcs11).])
		LIBPKCS11="-lpkcs11")
    else 
        if test "x$tryrsaref" != "xno"; then
            AC_CHECK_LIB(rsaref, RSAPrivateDecrypt)
            AC_CHECK_LIB(RSAglue, RSA_PKCS1_RSAref)
        fi

	if test "x${with_wolfssl}" != x; then
	    AC_CHECK_LIB([wolfssl], [wolfSSL_EVP_md5], [
	                   ac_cv_lib_EVP_md5=yes
			   CRYPTO="wolfssl"
			   LIBCRYPTO="-lwolfssl"
			 ])
	    AC_CHECK_HEADERS([wolfssl/wolfcrypt/asn.h		\
			      wolfssl/wolfcrypt/logging.h])
	    AC_DEFINE([HAVE_VOID_EVP_MD_CTX_INIT], [1],
                      [Define this macro if EVP_MD_CTX_init() returns void])
	else
	    ifdef(
	      [PKG_CHECK_EXISTS],
	      [
	        PKG_CHECK_EXISTS([openssl],
	          [PKG_CHECK_MODULES([OPENSSL], [openssl])])
	        if test "x${PKG_CONFIG}" != "x"; then
	          AC_MSG_NOTICE([OPENSSL_CFLAGS=${OPENSSL_CFLAGS}])
	          AC_MSG_NOTICE([OPENSSL_LIBS=${OPENSSL_LIBS}])
                  CRYPTO=use_pkg_config_for_openssl
	          LIBCRYPTO="-lssl ${OPENSSL_LIBS}"
                  netsnmp_save_LIBS="$LIBS"
	          LIBS="-lssl ${OPENSSL_LIBS}"
	          AC_CHECK_FUNC([EVP_md5])
                  LIBS="$netsnmp_save_LIBS"
	        fi
              ],
	      [
	        AC_MSG_ERROR([pkg-config must be installed before autoconf is run.])
	      ]
	    )
    
	    if test "x${PKG_CONFIG}" = "x"; then
	        AC_MSG_NOTICE([Could not find pkg-config.])
                AC_MSG_CHECKING([for -leay32])
                netsnmp_save_LIBS="$LIBS"
                LIBS="-leay32"
                AC_LINK_IFELSE([AC_LANG_PROGRAM([
                        #include <openssl/evp.h>],
                        [return EVP_md5();])],
                    [AC_MSG_RESULT(yes)
		     ac_cv_lib_EVP_md5=yes
		     CRYPTO="eay32"
		     LIBCRYPTO="-leay32"
		    ],
                    [AC_MSG_RESULT(no)]
                )
                LIBS="$netsnmp_save_LIBS"
        
                if test x$CRYPTO = x; then
                    AC_CHECK_LIB([crypto], [EVP_md5],
                                 [ac_cv_lib_EVP_md5=yes
			          CRYPTO="crypto"
			          LIBCRYPTO="-lcrypto"], [
                        unset ac_cv_lib_crypto_EVP_md5
                        AC_CHECK_LIB([crypto], [EVP_md5],
                                     [ac_cv_lib_EVP_md5=yes
			              CRYPTO="crypto"
				      LIBCRYPTO="-lcrypto -lz"],
				     [], [-lz])
                    ])
                fi
	    fi
	fi

        if test x$CRYPTO != x; then
            AC_DEFINE(HAVE_LIBCRYPTO, 1,
                [Define to 1 if you have the OpenSSL library (-lcrypto or -leay32).])
	fi
	AH_TEMPLATE([HAVE_AES_CFB128_ENCRYPT],
	    [Define to 1 if you have the `AES_cfb128_encrypt' function.])
	AH_TEMPLATE([HAVE_ASN1_STRING_GET0_DATA],
	    [Define to 1 if you have the `ASN1_STRING_get0_data' function.])
	AH_TEMPLATE([HAVE_DH_GET0_KEY],
	    [Define to 1 if you have the `DH_get0_key' function.])
	AH_TEMPLATE([HAVE_DH_GET0_PQG],
	    [Define to 1 if you have the `DH_get0_pqg' function.])
	AH_TEMPLATE([HAVE_DH_SET0_PQG],
	    [Define to 1 if you have the `DH_set0_pqg' function.])
	AH_TEMPLATE([HAVE_DTLS_METHOD],
	    [Define to 1 if you have the `DTLS_method' function.])
	AH_TEMPLATE([HAVE_DTLSV1_METHOD],
	    [Define to 1 if you have the `DTLSv1_method' function.])
	AH_TEMPLATE([HAVE_ERR_GET_ERROR_ALL],
	    [Define to 1 if you have the `ERR_get_error_all' function.])
	AH_TEMPLATE([HAVE_ERR_GET_ERROR_FUNC],
	    [Define to 1 if you have the `ERR_get_error_func' function.])
	AH_TEMPLATE([HAVE_EVP_MD_CTX_CREATE],
	    [Define to 1 if you have the `EVP_MD_CTX_create' function.])
	AH_TEMPLATE([HAVE_EVP_MD_CTX_DESTROY],
	    [Define to 1 if you have the `EVP_MD_CTX_destroy' function.])
	AH_TEMPLATE([HAVE_EVP_MD_CTX_FREE],
	    [Define to 1 if you have the `EVP_MD_CTX_free' function.])
	AH_TEMPLATE([HAVE_EVP_MD_CTX_NEW],
	    [Define to 1 if you have the `EVP_MD_CTX_new' function.])
	AH_TEMPLATE([HAVE_EVP_AES_128_CFB],
	    [Define to 1 if you have the `EVP_aes_128_cfb' function.])
	AH_TEMPLATE([HAVE_EVP_SHA224],
	    [Define to 1 if you have the `EVP_sha224' function.])
	AH_TEMPLATE([HAVE_EVP_SHA384],
	    [Define to 1 if you have the `EVP_sha384' function.])
	AH_TEMPLATE([HAVE_OPENSSL_SK_NUM],
	    [Define to 1 if you have the `OPENSSL_sk_num' function.])
	AH_TEMPLATE([HAVE_OPENSSL_SK_VALUE],
	    [Define to 1 if you have the `OPENSSL_sk_value' function.])
	AH_TEMPLATE([HAVE_OPENSSL_ADD_ALL_ALGORITHMS],
	    [Define to 1 if you have the `OpenSSL_add_all_algorithms' function.])
	AH_TEMPLATE([HAVE_SSL_CTX_SET_COOKIE_GENERATE_CB],
	    [Define to 1 if you have the `SSL_CTX_set_cookie_generate_cb' function.])
	AH_TEMPLATE([HAVE_SSL_GET1_PEER_CERTIFICATE],
	    [Define to 1 if you have the `SSL_get1_peer_certificate' function.])
	AH_TEMPLATE([HAVE_SSL_LIBRARY_INIT],
	    [Define to 1 if you have the `SSL_library_init' function.])
	AH_TEMPLATE([HAVE_SSL_LOAD_ERROR_STRINGS],
	    [Define to 1 if you have the `SSL_load_error_strings' function.])
	AH_TEMPLATE([HAVE_TLS_METHOD],
	    [Define to 1 if you have the `TLS_method' function.])
	AH_TEMPLATE([HAVE_TLSV1_METHOD],
	    [Define to 1 if you have the `TLSv1_method' function.])
	AH_TEMPLATE([HAVE_X509_NAME_ENTRY_GET_DATA],
	    [Define to 1 if you have the `X509_NAME_ENTRY_get_data' function.])
	AH_TEMPLATE([HAVE_X509_NAME_ENTRY_GET_OBJECT],
	    [Define to 1 if you have the `X509_NAME_ENTRY_get_object' function.])
	AH_TEMPLATE([HAVE_X509_GET_SIGNATURE_NID],
	    [Define to 1 if you have the `X509_get_signature_nid' function.])
	for symbol in				\
	    AES_cfb128_encrypt			\
	    ASN1_STRING_get0_data		\
	    DH_get0_key				\
	    DH_get0_pqg				\
	    DH_set0_pqg				\
	    DTLS_method				\
	    DTLSv1_method			\
	    ERR_get_error_all			\
	    ERR_get_error_func			\
	    EVP_MD_CTX_create			\
	    EVP_MD_CTX_destroy			\
	    EVP_MD_CTX_free			\
	    EVP_MD_CTX_new			\
	    EVP_aes_128_cfb			\
	    EVP_sha224				\
	    EVP_sha384				\
	    OPENSSL_sk_num			\
	    OPENSSL_sk_value			\
	    OpenSSL_add_all_algorithms		\
	    SSL_CTX_set_cookie_generate_cb	\
	    SSL_get1_peer_certificate		\
	    SSL_library_init			\
	    SSL_load_error_strings		\
	    TLS_method				\
	    TLSv1_method			\
	    X509_NAME_ENTRY_get_data		\
	    X509_NAME_ENTRY_get_object		\
	    X509_get_signature_nid		\
	; do
            [have_symbol=$(echo "HAVE_${symbol}" | tr '[:lower:]' '[:upper:]')]
	    AC_MSG_CHECKING([for ${symbol}])
		AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
		#ifdef HAVE_OPENSSL_AES_H
		#include <openssl/aes.h>
		#endif
		#ifdef HAVE_OPENSSL_ERR_H
		#include <openssl/err.h>
		#endif
		#ifdef HAVE_OPENSSL_EVP_H
		#include <openssl/evp.h>
		#endif
		#ifdef HAVE_OPENSSL_SSL_H
		#include <openssl/ssl.h>
		#endif
		#ifdef HAVE_OPENSSL_STACK_H
		#include <openssl/stack.h>
		#endif
		#ifdef HAVE_OPENSSL_X509_H
		#include <openssl/x509.h>
		#endif
		#ifdef HAVE_OPENSSL_X509V3_H
		#include <openssl/x509v3.h>
		#endif
		]], [[
		#ifndef ${symbol}
		return !${symbol}
		#endif
		]])],
		[AC_MSG_RESULT([yes])
		 eval "ac_cv_func_${symbol}=yes"
		 AC_DEFINE_UNQUOTED([${have_symbol}])],
		[AC_MSG_RESULT([no])])
	done
        AC_MSG_CHECKING([whether 2nd app_verify_cookie_cb() arg is const])
        netsnmp_save_CFLAGS="$CFLAGS"
        CFLAGS="$CFLAGS -Werror"
        AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
          #include <openssl/ssl.h>
        ]],[[
          int (*app_verify_cookie_cb)(SSL *ssl,
                                      const unsigned char *cookie,
                                      unsigned int cookie_len) = NULL;
          SSL_CTX_set_cookie_verify_cb(NULL, app_verify_cookie_cb);
        ]])],
        [AC_MSG_RESULT(yes); ac_cv_saccaq=const],
        [AC_MSG_RESULT(no)])
        AC_DEFINE_UNQUOTED([SECOND_APPVERIFY_COOKIE_CB_ARG_QUALIFIER],
                           [$ac_cv_saccaq],
                           [Qualifier for second argument of the appverify_cookie_cb() callback function])
        CFLAGS="$netsnmp_save_CFLAGS"

        if echo " $transport_result_list " | $GREP "DTLS" > /dev/null; then
	    if test x$ac_cv_func_DTLSv1_method = xyes -o \
                    x$ac_cv_func_DTLS_method = xyes; then
                AC_DEFINE(HAVE_LIBSSL_DTLS, 1,
                    [Define to 1 if your `ssl' library supports DTLS.])
                if test "x$with_wolfssl" = x; then
                    LIBCRYPTO=" -lssl $LIBCRYPTO"
		fi
	    else
		AC_MSG_ERROR([The DTLS based transports require the libssl library from OpenSSL to be available and support DTLS])
            fi
            TLSPROG=yes
        fi
        if echo " $transport_result_list " | $GREP "TLS" > /dev/null; then
	    if test x$ac_cv_func_TLSv1_method = xyes -o \
                    x$ac_cv_func_TLS_method = xyes; then
                AC_DEFINE(HAVE_LIBSSL, 1,
                    [Define to 1 if you have the `ssl' library.])
                if test "x$with_wolfssl" = x; then
                    LIBCRYPTO=" -lssl $LIBCRYPTO"
                fi
            else
		AC_MSG_ERROR([The TLS based transports require the libssl library from OpenSSL to be available])
            fi
            TLSPROG=yes
        fi
        if echo " $transport_result_list " | $GREP " SSH " > /dev/null; then
	    AC_SEARCH_LIBS(libssh2_session_handshake, [ssh2])
	    AC_CHECK_FUNCS(libssh2_session_handshake)
	    AC_CHECK_LIB(ssh2, libssh2_session_init_ex,
                AC_DEFINE(HAVE_LIBSSH2, 1,
                    [Define to 1 if you have the `ssh2' library (-lssh2).])
                LIBCRYPTO=" -lssh2 $LIBCRYPTO",
                AC_MSG_ERROR([The SSH transport requires the libssh2 library to be available]),)
            SSHPROG=yes
        fi
    fi
elif test "x$askedpkcs" = "xyes"; then
    AC_CHECK_LIB(pkcs11, C_Initialize,
        AC_DEFINE(HAVE_LIBPKCS11)
        LIBPKCS11="-lpkcs11")
fi

AH_TEMPLATE([HAVE_ASN_BOOLEAN],
    [Define to 1 if the `ASN_BOOLEAN' constant is defined in an SSL header file.])
AH_TEMPLATE([HAVE_ASN_INTEGER],
    [Define to 1 if the `ASN_INTEGER' constant is defined in an SSL header file.])
AH_TEMPLATE([HAVE_ASN_OBJECT_ID],
    [Define to 1 if the `ASN_OBJECT_ID' constant is defined in an SSL header file.])
AH_TEMPLATE([HAVE_ASN_SEQUENCE],
    [Define to 1 if the `ASN_SEQUENCE' constant is defined in an SSL header file.])
AH_TEMPLATE([HAVE_ASN_SET],
    [Define to 1 if the `ASN_SET' constant is defined in an SSL header file.])
for symbol in ASN_BOOLEAN ASN_INTEGER ASN_OBJECT_ID ASN_SEQUENCE ASN_SET; do
    AC_MSG_CHECKING([for ${symbol}])
    AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <openssl/ssl.h>]], [[
    #ifndef ${symbol}
    return !${symbol}
    #endif
    ]])],
    [AC_MSG_RESULT([yes])
     AC_DEFINE_UNQUOTED([HAVE_${symbol}])],
    [AC_MSG_RESULT([no])])
done

LIBS=$NONAGENTLIBS	 # restore old libraries
if test "x$SSHPROG" = "xyes"; then
    NETSNMP_BUILD_SSH_PROG_TRUE=''
    NETSNMP_BUILD_SSH_PROG_FALSE='#'
else
    NETSNMP_BUILD_SSH_PROG_TRUE='#'
    NETSNMP_BUILD_SSH_PROG_FALSE=''
fi
if test "x$TLSPROG" = "xyes" -a "x$enable_read_only" != "xyes" ; then
    NETSNMP_BUILD_TLS_PROG_TRUE=''
    NETSNMP_BUILD_TLS_PROG_FALSE='#'
else
    NETSNMP_BUILD_TLS_PROG_TRUE='#'
    NETSNMP_BUILD_TLS_PROG_FALSE=''
fi
if echo " ${security_mod_list} " | grep -q ' usm ' ; then
    if test "x$enable_read_only" != "xyes" ; then
    	NETSNMP_BUILD_USM_PROG_TRUE=''
    	NETSNMP_BUILD_USM_PROG_FALSE='#'
    else
    	NETSNMP_BUILD_USM_PROG_TRUE='#'
    	NETSNMP_BUILD_USM_PROG_FALSE=''
    fi
else
    NETSNMP_BUILD_USM_PROG_TRUE='#'
    NETSNMP_BUILD_USM_PROG_FALSE=''
fi
if test "x$enable_read_only" != "xyes" ; then
    NETSNMP_BUILD_SET_PROG_TRUE=""
    NETSNMP_BUILD_SET_PROG_FALSE="#"
else
    NETSNMP_BUILD_SET_PROG_TRUE="#"
    NETSNMP_BUILD_SET_PROG_FALSE=""
fi
AC_SUBST(NETSNMP_BUILD_SSH_PROG_TRUE)
AC_SUBST(NETSNMP_BUILD_SSH_PROG_FALSE)
AC_SUBST(NETSNMP_BUILD_TLS_PROG_TRUE)
AC_SUBST(NETSNMP_BUILD_TLS_PROG_FALSE)
AC_SUBST(NETSNMP_BUILD_USM_PROG_TRUE)
AC_SUBST(NETSNMP_BUILD_USM_PROG_FALSE)
AC_SUBST(NETSNMP_BUILD_SET_PROG_TRUE)
AC_SUBST(NETSNMP_BUILD_SET_PROG_FALSE)

#
# If we're using internal crypto support we need to compile in extra files
#
crypto_files_c=""
crypto_files_o=""
crypto_files_lo=""
if test "x$tryopenssl" != "xno" -a "x$CRYPTO" = "x"; then
   # If we:
   #   1) couldn't find needed openssl support
   #   2) OR were asked to use internal support
   #   3) AND we have the needed files
   # then use it
   CRYPTO="internal"
   crypto_files_c="openssl/openssl_sha1.c openssl/openssl_md5.c openssl/openssl_set_key.c openssl/openssl_des_enc.c openssl/openssl_cbc_enc.c openssl/openssl_aes_cfb.c openssl/openssl_aes_core.c openssl/openssl_cfb128.c"
   crypto_files_o="openssl/openssl_sha1.o openssl/openssl_md5.o openssl/openssl_set_key.o openssl/openssl_des_enc.o openssl/openssl_cbc_enc.o openssl/openssl_aes_cfb.o openssl/openssl_aes_core.oo openssl/openssl_cfb128.o"
   crypto_files_lo="openssl/openssl_sha1.lo openssl/openssl_md5.lo openssl/openssl_set_key.lo openssl/openssl_des_enc.lo openssl/openssl_cbc_enc.lo openssl/openssl_aes_cfb.lo openssl/openssl_aes_core.lo openssl/openssl_cfb128.lo"
   AC_DEFINE([HAVE_AES_CFB128_ENCRYPT], [1],
             [Define to 1 if you have the `AES_cfb128_encrypt' function.])
fi
AC_SUBST(crypto_files_c)
AC_SUBST(crypto_files_o)
AC_SUBST(crypto_files_lo)

#
# if we can do any sort of crypto then note it
#
if test "x$CRYPTO" != "x" ; then
  AC_DEFINE(NETSNMP_CAN_DO_CRYPTO, 1, "Define if crytography support is possible")
fi

#   TCP wrapper support
#
_cppflags="${CPPFLAGS}"
_ldflags="${LDFLAGS}"
if test "$with_libwrap" != "no"; then
  if test "$with_libwrap" != "yes"; then
    CPPFLAGS="${CPPFLAGS} -I$with_libwrap/include"
    LDFLAGS="${LDFLAGS} -L$with_libwrap/lib"
  fi

  _libs=${LIBS}
  AC_CHECK_HEADER(tcpd.h, ,
    AC_MSG_ERROR([Asked to use libwrap but I couldn't find tcpd.h.]))

  LIBS="$LIBS -lwrap"
  AC_MSG_CHECKING([for TCP wrappers library -lwrap])
  # XXX: should check for hosts_ctl
  AC_LINK_IFELSE([AC_LANG_PROGRAM([[#include <sys/types.h>
     #include <tcpd.h>
     int allow_severity = 0;
     int deny_severity  = 0;]],
    [[hosts_access((void *)0)]])],
    [AC_MSG_RESULT([yes])
     AC_DEFINE(NETSNMP_USE_LIBWRAP)
     test "$with_libwrap" != no -a "$with_libwrap" != yes && _wraplibs="-L$with_libwrap/lib"
     _wraplibs="$_wraplibs -lwrap"],
    [AC_MSG_RESULT([no])
     # Linux RedHat 6.1 won't link libwrap without libnsl
     AC_CHECK_FUNC(yp_get_default_domain, ,
       AC_CHECK_LIB(nsl, yp_get_default_domain))
     AC_MSG_CHECKING([for TCP wrappers library -lwrap linked with -lnsl])
     AC_LINK_IFELSE([AC_LANG_PROGRAM([[#include <sys/types.h>
	#include <tcpd.h>
	int allow_severity = 0;
	int deny_severity  = 0;]],
       [[hosts_access((void *)0)]])],
       [AC_MSG_RESULT(yes)
	AC_DEFINE(NETSNMP_USE_LIBWRAP)
	test "$with_libwrap" != no -a "$with_libwrap" != yes && _wraplibs="-L$with_libwrap/lib"
	_wraplibs="$_wraplibs -lwrap -lnsl"],
       [AC_MSG_RESULT(no)
	AC_MSG_ERROR(Asked to use libwrap but I couldn't find it.)])])
  LAGENTLIBS="$LAGENTLIBS $_wraplibs"
  CPPFLAGS=${_cppflags}
  LDFLAGS=${_ldflags}
  LIBS=${_libs}
fi


##
#   mysql
##
if test "x$with_mysql" = "xyes" ; then
  AC_PATH_PROGS(MYSQLCONFIG,mysql_config)
  test -x "$MYSQLCONFIG" \
	|| AC_MSG_ERROR([Could not find mysql_config and was specifically asked to use MySQL support])
  MYSQL_LIBS=`$MYSQLCONFIG --libs`
  MYSQL_INCLUDES=`$MYSQLCONFIG --include`
  _libs="${LIBS}"
  _cppflags="${CPPFLAGS}"
  LIBS="${LIBS} ${MYSQL_LIBS}"
  CPPFLAGS="${CPPFLAGS} ${MYSQL_INCLUDES}"
  AC_CHECK_HEADER(mysql.h,,
     [AC_MSG_ERROR([Could not find mysql.h and was specifically asked to use MySQL support])])
  AC_CHECK_HEADERS([my_global.h my_sys.h])
  AC_MSG_CHECKING([whether MY_INIT() works])
  AC_LINK_IFELSE([
    AC_LANG_PROGRAM(
      [[#ifdef HAVE_MY_GLOBAL_H
        #include <my_global.h>
        #endif
        #ifdef HAVE_MY_SYS_H
        #include <my_sys.h>
        #endif
        #include <mysql.h>]],
      [[MY_INIT("my_init_test")]])],
      [AC_MSG_RESULT(yes)
       AC_DEFINE([HAVE_MY_INIT], 1,
                 [Define if MY_INIT() is availabe in libmysqlclient])],
      [AC_MSG_RESULT(no)])
  AC_MSG_CHECKING([whether my_load_defaults() exists])
  AC_LINK_IFELSE([
    AC_LANG_PROGRAM(
      [[#ifdef HAVE_MY_GLOBAL_H
        #include <my_global.h>
        #endif
        #ifdef HAVE_MY_SYS_H
        #include <my_sys.h>
        #endif
        #include <mysql.h>]],
      [[my_load_defaults("my_init_test")]])],
      [AC_MSG_RESULT(yes)
       AC_DEFINE([HAVE_MY_LOAD_DEFAULTS], 1,
                 [Define if having my_load_defaults()])],
      [AC_MSG_RESULT(no)])
  AC_CHECK_FUNCS([load_defaults mysql_init mysql_options])
  CPPFLAGS="${_cppflags}"
  LIBS="${_libs}"
  AC_MSG_CACHE_ADD(MYSQL Trap Logging:         enabled)
else
  AC_MSG_CACHE_ADD(MYSQL Trap Logging:         unavailable)
fi
AC_SUBST(MYSQL_LIBS)
AC_SUBST(MYSQL_INCLUDES)

##
#   libpcap
##
NETSNMP_BUILD_PCAP_PROG_TRUE='#'
NETSNMP_BUILD_PCAP_PROG_FALSE=''
AC_CHECK_LIB([pcap], [pcap_create], [
   AC_CHECK_HEADERS([pcap/pcap.h], [
      NETSNMP_BUILD_PCAP_PROG_TRUE=''
      NETSNMP_BUILD_PCAP_PROG_FALSE='#'
       ])
])
AC_SUBST(NETSNMP_BUILD_PCAP_PROG_TRUE)
AC_SUBST(NETSNMP_BUILD_PCAP_PROG_FALSE)

##
#   Non-library checks
##

# nlist
AC_CHECK_FUNCS(nlist nlist64 knlist)

NETSNMP_SEARCH_LIBS([endwin], [curses ncurses], [
  AC_CHECK_HEADERS([curses.h])
  AC_CHECK_HEADERS([ncurses/curses.h])
  HAVE_LIBCURSES=TRUE],
  [HAVE_LIBCURSES=FALSE],,
  [LIBCURSES])
AC_SUBST([HAVE_LIBCURSES])
AC_SUBST([LIBCURSES])

#   libm for ceil
#
NETSNMP_SEARCH_LIBS(ceil, m,,,, LNETSNMPLIBS)
