#! /bin/sh
#
# mpefc : Script to compile and/or link MPI programs with MPE libraries.
#
MPE_ETCDIR=@etcbuild_dir@

MPI_F77="@MPI_F77@"
MPI_FLINKER=$MPI_F77

MPI_INC="@MPI_INC@"
MPI_LIBS="@MPI_LIBS@"
FFLAGS="@FFLAGS@"

LDFLAGS="@LDFLAGS@"
MPE_BUILD_FORTRAN2C=@MPE_BUILD_FORTRAN2C@

# Set $FC so mpe_xxx.conf recognize this is a fortran wrapper
FC="MPE Fortran Compiler Wrapper"

# Internal variables
# Show is set to echo to cause the compilation command to be echoed instead
# of executed.
Show=eval

# ------------------------------------------------------------------------
# Argument processing.
# Look through the arguments for arguments that indicate compile only.
# If these are *not* found, add the library options

linking=yes
allargs=""
for arg in "$@" ; do
    # Set addarg to no if this arg should be ignored by the C compiler
    addarg=yes
    qarg=$arg
    case $arg in
        # ----------------------------------------------------------------
        # Compiler options that affect whether we are linking or no
    -c|-S|-E|-M|-MM)
    # The compiler links by default
    linking=no
    ;;
        # ----------------------------------------------------------------
        # Options that control how we use mpicc (e.g., -show,
        # -cc=* -config=*
    -echo)
    addarg=no
    set -x
    ;;
    -mpif77=*)
    MPI_F77=`echo A$arg | sed -e 's/A-mpif77=//g'`
    MPI_FLINKER=$MPI_F77
    addarg=no
    ;;
    -show)
    addarg=no
    Show=echo
    ;;
    -mpilog|-mpitrace|-mpianim|-mpicheck|-graphics|-log|-nolog)
    profConf=`echo A$arg | sed -e 's/A-//g'`
    profConf="mpe_$profConf"
    addarg=no
    # Loading the profConf file is handled below
    ;;
    -h|-help)
cat <<EOF
mpefc : To compile or link Fortran MPI programs with MPE profiling libraries.
        In addition, the following special options are supported.
    -h|-help    : Display this help message.
    -echo       : Do set -x.
    -show       : Print the to be executed command.
    -mpif77     : Reset the MPI_F77 used in this script.
EOF
    if [ -f $MPE_ETCDIR/mpe_help.conf ] ; then
        . $MPE_ETCDIR/mpe_help.conf
    fi
    exit 1
    ;;
        # -----------------------------------------------------------------
        # Other arguments.  We are careful to handle arguments with
        # quotes (we try to quote all arguments in case they include
        # any spaces)
    *\"*)
    qarg="'"$arg"'"
    ;;
    *\'*)
    qarg='\"'"$arg"'\"'
    ;;
    *)
    ;;

    esac
    if [ $addarg = yes ] ; then
        allargs="$allargs $qarg"
    fi
done

# Handle the case of a profile switch
if [ -n "$profConf" ] ; then
    if [ -s "$MPE_ETCDIR/$profConf.conf" ] ; then
        . $MPE_ETCDIR/$profConf.conf
    else
        echo "mpefc: **** No profiling configuration file $profConf.conf is found in $MPE_ETCDIR"
        exit 1;
    fi
else
    echo "mpefc: **** No profiling option is specifed!"
fi

F_INCS="$PROFILE_INCPATHS $MPI_INC"
FC_FLAGS="$FFLAGS $F_INCS $MPE_FOPTS"
FC_LDFLAGS="$LDFLAGS $FFLAGS $F_INCS $MPE_LDOPTS"

if [ "$linking" = yes ] ; then
    $Show $MPI_FLINKER $FC_LDFLAGS $allargs $PROFILE_PRELIB $MPI_LIBS $PROFILE_POSTLIB
    rc=$?
else
    $Show $MPI_F77 $FC_FLAGS $allargs
    rc=$?
fi

exit $rc
