#! /bin/sh
#
# MPI-1.2 C++ bindings
#
# Copyright (C) 1997  Jeremy G. Siek, Michael P. McNally
#                     Jeffrey M. Squyres, Andrew Lumsdaine
# 
# INSERT LICENSE HERE
#
# The purpose of this script is to make compiling and linking MPI C++ programs
# easier by automatically putting in many of the right compiler options.
#
# Credits:
#
# This shell script is based heavily on mpiCC from MPICH-1.0.12.
# MPICH-1.0.12 is the MPI implementation developed by Argonne (Bill Gropp
# and Rusty Lusk) and Mississippi State (Tony Skjellum and Nathan Doss).
#


CXX=/usr/local/lam-6.3.2/bin/hcp
CXXFLAGS="-fexceptions   -I/usr/local/lam-6.3.2/include -I/usr/local/lam-6.3.2/include/mpi2c++-1.0.5"

CXXLINKER=/usr/local/lam-6.3.2/bin/hcp
LDFLAGS="-fexceptions   -L/usr/local/lam-6.3.2/lib -lmpi++ -lmpi   "


DoLink=1
DoCompile=0
Show=eval
allargs=
compileargs=
linkargs=
linkobjs=
gettinglinkarg=0
HasDashC=0


for arg in "$@" ; do
#    echo procssing arg $arg
    # Special processing for -o name
    if [ $gettinglinkarg = 1 ] ; then
	linkargs="$linkargs $arg"
	gettinglinkarg=0
	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"
	DoLink=0
	HasDashC=1
	;;
        -o)
	# Need to link
	allargs="$allargs $arg"
	linkargs="$linkargs $arg"
	compileargs="$compileargs -c"
	# Still need to add the target of the -o
	gettinglinkarg=1
	DoLink=1
	;;
	-show)
	Show=echo
	;;
	-help)
	echo "Balky is a program to compile or link MPI C++ programs."
	echo "It should be used just like the usual C++ compiler"
	echo ""
	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."
	echo ""
	echo "Options:"
	echo "    -show      - Show the commands that would be used without"
	echo "                 runnning them"
	echo "    -help      - Give this help"
	exit 1
	;;
        *\"*) 
	allargs="$allargs `echo $arg | sed 's/\"/\\\"/g'`"
	compileargs="$compileargs `echo $arg | sed 's/\"/\\\"/g'`"
	linkargs="$linkargs `echo $arg | sed 's/\"/\\\"/g'`"
	;;
        *) allargs="$allargs $arg"
	if [ -s "$arg" ] ; then
	    ext=`expr "$arg" : '.*\(\..*\)'`
	    if [ "$ext" = ".C" ] ; then
	        DoCompile=1
	        compileargs="$compileargs $arg"
	        fname=`basename $arg .C`
	        linkobjs="$linkobjs $fname.o"
	    elif [ "$ext" = ".cc" ] ; then
	        DoCompile=1
	        compileargs="$compileargs $arg"
	        fname=`basename $arg .cc`
	        linkobjs="$linkobjs $fname.o"
	    elif [ "$ext" = ".o" ] ; then
	        DoLink=1
	        linkobjs="$linkobjs $arg"
	    else
	        compileargs="$compileargs $arg"
	        linkargs="$linkargs $arg"
	    fi
	else
            compileargs="$compileargs $arg"
	    linkargs="$linkargs $arg"
	fi
	;;
    esac
done

status=0
if [ $DoCompile = 1 ] ; then 
    if [ $HasDashC != 1 ] ; then
        compileargs="-c $compileargs"
    fi
    $Show $CXX $CXXFLAGS $compileargs
    status=$?
    if [ $status != 0 ] ; then 
        exit $status
    fi
fi
if [ $DoLink = 1 ] ; then
    $Show $CXXLINKER $linkobjs $linkargs $LDFLAGS
    status=$?
fi
exit $status

