#! /bin/sh
DoLink=1
DoCompile=0
show_compile=0
show_link=0
MPILOG=
CCFLAGS="@CCFLAGS@"
Show=eval
allargs=
compileargs=
linkargs=
linkobjs=
gettinglinkarg=0
HasDashC=0
UsesPmpi=0
verbose=0
#
# Directory locations: Fixed for any MPI implementation
prefix=@prefix@
exec_prefix=@exec_prefix@
sysconfdir=@sysconfdir@
includedir=@includebuild_dir@
libdir=@libbuild_dir@
#
#
# Default compiler configuration.  A configuration file may override any
# of these (including the location of the MPI library), but must do so
# consistently with the MPICH library chosen.  By including the library
# and include paths here, we can use the same mpicc for a range of 
# libraries and compilers, including ones for 32 and 64 bit systems.
CCCBASE="@CPP_COMPILER@"
CCLINKERBASE="@CCLINKER@"
USER_CFLAGS="@USER_CFLAGS@"
ROMIO_TCFLAGS="@ROMIO_TCFLAGS@"
ROMIO_LFLAGS="@ROMIO_LFLAGS@"
BASE_LIB_LIST="@BASE_LIB_LIST@ @CPP_EXTRA_LIBS@"
LIB_PATH="@LIB_PATH@"
MPILIBNAME="@MPILIBNAME@"
MPIVERSION="@MPIVERSION@"
FLIBNAME="@FLIBNAME@"
hasMPE=@hasMPE@
#
MPI_WITH_PMPI="@MPI_WITH_PMPI@"
proflib=-lp${MPILIBNAME}
proflibfullname=${libdir}/libp${MPILIBNAME}.a
#
# Override the compilers using environment variables
CCLINKER="${MPICH_CCLINKER-$CCLINKERBASE}"
#
# C++ variables
cpplib=-l${MPILIBNAME}++
cpplibfullname=${libdir}/lib${MPILIBNAME}++.a
cppproflib=-lp${MPILIBNAME}++
cppproflibfulname=${libdir}/libp${MPILIBNAME}++.a
CXXFLAGS="@CXXFLAGS@"
LDFLAGS="@CPPLDFLAGS@"
CCC="${MPICH_CCC-$CCCBASE}"
CCLINKER="${MPICH_CCLINKER-$CCLINKERBASE}"

if [ -n "$MPICH_CCC" ] ; then
    CCC="$MPICH_CCC"
    CCCname=`echo $CCC | sed 's/ /-/g'`
    if [ -s $sysconfdir/mpiCC-$CCCname.conf ] ; then
        . $sysconfdir/mpiCC-$CCCname.conf
    fi
else
    CCC="$CCCBASE"
fi
UseSharedLib=${MPICH_USE_SHLIB-no}

if [ -z "@CPP_COMPILER@" -o -z "@CCLINKER@" ] ; then
    echo "No C++ compiler and linker specified when MPICH configured."
    exit 1
