#! /bin/tcsh -f

# no comments here please.  comment when updating cvs

#ps -fN -p $$ | grep $USER | grep $0 | grep -v grep && exit 5
source ~cdfsoft/cdf2.cshrc

set dat = `date`
echo " *************************** prod_used.tcsh ran at $dat *****************************"
#
setup cdfsoft2 6.1.1
#dbPing -dcdfonprd -s > /dev/null || exit 6

cd /cdf/scratch/cdfopr/cdfcalib/cdfopr/cdfcalib/cron_scripts/clone_L3


@ last = 0                             # Default.               

@ last = `grep . runs.txt | tail -1 | awk '{print int($1)}'` 


if ( $last == 0 ) then                  # If run number is invalid.
    echo "file runs.txt:"; echo "============"; cat runs.txt; exit 3    # Print file, and exit.
endif                                   # if ( $last == 0 ) then

rm l3.sql >& /dev/null; touch l3.sql || exit 1 # Create a new file to hold SQL commands.
cat >> l3.sql << EOF
set heading off;
set feedback off;
remark Following removes blank lines from output.
set pagesize 0;
set verify off;
spool runs.txt;
select process_run from used_sets where process_name = 'L3_PHYSICS_CDF' and proc_calib_version = 1 and process_run > $last
order by process_run;
quit;
EOF

@ static_svx_jobset = 1426491 #PROD_PHYSICS_SVX v1 for 191778


@ latest_fwd_validset = `perl getLatestValidSet L1_PHYSICS_FWD cdfonprd cdf_reader reader`
@ latest_tdc_validset = `perl getLatestValidSet L3_PHYSICS_CALTDC cdfonprd cdf_reader reader`

@ latest_tof_validset = 1440925


$ORACLE_CLIENT_DIR/bin/sqlplus -s cdf_reader/reader@cdfonprd @l3.sql || exit 2 # Get the last L3 used set.


if (! -z runs.txt) then                # If new data-taking runs.
    echo "found new runs"

endif                                  # if (-f runs.txt) then

# Check that all runs in list are numeric.
foreach r ( `grep . runs.txt` )        # Loop over runs.
    @ n = 0                            # Default.
    @ n = `echo $r | awk '{print int($1)}'` # Numeric run.
    if ( $n == 0 ) then                # If non-numeric run translated to zero.
	echo "Error: Exit because of non-numeric run $r"
	echo "Will not process any runs."
	echo "file runs.txt:"; echo "============"; cat runs.txt # Print file.
	echo $last >! runs.txt         # Put the last processed run in there.
        exit 4                         # Exit.
    endif                              # if ( $n == 0 )
end                                    # foreach r ( `grep . runs.txt` )

foreach r ( `grep . runs.txt` ) # Loop over runs.
    echo DBSetMerge OTL /@cdfonprd used $r PROD_PHYSICS_CDF L3_PHYSICS_CDF $latest_tof_validset $latest_fwd_validset $latest_tdc_validset $static_svx_jobset
    rm core >& /dev/null            # Remove core file, if it exists.
    DBSetMerge TESTMODE OTL /@cdfonprd used $r PROD_PHYSICS_CDF L3_PHYSICS_CDF $latest_tof_validset $latest_fwd_validset $latest_tdc_validset $static_svx_jobset
    rm core >& /dev/null            # Remove core file, if it exists.
    DBSetMerge OTL /@cdfonprd used $r PROD_PHYSICS_CDF L3_PHYSICS_CDF $latest_tof_validset $latest_fwd_validset $latest_tdc_validset $static_svx_jobset
end                             # foreach r ( `grep . runs.txt` )

if (-z runs.txt) then           # If no new runs.
	echo $last >> runs.txt  # Put the last processed run in there.
else



#filling PROD_PHYSICS_CDF pass JUNK with v1 PROD_PHYSICS_CDF we just created

rm passcalibs.dat >& /dev/null

echo "filling pass JUNK with  v1 PROD_PHYSICS_CDF we just created"

@ highest_pass_index_of_JUNK = `$CDFSOFT2_DIR/CalibDB/bin/PassTool.pl -c /@cdfonprd -n 0-JUNK -l | grep "0-JUNK" | grep "PROD_PHYSICS_CDF" | grep -v "YES" | grep "A" | awk '{print $1}' `

echo "got highest pass index for pass JUNK: $highest_pass_index_of_JUNK"

foreach r (`grep . runs.txt`)

    echo "$highest_pass_index_of_JUNK        $r          $r          1" >> passcalibs.dat

end

echo "filled passcalibs file"
cat passcalibs.dat

$CDFSOFT2_DIR/CalibDB/bin/PassCalibTool.pl  -c /@cdfonprd -s -r passcalibs.dat #to screen for cronjob
echo "made insert statements; now filling table"
$CDFSOFT2_DIR/CalibDB/bin/PassCalibTool.pl  -c /@cdfonprd   -r passcalibs.dat #to screen for cronjob
echo "filled passcalib table"


        echo "==============================end run===================================="
endif                           # if (-z runs.txt) then

