#!/bin/sh
# This script is used both as autopkgtest and at package build time.

set -e

export GNUSTEP_MAKEFILES=/usr/share/GNUstep/Makefiles

if [ -z $AUTOPKGTEST_TMP ]; then
# The script is invoked from the override_dh_auto_test target.
    STEXEC=test/usr/bin/stexec
    export GNUSTEP_CONFIG_FILE=`pwd`/test.conf
    export LD_LIBRARY_PATH=test/usr/lib/${DEB_HOST_MULTIARCH}
    export ADDITIONAL_INCLUDE_DIRS=-I`pwd`/test/usr/include/${DEB_HOST_MULTIARCH}/GNUstep
    export ADDITIONAL_LIB_DIRS=-L`pwd`/test/usr/lib/${DEB_HOST_MULTIARCH}
    LD=$OBJC
    dpkg-buildflags --export=sh
else
# Running as autopkgtest.
    STEXEC=stexec
fi

if [ ! "`echo "$DEB_BUILD_OPTIONS" | grep terse`" ]; then
    verbose="messages=yes"
fi

make -e -C Testing/leaks $verbose
echo "Running leaktest..."
./Testing/leaks/obj/leaktest
make -C Testing/leaks clean

# Some scripts are buggy but nevertheless exit with success.
for f in `find Testing -name \*.st`; do
    echo "Executing $f..."
    $STEXEC $f
done

for f in `find Examples/Smalltalk -name \*.st`; do
    case $f in
        # The shell script requires the ReadlineTranscript module which
        # is not built.
        *shell* )
        continue
        ;;
    esac
    echo "Executing $f..."
    $STEXEC $f
done

exit 0
