#!/bin/sh
#------------------------->  Bourne shell - script  <-------------------------#
#- Copyright (C) 199x by International Computer Science Institute            -#
#- This file is part of the GNU Sather package. It is free software; you may -#
#- redistribute  and/or modify it under the terms of the  GNU General Public -#
#- License (GPL)  as  published  by the  Free  Software  Foundation;  either -#
#- version 2 of the license, or (at your option) any later version.          -#
#- This  program  is distributed  in the  hope that it will  be  useful, but -#
#- WITHOUT ANY WARRANTY without even the implied warranty of MERCHANTABILITY -#
#- or FITNESS FOR A PARTICULAR PURPOSE. See Doc/GPL for more details.        -#
#- The license text is also available from:  Free Software Foundation, Inc., -#
#- 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA                     -#
#------------->  Please email comments to <bug-sather@gnu.org>  <-------------#

# define here how to start the different nodes. Note that
# "primary node" 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 "SATHER_CLUSTER_NUMBER" 
# 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 "SATHER_CLUSTER_NUMBER" 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

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

	 for machine in $SATHER_NODES
	 do
	     # make sure start just enough nodes
		  if expr $nodes_started = $nodes > /dev/null
		  then
				break
		  fi
	     # do not start another process on the local host!
		  if expr $machine != $HOST > /dev/null 
		  then
				# set clusters number to be used by the started process
				SATHER_CLUSTER_NUMBER=$nodes_started
				export SATHER_CLUSTER_NUMBER

				/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 "$machine: $SATHER_CLUSTER_NUMBER" -exec gdb $prog &

				nodes_started=`expr $nodes_started + 1`
		  fi
	 done
}

start

exit 0
