#!/bin/bash
#
# Scott Burleigh
# April 12, 2010

# documentation boilerplate
CONFIGFILES=" \
./global.ionrc \
./2.ipn.ltp/amroc.ltprc \
./2.ipn.ltp/amroc.bprc \
./2.ipn.ltp/amroc.ionconfig \
./2.ipn.ltp/global.ionrc \
./2.ipn.ltp/amroc.ionrc \
./2.ipn.ltp/amroc.ionsecrc \
./2.ipn.ltp/amroc.ipnrc \
./5.ipn.ltp/amroc.ltprc \
./5.ipn.ltp/amroc.bprc \
./5.ipn.ltp/amroc.ionconfig \
./5.ipn.ltp/global.ionrc \
./5.ipn.ltp/amroc.ionrc \
./5.ipn.ltp/amroc.ionsecrc \
./5.ipn.ltp/amroc.ipnrc \
./3.ipn.ltp/amroc.ltprc \
./3.ipn.ltp/amroc.bprc \
./3.ipn.ltp/amroc.ionconfig \
./3.ipn.ltp/global.ionrc \
./3.ipn.ltp/amroc.ionrc \
./3.ipn.ltp/amroc.ionsecrc \
./3.ipn.ltp/amroc.ipnrc \
"

echo "########################################"
echo
pwd | sed "s/\/.*\///" | xargs echo "NAME: "
echo
echo "PURPOSE: To determine if bundle status reports are generated and logged."
echo
echo "CONFIG: 3 node custom configuration:"
echo
for N in $CONFIGFILES
do
	echo "$N:"
	cat $N
	echo "# EOF"
	echo
done
echo "OUTPUT: Various status messages are expected to be found in
	./2.ipn.ltp/ion.log"
echo
echo "########################################"

echo "Cleaning up old ION..."
rm -f 2.ipn.ltp/ion.log 3.ipn.ltp/ion.log 5.ipn.ltp/ion.log
rm -f 5.ipn.ltp/testfile1
rm -f 5.ipn.ltp/testfile2
rm -f 5.ipn.ltp/testfile3
rm -f 5.ipn.ltp/testfile4
rm -f 5.ipn.ltp/testfile5
killm
sleep 1

echo "Starting ION..."
export ION_NODE_LIST_DIR=$PWD
rm -f ./ion_nodes

# Start nodes.
cd 2.ipn.ltp
ionadmin amroc.ionrc
cd ../3.ipn.ltp
ionadmin amroc.ionrc
cd ../5.ipn.ltp
ionadmin amroc.ionrc

cd ../2.ipn.ltp
ionadmin global.ionrc
cd ../3.ipn.ltp
ionadmin global.ionrc
cd ../5.ipn.ltp
ionadmin global.ionrc

cd ../2.ipn.ltp
ionsecadmin amroc.ionsecrc
cd ../3.ipn.ltp
ionsecadmin amroc.ionsecrc
cd ../5.ipn.ltp
ionsecadmin amroc.ionsecrc

cd ../2.ipn.ltp
ltpadmin amroc.ltprc
cd ../3.ipn.ltp
ltpadmin amroc.ltprc
cd ../5.ipn.ltp
ltpadmin amroc.ltprc

cd ../2.ipn.ltp
bpadmin amroc.bprc
cd ../3.ipn.ltp
bpadmin amroc.bprc
cd ../5.ipn.ltp
bpadmin amroc.bprc

echo "Starting bpsink on ipn:5.1 ..."
sleep 1
bpsink ipn:5.1 &

cd ../2.ipn.ltp
echo "Sending bptrace from ipn:2.1 to ipn:5.1, should be delivered to bpsink ..."
# Send first bundle from node 2 to node 5 requesting all status reports.
bptrace ipn:2.1 ipn:5.1 ipn:2.0 5 1.1 "Hello." rcv,ct,fwd,dlv,del

echo "Sending bptrace from ipn:2.1 to ipn:5.2, should expire and be deleted..."
# Send first bundle from node 2 to node 5 requesting all status reports.
bptrace ipn:2.1 ipn:5.2 ipn:2.0 5 1.1 "Hey." rcv,ct,fwd,dlv,del

# Wait for contacts to terminate.
echo "Waiting for contacts to terminate..."
sleep 15
echo "Contacts have terminated.  Verifying results..."

# Verify bundle arrival.
cd ../2.ipn.ltp
RETVAL=0

echo
echo "Checking for 'status 2' in ./2.ipn.ltp/ion.log..."
COUNT=`grep " \[s\] " ion.log | grep "status 2" | wc -l`
if [ $COUNT -eq 2 ]
then
	echo "OK: Initial custody of both bundles taken."
else
	echo "ERROR: Initial bundle custody not taken."
	RETVAL=1
fi

echo
echo "Checking for 'status 4' in ./2.ipn.ltp/ion.log..."
COUNT=`grep " \[s\] " ion.log | grep "status 4" | wc -l`
if [ $COUNT -eq 4 ]
then
	echo "OK: Both bundles forwarded twice."
else
	echo "ERROR: Bundles not forwarded."
	RETVAL=1
fi

echo
echo "Checking for 'status 3' in ./2.ipn.ltp/ion.log..."
COUNT=`grep " \[s\] " ion.log | grep "status 3" | wc -l`
if [ $COUNT -eq 2 ]
then
	echo "OK: Custody taken upon receipt of both bundles."
else
	echo "ERROR: No report of custody taken upon receipt."
	RETVAL=1
fi

echo
echo "Checking for 'status 9' in ./2.ipn.ltp/ion.log..."
COUNT=`grep " \[s\] " ion.log | grep "status 9" | wc -l`
if [ $COUNT -eq 1 ]
then
	echo "OK: One bundle delivered."
else
	echo "ERROR: Bundle not delivered."
	RETVAL=1
fi

echo
echo "Checking for 'status 17' in ./2.ipn.ltp/ion.log..."
COUNT=`grep " \[s\] " ion.log | grep "status 17" | wc -l`
if [ $COUNT -eq 1 ]
then
	echo "OK: One bundle deleted upon expiration."
else
	echo "ERROR: No deletion upon expiration reported."
	RETVAL=1
fi

# Shut down ION processes.
echo
echo "Stopping ION..."
cd ../2.ipn.ltp
./ionstop &
cd ../3.ipn.ltp
./ionstop &
cd ../5.ipn.ltp
./ionstop &

# Give all three nodes time to shut down, then clean up.
sleep 5
killm
echo "Status reports test completed."
exit $RETVAL
