#!/bin/bash

set -e
cd "$(dirname "$0")"

> control

# Note: Some tests are explicitly skipped because they require specific hardware (like TPU, multi-device),
# or they were commented out in the original control file (like big_modeling, modeling_utils).
SKIP_TESTS=(
    "test_big_modeling"
    "test_cli"
    "test_cpu"
    "test_dataclasses"
    "test_modeling_utils"
    "test_multidevice"
    "test_tpu"
)

# Function to check if an element is in an array
containsElement() {
    local e match="$1"
    shift
    for e; do [[ "$e" == "$match" ]] && return 0; done
    return 1
}

for f in ../../tests/test_*.py; do
    # Fallback in case there are no files
    [ -e "$f" ] || continue

    base=$(basename "$f" .py)

    if containsElement "$base" "${SKIP_TESTS[@]}"; then
        continue
    fi

    test_name=$(echo "$base" | sed 's/_/-/g' | sed 's/^test-/pytest-/')

    echo "Test-Command: HOME=\$AUTOPKGTEST_TMP pytest-3 -v tests/${base}.py" >> control
    echo "Depends: python3-pytest," >> control
    echo "         python3-pytest-env," >> control
    echo "         python3-pytest-mock," >> control
    echo "         python3-parameterized," >> control

    if [[ "$base" == "test_compile" ]]; then
        echo "         python3-wheel," >> control
        echo "         python3-setuptools," >> control
    fi

    if [[ "$base" == "test_utils" ]]; then
        echo "         python3-wheel," >> control
        echo "         python3-setuptools," >> control
        echo "         libtorch-dev," >> control
        echo "         libsleef-dev," >> control
        echo "         @," >> control
        echo "         g++," >> control
    else
        echo "         @" >> control
    fi

    echo "Restrictions: allow-stderr" >> control
    echo "Features: test-name=${test_name}" >> control
    if [[ "$base" == "test_utils" ]]; then
        echo "Architecture: !s390x" >> control
    fi

    echo "" >> control
done
