#!/bin/bash
#
# Coarray Fortran (CAF) Executable Launcher
#
# Copyright (c) 2015-2016, Sourcery, Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#     * Neither the name of the Sourcery, Inc., nor the
#       names of its contributors may be used to endorse or promote products
#       derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL SOURCERY, INC., BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# This script invokes the chosen Fortran compiler with the received command-line
# arguments.  Current assumptions:
#    1. The only argument is either an informational flag or a CAF executable
#       file.
#    2. The environment variable "FC" is used to determine the identity fo the Fortran compiler/linker.
#    3. If "FC" is empty, a default value of "mpif90" is used.

caf_version=1.7.4
usage()
{
    cmd=$(basename "$0")
    echo ""
    echo " $cmd - Coarray Fortran executable launcher for OpenCoarrays"
    echo ""
    echo " Usage: $cmd [options] ..."
    echo ""
    echo " Options:"
    echo "   --help, -h           Show this help message"
    echo "   --version, -v, -V    Report version and copyright information"
    echo "   --wraps, -w,         Report the name of the wrapped compiler"
    echo ""
    echo " Example usage:"
    echo ""
    echo "   $cmd -np 2 foo"
    echo "   $cmd -v"
    echo "   $cmd --help"
    echo ""
    echo " Notes:"
    echo "   [options] must a CAF executable file, one of the above arguments,"
    echo "   or an argument to the program name returned by caf --wraps"
    echo ""
    exit 1
}

# Print useage information if caf is invoked without arguments
if [ $# == 0 ]; then
  usage
elif [[ $1 == '-v' || $1 == '-V' || $1 == '--version' ]]; then
  echo ""
  # shellcheck disable=SC2154
  echo "OpenCoarrays Coarray Fortran Executable Launcher (caf version $caf_version)"
  echo "Copyright (C) 2015-2016 Sourcery, Inc."
  echo ""
  echo "OpenCoarrays comes with NO WARRANTY, to the extent permitted by law."
  echo "You may redistribute copies of OpenCoarrays under the terms of the"
  echo "BSD 3-Clause License.  For more information about these matters, see"
  echo "the file named LICENSE."
  echo ""
elif [[ $1 == '-w' || $1 == '--wraps' ]]; then
  mpirun -v
elif [[ $1 == '-h' || $1 == '--help' ]]; then
  usage
else
  mpirun "$@"
fi
