#!/bin/sh

set -e

# Move to the root of the repository so we can run this script from anywhere
cd "$(dirname "$0")/../.."

CONTROL=debian/tests/control

echo -n "" > $CONTROL

for test_file in $(cd tests && find . -name "test_*.py" -type f | sort); do
    test_file=${test_file#./}
    test_name="pytest-$(echo $test_file | sed 's/\.py$//' | sed 's/test_//g' | sed 's/_/-/g' | sed 's/\//-/g' | sed 's/^-//' | sed 's/-\+/-/g')"
    
    case "$test_name" in
        pytest-compressors-distributed-distributed-compression|\
        pytest-compressors-distributed-module-parallel|\
        pytest-compressors-model-compressors-model-compressor-distributed|\
        pytest-compressors-model-compressors-transformers-integration|\
        pytest-compressors-compress-decompress-module|\
        pytest-offload-cache-cpu|\
        pytest-offload-cache-device|\
        pytest-offload-cache-disk|\
        pytest-offload-cache-dist-cpu|\
        pytest-offload-cache-dist-device|\
        pytest-offload-cache-dist-disk|\
        pytest-offload-convert-convert|\
        pytest-offload-convert-from-accelerate|\
        pytest-offload-convert-to-accelerate|\
        pytest-offload-dispatch|\
        pytest-offload-interface|\
        pytest-offload-load|\
        pytest-offload-module|\
        pytest-offload-xpu-routing|\
        pytest-quantization-lifecycle-static-lifecycle|\
        pytest-transform-factory-serialization)
            echo "Skipping $test_name..."
            continue
            ;;
    esac

    cat << EOF >> $CONTROL
Test-Command: cd tests ; HOME=\$AUTOPKGTEST_TMP pytest-3 -v $test_file
Depends: @, python3-pytest, python3-pytest-mock, python3-pytest-env, black, python3-isort, python3-wheel, python3-flake8, python3-nbconvert, python3-accelerate
Restrictions: allow-stderr
Features: test-name=$test_name

EOF
done

echo "Generated $CONTROL with $(grep -c "Test-Command" $CONTROL) tests."
