#!/bin/bash

#rcomX script-- designed to launch a job in it's own window on
#                every machine in the cluster
#current maximum of 16 nodes (4x4 windows)
#sciprt is not completed
#NUMNODES is read in as a string instead of a number.  
#must find a way to convert to a number for use in a comparison

if [ "$NODES" != "" ]
        then
        Nodes=$NODES
        else
        Nodes=/etc/nodes
fi

Nodelist=`cat $Nodes`
declare -i NUMNODES=0;
NUMNODES=`wc $Nodes | awk '{print $1}'`
if [ $NUMNODES -le 16 ] ; then   #4x4
  XDIV=4
  YDIV=4
  XSIZE=36
  YSIZE=11
elif [ $NUMNODES -le 25 ] ; then #5x5
  XDIV=5
  YDIV=5
  XSIZE=27
  YSIZE=8
elif [ $NUMNODES -le 36 ] ; then #6x6
  XDIV=6
  YDIV=6
  XSIZE=21
  YSIZE=6
else #7x7- don't go higher or it will look bad
  XDIV=7
  YDIV=7
  XSIZE=19
  YSIZE=4
fi

#gets the width and height of your x display in pixels
XWIDTH=`xrdb -symbols | grep DWIDTH | awk 'BEGIN{ FS = "="}{print $2}'`
DX=`expr $XWIDTH / $XDIV`

XHEIGHT=`xrdb -symbols | grep DHEIGHT | awk 'BEGIN{ FS = "="}{print $2}'`
XHEIGHT=`expr $XHEIGHT - 50`   #subtract for the panel
DY=`expr $XHEIGHT / $YDIV`

echo $DX $DY
COUNT=0
declare -i XOFF=0
declare -i YOFF=0
declare -i COUNT2=1


# opens up an xterm and runs a ssh command on each node as
# specified on command line
# -geometry string should find a better way of making the charWxcharH
# parms work for all machines-  45x15 is optimized for 1024x768.
# 45x15 will make small windows on large displays

for i in $Nodelist; do
    ssh $i /usr/local/sbin/noblank.sh
    xterm -geometry ${XSIZE}x${YSIZE}+$XOFF+$YOFF -hold -T $i -e ssh $i $* &
    if [ $COUNT2 -eq $XDIV ]; then
      XOFF=0
      YOFF=$YOFF+$DY
      COUNT2=1
    else
      XOFF=$XOFF+$DX
      COUNT2=$COUNT2+1
    fi
done