fi
for arg in "$@" ; do
#    echo procssing arg $arg
    # Special processing for -o name
    if [ $gettinglinkarg = 1 ] ; then
	linkargs="$linkargs $arg"
	gettinglinkarg=0
	outputfilename="$arg"
	continue
    fi
    case "$arg" in 
	-c)
	# If -c is NOT specified, then we need to perform a link step.
	allargs="$allargs $arg"
	compileargs="$compileargs $arg"
        # If -o was set, then we need to move the output file option
        # to the compile line (note that this is non-standard, and should
        # not be used in portable codes)
        if [ $DoLink = 1 -a -n "$outputfilename" ] ; then
	    compileargs="$compileargs -o $outputfilename"
        fi
	DoLink=0
	HasDashC=1
	;;
        -o)
	# Need to link
	allargs="$allargs $arg"
        if [ $HasDashC = 1 ] ; then
            # Some BUT NOT ALL compilers support -o with -c.  Allow
            # the user to make use of the feature, IF IT EXISTS.
            compileargs="$compileargs $arg"	
        else
	    linkargs="$linkargs $arg"
	    # Still need to add the target of the -o
	    gettinglinkarg=1
	    DoLink=1
        fi
	;;
	-E|-M)
	# For compilers that support -E as a way to get at the C preprocessor
	# Also, for compilers that support -M for creating dependencies
	allargs="$allargs $arg"
	compileargs="$compileargs $arg"
	HasDashC=1
	DoLink=0
	;;

	-mpilog)
	if [ $UsesPmpi = 1 ] ; then
	    echo "Only one of -mpilog, -mpitrace, or -mpianim may be used."
	    exit 1
	else
	    UsesPmpi=1
        fi
	if [ "$hasMPE" = "yes" ] ; then
		MPILOG="-llmpe -lmpe"
	else
	    echo "-mpilog requires the MPE libraries"
	fi
	;;
	-mpitrace)
	if [ $UsesPmpi = 1 ] ; then
	    echo "Only one of -mpilog, -mpitrace, or -mpianim may be used."
	    exit 1
	else
	    UsesPmpi=1
        fi
	if [ "$hasMPE" = "yes" ] ; then
   	    MPILOG=-ltmpe
	else
	    echo "-mpitrace requires the MPE libraries"
	fi
	;;
	-mpianim)
	if [ $UsesPmpi = 1 ] ; then
	    echo "Only one of -mpilog, -mpitrace, or -mpianim may be used."
	    exit 1
	else
	    UsesPmpi=1
        fi
	if [ "$hasMPE" = "yes" ] ; then
	    MPILOG="-lampe -lmpe"
	else
	    echo "-mpianim requires the MPE libraries"
	fi
	;;
	-echo)
	set -x
	;;
	-CC=*)
	CCC=`echo A$arg | sed -e 's/A-CC=//g'`
	CCLINKER="$CCC"
	;;
	-compile_info)
	show_compile=1
	DoLink=0
	Show=echo
	;;
	-link_info)
	show_link=1
	Show=echo
	;;
	-v)
	verbose=1
	compileargs="$compileargs -v"
	linkargs="$linkargs -v"
	;;
	-show)
	Show=echo
	;;
	-l*)
	# This SHOULD be the -l<lib> argument.  Only for the linker
	linkargs="$linkargs $arg"
	allargs="$allargs $arg"
	;;
	-help)
	echo "This is a program to compile or link C++ MPI programs"
	echo "In addition, the following special options are supported"
	echo "    -mpilog    - Build version that generate MPE log files"
	echo "    -mpitrace  - Build version that generates traces"
	echo "    -mpianim   - Build version that generates real-time"
	echo "                 animation"
	echo "    -CC=pgm    - Change the program to use to compile and link"
        echo "                 MPI programs.  WARNING! The program that you"
        echo "                 choose MUST be compatible with the MPICH "
        echo "                 libraries.  If you have trouble, you should"
        echo "                 reconfigure and rebuild MPICH, selecting"
        echo "                 this compiler."
	echo "    -show      - Show the commands that would be used without"
	echo "                 runnning them"
	echo "    -compile_info - Show how to compile a program"
	echo "    -link_info - Show how to link a program"
	echo "    -help      - Give this help"
	echo "    -echo      - Show exactly what this program is doing."
	echo "                 This option should normally not be used."
	echo "This should be used just like the usual C++ compiler"
	echo "For example,"
	echo "   $0 -c foo.C "
	echo "and"
	echo "   $0 -o foo foo.o"
	echo "Combining compilation and linking in a single command"
	echo "   $0 -o foo foo.C"
	echo "may not work on some systems, and is not recommended."
	exit 1
	;;
	# Unrecognized args.  Because we do an eval, we need to
	# carefully quote any args that contain quotes.
        *\"*) 
	qarg="'"$arg"'"
	allargs="$allargs $qarg"
	compileargs="$compileargs $qarg"
	linkargs="$linkargs $qarg"
	;;
        *\'*) 
	qarg='\"'"$arg"'\"'
	allargs="$allargs $qarg"
	compileargs="$compileargs $qarg"
	linkargs="$linkargs $qarg"
	;;
        *) allargs="$allargs $arg"
	if [ -s "$arg" ] ; then
	    ext=`expr "$arg" : '.*\(\..*\)'`
	    if [ "$ext" = ".C" -o "$ext" = ".cxx" -o "$ext" = ".cpp" -o \
		 "$ext" = ".cc" -o "$ext" = ".c" -o "$ext" = ".CC" ] ; then
	        DoCompile=1
	        compileargs="$compileargs $arg"
	        fname=`basename $arg $ext`
	        linkobjs="$linkobjs $fname.o"
	    elif [ "$ext" = ".o" ] ; then
		if [ $HasDashC = 1 ] ; then
	            compileargs="$compileargs $arg"
                else
	            DoLink=1
	            linkobjs="$linkobjs $arg"
                fi
	    else
	        compileargs="$compileargs $arg"
	        linkargs="$linkargs $arg"
	    fi
	else
            compileargs="$compileargs $arg"
	    linkargs="$linkargs $arg"
	fi
	;;
    esac
