#!/bin/bash
#
# automate TestDAQ, and other scripts...
# Mark Krasberg, May, 2004
#
# your classpath must be previously defined!
#
#
# stuff to do at init:
#
echo STARTING AUTOMATE at `date`

rm -f stf.log
rm testdaq.log
rm domhubapp.log

killall rmiregistry
killall testdaq
killall python

sleep 2

#
# start rmi_registry
#
$JAVA_HOME/bin/rmiregistry &
#/usr/local/bin/startpulser &
#/home/testdaq/bin/startpulser2 &
#/usr/local/bin/startfilterwheel &
#/usr/local/bin/startmonochromator &
sleep 5

#
#
# now loop through the tests we want...
#
#

#steering_dir=$1/run_5e6_dn_fullreadout

#steering_dir=$1/special_steering_files/darknoise
###steering_dir=$1/interim_run_plan
#steering_dir=/mnt/data/testdaq/special_steering_files/flasherboard/part3/
#steering_dir=/mnt/data/testdaq/junk2/
#steering_dir=$1/dec2005checkout/ 
#steering_dir=$1/interim_run_plan2

#steering_dir=/mnt/data/testdaq/special_steering_files/verification/str21flasher
#steering_dir=/mnt/data/testdaq/special_steering_files/verification/icetop/
#steering_dir=/mnt/data/testdaq/special_steering_files/iniceonly/

#steering_dir=/mnt/data/testdaq/special_steering_files/verification/str21str29/
#steering_dir=$1/interim_run_plan3

#steering_dir=/mnt/data/testdaq/special_steering_files/verification/str29flasher/
#steering_dir=/mnt/data/testdaq/special_steering_files/verification/icetopflasher/
#steering_dir=/mnt/data/testdaq/special_steering_files/verification/str29lc/

#steering_dir=/mnt/data/testdaq/special_steering_files/verification/str21str29str39/flasher/
#steering_dir=/mnt/data/testdaq/special_steering_files/verification/str39/flasher/
#steering_dir=/mnt/data/testdaq/special_steering_files/verification/str21str29str39/flasher/
steering_dir=/mnt/data/testdaq/special_steering_files/verification/str21str29str39/flasher/HoleIce/

#while [ 1 -gt 0 ]; do
  if (alphabetically):
      SteeringFiles = $steering_dir/*.xml;
  elif (temporally):
      os.system('ls -rt1 > filename.list');
      SteeringFiles = open('filename.list').read().splitlines();

 

  for steering in $steering_dir/*.xml; do
    increment_run_number.pl
    sleep 1
    stbase=`basename $steering`
    runtype=${stbase%.xml}
    sync
    daqid=`cat /usr/local/etc/.domhub_name`
    runno=`cat /usr/local/etc/.run_number`
    mkdir $HOME/output${runno}

    tooquick=`ps -ef | grep prevent_too_many_bad_runs -c`
    while [ ${tooquick} -gt 1 ]; do
      echo " ERROR prevent_too_many_bad_runs already running - we will wait one minute before we start a new TestDAQ run"
      tooquick=`ps -ef | grep prevent_too_many_bad_runs -c`
      sleep 60
    done
    nohup prevent_too_many_bad_runs &

    testdaq $steering $HOME/output${runno} ${daqid}_run${runno}_$runtype \
        -d sps-ichub21 -d sps-ichub29 -d sps-ichub39
#        -d sps-ichub-dat01 -d sps-ichub-dat02 -d sps-ithub-dat01
#        -d sps-ichub-dat01 -d sps-ichub-dat02 
#        -d spts-ichub-cont01 -p localhost
#        -d domhub2 -d domhub3 -p localhost -m localhost -f localhost
#        -d spts-ichub-cont01 -p localhost
#        -p localhost -m localhost -f localhost
    bkgs=`ps -ef | grep background_it.pl -c`
    maxproc=3
    while [ ${bkgs} -gt ${maxproc} ]; do
       echo "sleeping for 60 seconds because there are $bkgs background_it.pl"
       sleep 60
       bkgs=`ps -ef | grep background_it.pl -c`
    done
#    sleep 20
  done
#done

