#!/bin/bash
set -e

pkg=dipy

export LC_ALL=C.UTF-8
if [ "${AUTOPKGTEST_TMP}" = "" ] ; then
  AUTOPKGTEST_TMP=$(mktemp -d /tmp/${pkg}-test.XXXXXX)
  # Double quote below to expand the temporary directory variable now versus
  # later is on purpose.
  # shellcheck disable=SC2064
  trap "rm -rf ${AUTOPKGTEST_TMP}" 0 INT QUIT ABRT PIPE TERM
fi

export HOME=/tmp
export MPLCONFIGDIR=/tmp
readonly XSRV_ARGS="-screen 0 1024x768x24 -ac +extension GLX +render -noreset"

# WARNING: test specifications should match d/rules.

readonly SKIP_NET_TESTS="\
not dipy/utils/tests/test_tractogram.py \
and not test_io_info \
and not test_concatenate_flow \
and not test_convert_tractogram_flow \
and not test_spike \
and not test_normalization \
and not test_kernel_input \
and not test_fbc \
and not test_concatenate"

readonly SKIP_LONG_TESTS="\
not dipy/align/tests/test_streamwarp.py \
and not dipy/denoise/tests/test_lpca.py \
and not dipy/reconst/tests/test_sfm.py \
and not dipy/workflows/tests/test_reconst_csa_csd.py \
and not dipy/workflows/tests/test_reconst_rumba.py"

readonly SKIP_S390X_TESTS="\
not dipy/align/tests/test_streamlinear.py \
and not dipy/align/tests/test_streamline.py \
and not dipy/tracking/tests/test_distances.py \
and not dipy/viz/tests/test_fury.py \
and not dipy/workflows/tests/test_align.py \
and not dipy/workflows/tests/test_segment.py \
and not dipy/align/tests/test_whole_brain_slr.py \
and not dipy/data/tests/test_data.py"

readonly SKIP_32BIT_TESTS="\
not test_check_img_shapes"

if [ "$(uname -m)" = s390x ]
then
	readonly TEST_SPEC="\
		$SKIP_NET_TESTS \
		and $SKIP_LONG_TESTS \
		and $SKIP_S390X_TESTS \
	"
elif [ "$(dpkg-architecture -q DEB_HOST_ARCH_BITS)" = 32 ]
then
	readonly TEST_SPEC="$SKIP_NET_TESTS and $SKIP_32BIT_TESTS and $SKIP_LONG_TESTS"
else
	readonly TEST_SPEC="$SKIP_NET_TESTS and $SKIP_LONG_TESTS"
fi

cp -a "/usr/lib/python3/dist-packages/dipy" "$AUTOPKGTEST_TMP"
cp -a "dipy/data" "$AUTOPKGTEST_TMP/dipy"
cd "$AUTOPKGTEST_TMP"

set -x

for python3 in $(py3versions --supported)
do
	xvfb-run --auto-servernum --server-num=20 -s "$XSRV_ARGS" \
		"$python3" -m pytest -v -k "$TEST_SPEC" dipy
done
