#!/bin/sh

. ../eval_tools.sh

HEADER 'AgentX subagent in Perl (using NetSNMP::agent)'

SKIPIF DISABLE_SNMPV2C
SKIPIFNOT USING_AGENTX_MASTER_MODULE
SKIPIFNOT USING_AGENTX_SUBAGENT_MODULE
# make sure the toolkit has been configured --with-perl-modules
[ -d ${SNMP_UPDIR}/perl/blib/arch/auto/NetSNMP/agent ] || SKIP

#
# Begin test
#

snmp_version=v2c
TESTCOMMUNITY=testcommunity
. ./Sv2cconfig

TESTOID=.1.3.6.1.4.1.8872.9999.9999.1.0
TESTOID2=.1.3.6.1.4.1.8872.9999.9999.2.0
AGENTX_SOCKET=${SNMP_TMPDIR}/agentx_socket
PERL_SUBAGENT=${SNMP_TMPDIR}/hello.pl
PERL_SUBAGENT_CONF=${SNMP_TMPDIR}/hello.conf

cat >$PERL_SUBAGENT <<END
#!${SNMP_PERLPROG}
# minimal AgentX subagent in Perl

use blib qw(${SNMP_UPDIR}/perl);
use NetSNMP::OID qw(:all);
use NetSNMP::agent qw(:all);
use NetSNMP::ASN qw(:all);
use NetSNMP::agent::default_store;
use NetSNMP::default_store qw(:all);
use SNMP;

## double-fork daemon
my \$pid;
unless (\$pid = fork) {
  unless (fork) {
    ## start main code
    my \$regat = '.1.3.6.1.4.1.8872.9999.9999'; # netSnmpPlayPen
    my \$myoid = \$regat . ".1.0"; # the "hello world" OID
    my \$myoid2 = \$regat . ".2.0"; # the "hello again world" OID

    my \$pidfile = shift; # take PID file as first argument 
    open(PIDFILE, ">\$pidfile") or die "Couldn't open \$pidfile";
    print PIDFILE \$\$;
    close(PIDFILE);

sub my_snmp_handler {
  my (\$handler, \$registration_info, \$request_info, \$requests) = @_;
  for(my \$request = \$requests; \$request; \$request = \$request->next()) {
    if (\$request_info->getMode() == MODE_GET) {
      if (\$request->getOID() == new NetSNMP::OID(\$myoid)) {
        \$request->setValue(ASN_OCTET_STR, "hello world");
        last;
      } elsif (\$request->getOID() == new NetSNMP::OID(\$myoid2)) {
        \$request->setValue(ASN_OCTET_STR, "hello again world");
        last;
      }
    }
  }
}
sub shut_it_down { \$running = 0; }

    netsnmp_ds_set_boolean(NETSNMP_DS_APPLICATION_ID,
                           NETSNMP_DS_AGENT_NO_ROOT_ACCESS, 1);
    my \$agent = new NetSNMP::agent('Name' => 'hello', 'AgentX' => 1);
    my \$regoid = new NetSNMP::OID(\$regat);
    \$agent->register("hello", \$regoid, \&my_snmp_handler);

    \$SIG{'INT'} = \&shut_it_down;
    \$SIG{'QUIT'} = \&shut_it_down;
    \$running = 1;
    while(\$running) {
      \$agent->agent_check_and_process(1);
    }
    \$agent->shutdown();
    ## end main code
    exit 0;
  }
  exit 0;
}
waitpid(\$pid, 0);
END

echo "agentxsocket $AGENTX_SOCKET" > $PERL_SUBAGENT_CONF

CONFIGAGENT master agentx
CONFIGAGENT agentxsocket $AGENTX_SOCKET

STARTAGENT

# make sure that we can fulfill all library dependencies
_ld_lib_path="${SNMP_UPDIR}/snmplib/.libs:${SNMP_UPDIR}/agent/.libs:${SNMP_UPDIR}/agent/helpers/.libs"
if [ "x$LD_LIBRARY_PATH" != "x" ]; then
  LD_LIBRARY_PATH="${_ld_lib_path}:${LD_LIBRARY_PATH}"
else
  LD_LIBRARY_PATH="${_ld_lib_path}"
fi
if [ "x$DYLD_LIBRARY_PATH" != "x" ]; then
  DYLD_LIBRARY_PATH="${_ld_lib_path}:${DYLD_LIBRARY_PATH}"
else
  DYLD_LIBRARY_PATH="${_ld_lib_path}"
fi
if [ "x$SHLIB_PATH" != "x" ]; then
  SHLIB_PATH="${_ld_lib_path}:${SHLIB_PATH}"
else
  SHLIB_PATH="${_ld_lib_path}"
fi
export LD_LIBRARY_PATH DYLD_LIBRARY_PATH SHLIB_PATH

# start perl subagent
/usr/bin/env SNMPCONFPATH="${SNMP_TMPDIR}:${SNMP_TMP_PERSISTENTDIR}" $SNMP_PERLPROG $PERL_SUBAGENT $SNMP_AGENTX_PID_FILE >$SNMP_AGENTX_LOG_FILE 2>&1
DELAY

## 1) SNMP GET against mib object implemented by the perl subagent
CAPTURE "snmpget -$snmp_version -c $TESTCOMMUNITY $SNMP_TRANSPORT_SPEC:$SNMP_TEST_DEST$SNMP_SNMPD_PORT $TESTOID"
CHECK "hello world"

## 2) persistency: reconfigure master agent (HUP), re-test
HUPAGENT
CAPTURE "snmpget -$snmp_version -c $TESTCOMMUNITY $SNMP_TRANSPORT_SPEC:$SNMP_TEST_DEST$SNMP_SNMPD_PORT $TESTOID2"
CHECK "hello again world"

## stop
STOPAGENT
STOPPROG $SNMP_AGENTX_PID_FILE

FINISHED
