#! /bin/bash

set -e
set -u

fail() {
    echo >&2 "${1:-E: Unknown error}"
    exit 1
}

if [ ! -v AUTOPKGTEST_TMP ]; then
  fail "E: opm-autopkgtest must be invoked by autopkgtest"
fi

sourcedir=${PWD}

dependencies=
mpi=
copy=

while [ $# -ge 1 ]; do
    case "${1}" in
        --depends)
            dependencies="${2}"
            shift 2
            ;;
        --mpi)
            mpi="${2}"
            shift 2
            ;;
	--copy)
	    # comma seperated list of files to copy from source directory
	    # to current directory, e.g. input files for the test.
	    copy="${2}"
	    shift 2
	    ;;
	--parms)
	    # parameter passed to dunecontrol
	    flags="${2}"
	    shift 2
	    ;;
        *)
            break
    esac
done

if [ $# -ne 1 ]; then
    fail "E: No source given"
fi
source=${1}

if [ -z "${dependencies}" ]; then
    dependencies=$(dpkg-parsechangelog --show-field Source)
fi

cd "${AUTOPKGTEST_TMP}"
duneproject "dune-autopkgtest" "${dependencies}" "0.0.1" "DUNE autopkgtest <invalid@invalid>" <<< y ||
    fail "E: Setting up dune-autopkgtest project failed"

cp "${sourcedir}/${source}" "${AUTOPKGTEST_TMP}/dune-autopkgtest/src/dune-autopkgtest.cc"


if test -n "${copy}"; then
    escape_source="$(echo "${sourcedir}" | sed 's/\//\\\//g')"
    copy="$(echo "${copy}"|sed -e "s/^/ ${escape_source}\//g" -e "s/,/ ${escape_source}\//g")"
    cp ${copy} dune-autopkgtest
fi

cd dune-autopkgtest

. ${DUNE_DEBIAN_ENV:-/usr/share/dune/dune-debian.env}

dunecontrol --current "${flags}" all ||
    fail "E: Building the package failed."

./build-cmake/src/dune-autopkgtest ||
    fail "E: Running the test program failed."
if [ -n "${mpi}" ]; then
    mpirun -np "${mpi}" ./build-cmake/src/dune-autopkgtest ||
        fail "E: Running the test program under MPI with ${mpi} ranks failed."
fi
