#!/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.ionconfig \
./amroc.ionsecrc \
./amroc.ipnrc \
./mib.amsrc
"
#./amsmib.xml
echo "########################################"
echo
pwd | sed "s/\/.*\///" | xargs echo "NAME: "
echo
echo "PURPOSE: Verify basic AMS functionality."
echo
echo "CONFIG: A simple AMS message space:"
echo
for N in $CONFIGFILES
do
    echo "$N:"
    cat $N
    echo "# EOF"
    echo
done
echo "OUTPUT: Searching for message reception stats in the amsbenchr output."
echo
echo "########################################"

./cleanup

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

# Start node 9.
./ionstart

# Start message logger.
amsbenchr > test_output &
sleep 3

# Start message sender.
amsbenchs 10 100 > /dev/null &

# Verify message was received.
RETVAL=0

# wait for output file to settle
sleep 20
echo "Checking test_output for 'Received 10 messages, a total of 1000 bytes'"
COUNT=$(grep "Received 10 messages, a total of 1000 bytes" test_output | wc -l)


echo -e "\nAMS basic functionality test completed."
if [ "$COUNT" -eq 1 ]
then
    echo -e "OK: Messages received: test success.\n"
else
    echo -e "ERROR: Messages not received.\n"
    RETVAL=1
fi

# Shut down ION processes.
echo "Stopping ION..."
./shutdown_proc

exit $RETVAL
