#!/bin/bash

# Test script for mount-nsenter-demo program
# Runs configurable iterations to test mount namespace functionality

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROGRAM="${SCRIPT_DIR}/mount-nsenter-demo"

# Default values
WORK_DIR="/home/ec2-user"
ITERATIONS=1000

# Function to display usage
usage() {
    cat << EOF
Usage: $0 [OPTIONS]

Options:
    -w, --work-dir DIR      Working directory for test files (default: /home/ec2-user)
    -n, --iterations NUM    Number of test iterations (default: 1000)
    -h, --help             Display this help message

Examples:
    $0                                    # Run with defaults
    $0 -w /tmp -n 500                     # Run 500 tests in /tmp
    $0 --work-dir /tmp --iterations 100   # Run 100 tests in /tmp
EOF
    exit 0
}

# Parse command-line arguments
while [[ $# -gt 0 ]]; do
    case $1 in
        -w|--work-dir)
            WORK_DIR="$2"
            shift 2
            ;;
        -n|--iterations)
            ITERATIONS="$2"
            shift 2
            ;;
        -h|--help)
            usage
            ;;
        *)
            echo "Error: Unknown option: $1"
            echo "Use -h or --help for usage information"
            exit 1
            ;;
    esac
done

# Validate inputs
if [ ! -d "$WORK_DIR" ]; then
    echo "Error: Work directory does not exist: $WORK_DIR"
    exit 1
fi

if ! [[ "$ITERATIONS" =~ ^[0-9]+$ ]] || [ "$ITERATIONS" -le 0 ]; then
    echo "Error: Iterations must be a positive integer"
    exit 1
fi

if [ ! -f "$PROGRAM" ]; then
    echo "Error: mount-nsenter-demo program not found at $PROGRAM"
    echo "Please build it first with: go build -o mount-nsenter-demo"
    exit 1
fi

echo "Running $ITERATIONS tests in $WORK_DIR"
echo ""

PASSED=0
FAILED=0

for ((id=0; id<ITERATIONS; id++)); do
    HELLO_FILE="${WORK_DIR}/hello-${id}"
    WORLD_FILE="${WORK_DIR}/world-${id}"
    COPIED_FILE="${WORK_DIR}/hello-${id}-copied"
    
    # Step 1: Create hello file
    echo "hello-${id}" > "$HELLO_FILE"
    
    # Step 2: Create world file
    echo "world-${id}" > "$WORLD_FILE"
    
    # Step 3: Run mount-nsenter-demo program
    sudo "$PROGRAM" "$HELLO_FILE" "$WORLD_FILE" > "${WORK_DIR}/log-${id}" 2>&1
    
    # Step 4: Verify the copied file matches world file
    if [ ! -f "$COPIED_FILE" ]; then
        echo "FAIL [${id}]: Copied file does not exist: $COPIED_FILE"
        ((FAILED+=1))
    elif ! diff -q "$COPIED_FILE" "$WORLD_FILE" > /dev/null 2>&1; then
        echo "FAIL [${id}]: Content mismatch between $COPIED_FILE and $WORLD_FILE"
        ((FAILED+=1))
    else
        ((PASSED+=1))
        # clean up on success
        sudo umount "$HELLO_FILE" 2>/dev/null || true
        rm -f "$HELLO_FILE" "$WORLD_FILE" "$COPIED_FILE"
    fi
    
    # Print progress every 10 iterations
    if (( (id + 1) % 10 == 0 )); then
        echo "Progress: $((id + 1))/$ITERATIONS iterations completed (Passed: $PASSED, Failed: $FAILED)"
    fi
done

echo ""
echo "========================================="
echo "Test Results:"
echo "  PASSED: $PASSED"
echo "  FAILED: $FAILED"
echo "  TOTAL:  $ITERATIONS"
echo "========================================="

if [ $FAILED -eq 0 ]; then
    echo "All tests passed!"
    exit 0
else
    echo "Some tests failed!"
    exit 1
fi
