#! /bin/sh
#
# Copyright 2011 Sandia Corporation. Under the terms of Contract
# DE-AC04-94AL85000 with Sandia Corporation, the U.S.  Government
# retains certain rights in this software.
#
# Copyright (c) 2017 Intel Corporation. All rights reserved.
# This software is available to you under the BSD license.
#
# This file is part of the Sandia OpenSHMEM software package. For license
# information, see the LICENSE file in the top level directory of the
# distribution.

SEARCH_LAUNCHERS="yod mpirun mpiexec srun"
LAUNCHER=""
SOS_LAUNCHER=`basename $0`

# Users can set the OSHRUN_LAUNCHER environment variable to specify the command
# that will be used by oshrun

if [ ! -z "${OSHRUN_LAUNCHER}" ]; then
    LAUNCHER="${OSHRUN_LAUNCHER}"

else
    for l in ${SEARCH_LAUNCHERS}; do
        if hash "$l" > /dev/null 2>&1; then
            LAUNCHER="$l"
            break;
        fi
    done
fi

want_help=0;

if [ "$1" = "--help" ] || [ "$1" = "-h" ]; then
    want_help=1
fi

if [ $want_help = 1 ]; then
    printf "== OSHRUN HELP ====================================\n";
    printf "usage: ${SOS_LAUNCHER} [oshrun_options] [launcher_arguments]\n\n";
    if [ -z "${OSHRUN_LAUNCHER}" ]; then
        printf "OSHRUN_LAUNCHER = (none)\n\n";
    else
        printf "OSHRUN_LAUNCHER = ${OSHRUN_LAUNCHER}\n\n";
    fi
    printf "Selected launcher: ${LAUNCHER}\n\n";
    printf "${SOS_LAUNCHER} options:\n";
    printf "\t--help | -h     Display this information\n\n";
    printf "Options provided by ${LAUNCHER} can also be used by ${SOS_LAUNCHER}.\n\n";
    printf "== ${LAUNCHER} HELP ====================================\n" | tr [a-z] [A-Z];
fi

if [ -z "${LAUNCHER}" ]; then
    echo "`basename $0`: Error, could not find a launcher.  Aborting."
    exit 1
fi

exec $LAUNCHER "$@"
