#!/bin/sh
set -e

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

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

# Determine default MPI implementation for this arch
HOSTFILE="${AUTOPKGTEST_TMP}"/hostfile.${script}
eval `grep ARCH_DEFAULT_MPI_IMPL /usr/share/mpi-default-dev/debian_defaults`
export ARCH_DEFAULT_MPI_IMPL
if [ "x$ARCH_DEFAULT_MPI_IMPL" = x"openmpi" ]
# Setup MPI environment to run on localhost with enough slots
then export OMPI_MCA_plm_rsh_agent=/bin/false
     export OMPI_MCA_btl_tcp_if_include=lo
     HOSTFILE="${AUTOPKGTEST_TMP}"/hostfile.${script}
     echo localhost slots=9 > "${HOSTFILE}"
     MPIRUN_CMD="mpirun.openmpi -np 1 --hostfile ${HOSTFILE} --oversubscribe"
elif [ "x$ARCH_DEFAULT_MPI_IMPL" = x"mpich" ]
then echo localhost > "${HOSTFILE}"
     MPIRUN_CMD="mpirun.mpich -np 1 --hostfile ${HOSTFILE}"
else echo "unknown MPI implementation: " $ARCH_DEFAULT_MPI_IMPL
     exit 1
fi

# Run yorick script
${MPIRUN_CMD} /usr/bin/yorick -i ./debian/tests/yorick-gyoto.i

exit 0
