#!/bin/sh
# demo.sh -- runs fsubagent demo from start to finish

# be verbose
set -x
# terminate script on error
set -e

# compile the fsubagent library (and run its puny unit tests)
./configure
make check

# compile the demo MIB into a .h file (ok, make check already did that)
#make podunk_rr_mib.h

# compile and run both demo apps (ok, make check already compiled 'em)
#make podunk_demo podunk_demo2
./podunk_demo train1 firstCar
./podunk_demo2 train2 secondCar

# bonus: dump the output file to ASCII.  Not really needed.
# Try running podunk_demo a few times with different arguments, and
# dump output file after each run, to see what's in that mysterious data file.
./fsubagentDump podunk_rr.fsa

# unpack ucd-snmp and install the demo mib and mib module into ucd-snmp
SNMP=ucd-snmp-4.2.1
if [ -d $SNMP ]; then
	rm -rf $SNMP
fi
tar -xzvf $SNMP.tar.gz
sh cp2ucd.sh $SNMP
cd $SNMP
./configure --with-defaults --with-mib-modules="podunk_rr"
# build snmp apps and daemon
make
if [ ! -x agent/snmpd ]; then
	echo make snmpd failed
	exit 1
fi
cd ..

# Set up config file for SNMP clients
echo mibdirs $SNMP/mibs       > snmp.conf

# Sanity check: verify that snmp apps know about our MIB.  Should print out blah.blah.9999
SNMPCONFPATH=. $SNMP/apps/snmptranslate -IR podunk

# Set up config file for SNMP daemon
PORT=2222
echo agentaddress $PORT >  snmpd.conf
echo cacheSeconds 0     >> snmpd.conf

# Run the snmp daemon
SNMPCONFPATH=. $SNMP/agent/snmpd -P $PORT

# Retrieve the demo app's values from the snmpd via snmpdump
# Should print out values for train1 and firstCar in podunk tree
SNMPCONFPATH=. $SNMP/apps/snmpwalk localhost:$PORT x podunk

# All done.  Stop the snmpd we started.
# Dunno why it renames itself 'lt-snmpd'...
killall lt-snmpd

