#!/bin/sh 
# define here how to start the different nodes. Note that
# "primary nide" is already running and should NOT be started 
# here. The number of nodes to run is passed into this 
# script as the first parameter. The second paramter
# is the program to start on each node (this may be an
# absolute pathname or a relative pathname), and the
# rest are the arguments for this program
#
# In addition to staring up the processes on remote clusters,
# the script sets an environment variable "SECONDARY_CLUSTER" 
# on remote nodes to let the new processes know that they
# where started by this script rather than directlty. Only
# a "primary cluster" (the one that calls start_nodes) doesn't
# have "SECONDARY_CLUSTER" set. This functionality is necessary
# as in most cases we need to do slightly different things
# for the primary and secondary nodes.


# PWD=`pwd | sed "s/.*$LOGNAME/\/u\/$LOGNAME/"`
nodes=$1
shift
#prog=`echo $1 | sed "s/.*$LOGNAME/\/u\/$LOGNAME/"`
prog=$1
shift
options="$prog $*"
#echo $options

machines="icsib78 samosa icsib18 icsib8 ale crape icsib31 icsib95 kimchi icsib92 spaetzle"

# let the remote processes know how they came to life
SECONDARY_CLUSTER=true
export SECONDARY_CLUSTER

start() {
    # one node is running already
	 nodes_started=1

	 for machine in $machines 
	 do
	     # do not start another process on the local host!
		  if expr $machine != $HOST > /dev/null 
		  then
				/usr/local/bin/export -attr $machine -force sh -c "$options" & 
				# Stuff for debugging
				# xhost +$machine > /dev/null
#				/usr/local/bin/export -attr $machine -force xterm -display 'icsib78:0' -fn 7x13 -T "$1: $prog" -exec sh -c "$options" &

				#/usr/local/bin/export -attr $machine -force xterm -fn 7x13 -T "$1: $prog" -exec sh -c "$options" & 
#				 /usr/local/bin/export -attr $machine -force xterm -display 'icsib78:0' -fn 7x13 -T "$1: $prog" -exec gdb $prog &
				nodes_started=`expr $nodes_started + 1`
		  fi
		  
		  # make sure start just enough nodes
		  if expr $nodes_started = $nodes > /dev/null
		  then
				break
		  fi
	 done
}

start

exit 0
