#!/bin/bash

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

source ~/fat.setup
# 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 [doms/mb] [update]"; exit 1
fi
if [ $# -lt 2 ]
then
    echo "usage: $0 temperature A/B [doms/mb] [update]"; exit 1
fi
update=""
doms=""
if [ $# -gt 2 ]
then
    if [ $3 != "update" ] && [ $3 != "doms" ] && [ $3 !="mb" ]
	then
	    echo "usage: $0 temperature A/B [doms/mb] [update]"; exit 1
	fi
    if [ "$3" == "update" ]; then
	update="update"
     fi
    if [ "$3" == "doms" ]; then
	 doms="doms"
 fi
    if [ "$3" == "mb" ]; then
	 doms="mb"
 fi

fi
if [ $# -gt 3 ]
then
    if [ $4 != "update" ]  && [ $4 != "doms" ] && [ $4 !="mb" ]
	then
	    echo "usage: $0 temperature A/B [doms] [update]"; exit 1
	fi
    if [ "$4" == "update" ]; then 
	update="update"
    fi
    if [ "$4" == "doms" ]; then 
	doms="doms"
    fi
    if [ "$4" == "mb" ]; then 
	doms="mb"
    fi
fi
echo *$3*$4*
if [ ! -d ~/Results/DOMCal ]
then
        echo "No ~/Results/DOMCal directory found"; exit 1
fi
cd ~/Results/DOMCal

if [ -d ~/Results/DOMCal/$1$2 ] 
then
        gzip $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....$doms output in ~/Results/DOMCal/$1$2
ndoms=0;
if [ "$doms" != "doms" ]
then
   ndoms=2
   nohup java icecube.daq.domcal.DOMCal localhost $FAT_SYNCH_BOARD 1 $HOME/Results/DOMCal/$1$2 calibrate dom &
   nohup java icecube.daq.domcal.DOMCal localhost $FAT_REF_BOARD 1 $HOME/Results/DOMCal/$1$2 calibrate dom &
   echo 'for 2 boards'
fi
if [ "$doms" != "mb" ]
then
  echo nohup java -Xmx1024M icecube.daq.domcal.DOMCal localhost $FAT_PORT $FAT_NDOMS $HOME/Results/DOMCal/$1$2 calibrate dom calibrate hv &
  nohup java -Xmx1024M icecube.daq.domcal.DOMCal localhost $FAT_PORT $FAT_NDOMS $HOME/Results/DOMCal/$1$2 calibrate dom calibrate hv &
  let ndoms=$ndoms+$FAT_NDOMS
  echo  for $FAT_NDOMS $ndoms doms
fi
nproc=1.
let nproc=1+0
echo nproc=$proc
outp=1.
xmlp=1.
xmlold=0.
~/hagar/domcal-loop $ndoms
nd=0
ns=0
nd=`ls * | grep nohup -v| grep out -c`
ns=`tail *.out |grep successfully. -c`
echo 'done domcal at $1$2 for $ndoms, $nd xml files created, $ns finished succesfully  '

# 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
################################
echo starting checkDOMCal.pl $ndoms $update
echo ~/hagar/checkDOMCal.pl $ndoms $update 
~/hagar/checkDOMCal.pl $ndoms $update > check.txt
if [ $ns != $nd ] 
then
	echo Failed to calibrate: >>check.txt
	grep successfully. *.out -L >> check.txt
fi  
cat check.txt | mail $FAT_OPERATOR -s "DOMCAL_$1$2_finished on hub $FAT_HUB for fat$FAT_NUM"

################################
# 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
#############################################
echo Generating histos
if [ ! -d ~/Results/DOMCal/$1$2/histos ] 
then
	mkdir histos
fi
java icecube.daq.domcal.HVHistogramGrapher  ./  ./histos   ./
pa=`pwd`
mozilla $pa/histos/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"
