#!/bin/sh

set -e

if [ "$LINTIAN_TEST_INSTALLED" = "yes" ]; then
    LINTIAN_FRONTEND="/usr/bin/lintian"
    LINTIAN_ROOT="/usr/share/lintian"
else
    unset LINTIAN_FRONTEND
    LINTIAN_ROOT=""
fi

if [ "$1" = "-j" ] ; then
    NUMJOBS="$2"
    test "$NUMJOBS" -gt 0 || (
        echo "$NUMJOBS is not an integer greater than 0" >&2 && exit 1)
    PARALLEL_ARGS="-j $NUMJOBS"
    shift
    shift
fi

NEW_SUITE=1
OLD_SUITE=1

if [ "$1" ] ; then
    if [ "$1" = "-t" ] || echo "$1" | grep -q ':' ; then
        # Only the new suite "gets" this, so skip the old one.
        OLD_SUITE=0
    elif [ "$1" = "--legacy-only" ] ; then
        # We were asked only to run the old one
        NEW_SUITE=0
        shift
    fi
fi

LC_ALL="C"
LINTIAN_PROFILE=debian
NO_PKG_MANGLE=true
unset MAKEFLAGS
# Ensure Lintian works without $ENV{HOME}
unset HOME

[ "$TEST_WORK_DIR" ] || TEST_WORK_DIR="debian/test-out"

export LC_ALL
export LINTIAN_FRONTEND
export LINTIAN_ROOT
export LINTIAN_PROFILE
export NO_PKG_MANGLE

fail(){
    echo "$1" >&2
    exit 1
}

if [ "${NEW_SUITE}" = 1 ] ; then
    t/runtests --dump-logs -k $PARALLEL_ARGS t "$TEST_WORK_DIR" "$@"
fi
if [ "${OLD_SUITE}" = 1 ] ; then
    echo " --- LEGACY TEST SUITE ---"
    testset/runtests -k testset "$TEST_WORK_DIR" "$@"
fi

# Local Variables:
# indent-tabs-mode: nil
# End:
# vim: syntax=sh sw=4 sts=4 sr et
