#!/bin/bash

usage() {

cat  <<EOF
Usage: 
$0 N hosts "ARGS"
where:
      N: number of MPI ranks
      hosts: host1[:N1],host2[:N2],... comma-delimited hosts list with optional host's ranks amount
      "ARGS": arguments to pass to Arion (multi-word arguments must be enclosed in double quotes)
EOF
exit

}

if [[ "$0" != \/* ]]; then
    echo Call to this script must be an absolute path
    usage
fi

minargs=3
if [ $# -lt $minargs ]; then
    usage
fi


basedir=$(dirname "$0")

exewrapper="$basedir/arion2.75_impi2021.sh"
mpirunexe="mpirun"

source $basedir/impi/2021.16/env/vars.sh 2>&1
exit_status=$?
if [ $exit_status -ne 0 ]; then
    echo Failed to setup IMPI
    exit $exit_status
fi

out=$$.out 
exec "$mpirunexe" -print-rank-map -n $1 -hosts $2 "$exewrapper" "$3"  -crc.sections 1 2>&1 | tee $out
crcerr=$(egrep -e "^#.*error in input: .*; .-crc\.section.*; garbage after the end of the input" $out | wc -l)

if [ $crcerr -gt 0 ]; then 
    echo "Detected input parsing error message about crc.section directive, make sure that --uns section is last section in the last input file" 
fi

rm $out



