#!/usr/bin/env bash
trap 'killm f' EXIT
#
# Test LTP explicit configuration: Global Unified Mode with packet loss
#
# This test verifies that the new "m maxretries" and "m maxseglossrate"
# commands work correctly in unified (symmetric) mode with induced packet loss.

# Documentation boilerplate
CONFIGFILES=" \
./owlt.rc \
./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 \
./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: Testing new LTP explicit configuration commands in unified mode."
echo "         Verifies 'm maxretries' and 'm maxseglossrate' work correctly"
echo "         with 10% packet loss induced by owltsim."
echo "         Tests that LTP successfully retransmits lost segments and"
echo "         delivers all bundles despite packet loss."
echo
echo "CONFIG: 2 node custom LTP with owltsim:"
echo
for N in $CONFIGFILES
do
    echo "$N:"
    cat $N
    echo "# EOF"
    echo
done
echo "OUTPUT: Test results and retransmission verification."
echo
echo "########################################"

./cleanup
sleep 1

echo "Starting owltsim (10% packet loss simulator)..."
owltsim owlt.rc -v &
OWLTSIM_PID=$!
sleep 2

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

# Start node 2
cd 2.ipn.ltp
./ionstart > node2.stdout 2>&1
../../../system_up -i "p 30" -l "p 30" -b "p 30"

if [ $? -eq 3 ]
then
    echo "Node 2 started successfully"
else
    echo "Node 2 not started: Aborting Test"
    kill $OWLTSIM_PID 2>/dev/null
    exit 1
fi

# Start node 3
cd ../3.ipn.ltp
./ionstart > node3.stdout 2>&1
../../../system_up -i "p 30" -l "p 30" -b "p 30"

if [ $? -eq 3 ]
then
    echo "Node 3 started successfully"
else
    echo "Node 3 not started: Aborting Test"
    kill $OWLTSIM_PID 2>/dev/null
    exit 1
fi

echo "Starting bpcounter on node 3..."
sleep 1
bpcounter ipn:3.1 100 &
BPCOUNTER_PID=$!

cd ../2.ipn.ltp
echo "Sending 100 bundles from node 2 to node 3..."
bpdriver 100 ipn:2.1 ipn:3.1 -10000 &

# Wait for transmission to finish
echo "Waiting for transmission to finish..."
RUNNING=1
TIMER=0
while [ $RUNNING -eq 1 ]
do
    TIMER=$((++TIMER))
    sleep 1
    echo "...receiving..."
    # Check if bpcounter is still running
    if [ "$1" == "windows" ]
    then
        ps | grep "$BPCOUNTER_PID" >& /dev/null && RETURN_VALUE=1 || RETURN_VALUE=0
    else
        ps $BPCOUNTER_PID >& /dev/null && RETURN_VALUE=1 || ps -p $BPCOUNTER_PID >& /dev/null && RETURN_VALUE=1 || RETURN_VALUE=0
    fi
    if [ $RETURN_VALUE -eq 0 ]
    then
        echo "...transmission finished."
        RUNNING=0
    fi
    if [ $TIMER -gt 120 ]
    then
        #infinite loop protection
        echo "...120 seconds passed; giving up."
        RUNNING=0
        RETVAL=1
        kill -9 $BPCOUNTER_PID 2>/dev/null
    fi
done

echo "Verifying results..."
cd ..

# Check for retransmissions (@ character in watch output)
COUNT=`grep "@" 2.ipn.ltp/node2.stdout | wc -l`
if [ $COUNT -eq 0 ]
then
    echo "FAIL: No LTP retransmissions detected (expected with 10% loss)"
    RETVAL=1
else
    echo "PASS: $COUNT retransmissions detected"
fi

# Check for explicit config message in logs (ion.log contains span configuration)
if grep -q "explicit config" 2.ipn.ltp/ion.log
then
    echo "PASS: Explicit config mode confirmed in node 2"
else
    echo "FAIL: Not using explicit config mode"
    RETVAL=1
fi

if grep -q "explicit config" 3.ipn.ltp/ion.log
then
    echo "PASS: Explicit config mode confirmed in node 3"
else
    echo "FAIL: Not using explicit config mode in node 3"
    RETVAL=1
fi

# Check unified mode (same xmit/recv loss rates of 0.10)
# Grep to find final configuration (spans configured at startup)
if grep "engine 3" 2.ipn.ltp/ion.log | grep "explicit config" | tail -1 | grep -q "xmit segment loss rate 0.10.*recv segment loss rate 0.10"
then
    echo "PASS: Unified mode confirmed in node 2 (same xmit/recv rates)"
else
    echo "WARNING: Could not verify unified mode rates in node 2"
fi

# Check for max timeouts = 8 (8 retries * SIGNAL_REDUNDANCY=1)
if grep "engine 3" 2.ipn.ltp/ion.log | grep "explicit config" | tail -1 | grep -q "max timeouts 8"
then
    echo "PASS: Max timeouts = 8 confirmed (8 retries * 1 redundancy)"
else
    echo "WARNING: Could not verify max timeouts value"
fi

echo "Stopping..."
kill $OWLTSIM_PID 2>/dev/null
sleep 1

if [ $RETVAL -eq 0 ]
then
    echo "LTP config unified test PASSED"
else
    echo "LTP config unified test FAILED"
fi

exit $RETVAL
