#!/bin/bash
set -e

# MPI tests are set up to run on 3 processes.
N_MPI=3
export OMPI_MCA_plm_rsh_agent=/bin/false
export OMPI_MCA_rmaps_base_oversubscribe=1
export OMPI_MCA_btl_base_warn_component_unused=0

DEB_HOST_ARCH=$( dpkg-architecture -q DEB_HOST_ARCH )

# store demos to be skipped in this array variable
declare -a SKIP_DEMO_LIST

# the -k keyword option doesn't handle the hyphen in navier-stokes or cahn-hilliard with multiple tests skipped
# so need to identify by single name
SKIP_DEMO_LIST=(${SKIP_DEMO_LIST[@]} cahn hyperelasticity elastodynamics navier elasticity)

case " i386 armhf " in \
  *\ ${DEB_HOST_ARCH}\ *) SKIP_DEMO_LIST=(${SKIP_DEMO_LIST[@]} iterative block meshview assignment);; \
esac

case " s390x " in \
  *\ ${DEB_HOST_ARCH}\ *) SKIP_DEMO_LIST=(${SKIP_DEMO_LIST[@]} matnest);; \
esac

SKIP_DEMOS=""
list_initialised=0
for t in ${SKIP_DEMO_LIST[@]}; do
    if [ ${list_initialised} = 0 ]; then
        SKIP_DEMOS=$t
        list_initialised=1
    else
        SKIP_DEMOS="${SKIP_DEMOS} or $t"
    fi
done
if [ "x${SKIP_DEMOS}" != "x" ]; then
    SKIP_DEMOS="not ( ${SKIP_DEMOS} )"
fi

python3 python/demo/generate-demo-files.py

echo "=== python demo test (serial) ==="
if [ "x${SKIP_DEMOS}" != "x" ]; then
    echo "skipping demos with SKIP_DEMOS=${SKIP_DEMOS}"
fi
python3 -m pytest -v --durations=20 -k "${SKIP_DEMOS}" python/demo/test.py


# these demos sometimes hang (race condition) or fail in the MPI test environment
declare -a MPI_SKIP_DEMO_LIST
MPI_SKIP_DEMO_LIST=(${SKIP_DEMO_LIST[@]} block-assembly-2D2D contact-vi-tao meshview-2D2D meshview-3D1D mixed-poisson)
MPI_SKIP_DEMOS=""
list_initialised=0
for t in ${MPI_SKIP_DEMO_LIST[@]}; do
    if [ ${list_initialised} = 0 ]; then
        MPI_SKIP_DEMOS=$t
        list_initialised=1
    else
        MPI_SKIP_DEMOS="${MPI_SKIP_DEMOS} or $t"
    fi
done
if [ "x${MPI_SKIP_DEMOS}" != "x" ]; then
    MPI_SKIP_DEMOS="not ( ${MPI_SKIP_DEMOS} )"
fi
echo "=== python demo test (MPI) ==="
if [ "x${MPI_SKIP_DEMOS}" != "x" ]; then
    echo "skipping MPI demos with MPI_SKIP_DEMOS=${MPI_SKIP_DEMOS}"
fi
python3 -m pytest -v --durations=20 -k "${MPI_SKIP_DEMOS}" python/demo/test.py --mpiexec=mpiexec --num-proc=${N_MPI}
