#! /bin/tcsh -f
# -----------------------------------------------------------------------------
#
# Name       : prod_used.tcsh
#
# Purpose    : Make PROD_PHYSICS_CDF used set for production.
#
# Usage      : prod_used.tcsh
#
# Options    : None.
#
# Arguements : None.
#
# Created    : 2001-06-05    Rob Snihur RMS20010605
# Updated    : 2001-08-01    Rob Snihur RMS20010801 Move PESLASER merge to
#                            version 1, and add PROD_PHYSICS_SVX to v2.
#                            Note PROD_PHYSICS_SVX is from valid sets.
# Updated    : 2001-08-05    Rob Snihur RMS20010805 Make v3 w/ SVX jobset 24999
# Updated    : 2001-08-05    Rob Snihur RMS20010808 Make v4 w/ SVX jobset 25239
# Updated    : 2001-08-14    Rob Snihur RMS20010814 New exe & oracle client;
#                            new SVX jobset 25979; trigger replication.
# Updated    : 2001-08-20    Rob Snihur RMS20010820 Turned off replication.
# Updated    : 2001-08-21    Rob Snihur RMS20010821 Replicate with db_rep.
# Updated    : RMS20010911 Copy differences from svx_tmp/prod_used_svx.tcsh
#                          Get SVX set w/ highest run.  Exit if sqlplus fails.
# Updated    : RMS20011115 Make L3_TEST_CDF = L3_PHYSICS_CDF + L3_PHYSICS_CAL
# Updated    : RMS20020115 PROD_PHYSICS_CDF += L3_PHYSICS_CAL; add 4 versions.
#                          Also change exe link, and print out exe info.
# Updated    : RMS20020118 Remove check for new SVX valid_sets to prevent
#                          contention with cron job that does it every 5 min.
# Updated    : RMS20020119 Print out dereferenced (ls -L) exe info also.
# Updated    : RMS20020430 Remove replication.
# Updated    : RMS20020723 Immediate exit if 
#                          a) script already running.
#                          b) ping of cdfonprd (=b0dau35) fails.
# Updated    : RMS20020830 Remove all unnecessary version of PROD_PHYSICS_CDF.
# Updated    : RMS20021119 Output date.
# Updated    : RMS20030604 Check for numeric runnumber; add exit 3 and 4.
# Updated    : 01/29/05 Ben Cooper. Now merge in latest valid_set of L1_PHYSICS_FWD 
#              and PROD_PHYSICS_TOF after these were removed from L3_PHYSICS_CDF.
# Updated    : 02/09/05 Ben Cooper. Now merge in latest valid set of L3_PHYSICS_CALTDC
#              after these new calibrations were created.
#
# -----------------------------------------------------------------------------
ps -fN -p $$ | grep $USER | grep $0 | grep -v grep && exit 5
source ~cdfsoft/cdf2.cshrc
setup cdfsoft2 5.3.3
setup cdfdb
dbPing -dcdfonprd -s > /dev/null || exit 6
cd /data1/cdfcalib/cron_scripts/clone_L3

@ last = 0                             # Default.               
#set last = `grep . runs.txt | tail -1` # Get the last processed run number.
@ last = `grep . runs.txt | tail -1 | awk '{print int($1)}'` # Get the last processed run number.
#echo last run is $last
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 process_run > $last;
quit;
EOF

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

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

#
# Print out exe info.
#
if (! -z runs.txt) then                # If new data-taking runs.
    set dat = `date`
    echo " Script ran at $dat "

    ls -lrt ~snihur/DBSetMerge.exe/DBSetMerge.20020111                 # Show date and size of exe.
    ls -Llrt ~snihur/DBSetMerge.exe/DBSetMerge.20020111             # Dereferenced info, but link name.
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 ~snihur/DBSetMerge.exe/DBSetMerge.20020111 OTL /@cdfonprd used $r PROD_PHYSICS_CDF L3_PHYSICS_CDF $latest_tof_validset $latest_fwd_validset $latest_tdc_validset
	rm core >& /dev/null            # Remove core file, if it exists.
           ~snihur/DBSetMerge.exe/DBSetMerge.20020111 TESTMODE OTL /@cdfonprd used $r PROD_PHYSICS_CDF L3_PHYSICS_CDF $latest_tof_validset $latest_fwd_validset $latest_tdc_validset
	rm core >& /dev/null            # Remove core file, if it exists.
           ~snihur/DBSetMerge.exe/DBSetMerge.20020111 OTL /@cdfonprd used $r PROD_PHYSICS_CDF L3_PHYSICS_CDF $latest_tof_validset $latest_fwd_validset $latest_tdc_validset
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
        echo "==============================end run===================================="
endif                           # if (-z runs.txt) then

