#! /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';


use DBI;

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

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

my @runs;

&get_missing_runs_with_jobset($dbh,'PROD_PHYSICS_CDF',3,'10A',\@runs);


exit unless @runs;


# set the jobsets that need to be merged into PROD_PHYSICS_CDF

my @jobsets;

#jobsets to be merged
push @jobsets, 1553712; # align_jobset
push @jobsets, 1387648; #  mpa_jobset 

# keep track of all the runs that need to be added to 10A

my $all_errors;

my %pass_runs;

#loop through the runs and create PROD_PHYSICS_CDF
foreach my $run ( @runs ) 
{

# why the hell do these runs have L3_PHYSICS_CDF?
  next if $run == 1000000001 || $run == 1000000002 || $run == 1000000003 || $run == 1000000004;

  print "********* ".localtime().": Found new run $run not in pass 10A.  Merging run....\n";

  # first we want to check that they have all the necessary tables

  my $s0cotbeam  = &get_jobset($dbh,$run,'PAD_PHYSICS_S0COTBEAM',1); 
  my $cot_jobset = &get_jobset($dbh,$run,'PROD_PHYSICS_COT',1); 
  my $svx_jobset = &get_jobset($dbh,$run,'PROD_PHYSICS_SVX',1); 
  my $l3_jobset  = &get_jobset($dbh,$run,'L3_PHYSICS_CDF',1); 

  my $latest_version = &get_latest_version($dbh,$run,'PROD_PHYSICS_CDF');
  my $latest_jobset  = &get_jobset($dbh,$run,'PROD_PHYSICS_CDF',$latest_version);

  my $error_txt;
  
  $error_txt .= "PAD_PHYSICS_S0COTBEAM\n" if ! $s0cotbeam  || &diff_tables($dbh,$s0cotbeam, $latest_jobset);
  $error_txt .= "PROD_PHYSICS_SVX\n"      if ! $svx_jobset || &diff_tables($dbh,$svx_jobset,$latest_jobset);
  $error_txt .= "PROD_PHYSICS_COT\n"      if ! $cot_jobset || &diff_tables($dbh,$cot_jobset,$latest_jobset);
  $error_txt .= "L3_PHYSICS_CDF\n"         &remove_line(&diff_tables($dbh,$l3_jobset,$latest_jobset),'COT');


  if ( $error_txt )
  {
    $error_txt   = "run $run not merged.  Missing:\n$error_txt\n";
    $all_errors .= $error_txt;
    print $error_txt;
    next;
  }

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

  # now compare with the reference jobset

  # version 4 of run 228738
  my $ref_jobset = 1571239;

  $latest_version = &get_latest_version($dbh,$run,'PROD_PHYSICS_CDF');
  $latest_jobset  = &get_jobset($dbh,$run,'PROD_PHYSICS_CDF',$latest_version);

  $error_txt = &diff_all_tables($dbh,$latest_jobset,$ref_jobset);

 # removed the tables we expect to change run by run
 $error_txt = &remove_changing_tables($error_txt);

  if ( $error_txt )
  {
    $error_txt   = "\n10A jobset for run $run has following differences between reference jobset $ref_jobset:\n$error_txt";
    $all_errors .= $error_txt;

    print $error_txt;
  }

  # add the runs that are going to be added to pass 10A
  $pass_runs{$run} = $latest_version; 
}

my $pass_index = &get_pass_index($dbh,'10A');


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


&send_mail('tvine@fnal.gov,smalik@fnal.gov',$all_errors,'Error in fill_10A.pl') if $all_errors;

#__________________________________________________________________
sub remove_changing_tables
{
  my $txt = shift;

 $txt =  &remove_line($txt,'COTCHANT0');
 $txt =  &remove_line($txt,'COTDEADCH');
 $txt =  &remove_line($txt,'COTSTAGE0');
 $txt =  &remove_line($txt,'ICHIPPED');
 $txt =  &remove_line($txt,'SICHIPTHRESH');
 $txt =  &remove_line($txt,'SIDPSPARMS');
 $txt =  &remove_line($txt,'SISTRIPDH');
 $txt =  &remove_line($txt,'SISTRIPMASK');
 $txt =  &remove_line($txt,'SICHIPON');
 $txt =  &remove_line($txt,'COTPROBCH');
 $txt =  &remove_line($txt,'SVXBEAMPOSITION');
 $txt =  &remove_line($txt,'COTBEAMPOSITION');

  return $txt;

}
#__________________________________________________________________
sub remove_line
{
  # removes the line beginning with the string passed
  # the ? is for non-greedy matching (ie the first new line not the last!)
  my ($str,$line) = @_;
  $str =~  s/$line.*?\n//g;
  return $str;
}
