#!/bin/sh

set -e

cd tests

export PATH="$PATH:/usr/libexec:/usr/lib/$(dpkg-architecture --query=DEB_HOST_GNU_TYPE 2>/dev/null)"

for f in $(find . -type f -executable | sort) ; do
    echo "I: Preparing test $f"
    # replace socket activation with socat
    if [ "$f" = './adv' ] ; then
        sed -i -e 's;\$SD_ACTIVATE -l "127.0.0.1:\$PORT" -a \$VALGRIND tangd \$TMP/cache;socat TCP-LISTEN:$PORT,bind=127.0.0.1,fork "EXEC:tangd $TMP/cache";' $f
    elif [ "$f" = './rec' ] ; then
        sed -i -e 's;\$SD_ACTIVATE -l 127.0.0.1:\$port -a \$VALGRIND tangd \$TMP/cache;socat TCP-LISTEN:$port,bind=127.0.0.1,fork "EXEC:tangd $TMP/cache";' $f
    fi
    echo "I: Running test $f"
    # test are very noisy, keep them silent as long as they pass
    chronic "$f" || :
done

exit 0
