#!/bin/sh
# autopkgtest for relion: build and run the upstream Catch2 unit test.

set -e

pkg=relion

# autopkgtest starts the test in the unpacked upstream source tree.  For
# manual runs the source directory can also be passed as an argument.
srcdir=${1:-$PWD}
if [ ! -f "${srcdir}/tests/tests.cpp" ]; then
    echo "Usage: $0 [path-to-relion-source-directory]" >&2
    echo "Run this test from an unpacked relion source tree." >&2
    exit 1
fi

if [ -z "${AUTOPKGTEST_TMP}" ]; then
    AUTOPKGTEST_TMP=$(mktemp -d /tmp/${pkg}-test.XXXXXX)
    trap 'rm -rf "${AUTOPKGTEST_TMP}"' 0 INT QUIT ABRT PIPE TERM
fi

cd "${srcdir}"

# tests.cpp includes ctf.cpp, so compiling only the test source is enough;
# the remaining symbols come from the installed relion library.
mpicxx -std=c++17 -fopenmp \
    -DHAVE_JPEG -DHAVE_PNG -DHAVE_SINCOS -DHAVE_TIFF \
    -I. -Itests -I/usr/include/eigen3 \
    tests/tests.cpp -o "${AUTOPKGTEST_TMP}/relion-tests" \
    -L/usr/lib/relion -lrelion_lib \
    -lfftw3f -lfftw3 -ltiff -lpng -ljpeg \
    -Wl,-rpath,/usr/lib/relion

"${AUTOPKGTEST_TMP}/relion-tests"

echo "PASS"
