#!/bin/sh
#
# Return values of this script:
# 0   - Success

#
# Displays a warning message
#
warn_msg () {
    if [ "x$I2G_MPI_START_VERBOSE" == "x1" ]; then 
        echo "mpi-start [WARNING]:" $@
    fi
}

#
# Displays a warning message
#
error_msg () {
    echo "mpi-start [ERROR  ]:" $@
}

#
# Display a debug message in the case that I2G_MPI_START_DEBUG
# is 1.
#
debug_msg () {
    if [ "x$I2G_MPI_START_VERBOSE" == "x1" ]; then 
        if [ "x$I2G_MPI_START_DEBUG" == "x1" ] ; then
            echo "mpi-start [DEBUG  ]: "$@
        fi
    fi
}

#
# Display a debug message in the case that I2G_MPI_START_DEBUG
# is 1.
#
info_msg () {
    if [ "x$I2G_MPI_START_VERBOSE" == "x1" ]; then 
        echo "mpi-start [INFO   ]: "$@
    fi
}

#
# Dump environment.
# $1 level
#
dump_env () {
    for i in `env`; do
        echo "mpi-start [DUMPENV]:" $i
    done
}


#
# Output general information
#
if [ "x$I2G_MPI_START_VERBOSE" == "x1" ]; then 
    echo "************************************************************************"
    echo "UID     = " `whoami`
    echo "HOST    = " `hostname`
    echo "DATE    = " `date`
	echo "VERSION =  0.0.33" 
    echo "************************************************************************"
fi

# debug me 
if [ "x$I2G_MPI_START_DEBUG" == "x1" ] ; then 
    debug_msg "Configuration"
    debug_msg "=> I2G_MPI_APPLICATION=$I2G_MPI_APPLICATION"
    debug_msg "=> I2G_MPI_APPLICATION_ARGS=$I2G_MPI_APPLICATION_ARGS"
    debug_msg "=> I2G_MPI_TYPE=$I2G_MPI_TYPE"
    debug_msg "=> I2G_MPI_VERSION=$I2G_MPI_VERSION"
    debug_msg "=> I2G_MPI_PRE_RUN_HOOK=$I2G_MPI_PRE_RUN_HOOK"
    debug_msg "=> I2G_MPI_POST_RUN_HOOK=$I2G_MPI_POST_RUN_HOOK"
    debug_msg "=> I2G_MPI_PRECOMMAND=$I2G_MPI_PRECOMMAND"
    debug_msg "=> I2G_MPI_FLAVOUR=$I2G_MPI_FLAVOUR"
    debug_msg "=> I2G_MPI_JOB_NUMBER=$I2G_MPI_JOB_NUMBER"
    debug_msg "=> I2G_MPI_STARTUP_INFO=$I2G_MPI_STARTUP_INFO"
    debug_msg "=> I2G_MPI_RELAY=$I2G_MPI_RELAY"
fi

# trace me
if [ "x$I2G_MPI_START_TRACE" == "x1" ] ; then
      debug_msg "enable debugging"
      set -x
fi

# check for valid I2G_MPI_START variable
if [ "x$I2G_MPI_START" == "x" ] ; then
    error_msg "I2G_MPI_START not set"
    error_msg "dump environment:"
    dump_env
    exit -1;
fi

# global environment variables 
MPI_START_PREFIX=`dirname $I2G_MPI_START`
MPI_START_MACHINEFILE=""
MPI_START_READY=-1 

# check for scheduling system and set environment variables
info_msg "search for scheduler"
for i in $MPI_START_PREFIX/../etc/mpi-start/*.scheduler  ; do 
	# source the function definitions
    unset scheduler_available
    unset scheduler_get_machinefile
    debug_msg "source $i"
    source $i
    if [ $? != 0 ] ; then 
        warn_msg "failed to source : $i"
    fi

	# check if support for this kind of schedulers is supported
    debug_msg "checking for scheduler support :" `basename $i .scheduler`
	scheduler_available
    result=$?

	if [ "x$result" == "x0" ] ; then 
        info_msg "activate support for " `basename $i .scheduler`

		# support for this scheduler is found. So lets setup the internal environment.
		scheduler_get_machinefile

		# mark MPI_START as ready to go
		MPI_START_READY=0
		
		break
	fi	
done

# check if we have a scheduler 
if [ $MPI_START_READY != 0 ] ; then 
	error_msg "cannot find scheduler"
	error_msg "dump environment:"
    dump_env
	exit -1
fi

# dump machinefile
debug_msg "Dump machinefile:"
for i in `cat $MPI_START_MACHINEFILE` ; do
    debug_msg "=> $i"
done

# setup the np count 
I2G_MPI_NP=`cat $MPI_START_MACHINEFILE | wc -l`
debug_msg "starting with $I2G_MPI_NP processes."


# check for EGEE environement
# TODO: add the compiler specific stuff.
debug_msg "check for EGEE environment"
MPI_TYPE=`echo $I2G_MPI_TYPE | tr "[:lower:]" "[:upper:]" | tr "-" "_"`
US=_
MPI_PREFIX="MPI_"
MPI_PATH_SUFFIX="_PATH"
MPI_PATH_VERSION="_VERSION"
if [ "x$I2G_MPI_VERSION" != "x" ] ; then 
    # check for requested version
    debug_msg "check for user requested MPI version : $I2G_MPI_VERSION"
    MPI_VERSION=`echo $I2G_MPI_VERSION | sed -e s/\\\\./__/g`
    VALUE=`eval echo \\$$MPI_PREFIX$MPI_TYPE$US$MPI_VERSION$MPI_PATH_SUFFIX`
    if [ "x$VALUE" != "x" ] ; then
        debug_msg "found user requested version"
        export MPI_START_MPI_PREFIX=$VALUE
    else
        error_msg "cannot find user request MPI version"
        dump_env
        exit -1
    fi
else
    # check for default 
    debug_msg "check for default MPI"
    VALUE=`eval echo \\$$MPI_PREFIX$MPI_TYPE$MPI_PATH_SUFFIX`
    if [ "x$VALUE" != "x" ] ; then 
        debug_msg "found default MPI in : $VALUE"
        export MPI_START_MPI_PREFIX=$VALUE
    else
        debug_msg "coulnd't find EGEE environment"
        export MPI_START_MPI_PREFIX=
    fi
fi


# load the mpi plugin
if [ ! -e $MPI_START_PREFIX/../etc/mpi-start/$I2G_MPI_TYPE.mpi ] ; then 
	error_msg "failed to find requested MPI type : $I2G_MPI_TYPE"
	error_msg "Dump environment"
	dump_env 
	exit -2
fi

# source the MPI specific configuration file 
info_msg "activate support for $I2G_MPI_TYPE"
debug_msg "source : " $MPI_START_PREFIX/../etc/mpi-start/$I2G_MPI_TYPE.mpi
source $MPI_START_PREFIX/../etc/mpi-start/$I2G_MPI_TYPE.mpi

# call the MPI specific startup functions
info_msg "call backend MPI implementation"
mpi_start
result=$?

exit $result
