#!/bin/sh

#
# Check if $MPIDIR is set
#
if [ "$MPIDIR" = "" ]; then
	echo "Variable MPIDIR pointing to MPI installation not set in the environment"
	exit 1
fi

#
# Check if mpicc is in $PATH 
#
if test ! -f $MPIDIR/bin/mpicc; then
	echo "mpicc to compile MPI code does not exist in $MPIDIR directory"
	exit 2
fi

#
# Check if mpiCC is in $PATH 
#
if test ! -f $MPIDIR/bin/mpiCC; then
	echo "mpiCC to compile MPI code does not exist in $MPIDIR directory"
	exit 2
fi

#
# Check if $MPCHOME is set
#
if [ "$MPCHOME" = "" ]; then
	echo "Variable MPCHOME pointing to MPC installation not set in the environment"
	exit 1
fi

#
# Check if $HMPI_HOME is set
#
if [ "$HMPI_HOME" = "" ]; then
	echo "Variable HMPI_HOME where the HeteroMPI installation is created is not set in the environment"
	exit 1
fi

clear

echo
echo
echo
echo
echo
echo
echo
echo
echo ==============================================================================
echo 	               Welcome to HeteroMPI installation                                 
echo                                                                                  
echo 	  HeteroMPI provides all the features to the user to write portable and       
echo 	  efficient parallel applications on Heterogeneous Networks of Computers.    
echo 	  These features automate all the essential steps involved in application   
echo 	  development on Heterogeneous Networks of Computers                         
echo ==============================================================================
echo
echo
echo
echo
echo
echo
echo
echo

sleep 5
clear

HMPIOS=`uname`
HMPIARCH=`uname -i`

if [ "$HMPIOS" = "Linux" ]; then
        if [ "$HMPIARCH" = "ia64" ]; then
		(cd ./Third_Party_Software/hmetis-1.5/DummyLibSrc && make)
		echo "HMETIS_DIR      = ../Third_Party_Software/hmetis-1.5/hmetis-1.5-linux" > ./src/Make.hmetis
		/bin/cp -f ./src/Makefile.$HMPIOS ./src/Makefile
	else
		echo "HMETIS_DIR      = ../Third_Party_Software/hmetis-1.5/hmetis-1.5-linux" > ./src/Make.hmetis
		/bin/cp -f ./src/Makefile.$HMPIOS ./src/Makefile
	fi
elif [ "$HMPIOS" = "SunOS" ]; then
echo "HMETIS_DIR      = ../Third_Party_Software/hmetis-1.5/hmetis-1.5-sun4u-USparc" > ./src/Make.hmetis
/bin/cp -f ./src/Makefile.$HMPIOS ./src/Makefile
else
(cd ./Third_Party_Software/hmetis-1.5/DummyLibSrc && make)
echo "HMETIS_DIR      = ../Third_Party_Software/hmetis-1.5/hmetis-1.5-platform" > ./src/Make.hmetis
/bin/cp -f ./src/Makefile.platform ./src/Makefile
fi

make clean

make build_third_party_software

if [ $? -gt 0 ]; then
	echo ""
	echo "Could not build the METIS Graph Partitioning Library"
        echo "Building dummy library, Some graph partitioning "
	echo "functionality may not work"
	echo ""
	(cd ./Third_Party_Software/metis-4.0/DummyLibSrc && make)
fi

make
