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

# documentation boilerplate
CONFIGFILES=" \
./amroc.ltprc \
./amroc.bprc \
./amroc.ionconfig \
./global.ionrc \
./amroc.ionrc \
./amroc.ionsecrc \
./amroc.ipnrc \
"
echo "########################################"
echo
pwd | sed "s/\/.*\///" | xargs echo "NAME: "
echo
echo "PURPOSE: To test out Contact Graph Routing behavior in a very large
	contact graph.  Only one node is actually run, though there are several
	nodes defined in the graph."
echo
echo "CONFIG: Large contact graph, with only one configured node:"
echo
for N in $CONFIGFILES
do
	echo "$N:"
	cat $N
	echo "# EOF"
	echo
done
echo "OUTPUT: Searching for bundle forwarding messages in ion.log to ensure 
	that the CGR engine is capable of making decisions."
echo
echo "########################################"

echo "Cleaning up old ION..."
rm -f ion.log
killm
sleep 1

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

# Start node 9, loading large contact plan.
./ionstart

sleep 5

echo "Send a bundle from ipn:9.1 destined for (noexistent) node ipn:1.1..."
bptrace ipn:9.1 ipn:1.1 ipn:9.0 200000 1.1 "Hi"

# Route should be computed quickly.
echo "The 'b' ("forwarded") watch character should appear almost immediately."
sleep 1
echo "The 'b' ("forwarded") watch character should have appeared by now."

# Wait for all sessions to terminate.
echo "Waiting for initial contact to terminate..."
sleep 120
echo "Contact has terminated.  Verifying results..."

# Verify bundle was forwarded.
RETVAL=0

echo "Searching ion.log for a single 'fwd' message '(+) 1 3'..."
COUNT=`grep fwd ion.log | grep "(+) 1 3" | wc -l`
if [ $COUNT -gt 0 ]
then
	echo "OK: Route was computed."
else
	echo "ERROR: Route was not computed."
	RETVAL=1
fi

# Shut down ION processes.
echo "Stopping ION..."
./ionstop
killm
echo "CGR test completed."
exit $RETVAL
