#!/usr/bin/env bash
trap 'killm f' EXIT
#
# Scott Burleigh
# April 21, 2010
# documentation boilerplate
CONFIGFILES=" \
./amroc.ltprc \
./amroc.bprc \
./global.ionrc \
./amroc.ionrc \
./amroc.ionsecrc \
./amroc.ipnrc
"

echo "########################################"
echo
pwd | sed "s/\/.*\///" | xargs echo "NAME: "
echo
echo "PURPOSE: Verify that non-zero loopback one-way light time is supported."
echo
echo "CONFIG: A contact graph with 100-second loopback one-way light time:"
echo
for N in $CONFIGFILES
do
    echo "$N:"
    cat $N
    echo "# EOF"
    echo
done
echo "OUTPUT: Searching for bundle reforwarding messages in ion.log to ensure
    that non-zero loopback one-way light time is correctly handled."
echo
echo "########################################"

echo "Cleaning up old ION..."
./cleanup
sleep 1

echo "Starting ION..."
export ION_NODE_LIST_DIR=$PWD

# Start node 9, with 20-second loopback range.
./ionstart
../../system_up -i "p 30" -l "p 30" -b "p 30"

if [ $? -eq 3 ]
then
    echo ""
else
    echo "Node not started: Aborting Test"
    exit 1
fi

# Send a loopback bundle with lifetime = 300.
echo "Sending 10-byte bundle with 5-minute lifetime from ipn:9.1 to ipn:9.2."
bptrace ipn:9.1 ipn:9.2 ipn:9.0 300 0.1 "Hello Bob"
sleep 2
echo ""
echo "Watch characters should be 'abcdefg'."
# Send a non-loopback bundle with lifetime = 300.
echo "Sending 3-byte bundle with 5-minute lifetime from ipn:9.1 to ipn:5.2."
bptrace ipn:9.1 ipn:5.2 ipn:9.0 300 0.1 "Hi"
sleep 2
echo ""
echo "Watch characters should be 'abcdefg'."
echo "Both should be forwarded to node 5: the loopback owlt is so long that CGR decides it will be faster to transmit to node 5 and then back again."

# Wait for all sessions to terminate.
echo "Both bundles should be reforwarded three times on block transmission failure due to excessive ack timeouts (watch character '#'), because even the loopback bundle was sent to nonexistent node 5 due to the length of the loopback RTT."
echo "Waiting for contact to terminate..."
sleep 70
echo "Contact has terminated.  Verifying results..."

# Verify bundle was forwarded.
RETVAL=0

echo "Checking ion.log for exactly 2 sourced bundles..."
COUNT=`grep src ion.log | grep "(+) 2 " | wc -l`
if [ $COUNT -gt 0 ]
then
    echo "OK: Exactly 2 bundles sourced."
else
    echo "ERROR: Expected exactly 2 sourced bundles."
    RETVAL=1
fi

echo "Checking ion.log for non-zero forwarded bundles..."
COUNT=`grep fwd ion.log | grep -v "(+) 0 0" | wc -l`
if [ $COUNT -gt 0 ]
then
    echo "OK: Bundles were forwarded (non-zero)."
else
    echo "ERROR: No bundles forwarded."
    RETVAL=1
fi

echo "Checking ion.log for non-zero reforwarded bundles..."
COUNT=`grep rfw ion.log | grep -v "(+) 0 0" | wc -l`
if [ $COUNT -gt 0 ]
then
    echo "OK: Bundles were reforwarded (non-zero)."
else
    echo "ERROR: No bundles reforwarded."
    RETVAL=1
fi

# Shut down ION processes.
echo  -e "\nStopping ION..."
#./ionstop &
echo -e "Non-zero loopback range test completed.\n"
exit $RETVAL
