# -*- autoconf -*-
#########################################
##
# Checks for libraries
##
#########################################

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

##
#   Simple checks:
##

# AC_CHECK_LIB(des, main)
# AC_CHECK_LIB(m,   asin)

#   dmalloc
#
if test "${with_dmalloc+set}" = set; then
   if test "$with_dmalloc" = yes -a -d $with_dmalloc ; then
       AC_ADD_SEARCH_PATH($with_dmalloc)
   fi
   AC_CHECK_HEADERS([dmalloc.h])
   AC_SEARCH_LIBS([dmalloc_message], [dmalloc])
fi

#   EFence
#       (This used to be on by default for developer checkout)
#
if test "x$use_efence" = "xyes"; then
    AC_SEARCH_LIBS([EF_Exit], [efence])
fi


#   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)
#
AC_SEARCH_LIBS(socket, socket,,AC_MSG_ERROR([socket(2) not found.]))
AC_SEARCH_LIBS(gethostbyname, [nsl socket])
AC_CHECK_FUNCS(gethostbyname)


#   kstat library                                       (Solaris)
#
NETSNMP_SEARCH_LIBS(kstat_lookup, kstat,
        AC_DEFINE(HAVE_LIBKSTAT, 1,
        	[Define to 1 if you have the `kstat' library (-lkstat).]),,,
        LNETSNMPLIBS)


#   libkvm
#
NETSNMP_SEARCH_LIBS(kvm_read, kvm,,,, LAGENTLIBS)
NETSNMP_SEARCH_LIBS(kvm_openfiles, kvm,,,, LAGENTLIBS)
NETSNMP_SEARCH_LIBS(kvm_getprocs, kvm,,,, LMIBLIBS)
NETSNMP_SEARCH_LIBS(kvm_getswapinfo, kvm,,,, LMIBLIBS)
netsnmp_save_LIBS="$LIBS"
LIBS="$LAGENTLIBS $LMIBLIBS $LIBS"
AC_CHECK_FUNCS([kvm_openfiles kvm_getprocs kvm_getswapinfo])
LIBS="$netsnmp_save_LIBS"


#   dynamic module support
#
NETSNMP_SEARCH_LIBS(dlopen, dl,,,,LMIBLIBS)
netsnmp_save_LIBS="$LIBS"
LIBS="$LMIBLIBS $LIBS"
AC_CHECK_FUNCS([dlopen])
LIBS="$netsnmp_save_LIBS"


##
#   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


#   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

#
# libnl (netlink)
#

if test "x$with_nl" != "xno"; then
    case $target_os in
    linux*) # Check for libnl (linux)
	NETSNMP_SEARCH_LIBS(
	    nl_connect, nl,
	    [AC_CHECK_HEADERS(netlink/netlink.h)],,, LMIBLIBS)
    ;;
    esac
fi



##
#   More complex checks:
##

#   Security related functions
#
NONAGENTLIBS=$LIBS	 # save old libraries
SSHINSTALLBINPROGS=""
if test "x$tryopenssl" != "xno"; then
    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

        AC_CHECK_LIB(crypto, EVP_md5,
            AC_DEFINE(HAVE_LIBCRYPTO, 1,
                [Define to 1 if you have the `crypto' library (-lcrypto).])
            LIBCRYPTO="-lcrypto")
        AC_CHECK_LIB(crypto, AES_cfb128_encrypt, 
            AC_DEFINE(HAVE_AES_CFB128_ENCRYPT, 1,
                [Define to 1 if you have the `AES_cfb128_encrypt' function.]))
        if echo " $transport_result_list " | $GREP " DTLS" > /dev/null; then
	    AC_CHECK_LIB(ssl, SSL_library_init,
                AC_DEFINE(HAVE_LIBSSL, 1,
                    [Define to 1 if you have the `ssl' library (-lssl).])
                LIBCRYPTO=" -lssl $LIBCRYPTO",	
		AC_MSG_ERROR([The DTLS based transports require the libssl library from OpenSSL to be available])	,-lcrypto)
        fi
        if echo " $transport_result_list " | $GREP " SSH " > /dev/null; then
	    AC_CHECK_LIB(ssh2, libssh2_session_startup,
                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]),)
            SSHINSTALLBINPROGS=sshtosnmp
        fi
    fi
elif test "x$askedpkcs" = "xyes"; then
    AC_CHECK_LIB(pkcs11, C_Initialize,
        AC_DEFINE(HAVE_LIBPKCS11)
        LIBPKCS11="-lpkcs11")
fi
LIBS=$NONAGENTLIBS	 # restore old libraries
AC_SUBST(SSHINSTALLBINPROGS)


#   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_TRY_LINK(
    [#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_TRY_LINK(
       [#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="$_wraplibs"
  CPPFLAGS=${_cppflags}
  LDFLAGS=${_ldflags}
  LIBS=${_libs}
fi


##
#   RPM checks
#
##
AC_CHECK_HEADERS(rpm/rpmlib.h rpm/header.h)

AC_CHECK_HEADER(rpm/rpmlib.h,rpmlib_h=yes,rpmlib_h=no)
AC_CHECK_HEADER(rpm/header.h,rpmheader_h=yes,rpmheader_h=no)

if test "x$rpmheader_h" = "xno" -o "x$rpmlib_h" = "xno" ; then
  if test "x$with_rpm" = "xyes" ; then
    AC_MSG_ERROR([Could not find either the RPM header files needed and was specifically asked to use RPM support])
  else
    with_rpm=no
  fi
fi

#
# rpm libraries only needed for the host resources mib software
# installed tables (on linux in particular)
#
if test "x$with_rpm" != "xno" && \
        echo " $module_list " | $GREP " host/hr_swinst " > /dev/null; then
  # ARG.  RPM is a real pain.
  # FWIW librpm.la, librpmio.la, and libpopt.la have correct dependencies.
  _rpmlibs=""

  # zlib is required for newer versions of rpm
  _cppflags="${CPPFLAGS}"
  _ldflags="${LDFLAGS}"

  # check for zlib
  if test "x${with_zlib}" != "xno" -a -d "${with_zlib}"; then
    CPPFLAGS="${CPPFLAGS} -I${with_zlib}/include"
    LDFLAGS="${LDFLAGS} -L${with_zlib}/lib"
  fi

  # dunno if this is needed for rpm-4.0.x, earlier probably needs.
  AC_CHECK_HEADER(zlib.h,
      NETSNMP_SEARCH_LIBS(gzread, z,,
          CPPFLAGS=${_cppflags} LDFLAGS=${_ldflags}, $LMIBLIBS, _rpmlibs),
      CPPFLAGS=${_cppflags} LDFLAGS=${_ldflags})

  # two variants of bzip2 need checking.
  if test "x${with_bzip2}" != "xno"; then
    if test -d "${with_bzip2}"; then
      CPPFLAGS="${CPPFLAGS} -I${with_bzip2}/include"
      LDFLAGS="${LDFLAGS} -L${with_bzip2}/lib"
    fi
    NETSNMP_SEARCH_LIBS(bzread, bz2,,, $LMIBLIBS, _rpmlibs)
    NETSNMP_SEARCH_LIBS(BZ2_bzread, bz2,,, $LMIBLIBS, _rpmlibs)
  fi

  # two variants of db1 need checking.
  NETSNMP_SEARCH_LIBS(dbopen, [db1 db],,, $LMIBLIBS, _rpmlibs)

  # two variants of db3 need checking.
  NETSNMP_SEARCH_LIBS(db_create, [db-3.1 db-3.0],,, $LMIBLIBS, _rpmlibs)
  
  # rpm-3.0.5 and later needs popt.
  NETSNMP_SEARCH_LIBS(poptParseArgvString, popt,,, $LMIBLIBS, _rpmlibs)
  
  # rpm-4.0.x needs rpmio.
  NETSNMP_SEARCH_LIBS(Fopen, rpmio,,, $LMIBLIBS, _rpmlibs)
  
  # rpm-4.0.3 librpmdb actually contains what we need.
  NETSNMP_SEARCH_LIBS(rpmdbOpen, rpmdb,,, $LMIBLIBS, _rpmlibs)
  # now check for rpm using the appropriate libraries.
  NETSNMP_SEARCH_LIBS(rpmGetFilesystemList, rpm, [
    AC_DEFINE(HAVE_LIBRPM, 1,
      [Define to 1 if you have the `rpm' library (-lrpm).])
    LMIBLIBS="$_rpmlibs $LMIBLIBS"
    CFLAGS="$CFLAGS -I/usr/include/rpm"
  ],, $LMIBLIBS, _rpmlibs)
fi

##
#   mysql
##
if test "x$with_mysql" = "xyes" ; then
  AC_PATH_PROGS(MYSQLCONFIG,mysql_config)
  AC_CHECK_HEADERS(mysql/mysql.h)
  AC_CHECK_HEADER(mysql/mysql.h,mysql_h=yes,mysql_h=no)
  if test "x$MYSQLCONFIG" = "x" -o "x$mysql_h" = "xno"; then
     AC_MSG_ERROR([Could not find mysql_config or mysql.h and was specifically asked to use MySQL support])
  fi
  MYSQL_LIBS=`$MYSQLCONFIG --libs`
  MYSQL_INCLUDES=`$MYSQLCONFIG --cflags`
  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)
  

##
#   Non-library checks
##

# nlist
AC_CHECK_FUNCS(nlist nlist64 knlist)

