#!/bin/sh

PARAMETERS=$@
EXECUTABLE=NO

if test $# != 0; then
   while test $# != 0
   do
     case $1 in
     -help | -h | --help | --h | --he | --hel)
      echo "Creates the executable"
      echo "run '`basename $0` -v' for version information"
      echo "Usage: `basename $0`"
      exit 0
      ;;
     -o)
      EXECUTABLE=YES
      ;;
     -v | -version | --v | --version)
      $HMPI_HOME/scripts/hmpi_version_info
      exit 0
      ;;
     esac 
     shift
   done
fi

#
# For MPICH MPI, mpcrun needs the executable to
# be present in $MPCLOAD, which creates a problem
#
#Obtain MPI information
if test -f $MPIDIR/bin/lamd; then
	CURRENTMPI=LAM
fi

if test -f $MPIDIR/lib/liblam.a; then
	CURRENTMPI=LAM
fi

if test -f $MPIDIR/bin/mpichversion; then
	CURRENTMPI=MPICH
fi

if test -f $MPIDIR/lib/libmpich.a; then
	CURRENTMPI=MPICH
fi

if test -f $MPIDIR/bin/ompi_info; then
	CURRENTMPI=OPENMPI
fi

case $CURRENTMPI in
  LAM*)
	mpcrun create_hmpiload -- $PARAMETERS

	if test -f $MPCLOAD/run_hmpiload.sh; then
		$MPCLOAD/run_hmpiload.sh
	
		mpcrun hmpiloadclean
	else
		echo Error loading the files
		exit 1
	fi
  ;;
  MPICH*)
	if [ "$EXECUTABLE" = "YES" ]; then
		echo mpcload -het -I\$HMPI_HOME/include $PARAMETERS -L\$HMPI_HOME/lib -lhmpi -lmetis -lhmetis
		mpcload -het -I\$HMPI_HOME/include $PARAMETERS -L\$HMPI_HOME/lib -lhmpi -lmetis -lhmetis
	else
		echo mpcload -het -I\$HMPI_HOME/include $PARAMETERS
		mpcload -het -I\$HMPI_HOME/include $PARAMETERS
	fi
  ;;
  OPENMPI*)
	if [ "$EXECUTABLE" = "YES" ]; then
		echo mpcload -het -I\$HMPI_HOME/include $PARAMETERS -L\$HMPI_HOME/lib -lhmpi -lmetis -lhmetis
		mpcload -het -I\$HMPI_HOME/include $PARAMETERS -L\$HMPI_HOME/lib -lhmpi -lmetis -lhmetis
	else
		echo mpcload -het -I\$HMPI_HOME/include $PARAMETERS
		mpcload -het -I\$HMPI_HOME/include $PARAMETERS
	fi
  ;;
esac

exit 0
