#!/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/fat-4.7
#steering_dir=$1/special_steering_files/local_coincidence
#steering_dir=$1/special_steering_files/cable_system_test3
#steering_dir=$1/special_steering_files/chiba
#steering_dir=/home/testdaq/special_steering_files/dima
#steering_dir=$1/special_steering_files/icetop
#steering_dir=$1/special_steering_files/icetop/afterpulsesearch
#steering_dir=$1/special_steering_files/dima
#steering_dir=/home/testdaq/special_steering_files/release305test/inice
#steering_dir=/home/testdaq/special_steering_files/release305test/inice/nopulse
#steering_dir=$1/special_steering_files/first_icetop_runs
#steering_dir=$1/special_steering_files/suitcase_doms
#steering_dir=$1/special_steering_files/first_inice_runs
#steering_dir=$1/special_steering_files/flasherboard
#steering_dir=$1/special_steering_files/darknoise
#steering_dir=$1/special_steering_files/testofinicedomhub2
#steering_dir=$1/special_steering_files/iniceicetop
#steering_dir=$1/special_steering_files/marc
#steering_dir=$1/run_iniceicetop_dn
steering_dir=$1/interim_run_plan

while [ 1 -gt 0 ]; do
  for steering in $steering_dir/*.xml; do
    /usr/local/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}
    testdaq $steering $HOME/output${runno} ${daqid}_run${runno}_$runtype \
        -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
while [ 1 -gt 0 ]; do
  for steering in $steering_dir/darknoise/*.xml; do
    /usr/local/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`
    testdaq $steering $HOME/output ${daqid}_run${runno}_$runtype \
        -d domhub2 -d domhub3 \
	-p localhost -m localhost -f localhost
    sleep 20
  done
done

sleep 10
done