done
#
status=0
if [ $DoCompile = 1 -o $show_compile = 1 ] ; then 
    if [ $HasDashC != 1 ] ; then
        compileargs="-c $compileargs"
    fi
    # note the use of HAVE_MPI_CPP to cause the mpi++.h file to be 
    # included from mpi.h
    $Show $CCC $CCFLAGS @ROMIO_TCPPFLAGS@ -I${includedir} -DHAVE_MPI_CPP $CXXFLAGS $compileargs

    status=$?
    if [ $status != 0 ] ; then 
	exit $status
    fi
fi
if [ $DoLink = 1 -o $show_link = 1 ] ; then
    # Figure out the library list.  Because we want to support both a single
    # mpi library containing both C and Fortran interfaces, as well as 
    # a library that allows multiple Fortran interfaces, we may need different
    # library link lines.  With a unified C/Fortran library, we just
    # use -l${MPILIBNAME}.  With separate Fortran libraries, we need
    # -l<fortranwrapperlib> -l${MPILIBNAME} -l<fortransuplib>
    # We also handle the profiling library here, which may not be needed
    # for those systems that support weak symbols.
    if [ "${MPILIBNAME}" = "${FLIBNAME}" -o -z "${FLIBNAME}" ] ; then
        mpilibs="-l${MPILIBNAME}"
    else
        mpilibs="-l${FLIBNAME} -l${MPILIBNAME} -l${FLIBNAME}fsup"
    fi
    # If the profiling library doesn't exist, or MPICH_NO_PROF environment
    # variable is set, skip the profiling library.
    if [ -n "$MPICH_NO_PROF" -o ! -s "$proflibfullname" ] ; then
        proflib=""
    fi
    #
    # Some programs need the profiling library, so add it if it exists
    # The C++ libraries use only a single library name
    if [ -s "$cppproflibfulname" ] ; then
        cpplib="${cppproflib}"
    elif [ ! -s "$cpplibfullname" ] ; then
        cpplib=""
    fi
    # IRIX complains if we include a library twice.  In the case of a
    # library using weak symbols, we don't need the proflib.  Just
    # in case we do, there is an enviroment variable that
    # can be used to override this test.
    if [ "${MPI_WITH_PMPI}" = "yes" -a "$MPICH_INCLUDE_PROFLIB" != yes ] ; then
        proflib=""
    fi
    # If proflib is non-empty, then add it
    if [ -n "$proflib" ] ; then
        mpilibs="$proflib $mpilibs $proflib"
    fi
    # If the C++ library doesn't exist, skip it
    # This may be needed because the C++ build wants to build executables
    # before the libraries are built.
    $Show $CCLINKER $CCFLAGS $ROMIO_LFLAGS $LIB_PATH $linkobjs $linkargs $LDFLAGS $cpplib $MPILOG $mpilibs $BASE_LIB_LIST
    status=$?
fi
exit $status
