dnl
dnl $Id: config.m4 310936 2011-05-10 20:04:26Z lytboris $
dnl

PHP_ARG_WITH(snmp,for SNMP support,
[  --with-snmp[=DIR]       Include SNMP support])

PHP_ARG_WITH(openssl-dir,OpenSSL dir for SNMP,
[  --with-openssl-dir[=DIR]  SNMP: openssl install prefix], no, no)

if test "$PHP_SNMP" != "no"; then

  if test "$PHP_SNMP" = "yes"; then
    AC_PATH_PROG(SNMP_CONFIG,net-snmp-config,,[/usr/local/bin:$PATH])
  else
    SNMP_CONFIG="$PHP_SNMP/bin/net-snmp-config"
  fi

  if test -x "$SNMP_CONFIG"; then
    SNMP_LIBS=`$SNMP_CONFIG --netsnmp-libs`
    SNMP_LIBS="$SNMP_LIBS `$SNMP_CONFIG --external-libs`"
    SNMP_PREFIX=`$SNMP_CONFIG --prefix`
    snmp_full_version=`$SNMP_CONFIG --version`
    ac_IFS=$IFS
    IFS="."
    set $snmp_full_version
    IFS=$ac_IFS
    SNMP_VERSION=`expr [$]1 \* 1000 + [$]2`
    if test "$SNMP_VERSION" -ge "5003"; then
      if test -n "$SNMP_LIBS" && test -n "$SNMP_PREFIX"; then
        PHP_ADD_INCLUDE(${SNMP_PREFIX}/include)
        PHP_EVAL_LIBLINE($SNMP_LIBS, SNMP_SHARED_LIBADD)
        SNMP_LIBNAME=netsnmp
      else
        AC_MSG_ERROR([Could not find the required paths. Please check your net-snmp installation.])
      fi
    else
      AC_MSG_ERROR([Net-SNMP version 5.3 or greater reqired (detected $snmp_full_version).])
    fi
  else 
    AC_MSG_ERROR([Could not find net-snmp-config binary. Please check your net-snmp installation.])
  fi

  dnl Test build.
  PHP_CHECK_LIBRARY($SNMP_LIBNAME, init_snmp,
  [
    AC_DEFINE(HAVE_SNMP,1,[ ])
  ], [
    AC_MSG_ERROR([SNMP sanity check failed. Please check config.log for more information.])
  ], [
    $SNMP_SHARED_LIBADD
  ])

  dnl Check whether shutdown_snmp_logging() exists.
  PHP_CHECK_LIBRARY($SNMP_LIBNAME, shutdown_snmp_logging,
  [
    AC_DEFINE(HAVE_SHUTDOWN_SNMP_LOGGING, 1, [ ])
  ], [], [
    $SNMP_SHARED_LIBADD
  ])

  dnl Check for buggy snmp_snprint_value() (net-snmp BUGid 2027834)
  AC_CACHE_CHECK([for buggy snmp_snprint_value], ac_cv_buggy_snprint_value,[
    save_CFLAGS="$CFLAGS"
    CFLAGS="$CFLAGS -I${SNMP_PREFIX}/include $SNMP_SHARED_LIBADD"
    AC_TRY_RUN( [
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>

u_char uname[] = "Linux nex1.php.net 2.6.18-194.32.1.el5 #1 SMP Wed Jan 5 17:53:09 EST 2011 i686";

int main(int argc, char **argv)
{
	struct variable_list vars;
	char buf1[2048];
	char buf2[sizeof(buf1)];
	
	memset(&(buf1[0]), 0, sizeof(buf1));
	memset(&(buf2[0]), 0, sizeof(buf2));
	memset(&vars, 0, sizeof(vars));
	vars.type = 4;
	vars.val.integer = (long *)&(uname[0]);
	vars.val.string = &(uname[0]);
	vars.val.bitstring = &(uname[0]);
	vars.val.counter64 = (struct counter64 *)&(uname[0]);
	vars.val.floatVal = (float *)&(uname[0]);
	vars.val_len = sizeof(uname),
	vars.name_loc[0] = 1;
	vars.name_loc[1] = 3;
	vars.name_loc[2] = 6;
	vars.name_loc[3] = 1;
	vars.name_loc[4] = 2;
	vars.name_loc[5] = 1;
	vars.name_loc[6] = 1;
	vars.name_loc[7] = 1;
	vars.name = (oid *)&(vars.name_loc);
	vars.name_length = 9;

	init_snmp("snmpapp");

	netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT, 0);

	snprint_value(buf1, (sizeof(uname) + 32), vars.name, vars.name_length, &vars);
	snprint_value(buf2, sizeof(buf2), vars.name, vars.name_length, &vars);
	exit((strncmp(buf1, buf2, sizeof(buf1)) != 0));
}
    ],[
      ac_cv_buggy_snprint_value=no
    ],[
      ac_cv_buggy_snprint_value=yes
    ],[
      ac_cv_buggy_snprint_value=no
    ])
    CFLAGS="$save_CFLAGS"
  ])
  if test "$ac_cv_buggy_snprint_value" = "yes"; then
     AC_DEFINE(BUGGY_SNMPRINT_VALUE, 1, [ ])
  fi

  PHP_NEW_EXTENSION(snmp, snmp.c, $ext_shared)
  PHP_SUBST(SNMP_SHARED_LIBADD)
fi
