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

MPI_F77="f77 "
MPI_FLINKER=$MPI_F77

MPI_FFLAGS="-I/usr/lib/openmpi/include "
MPI_LIBS="-L/usr/lib/openmpi/lib -lmpi_f77 -lmpi_f90 -lmpi -lm"
FFLAGS="-pipe -Wall -g -O2 -march=i586 -mtune=i686 -fPIC -DPIC"

LDFLAGS="-L/usr/lib"
MPE_BUILD_FORTRAN2C=yes

# 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

# MPE include path, x_INCS, goes in front of MPI include path, MPI_xFLAGS,
# in case MPI include path is in MPI_xFLAGS containing old MPE include path.
F_INCS="$PROFILE_INCPATHS $MPI_FFLAGS"
MPIFC_FLAGS="$F_INCS $MPE_FOPTS"
MPIFC_LDFLAGS="$LDFLAGS $F_INCS $MPE_LDOPTS"

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

exit $rc
