set -e

export PRTE_MCA_plm_ssh_agent=/bin/false
export PRTE_MCA_rmaps_default_mapping_policy=:oversubscribe
export OMPI_MCA_btl_tcp_if_include=lo
export OMPI_ALLOW_RUN_AS_ROOT=1
export OMPI_ALLOW_RUN_AS_ROOT_CONFIRM=1

# testing on 5 processes is recommended by mpi4py upstream where possible
# and is safe to oversubscribe with OpenMPI
# (but oversubscribing might be a problem  - runs slow - with mpich)
# But the kspsolve demo requires a power of 2, so use 4 processes not 5.
N_MPI=4

# don't try to display plots generated with matplotlib
export MPLBACKEND=Agg

PETSC_DIR=/usr/lib/petsc64
echo "Running 64-bit petsc4py demos using PETSC_DIR=${PETSC_DIR}"

# test demos as installed
cp -r /usr/share/doc/python-petsc4py-doc/examples/demo test-demos
gunzip -r test-demos

for pyver in `py3versions -vs`; do
  echo "Run 64-bit demos (single processor) with python$pyver"
  make PETSC_DIR=${PETSC_DIR} PYTHON=python$pyver F2PY=f2py3 -C test-demos/legacy all

  echo "Run 64-bit demos (MPI) with python$pyver"
  echo "skipping wrap-swig and wrap-f2py MPI tests with 64-bit PETSc"
  # Is it a clash with 32-bit MPI? Revisit once 64-bit MPI is available.
  sed '0,/wrap-swig/!{s/\(.*wrap-swig\)/#\1/}; 0,/wrap-f2py/!{s/\(.*wrap-f2py\)/#\1/; }' -i test-demos/legacy/makefile
  make PETSC_DIR=${PETSC_DIR} PYTHON=python$pyver F2PY=f2py3 MPIEXEC_FLAGS="-n ${N_MPI}" -C test-demos/legacy all-mpi
done
