#!/bin/bash
#
# script to launch Java "workers" on a list of machines 
# CAVEAT:  fails if any of parameters to pass to program contain spaces
# run from directory containing .class files
#
if [ -z "$3" ]
then
    echo "usage `basename $0` numWorkers hosts class [pgm_parameters]"
    echo "  (hosts is a comma-separated list)"
    exit 1
fi
num_workers=$1
hostlist=$2
class=$3
shift 3

objdir=`pwd`

declare -a hosts
num_hosts=0
for h in `echo $hostlist | sed 's/,/ /g'`
do
    hosts[$num_hosts]=$h
    num_hosts=$((num_hosts+1))
done

index=0
for count in `seq -w 0 $((num_workers-1))`
do
    host=${hosts[$index]}
    index=$(((index+1) % num_hosts))
    #uncomment these lines to get output in files (and comment out other ssh)
    #outfile=OUT.$count
    #echo starting worker $count on $host with output in $outfile
    #ssh $host "cd $objdir; java $class $* >$outfile 2>&1" &
    echo starting worker $count on $host 
    ssh $host "cd $objdir; java $class $* 2>&1 | sed \"s/^/worker $count:  /\"" &
done
echo ""

