#!/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 &
#/mnt/data/testdaq/bin/startpulser2 &
#/usr/local/bin/startfilterwheel &
#/usr/local/bin/startmonochromator &
sleep 5

#
#
# now loop through the tests we want...
#
#
steering_dir=/mnt/data/testdaq/special_steering_files/fat06/roomB
steering_dir=/mnt/data/testdaq/special_steering_files/fat06/minus45B
steering_dir=/mnt/data/testdaq/special_steering_files/fat7/plus25/
#steering_dir=/mnt/data/testdaq/special_steering_files/fat7/plus25-hub52
#steering_dir=/mnt/data/testdaq/special_steering_files/fat7/minus45A/
#steering_dir=/mnt/data/testdaq/special_steering_files/fat7/minus55
#steering_dir=/mnt/data/testdaq/special_steering_files/fat7/minus55special
#steering_dir=/mnt/data/testdaq/special_steering_files/fat7/minus45B/

#steering_dir=/mnt/data/testdaq/steering_files/initial_config/
#steering_dir=/mnt/data/testdaq/steering_files/chiba/
steering_dir=/mnt/data/testdaq/steering_files/monolith-v2-testing/


daqid=`cat /usr/local/etc/.domhub_name`
runno=`cat /usr/local/etc/.run_number`
#checklogs.pl init ${daqid}_run${runno}


# while [ 1 -gt 0 ]; do
  for steering in $steering_dir/*.xml; do
    $HOME/bin/increment_run_number.pl
    stbase=`basename $steering`
    runtype=${stbase%.xml}
    daqid=`cat /usr/local/etc/.domhub_name`
    runno=`cat /usr/local/etc/.run_number`
    mkdir $HOME/output${runno}
    rm $HOME/output${runno}/*
    echo $* > $HOME/output${runno}/${daqid}_run${runno}.comment
    testdaq $steering $HOME/output${runno} ${daqid}_run${runno}_$runtype \
        -d spts-ichub-cont01
#        -p localhost -m localhost -f localhost
#        -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 "`date` 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

sleep 10




