#!/bin/sh
#
# Copyright (C) 2020 Simon Byrne, Mosè Giordano
# License is MIT "Expat"
#
### Commentary:
#
# Command line utility to call the `mpiexec` binary used by the `MPI.jl` version
# in the given Julia project.  It has the same syntax as the `mpiexec` binary
# that would be called, with the additional `--project=...` flag to select a
# different Julia project.
#
# Examples of usage (the MPI flags available depend on the MPI implementation
# called):
#
#   $ mpiexecjl --version
#   $ mpiexecjl -n 40 julia mpi-script.jl
#   $ mpiexecjl --project=my_experiment -n 80 --oversubscribe julia mpi-script.jl
#
### Code:

usage () {
    echo "Usage: ${0} [--project=...] MPIEXEC_ARGUMENTS..."
    echo "Call the mpiexec binary in the Julia environment specified by the --project option."
    echo "If no project is specified, the MPI associated with the global Julia environment will be used."
    echo "All other arguments are forwarded to mpiexec."
}

for arg; do
    shift
    case "${arg}" in
        --project | --project=*)
            PROJECT_ARG="${arg}"
            ;;
        -h | --help)
            usage
            echo "Below is the help of the current mpiexec."
            echo
            set -- "${@}" "${arg}"
            ;;
        *)
            set -- "${@}" "${arg}"
            ;;
    esac
done

if [ -z "${*}" ]; then
    echo "ERROR: no arguments specified." 1>&2
    echo
    usage
    exit 1
fi

if [ -n "${JULIA_BINDIR}" ]; then
    JULIA_CMD="${JULIA_BINDIR}/julia"
else
    JULIA_CMD="julia"
fi

# shellcheck disable=SC2016
SCRIPT='
using MPI
ENV["JULIA_PROJECT"] = dirname(Base.active_project())
proc = run(pipeline(`$(mpiexec()) $(ARGS)`; stdout, stderr); wait=false)
wait(proc)
if !iszero(proc.exitcode)
   @error "The MPI process failed" proc
end
exit(proc.exitcode)
'

if [ -n "${PROJECT_ARG}" ]; then
    "${JULIA_CMD}" "${PROJECT_ARG}" --color=yes --startup-file=no -q --compile=min -O0 -e "${SCRIPT}" -- "${@}"
else
    "${JULIA_CMD}" --color=yes --startup-file=no -q --compile=min -O0 -e "${SCRIPT}" -- "${@}"
fi
