#!/bin/sh
set -e

# Name of this script
script=`basename "$0"`

# Handle case of manually running this script
if [ "$ADTTMP" = "" ]; then
    ADTTMP=`mktemp -d --tmpdir "$script"-XXX`;
    echo "ADTTMP is $ADTTMP";
fi
if [ "$ADT_ARTIFACTS" = "" ] ; then
    ADT_ARTIFACTS=${ADTTMP}/artifacts
fi
mkdir -p "${ADTTMP}"
mkdir -p "${ADT_ARTIFACTS}"

# Save current directory (must be top of source tree)
SRC_ROOT=`pwd`

# Get list of non-Lorene examples
EXAMPLES_DIR=${SRC_ROOT}/doc/examples
BASE_NAMES=`basename -s.xml ${EXAMPLES_DIR}/*.xml | sed 's/example-[^ ]*-rotstar3_1//'`

# Build Gyoto command line
GYOTO="/usr/bin/gyoto --nprocesses=4 --nthreads=1 --resolution=32 --plugins=stdplug"

# Setup MPI environment to run on localhost with enough slots
export OMPI_MCA_plm_rsh_agent=/bin/false
export OMPI_MCA_btl_tcp_if_include=lo
HOSTFILE="${ADTTMP}"/hostfile.${script}
echo localhost slots=5 > "${HOSTFILE}"
MPIRUN_CMD="mpirun.openmpi -np 1 --hostfile ${HOSTFILE}"

# Run each example in this environment
for base in ${BASE_NAMES} ; do
    ${MPIRUN_CMD} ${GYOTO} \
		  ${EXAMPLES_DIR}/${base}.xml \
		  \!${ADT_ARTIFACTS}/${base}-mpi.fits ;
done

exit 0
