set -e

export PRTE_MCA_plm_ssh_agent=/bin/false
export PRTE_MCA_rmaps_default_mapping_policy=:oversubscribe
export OMPI_MCA_btl_base_warn_component_unused=0

# test_commond_threads uses internal functions from
# libscotch/common_thread.c (threadReduce etc)
# so needs flags set to get the thread functions defined,
# including -DCOMMON_PTHREAD and -DSCOTCH_RENAME.
# So use the flags set in debian/rules
CFLAGS="${CFLAGS} \
    -O3 -fPIC -I. \
    -I/usr/include/metis \
    -Drestrict=__restrict \
    -DCOMMON_FILE_COMPRESS_GZ \
    -DCOMMON_FILE_COMPRESS_BZ2 \
    -DCOMMON_FILE_COMPRESS_LZMA \
    -DSCOTCH_PTHREAD -DCOMMON_PTHREAD \
    -DSCOTCH_PTHREAD_NUMBER=2 \
    -DCOMMON_PTHREAD_FILE \
    -DCOMMON_RANDOM_FIXED_SEED \
    -DSCOTCH_RENAME -DSCOTCH_RENAME_PARSER"

FFLAGS="${FFLAGS} -I/usr/include/metis"

# likewise need -lm for maths functions when building from internal source
LDFLAGS="${LDFLAGS} -lm"

cp debian/Makefile_int.inc src/Makefile.inc
# patch src/check/Makefile to use installed scotch
patch -p0 < debian/tests/check.Makefile.patch
cd src/check

echo "Test libscotch.so (sequential)"
make realclean
make check CFLAGS="${CFLAGS}" FFLAGS="${FFLAGS}" LDFLAGS="${LDFLAGS}"

echo "Test libptscotch.so (parallel)"
make realclean
make ptcheck CFLAGS="${CFLAGS}" FFLAGS="${FFLAGS}" LDFLAGS="${LDFLAGS}" 2>ptcheck.stderr
# expect multiple "Proc  1 of  3, pid ....." piped to stderr
if [ -s ptcheck.stderr ]; then
    echo "stderr reported:"
    cat ptcheck.stderr
fi
