#!/bin/bash

#
# One click Domcal-wrapper
# Hagar Landsman, April 2006
#


# Files setup, and cleanup
####################################

nproc=`ps -ef | grep icecube.daq.domcal.DOMCal | grep -v grep -c`
if [ $nproc -ne 0 ]
then
    echo " DOMCal process already active";
    exit 1
fi
source setclasspath $HOME/work
if [ $# -gt 4 ]
then
    echo "usage: $0 temperature A/B ndoms [update]"; exit 1
fi
if [ $# -lt 3 ]
then
    echo "usage: $0 temperature A/B ndoms [update]"; exit 1
fi
update="";
if [ $# -eq 4 ]
then
    if [ $4 != "update" ]
	then
	    echo "usage: $0 temperature A/B ndoms [update]"; exit 1
	    update="update";
	fi
fi

if [ ! -d ~/Results/DOMCal ]
then
        echo "No ~/Results/DOMCal directory found"; exit 1
fi
cd ~/Results/DOMCal

if [ -d ~/Results/DOMCal/$1$2 ] 
then
        rm -r $1$2/* 
fi
if [ ! -d ~/Results/DOMCal/$1$2 ] 
then
        mkdir $1$2
fi
$dir=~/Results/DOMCal/$1$2

cd $1$2
####################################
# dtsxinit
####################################
stoptestdaq  # just to be on the safe side
dtsxinit
####################################
# DOMCal
###################################
echo 'starting domcal....'
echo nohup java icecube.daq.domcal.DOMCal localhost 5061 2 $HOME/Results/DOMCal/$1$2 calibrate dom &
nohup java icecube.daq.domcal.DOMCal localhost 5061 2 $HOME/Results/DOMCal/$1$2 calibrate dom &
nohup java -Xmx1024M icecube.daq.domcal.DOMCal localhost 5001 60 $HOME/Results/DOMCal/$1$2 calibrate dom calibrate hv &
nproc=1.
let nproc=1+0
echo nproc=$proc
outp=1.
xmlp=1.
xmlold=0.
~/hagar/domcal-loop $3
echo 'done domcal....'
#################################
# Verify that all is finished
################################
ps -ef |grep domcal
# add a check that the first process on the boards is done before going on
# add count number of DOMs with .out, number of DOMs with .xml

################################
# Update db if neccesary, check and compare to old results, copy file to xml
################################
~/hagar/checkDOMCal.pl $3 $update> check.txt

################################
# copy file to dated directory for Mark
################################
year=$(date +%Y)
month=$(date +%m)
day=$(date +%d)
hour=$(date +%H)
minute=$(date +%M)
seconds=$(date +%S)
mkdir $HOME/domcal/new-files/$year$month$day-$hour$minute$seconds
cp *  $HOME/domcal/new-files/$year$month$day-$hour$minute$seconds/.

#############################################
# Generate histos
#############################################
if [ ! -d ~/Results/DOMCal/$1$2/histos ] 
then
	mkdir histos
fi
java icecube.daq.domcal.HVHistogramGrapher  ./  ./histos   ./
mozilla HV.html  # What browser do we have?

##########################################
# Generate summary file
##########################################
# add- txt file with calculated gain and HV to be used in spread sheet.
echo "done calibration. db updated. histogram made. xml moved to right directory"
