#!/bin/sh

set -e -u
RETVAL=0

# Perform a minimal build to compile cmake-based parts of the upstream testsuite
export DEB_BUILD_OPTIONS=nocheck
dh_autoreconf
dh_auto_configure -- -DPAR2_SYSTEM_CXXOPTS=ON -DPAR2_BUILD_TESTS=ON -DPAR2_INSTALL=OFF -DPAR2_KEEP_ASSERTS=ON -DPAR2_BUILD_TOOL=OFF
dh_auto_build

# Symlink the packaged par2 executable for the cmake tests
OBJ_DIR="obj-$(dpkg-architecture -qDEB_HOST_GNU_TYPE)"
rm --verbose --force "$OBJ_DIR/par2"
ln --verbose --symbolic /usr/bin/par2 "$OBJ_DIR/par2"

# Run the compiled part of the testsuite
ctest --verbose --test-dir "$OBJ_DIR/tests" || RETVAL=$?

# Copy shell-based tests into an empty directory
cp --verbose --archive ./tests "$AUTOPKGTEST_TMP"
cd "$AUTOPKGTEST_TMP"
# Symlink the packaged par2 executable
ln --verbose --symbolic /usr/bin/par2 "$AUTOPKGTEST_TMP"

# Run the shell-based tests
for t in $(find tests/ -name 'test*' -not -name '*.ps1' -not -name testMem -executable -type f | sort -V); do
       echo "$t:"
       /bin/sh "$t" 2>&1 || RETVAL=$((RETVAL + $?))
done

exit $RETVAL
