#!/bin/bash
# example file to be used with --hookdir
#
# run tests. Current directory is top of source-code.
#
# 2005, 2007 Junichi Uekawa
#
set -e

BUILDDIR="${BUILDDIR:-/tmp/buildd}"

echo "Installing the prerequisites"
for PKG in $(ls "$BUILDDIR"/*.deb | sed -e's,.*/,,;s,_.*,,' ); do
    apt-get install -y --force-yes "$PKG" || true
    apt-get remove -y "$PKG" || true
done
# ignore the failures since they are not the prime interest

dpkg -i "$BUILDDIR"/*.deb || true
apt-get install -y -f --force-yes

if chmod a+x "$BUILDDIR"/*/debian/pbuilder-test/*; then
    :
else
    echo "W: no pbuilder-test script found, skipping"
    exit 0
fi
SUCCESS=0
COUNT=0
unset FAIL || true

EXIT_CODE=0

# The current directory is the top of the source-tree.
cd "$BUILDDIR"/*/debian/..

for SCRIPT in $(run-parts --test "$BUILDDIR"/*/debian/pbuilder-test) ; do
    echo "--- BEGIN test: ${SCRIPT##*/}"
    if "${SCRIPT}"; then
    	echo SUCCESS
	((SUCCESS=SUCCESS+1))
    else
    	echo FAIL
	FAIL[${#FAIL[@]}]="${SCRIPT##*/}"
	EXIT_CODE=1
    fi
    echo "--- END test: ${SCRIPT##*/}"
    ((COUNT=COUNT+1))
done

echo "Summary:"
echo "=== $SUCCESS out of $COUNT tests passed"
echo "${FAIL[@]/#/ failed }"
echo "-- end of testsuite."

exit "${EXIT_CODE}"
