#!/bin/sh
#
# bpinspect operations regression test
# Tests cancel, filter+cancel, and dry-run operations
#
# Nate Richard
# 2026-07-22
trap 'killm f' EXIT
set -e

CONFIGFILES=" \
./node2/config.bprc \
./node2/config.ionconfig \
./node2/config.ionrc \
./node2/config.ionsecrc \
./node2/config.ipnrc \
./global.ionrc \
"

echo "########################################"
echo
pwd | sed "s/\/.*\///" | xargs echo "NAME: "
echo
echo "PURPOSE: Verify functionality of bpinspect to inspect, cancel, suspend,
    and filter+cancel bundles inflight."
echo
echo "CONFIG: single node custom:"
echo
for N in $CONFIGFILES
do
    echo "$N:"
    cat "$N"
    echo "# EOF"
    echo
done

echo "Checking test prerequisites..."

# Find the latest available Python 3 version >= 3.10
if [ -n "$VIRTUAL_ENV" ] && [ -x "$VIRTUAL_ENV/bin/python3" ]; then
    PYTHON_CMD="$VIRTUAL_ENV/bin/python3"
elif [ -n "$VIRTUAL_ENV" ] && [ -x "$VIRTUAL_ENV/bin/python" ]; then
    PYTHON_CMD="$VIRTUAL_ENV/bin/python"
else
    PYTHON_CMD="python3"
fi

echo "Using Python $PYTHON_VERSION (via $PYTHON_CMD)"
echo

if ! "$PYTHON_CMD" -c "import bespokebpv7" ; then
    echo "Error: bespokebpv7 is not installed or import failed."
    exit 2
fi

# cleanup any previous test runs before starting
./cleanup

export ION_NODE_LIST_DIR="$PWD"

TEST_RESULT=1

echo ""
echo "Starting Node 2..."
cd node2
./ionstart
../../../system_up -i "p 30" -b "p 30"
STATUS=$?
if [ "$STATUS" -ne 2 ]; then
    echo "Node 2 not started: Aborting Test"
    exit 1
fi
cd ..

echo ""
echo "Running Python test suite..."
"$PYTHON_CMD" runtest.py --induct-port 2113
TEST_RESULT=$?

exit $TEST_RESULT
