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

MPI_CC="cc "
MPI_CLINKER=$MPI_CC

MPI_CFLAGS="-I/usr/lib/openmpi/include   -g -Wall -O2 -Wstrict-prototypes -Wmissing-prototypes -Wundef -Wpointer-arith -Wbad-function-cast -ansi -DGCC_WALL -std=c89"
MPI_LIBS="-L/usr/lib/openmpi/lib -lmpi_f77 -lmpi_f90 -lmpi -lm"
CFLAGS="-pipe -Wall -g -O2 -march=i586 -mtune=i686 -fPIC -DPIC -I/usr/include  -g -Wall -O2 -Wstrict-prototypes -Wmissing-prototypes -Wundef -Wpointer-arith -Wbad-function-cast -ansi -DGCC_WALL -std=c89"

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

# 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
    ;;
    -mpicc=*)
    MPI_CC=`echo A$arg | sed -e 's/A-mpicc=//g'`
    MPI_CLINKER=$MPI_CC
    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
mpecc : To compile or link C 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       : Show the command that would be used without executing it.
    -mpicc      : Reset the MPI_CC 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 "mpecc: **** No profiling configuration file $profConf.conf is found in $MPE_ETCDIR"
        exit 1;
    fi
else
    echo "mpecc: **** 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.
C_INCS="$PROFILE_INCPATHS $MPI_CFLAGS"
MPICC_FLAGS="$C_INCS $MPE_COPTS"
MPICC_LDFLAGS="$LDFLAGS $C_INCS $MPE_LDOPTS"

if [ "$linking" = yes ] ; then
    $Show $MPI_CLINKER $MPICC_LDFLAGS $allargs $PROFILE_PRELIB $MPI_LIBS $PROFILE_POSTLIB
    rc=$?
else
    $Show $MPI_CC $MPICC_FLAGS $allargs
    rc=$?
fi

exit $rc
