#!/bin/csh
# This script starts jobs.  It is intended for the use of p4, p5, and mpi.  It
# is being developed as a stand-in for DQS.
#
# only one of -arch or -host should be specified
# -logname is optional
# -comm is optional
# -numprocs defaults to 1  (It is for telling the process to fork copies of
#  itself, for shared-memory multiprocessing.)
# -host or -arch must be first in a group (separates groups)
# -repeat is optional, defaults to 1, and must be last (it is a repetition
#    factor for the group)
# -args is optional.  These are user-specified arguments.
#
# For example,
#
#     starter -arch=sun4 -pgm=/home/me/a.out -repeat=3
#
# starts a.out on 3 sun4's
#
#    starter -host=skippy -pgm=/home/me/master -args='-p4dbg 20 -p4rdbg 10' \
#            -arch=rs6000 -pgm=/home/me/slave -logname=remoteuserid -repeat=4 \
#            -host=sequent -pgm=/home/me/sequent/slave -numprocs=20
#
# starts the master on skippy with args, starts the slave on 4 RS6000's, and
#    starts one process on the sequent, which will be told via its command
#    line to fork nineteen more copies of itself.
#
# for testing:
# echo number of arguments is $#argv
# @ j = 0
# while ($j <= $#argv)
#     echo $argv[$j]
#     @ j++
# end
# echo end of args
#
set port = 5555  # default
set arch = ('' '' '' '' '' '' '' '' '' '' '' '')
set host = ('' '' '' '' '' '' '' '' '' '' '' '')
set pgm  = ('' '' '' '' '' '' '' '' '' '' '' '')
set numprocs = ('' '' '' '' '' '' '' '' '' '' '' '')
set repeat = ('' '' '' '' '' '' '' '' '' '' '' '')
set logname = ('' '' '' '' '' '' '' '' '' '' '' '')
set comm = ('' '' '' '' '' '' '' '' '' '' '' '')
set args = ''
@ j = 1
@ line = 0
while ($j <= $#argv)
    set arg = "$argv[$j]"
    switch ("$arg")
      case -port=*:
        set port = `echo $arg | sed 's/-port=//'`
        breaksw
      case -arch=*:
        @ line += 1
        @ repeat[$line]	  = 1              # default
        @ numprocs[$line] = 1              # default
        set arch[$line] = `echo $arg | sed 's/-arch=//'`
        echo -arch not yet implemented
        breaksw
      case -host=*:
        @ line += 1
        @ repeat[$line]	  = 1              # default
        @ numprocs[$line] = 1              # default
        set host[$line] = `echo $arg | sed 's/-host=//'`
        breaksw
      case -pgm=*:
        set pgm[$line] = `echo $arg | sed 's/-pgm=//'`
        breaksw
      case -numprocs=*:
        set numprocs[$line] = `echo $arg | sed 's/-numprocs=//'`
        breaksw
      case -repeat=*:
        set repeat[$line] = `echo $arg | sed 's/-repeat=//'`
        breaksw
      case -logname=*:
        set logname[$line] = `echo $arg | sed 's/-logname=//'`
        breaksw
      case -comm=*:
        set comm[$line] = `echo $arg | sed 's/-comm=//'`
        breaksw
      case -args=*:
        set tempargs1 = "`echo $arg | sed 's/-args=//'`"
        set tempargs2 = "`echo $tempargs1 | sed 's/=/ /g'`"
        set args = "`echo $tempargs2 | sed 's/,/ /g'`"
        breaksw
      case *:
        echo invalid argument $arg  argument should be one of
        echo '-host=<host> -arch=<arch> numprocs repeat logname comm args'
    endsw
    @ j++
end

# For testing.
# set i = 1
# while ( $i <= $line )
#     if ($arch[$i] != '') then
#         echo -n arch=$arch[$i]
#     else
#         echo -n host=$host[$i]
#     endif
#     echo -n '' pgm=$pgm[$i] logname=$logname[$i] comm=$comm[$i]
#     echo '' numprocs=$numprocs[$i] repeat=$repeat[$i] args=$args
#     @ i += 1
# end

# Test port.  This not completely safe but close enough for now.
#telnet $host[1] $port < /dev/null |& grep refused > /dev/null
#if ($status != 0) then
#    echo bad port $port, try another one
#    exit $status
#endif

# Count number of processes (nodes) to be started.  This includes
# multiple nodes on a single host, if requested.
@ numnodes = 0
@ i = 1
while ( $i <= $line )
    @ numnodes = $numnodes + $repeat[$i]
    @ i++
end

# Start the processes.
@ nodenum = 1
@ i = 1
while ( $i <= $line )
    @ j = 1   
    while ( $j <= $repeat[$i] )
        @ nodenum  = $i - 1
	set remotecmd = \
	    "  $pgm[$i] $args -p4pginfo $nodenum $host[$i] $numprocs[$i] $host[1] $port $numnodes  "

	if ( $i == 1 ) then
	    @ k = 2
	    while ( $k <= $line )
		set remotecmd = " $remotecmd $host[$k] $numprocs[$k] "
		@ k++
	    end
	endif

	@ n = $i - 1
	# echo $remotecmd > p$n
	# chmod u+x p$n

	echo " "
	echo host is $host[$i]
	echo remotecmd is $remotecmd
        rsh $host[$i] $remotecmd &
	@ nodenum++
        @ j++
    end   
    @ i++
end
echo " "
