#!/bin/sh

module=
py2_package=
py3_package=
pypy_package=

# Try source package
source_package=$(grep-dctrl -n -s Source -F Source -r '^python3\?-.*$' debian/control || true)
if [ -n "$source_package" ] ; then
    module=${source_package#python-}
    py2_package=python-$module
    py3_package=python3-$module
fi

source_package=$(grep-dctrl -n -s Source -F Source -r '^pypy-.*$' debian/control || true)
if [ -n "$source_package" ] ; then
    module=${source_package#pypy-}
    pypy_package=pypy-$module
fi

# Try binary package(s)
if [ -z "$source_package" ] ; then
    binary_packages=$(grep-dctrl -n -s Package -F Package -r '^python3\?-.*$' debian/control || true)
    if [ -n "$binary_packages" ] ; then
        for binary_package in $binary_packages ; do
            module=${binary_package#*-}
            case $module in
                *-doc|*-dbg|*-dbgsym|*-dev)
                    continue
                ;;
            esac

            py2_package=python-$module
            py3_package=python3-$module
            break
        done
    fi
fi

# Try binary package(s)
if [ -z "$source_package" ] ; then
    binary_packages=$(grep-dctrl -n -s Package -F Package -r '^pypy-.*$' debian/control || true)
    if [ -n "$binary_packages" ] ; then
        for binary_package in $binary_packages ; do
            module=${binary_package#*-}
            case $module in
                *-doc|*-dbg|*-dbgsym|*-dev)
                    continue
                ;;
            esac

            pypy_package=pypy-$module
            break
        done
    fi
fi

module=$(echo "$module" | sed 's/-/_/g')

# Python2
if [ -n "$py2_package" ]; then
    if [ "$(grep-dctrl -n -s Package -F Package -X "$py2_package" debian/control)" ] ; then
        cat <<EOF
Test-Command: set -e ; for py in \$(pyversions -r 2>/dev/null) ; do cd "\$AUTOPKGTEST_TMP" ; echo "Testing with \$py:" ; \$py -c "import $module; print $module" ; done
Depends: python-all, $py2_package
Restrictions: allow-stderr, superficial
Features: test-name=autodep8-python2

EOF
    fi
fi

# Python3
if [ -n "$py3_package" ]; then
    if [ $(grep-dctrl -n -s Package -F Package -X "$py3_package" debian/control) ] ; then
        cat <<EOF
Test-Command: set -e ; for py in \$(py3versions -r 2>/dev/null) ; do cd "\$AUTOPKGTEST_TMP" ; echo "Testing with \$py:" ; \$py -c "import $module; print($module)" ; done
Depends: python3-all, $py3_package
Restrictions: allow-stderr, superficial
Features: test-name=autodep8-python3

EOF
    fi
fi

# PyPy
if [ -n "$pypy_package" ]; then
    if [ $(grep-dctrl -n -s Package -F Package -X "$pypy_package" debian/control) ] ; then
        cat <<EOF
Test-Command: cd "\$AUTOPKGTEST_TMP" ; pypy -c "import $module; print $module"
Depends: $pypy_package
Restrictions: allow-stderr, superficial
Features: test-name=autodep8-pypy

EOF
    fi
fi
