cmd=$(basename "$0")

usage()
{
    echo ""
    echo " $cmd - Fortran compiler wrapper for OpenCoarrays"
    echo ""
    echo " Usage: $cmd <fortran-source-file> [options] ..."
    echo ""
    echo " Options:"
    echo "   --help, -h               Show this help message"
    echo "   --version, -v, -V        Report version and copyright information"
    echo "   --wrapping, -w, --wraps  Report the name of the wrapped compiler"
    echo ""
    echo " Example usage:"
    echo ""
    echo "   $cmd foo.f90 -o foo"
    echo "   $cmd -v"
    echo "   $cmd --help"
    echo ""
    echo "OpenCoarrays $caf_version $cmd supports three categories of compilers"
    echo "with the following restrictions for each use case:"
    echo ""
    echo " 1. With an OpenCoarrays-Aware (OCA) compiler (GNU 5.1.0 or later),"
    echo "   a. If any of the options listed above appear, any remaining arguments are ignored."
    echo "   b. If present, <fortran-source-file> must"
    echo "      * be a Fortran source file,"
    echo "      * appear before all other arguments,"
    echo "      * be the only Fortran source file in the argument list,"
    echo "      * have a name of the form *.f90, *.F90, *.f, or *.F. "
    echo "   c. The environment variable 'CAFC' must be empty or point to a Fortran compiler/linker. "
    echo "   d. If 'CAFC' is empty, a default value of 'mpif90' is used. "
    echo ""
    echo " 2. With non-OCA CAF compilers (Intel or Cray),"
    echo "   a. Observe restrictions 1a-d above."
    echo "   b. Access OpenCoarrays collective subroutines via use association with an only clause,"
    echo "      e.g., 'use opencoarrays, only : co_sum,co_broadcast' "
    echo ""
    echo " 3. With non-CAF compilers (all compilers not named above),"
    echo "   a. Observe restrictions 1a-d above."
    echo "   b. Access OpenCoarrays capabilities via use association ('use opencoarrays')."
    echo "   c. The only CAF statements or expressions allowed are the following:"
    echo "      * 'num_images()' "
    echo "      * 'this_image()' with or without arguments"
    echo "      * 'sync all' with or without arguments."
    echo "      * 'sync images' with or without arguments."
    echo "      * 'error stop' without arguments."
    echo "      * 'co_sum', 'co_broadcast', 'co_max', 'co_min', or 'co_reduce'"
    echo ""
    echo " The caf wrapper will append -L, -l, and other required arguments as necessary"
    echo " using values that get set during the OpenCoarrays build and installation."
    echo ""

    exit 1
}

# Print useage information if caf is invoked without arguments
if [ $# == 0 ]; then
  usage | less
  exit 1
fi

# Default to "mpif90" Fortran compiler if environment variable CAFC is empty or unset:
CAFC=${CAFC:-mpif90}

# TODO -- improve the syntax of the "set" command below to accepted an unlimited number of arguments
max_arguments=100
link_args="-fcoarray=lib -lcaf_mpi"

if [[ $1 == '-v' || $1 == '-V' || $1 == '--version' ]]; then
    echo ""
    echo "OpenCoarrays Coarray Fortran Compiler Wrapper (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 == '--wrapping' || $1 == '--wraps' ]]; then
  echo "caf wraps CAFC=$CAFC"
elif [[ $1 == '-h' || $1 == '--help' ]]; then
  # Print usage information
  usage | less
  exit 1
elif [ "$caf_compiler" = "true" ]; then
  # Nothing to do other than invoke the compiler with all the command-line arguments:
  $CAFC "$@" -L "$caf_lib_dir" $link_args
else
  # Verify that a file with the .f90, .F90, .f, or .F extension is the first argument:
  src_extension=$(echo "$1" | cut -f2 -d'.')
  if [[ $src_extension == 'f90' || $src_extension == 'F90' || $src_extension == 'f' || $src_extension == 'F' ]]; then
    # copy the source file into a new file for pre-processing (preprending "caf-" to the new file name):
    cp "$1" "caf-$1"
    # Edit the copied source to replace CAF syntax with calls to public procedures in opencoarrays.f90:
    if [ "$linux" = "true" ]; then
      sed -i'' 's/sync all/call sync_all/g' "caf-$1"
      sed -i'' 's/error stop/call error_stop/g' "caf-$1"
      sed -i'' 's/sync images/call sync_images/g' "caf-$1"
    else
      # This works on OS X and other POSIX-compliant operating systems:
      sed -i '' 's/sync all/call sync_all/g' "caf-$1"
      sed -i '' 's/error stop/call error_stop/g' "caf-$1"
      sed -i '' 's/sync images/call sync_images/g' "caf-$1"
    fi
    # Replace the file name in command-line argment 1 with the new name beofre invoking the compiler:
    set -- "caf-$1" "${@:2:$max_arguments}"
    # Invoke the compiler along with all command-line arguments:
    $CAFC "$@" -L "$caf_lib_dir" -I "$caf_mod_dir" $link_args
  else
    # Print usage information upon encountering an unknowon CAF source file extension
    usage | less
    exit 1
  fi
fi
