#! /cdf/scratch/cdfopr/cdfcalib/products/bin/perl

use strict;
use lib '/cdf/scratch/cdfopr/cdfcalib/perl_libs';
require "db_queries.pl";

$ENV{'ORACLE_HOME'} = '/home/cdfsoft/products/oracle_client/v9_2_0_1a/Linux+2';
$ENV{'TNS_ADMIN'} = '/home/cdfsoft/dist/oracle_tnsnames';

# first we need to figure out which runs are not in pass 0-JUNK

use DBI;

my $dbh = DBI->connect('dbi:Oracle:cdfonprd','cdf_reader','reader') or die "Error connecting $DBI::errstr\n";

my @runs;

&get_missing_runs_with_jobset($dbh,'L3_PHYSICS_CDF',1,'0-JUNK',\@runs);

exit unless @runs;


# set the jobsets that need to be merged into PROD_PHYSICS_CDF

my @jobsets;

push @jobsets, 1426491;  #PROD_PHYSICS_SVX v1 for 191778
push @jobsets, 1440925;
push @jobsets, &get_latest_validset($dbh,'L1_PHYSICS_FWD');
push @jobsets, &get_latest_validset($dbh,'L3_PHYSICS_CALTDC');


# keep track of all the runs that need to be added to 0-JUNK
my %pass_runs;

#loop through the runs and create PROD_PHYSICS_CDF and add it to pass 0-JUNK
foreach my $run ( @runs ) 
{

  print "********* ".localtime().": Found new run $run not in pass 0-JUNK.  Merging run....\n";

  print &db_set_merge($run,'L3_PHYSICS_CDF',"@jobsets", 'test' );
  print &db_set_merge($run,'L3_PHYSICS_CDF',"@jobsets", 'write' );

  # add the runs that are going to be added to pass 0-JUNK
  $pass_runs{$run} = 1; 
}

my $pass_index = &get_pass_index($dbh,'0-JUNK');


# add all of the recently merged runs into 0-JUNK
print &fill_pass($pass_index,\%pass_runs,'write');
