#!/bin/bash
#
# script to launch Java "workers" on a list of machines
# CAVEAT:  fails if any of parameters to pass to program contain spaces
#
if [ -z "$3" ]
then
    echo usage `basename $0` numWorkers hostfile class \[pgm_parameters\]
    exit 1
fi
num_workers=$1
hostfile=$2
class=$3
shift 3
if [ ! -r $hostfile ]
then
    echo hostfile $hostfile not found
    exit 1
fi

dir=`pwd`

declare -a hosts
hosts=( `cat $hostfile` )
num_hosts=${#hosts[*]}
if [ $num_hosts -eq 0 ]
then
    echo no hosts in $hostfile
    exit 1
fi

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

