#!/bin/sh
# We use sh because it is quiter while spawning jobs.  It is awkward 
# because the original code used arrays in csh
#
jobid=5555  # default
dbx=0
j=1
line=0
islocal=0
for arg in "$@" ; do
    case $arg in 
      -echo)
	set -x
	;;
      -jobid=*)
        jobid=`echo $arg | sed 's/-jobid=//'`
	;;
      -arch=*)
        line=`expr $line + 1`
        eval repeat$line=1
        eval numprocs$line=1
        eval arch$line=`echo $arg | sed 's/-arch=//'`
        echo -arch not yet implemented
        ;;
      -islocal)
	islocal=1
	;;
      -host=*)
        line=`expr $line + 1`
        eval repeat$line=1  
        eval numprocs$line=1
        eval host$line=`echo $arg | sed 's/-host=//'`
        ;;
      -pgm=*)
        eval pgm$line=`echo $arg | sed 's/-pgm=//'`
        ;;
      -numprocs=*)
	echo $arg
	# This wierd code is because a -n at the begining of echo 
	# causes echo to silently IGNORE THE LINE!!!!!!
	value=`echo a"$arg" | sed 's/a-numprocs=//'`
        eval numprocs$line=$value
        ;;
      -repeat=*)
        eval repeat$line=`echo $arg | sed 's/-repeat=//'`
        ;;
      -logname=*)
        eval logname$line=`echo $arg | sed 's/-logname=//'`
        ;;
      -comm=*)
        eval comm$line=`echo $arg | sed 's/-comm=//'`
        ;;
      -args=*)
        eval args$line="`echo $arg | sed 's/-args=//'`"
        ;;
      -arg=*)
        eval args=$"args$line"
        eval args$line="'""$args `echo $arg | sed 's/-arg=//'`""'"
        ;;
      -dbx)
        dbx=1
	;;
      *)
        echo invalid argument $arg argument should be one of
        echo '-host=<host> -arch=<arch> numprocs repeat logname comm args'
    esac
done

# Count number of processes (nodes) to be started.  This includes
# multiple nodes on a single host, if requested.
numnodes=0
i=1
while [ $i -le $line ] ; do
    eval repeat=$"repeat$line"
    numnodes=`expr $numnodes + $repeat`
    i=`expr $i + 1`
done

# Compute remoteinfo
# @ i=2
# while ( $i <= $line )
    # remoteinfo=" $host[$i] $i $numprocs[$i] $pgm[$i] "
# end
MYDISPLAY=`hostname`:0
# Start the processes.
nodenum=1
i=$line
while [ $i -ge 1 ] ; do
    j=1   
    eval repeati=$"repeat$i"
    while [ $j -le $repeati ] ; do
        nodenum=`expr $i - 1`
	if [ $dbx != 1 ] ; then
            eval pgm=$"pgm$i"
	    remotecmd=" $pgm "
        else
	    remotecmd=" "
        fi
	remotecmd=" $remotecmd -execer_id PDQS "
        remotecmd=" $remotecmd -master_host $host1 "
	eval host=$"host$i"
        remotecmd=" $remotecmd -my_hostname $host "
        remotecmd=" $remotecmd -my_nodenum $nodenum "
	eval numprocs=$"numprocs$i"
	if [ -z "$numprocs" ] ; then
	   echo "Invalid number of processes for host $host ($i)"
	   exit 1
	fi
        remotecmd=" $remotecmd -my_numprocs $numprocs "
	remotecmd=" $remotecmd -total_numnodes $numnodes "
	remotecmd=" $remotecmd -job_id $jobid "
	remotecmd=" $remotecmd -remote_info "
	k=1
	while [ $k -le $line ] ; do
	    eval host=$"host$k"
	    remotecmd=" $remotecmd $host "
	    n=`expr $k - 1`
	    eval numprocs=$"numprocs$k"
	    eval pgm=$"pgm$k"
	    remotecmd=" $remotecmd $n $numprocs $pgm "
	    k=`expr $k + 1`
	done
	eval args=$"args$i"
	remotecmd=" $remotecmd $args "
	if [ $dbx = 1 ] ; then
	    cat > ~/.exs$i <<.
cd `pwd`
run $remotecmd	
.
	fi
	eval repeat=$"repeat$i"
	eval host=$"host$i"
	eval pgm=$"pgm$i"
	if [ $j -eq $repeat -a $i -eq 1 ] ; then
#	  echo backgrounding on $host
#	  echo remotecmd: $remotecmd
	  if [ $dbx = 1 ] ; then
	      if [ $islocal = 1 ] ; then
	          xterm -display $MYDISPLAY -e /usr/ucb/dbx $pgm -sr ~/.exs$i
	      else
	      rsh -n $host xterm -display $MYDISPLAY -e /usr/ucb/dbx $pgm -sr ~/.exs$i
	      fi
          else
	      if [ $islocal = 1 ] ; then
		  $remotecmd
	      else
	          rsh -n $host $remotecmd
	      fi
	  fi
        else
#	  echo backgrounding on $host
#	  echo remotecmd: $remotecmd
	  if [ $dbx = 1 ] ; then
	      rsh -n $host xterm -display $MYDISPLAY -e /usr/ucb/dbx $pgm -sr ~/.exs$i &
	  else
              rsh -n $host $remotecmd &
          fi
	fi
#	echo ""
	nodenum=`expr $nodenum + 1`
        j=`expr $j + 1`
    done
    i=`expr $i - 1`
done
wait
