#!/bin/sh
# Run a regression test before pushing to code.haskell.org
# People don't like code that doesn't compile

# Configuration
TESTDIR=tmp/language_c
DEFAULT_BROWSER=firefox

function die() {
    echo "*** Regression test failed: $1 ***" 1>&2; exit 1
}

if [ ! -d "$TESTDIR" ] ; then
    die "'$TESTDIR' directory does not exist " \
        "(needs to be a checkout of your local HEAD)"
fi

if [ ! -d "$TMPDIR" ] ; then
    die "TMPDIR ('$TMPDIR') directory does not exist " \
        "(absolute path to a directory for temporary files)"
fi


cd "${TESTDIR}"
darcs pull || die "darcs pull (from local HEAD) failed"

echo "Building via cabal"
cabal configure || die "cabal configure failed"
cabal build || die "cabal build failed"
cabal haddock || die "cabal haddock failed"
echo "Finished building via cabal"

cd test
echo "Building test suite"
make || die "make failed in /test"

(cd harness && make) || die \
    "test harness failed"

(cd suite && yes | bash run-smoke.sh && yes | bash run-bugs.sh) || die \
    "run-dg.sh failed - make sure there is a symlink or copy " \
    "to the gcc.dg testsuite in $TMPDIR/test/suite"

( cd results && ../bin/RenderTests regression *dat) || die "rendering tests failed"

${BROWSER:-${DEFAULT_BROWSER}} results/index.html

exit 0

