#!/bin/tcsh -f
#
#  ps_scaler_start - starts server and client for "rocps1" and "rocps2"
#
#  SP, 04-Mar-2015


set hh  = `echo $HOST | cut -c 1-5 `

if ($hh != "gluon") then
  echo ""
  echo " You should be logged into gluon machine"
  echo ""
  exit (1)
endif
echo ""

set xpos  = 1100
set ypos  = 50
set yinc  = 160
set lines = 8
set color = "-bg blue -fg white -sb -sl 5000"
set geom  = "-geometry 70x$lines+$xpos+"

set dir   = "/home/hdops/CDAQ/config/hd_server"

set GUI_t = "-T Scaler_Client_GUI_PS"
set XT_t  = "-T Scaler_Server_"

set GUI   = daq_comp_ps.tcl
set XT    = daq_comp_ps.tcl

set GUI_comp  = "ps_client"
set XT_comp1  = ""
set XT_comp2  = " sc_server "


set rocN = 2
set rocs = ( ROCPS1 ROCPS2 )

foreach roc ($rocs)
  echo " Start connection to CRATE_$roc"
  set smroc = `echo $roc | tr '[A-Z]' '[a-z]' `
  set tmp   = `scServer_control.sh off $smroc `
  set tmp   = `modify_setfile.sh set $smroc scStandalone.set `
  rm -f $dir/$smroc.gui
  set ypos  = `expr $ypos + $yinc`
  set geom  = "-geometry 70x10+$xpos+"
  set color = "-sb -sl 5000 -bg lightgreen -fg black"
  set XT_c  = "$XT_comp1$roc$XT_comp2"

  set tmp   = `xterm $XT_t$roc $color $geom$ypos -e $XT $XT_c >& /dev/null &`
end



echo -n " Starting Scaler Client GUI PS"
set ypos  = 20
set color = "-bg yellow -fg black -sb -sl 5000"

set rr = 0
while ( $rr != $rocN )
  echo -n "."
  sleep 1
  set tmp   = `ls -l $dir/rocps*.gui >& /dev/null &`
  set nn = 0
  foreach roc ($rocs)
    set smroc = `echo $roc | tr '[A-Z]' '[a-z]' `
    if ( -e $dir/$smroc.gui ) then
        @ nn++
    endif
  end
  set rr = $nn
end
echo ""

set tmp   = `xterm $GUI_t $color $geom$ypos -e $GUI $GUI_comp >& /dev/null &`
sleep 2
set pp = `ps -ef | grep -v grep | grep -i $GUI_t | awk '{print $2}' `
while ( $pp < 1 )
  set tmp   = `xterm $GUI_t $color $geom$ypos -e $GUI $GUI_comp >& /dev/null &`
  sleep 2
  set pp = `ps -ef | grep -v grep | grep -i $GUI_t | awk '{print $2}' `
  echo " Client GUI pid = >$pp<"
end


echo ""
echo " ps_scaler_start done"
echo ""

exit