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


##
#   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"; then
  # ARG.  RPM is a real pain.
  # FWIW librpm.la, librpmio.la, and libpopt.la have correct dependencies.
  _rpmlibs=""

  # rpm-4.0.x needs rpmio.
  NETSNMP_SEARCH_LIBS(rpmGetPath, rpmio,,, $LMIBLIBS, _rpmlibs)
  
  # now check for rpm using the appropriate libraries.
  NETSNMP_SEARCH_LIBS(rpmReadConfigFiles, rpm, [
    AC_DEFINE(HAVE_LIBRPM, 1,
      [Define to 1 if you have the `rpm' library (-lrpm).])
    LMIBLIBS="$_rpmlibs $LMIBLIBS"
  ],, $LMIBLIBS, _rpmlibs)

  # rpm 4.6 has incompatible API, turn on the legacy one
  NETSNMP_SEARCH_LIBS(headerGet,, [
    AC_DEFINE(HAVE_HEADERGET, 1,
      [Define to 1 if you have the headerGet function.])],, $LMIBLIBS)
fi

#
# getfsstat/getvfsstat
#
AC_SEARCH_LIBS(getfsstat, [nbutil])
AC_CHECK_FUNCS(getfsstat)
AC_CHECK_FUNCS(getvfsstat)
AC_FUNC_GETMNTENT

#
# kinfo_get_cpus
#
AC_SEARCH_LIBS(kinfo_get_cpus, [kinfo])
AC_CHECK_FUNCS(kinfo_get_cpus)

#
# diskIOLAx requires exp which may require -lm
#
NETSNMP_SEARCH_LIBS([exp], [m],,,, [LMIBLIBS])

#
#   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_getproc2, kvm,,,, LMIBLIBS)
NETSNMP_SEARCH_LIBS(kvm_getfiles, kvm,,,, LMIBLIBS)
NETSNMP_SEARCH_LIBS(kvm_getfile2, 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_getproc2 kvm_getswapinfo kvm_getfiles kvm_getfile2])
LIBS="$netsnmp_save_LIBS"

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

#
#   regex in process table
#
if test "x$with_pcre" != "xno"; then
  AC_CHECK_HEADER([pcre.h], [
      AC_DEFINE([HAVE_PCRE_H], [1], [Define to 1 if you have <pcre.h>.])
      pcre_h=yes
    ],
    [pcre_h=no]
  )
fi
if test "x$pcreheader_h" = "xno" -o "x$pcre_h" = "xno" ; then
  if test "x$with_pcre" = "xyes" ; then
    AC_MSG_ERROR([Could not find the pcre header file needed and was specifically asked to use pcre support])
  else
    with_pcre=no
  fi
fi

#
# pcre libraries only needed for the ucd process agent mib software
#
if test "x$with_pcre" != "xno"; then
  NETSNMP_SEARCH_LIBS([pcre_exec], [pcre], [
    LMIBLIBS="$LMIBLIBS -lpcre"
    ],,, LAGENTLIBS)
  AC_SUBST(LAGENTLIBS)
  AC_SUBST(LMIBLIBS)
fi

#
# libnl (netlink)
#

# The test below verifies whether the libnl-3 package been installed.
# This test works as follows:
# - If pkg-config is not installed at the time autoreconf is run,
#   autoreconf will fail.
# - If pkg-config is installed at the time autoreconf is run,
#   the generated configure script will try to detect the presence of
#   the libnl-3 library by looking up compile and linker flags in the
#   file called libnl-3.pc.
# - pkg-config settings can be overridden via the configure variables
#   LIBNL3_CFLAGS and LIBNL3_LIBS (added by the pkg-config m4 macro's to
#   the configure script -- see also ./configure --help).
# - The LIBNL3_CFLAGS and LIBNL3_LIBS configure variables can be used
#   even if the pkg-config executable is not present on the system on
#   which the configure script is run.
# - Although libnl-3 is only used on Linux, test presence of libnl-3 on
#   all platforms because this code is the first occurrence in the configure
#   script of PKG_* macros and because the search for the pkg-config executable
#   only occurs once.
if test "x$with_nl" != "xno"; then
    ifdef(
      [PKG_CHECK_EXISTS],
      [
        PKG_CHECK_EXISTS([libnl-3.0],
	  [PKG_CHECK_MODULES([LIBNL3], [libnl-3.0])])
        PKG_CHECK_EXISTS([libnl-route-3.0],
          [PKG_CHECK_MODULES([LIBNLROUTE3], [libnl-route-3.0])])
      ],
      [
        AC_MSG_ERROR([pkg-config must be installed before autoconf is run.])
      ]
    )

    case $target_os in
    linux*) # Check for libnl (linux)
        netsnmp_save_CPPFLAGS="$CPPFLAGS"
        CPPFLAGS="${LIBNL3_CFLAGS} ${LIBNLROUTE3_CFLAGS} $CPPFLAGS"
        NETSNMP_SEARCH_LIBS(nl_connect, nl-3,
            [AC_CHECK_HEADERS([netlink/cache.h   netlink/msg.h   ]dnl
			      [netlink/netlink.h netlink/socket.h])
            EXTERNAL_MIBGROUP_INCLUDES="$EXTERNAL_MIBGROUP_INCLUDES ${LIBNL3_CFLAGS}"],
            [CPPFLAGS="$netsnmp_save_CPPFLAGS"], [], [LMIBLIBS])
        NETSNMP_SEARCH_LIBS(rtnl_link_alloc_cache, nl-route-3,
            [AC_CHECK_HEADERS([netlink/route/addr.h netlink/route/link.h])
            EXTERNAL_MIBGROUP_INCLUDES="$EXTERNAL_MIBGROUP_INCLUDES ${LIBNLROUTE3_CFLAGS}"],
            [CPPFLAGS="$netsnmp_save_CPPFLAGS"], [], [LMIBLIBS])
        if test "x$ac_cv_header_netlink_netlink_h" != xyes; then
            NETSNMP_SEARCH_LIBS(nl_connect, nl, [
                AC_CHECK_HEADERS([netlink/cache.h   netlink/msg.h   ]dnl
				  [netlink/netlink.h netlink/socket.h])],
		[], [], LMIBLIBS)
        fi
        if test "x$ac_cv_header_netlink_netlink_h" = xyes; then
            AC_EGREP_HEADER([nl_socket_free], [netlink/socket.h],
                            [AC_DEFINE([HAVE_LIBNL3], [1],
                            [Define to 1 if <netlink/netlink.h> provides the
                            libnl3 API])])
        fi
    ;;
    esac
fi
