#!/bin/csh

# Script to set all DAC values for the FDC and CDC and commit them to the ccdb database
#
set CDCONLY = 1
set FDCONLY = 1
if ($1 == "CDC") then
    set FDCONLY = 0
    echo "CDC ONLY" 
endif
if ($1 == "FDC") then
    set CDCONLY = 0
    echo "FDC ONLY" 
endif

set thisDate = `date +%m-%d-%y_%H:%M`

#For Debugging
#set thisDate = 888

# First we will run the dobaseline*WithSearch scripts for the FDC and CDC
echo "Now running the baseline search ( May take a few minutes)"

if ($CDCONLY) then
    utilities/dobaselineCDCWithSearch.csh 0 ${thisDate}
endif
if ($FDCONLY) then
    utilities/dobaselineFDCWithSearch.csh 0 ${thisDate}
endif
# The program should abort if there is a coda_roc running on the crate
# This needs to be checked.
#
if ($CDCONLY) then
    foreach cdcROCID (1 2 3 4)
	    #echo "ROCCDC${cdcROCID}"
	    set errCDC = `tail /gluonfs1/home/hdcdcops/code/readout/log/roccdc${cdcROCID}_${thisDate}_baseline.log | grep coda_roc`
	    if ($errCDC != "") then
		    echo "ROCCDC${cdcROCID}"
		    echo $errCDC
		    exit 1
	    endif 
    end
endif

if ($FDCONLY) then
    foreach fdcROCID (2 3 5 6 7 8 9 10 11 12)
	    #echo "ROCFDC${fdcROCID}"
	    set errFDC = `tail /gluonfs1/home/hdcdcops/code/readout/log/rocfdc${fdcROCID}_${thisDate}_baseline.log | grep coda_roc`
	    if ($errFDC != "") then
		    echo "ROCFDC${fdcROCID}"
		    echo $errFDC
		    exit 1
	    endif
    end
endif

# Now we need to wait for the output, Will try to do this in a not-so-ugly way
# Will print to screen the number of crates that have completed
#

if ($CDCONLY) then
    @ numCDCDone = `find /gluonraid1/Users/hdcdcops/cdcdata/DAC/ -name "*${thisDate}*" | wc -l`
    printf  "${numCDCDone}/4 CDC DAC Output files found"
endif
if ($FDCONLY) then
    @ numFDCDone = `find /gluonraid1/Users/hdcdcops/fdcdata/DAC/ -name "*${thisDate}*" | wc -l`
    printf  "${numFDCDone}/10 FDC DAC Output files found"
endif

if (($CDCONLY) && ($FDCONLY)) then
    while ($numCDCDone != 4 || $numFDCDone != 10)
	    sleep 3
	    @ numCDCDone = `find /gluonraid1/Users/hdcdcops/cdcdata/DAC/ -name "*${thisDate}*" | wc -l`
	    @ numFDCDone = `find /gluonraid1/Users/hdcdcops/fdcdata/DAC/ -name "*${thisDate}*" | wc -l`
	    printf "\r\033[K${numCDCDone}/4 CDC and ${numFDCDone}/10 FDC DAC Output files found"
    end
endif
if (($CDCONLY) && (! $FDCONLY)) then
    while ($numCDCDone != 4 )
	    sleep 3
	    @ numCDCDone = `find /gluonraid1/Users/hdcdcops/cdcdata/DAC/ -name "*${thisDate}*" | wc -l`
	    printf "\r\033[K${numCDCDone}/4 CDC DAC Output files found"
    end
endif
if ((! $CDCONLY) && ($FDCONLY)) then
    while ($numFDCDone != 10)
	    sleep 3
	    @ numFDCDone = `find /gluonraid1/Users/hdcdcops/fdcdata/DAC/ -name "*${thisDate}*" | wc -l`
	    printf "\r\033[K${numFDCDone}/10 FDC DAC Output files found"
    end
endif




echo

echo "Printing results of convergence"

if ($CDCONLY) then
    foreach cdcROCID (1 2 3 4)
	echo "ROCCDC${cdcROCID}"
	    tail -n 50 /gluonfs1/home/hdcdcops/code/readout/log/roccdc${cdcROCID}_${thisDate}_baseline.log | grep converged
	    tail -n 50 /gluonfs1/home/hdcdcops/code/readout/log/roccdc${cdcROCID}_${thisDate}_baseline.log | grep ERROR
    end
endif

if ($FDCONLY) then
    foreach fdcROCID (2 3 5 6 7 8 9 10 11 12)
	echo "ROCFDC${fdcROCID}"
	    tail -n 50 /gluonfs1/home/hdcdcops/code/readout/log/rocfdc${fdcROCID}_${thisDate}_baseline.log | grep converged
	    tail -n 50 /gluonfs1/home/hdcdcops/code/readout/log/rocfdc${fdcROCID}_${thisDate}_baseline.log | grep ERROR
    end
endif

# Now all of the files have been created, Now we can concatenate the output into one file for each detector
#
echo "Concatenating DAC files" 
if ($CDCONLY) then
    set outputFileCDC = dacvalues/dacvalues_CDCAll_${thisDate}.dat
    foreach cdcROCID (1 2 3 4)
	cat /gluonraid1/Users/hdcdcops/cdcdata/DAC/dacvalues_ROCCDC${cdcROCID}_${thisDate}_ALLslots.dat >> ${outputFileCDC}
    end
endif

if ($FDCONLY) then
    set outputFileFDC = dacvalues/dacvalues_FDCAll_${thisDate}.dat
    foreach fdcROCID (2 3 5 6 7 8 9 10 11 12)
        cat /gluonraid1/Users/hdcdcops/fdcdata/DAC/dacvalues_ROCFDC${fdcROCID}_${thisDate}_ALLslots.dat >> ${outputFileFDC}
    end
endif


# We have made all of the files, now all that is left to do is to commit them to the database
#
echo "Committing to ccdb"

if ($CDCONLY) then
    ccdb add /CDC/dac_values/ ${outputFileCDC}
endif

if ($FDCONLY) then
    ccdb add /FDC/dac_values/ ${outputFileFDC}
endif

echo "DAC values successfully added to database"
exit 0
