#!/usr/bin/env bash
trap 'killm f' EXIT
#
# Scott Burleigh
# October 11, 2010
#
# documentation boilerplate
CONFIGFILES=" \
./3.ipn.ltp/amroc.bprc \
./3.ipn.ltp/amroc.ltprc \
./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 \
"

CONFIGFILES=$CONFIGFILES"
./2.ipn.ltp/amroc.bprc \
./2.ipn.ltp/amroc.ltprc \
./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
"

echo "########################################"
echo
pwd | sed "s/\/.*\///" | xargs echo "NAME: "
echo
echo "PURPOSE: Testing the issue 324 bug fix: leftover LTP acquisition files.

This test sends four large files via LTP and a one-way light time
simulator configured to drop a high percentage (5%) of LTP segments,
resulting in heavy retransmission.  All files should be received at
node 3, and no residual partial LTP acquisition files should remain
in the working directory of node 3 at the conclusion of the test."

echo
echo "CONFIG: 2 node custom LTP:"
echo
for N in $CONFIGFILES
do
    echo "$N:"
    cat $N
    echo "# EOF"
    echo
done
echo "OUTPUT: counts of delivered files and residual acquisition files."
echo
echo "########################################"

# Establish absolute base directory for reliable pathing
BASE_DIR="$PWD"

./cleanup
sleep 1
echo "Starting ION..."
export ION_NODE_LIST_DIR="$BASE_DIR"
rm -f ./ion_nodes
RETVAL=0

ITERCOUNT=1
GAVEUP=1
while [ $ITERCOUNT -le 4 ] && [ $GAVEUP -eq 1 ]
do
    # Anchor the start of the loop
    cd "$BASE_DIR"

    echo "Starting channel simulator (owltsim)..."
    owltsim owlt.rc &
    OWLTSIM_PID=$!

    # Start nodes.
    cd "$BASE_DIR/3.ipn.ltp"

    ./ionstart

    ../../../system_up -i "p 30" -l "p 30" -b "p 30"

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

    cd "$BASE_DIR/2.ipn.ltp"

    # direct to output
    ./ionstart > output

    ../../../system_up -i "p 30" -l "p 30" -b "p 30"

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

    # Load contacts (Currently in 2.ipn.ltp)
    ionadmin global.ionrc

    cd "$BASE_DIR/3.ipn.ltp"
    ionadmin global.ionrc
    sleep 1

    # Start file receiver on node 3.
    echo "Starting bprecvfile..."
    bprecvfile ipn:3.1 &
    RCVPID=$!
    sleep 1

    # Send files from node 2.
    cd "$BASE_DIR/2.ipn.ltp"
    echo "Sending four files from node 2 to node 3 (1-hour TTL):"

    bpsendfile ipn:2.1 ipn:3.1 shakespeare1 0.1 3600
    echo "  shakespeare1"

    bpsendfile ipn:2.1 ipn:3.1 shakespeare2 0.1 3600
    echo "  shakespeare2"

    bpsendfile ipn:2.1 ipn:3.1 shakespeare3 0.1 3600
    echo "  shakespeare3"

    bpsendfile ipn:2.1 ipn:3.1 shakespeare4 0.1 3600
    echo "  shakespeare4"

    echo ""
    echo "...waiting up to 5 minutes for data transfer to complete..."

    # Jump to node 3 to monitor the file arrival
    cd "$BASE_DIR/3.ipn.ltp"

    WAIT_TIME=0
    MAX_WAIT=300
    while [ $WAIT_TIME -le $MAX_WAIT ]; do
        COUNT=`ls -l testfile4 2>/dev/null | wc -l`
        if [ $COUNT -eq 1 ]; then
            echo "Success: All files received early after $WAIT_TIME seconds!"
            # Give LTP time to exchange the final RS/RA and delete the temp file
            sleep 5
            break
        fi
        sleep 5
        let WAIT_TIME+=5
    done

    # Verify that all four files arrived.
    if [ $COUNT -eq 1 ]
    then
        echo ""
        echo "Okay: 4 files received."
        RETVAL=0
    else
        echo ""
        echo "Error: files not received."
        RETVAL=1
    fi

    echo ""

    # Allow LTP to cleanup any lingering ltpblock
    sleep 5

    # Verify that no leftover files remain.
    COUNT=`ls -l *ltpblock* 2>/dev/null | wc -l`
    if [ $COUNT -eq 0 ]
    then
        echo ""
        echo "Okay: no leftover acquisition files."
    else
        echo ""
        echo "Error: acquisition files left over."
        RETVAL=1
    fi

    #Kill file receiver
    echo "1st attempt to kill file receiver..."
    kill -2 $RCVPID 2>/dev/null
    sleep 2
    echo "2nd attempt to kill file receiver... Maybe redundant but to be sure..."
    kill -9 $RCVPID 2>/dev/null
    sleep 1
    rm testfile*

    #adjust loop condition
    cd "$BASE_DIR/2.ipn.ltp"
    let  ITERCOUNT++
    COUNT=`grep "!" output | wc -l`
    if [ $RETVAL -eq 1 ]; then
        if [ $COUNT -eq 0 ]
        then
            echo ""
            echo "No Bundles Expired"
            GAVEUP=0
        else
            echo ""
            echo "Bundles Expired. Retry"
            GAVEUP=1
        fi
    else
        GAVEUP=0
    fi

    echo ""

    # Shut down channel simulator.
    OSNAME=`uname`
    if [ "$OSNAME" == "SunOS" ]; then
        killcmd="pkill"
    else
        killcmd="killall"
    fi

    $killcmd -KILL "owltsim" &> /dev/null
    sleep 1

    # Terminate ION daemons and IPC resources
    killm f

    # Return to safe harbor before next iteration
    cd "$BASE_DIR"
done

echo "issue-324-ltp-files test completed."
exit $RETVAL
