#!/bin/sh
#
# Usage:
#  mpprun machines_file         Class
#  mpprun machine1,machine2,... Class
#
# A machines file with names of machines to use, the first must
# be the local machine.
# The comma-separated list are the machines to use, the first must
# be the local machine. 
# The remaining arguments are passed to the JVM on each host.
# Connections are made using ssh or rsh

# Basic java command
#BCLASSPATH=/usr/local/opt/j2sdk1.5.0/jre/lib/ext/mpp.jar
#BCLASSPATH=./mpp.jar
BCLASSPATH=/home/kredel/java/lib/mpp.jar
JAVA="java -server -Xbootclasspath/a:$BCLASSPATH"

# Remote shell command
# e.g. ssh or rsh
REMOTESHELL="ssh -f"


# No need to modify below this line
#----------------------------------------------------------------------
if [ $# -le 1 ]
then
   echo "usage: $0 machines_file         Class"
   echo "or     $0 machine1,machine2,... Class"
   exit
fi

MACHINES=$1
#Check if machines file
if [ -f $MACHINES ]
then
   # Get the hosts from machines file
   SHOSTS=`awk -F# '{ print $1 }' $MACHINES`
   HOSTS=`echo $SHOSTS | sed 's/ /,/g'`
else
   # Get the hosts from command line
   HOSTS=$1
   SHOSTS=`echo $HOSTS | sed 's/,/ /g'`
fi
echo hosts $HOSTS

# The command to execute
shift
ARGS=$*

echo arguments $ARGS

# Startup
NUM=0
for host in $SHOSTS
do
    if [ $NUM -ne 0 ]
    then
	$REMOTESHELL $host "cd `pwd`; $JAVA -Dmpp.peers=$HOSTS -Dmpp.rank=$NUM $ARGS"
    fi
    let NUM++
done
$JAVA -Dmpp.peers=$HOSTS -Dmpp.rank=0 $ARGS
