#! perl 

# written by troy sometime in september 2005

# merges used_sets into PRDO_PHYSICS_CDF and then created the correct 
# passcalibs.dat needed to fill pass


my $documentation = 
"
merges the jobsets specified in the file with the latest version of PROD_PHYSICS_CDF

input file format:

PROCESS_RUN followed by any combinations of JOBSET and PROCESS_NAME

  -w               write to database
  -f filename      default is merge.dat 
  -i pass_index    specify pass_index for passcalibs.dat file for filling

to merge with a specific pass version of PROD_PHYSICS_CDF

  -p pass       

";


use strict;
use Getopt::Std;
use DBI;

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

my %opt;
getopts('f:wi:hp:',\%opt) or die $documentation;

die $documentation if ( $#ARGV >= 0 || $opt{h} );

my $file = "merge.dat";    
my $mode = 'test';
my @patched_runs;

$mode = 'write' if ($opt{w});
$file = $opt{f} if $opt{f};


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

my $pass_index;
$pass_index = &get_pass_index($dbh,$opt{p}) if $opt{p};

open FILE,"< $file" or die "cant open $file";

while ( <FILE> )
{
    my ($run,$jobsets) = split (/ +/,$_,2);
   
    chomp $jobsets;
    
    my $cdf_jobset = 'PROD_PHYSICS_CDF';  # get the lastest version

    if ($pass_index ) 
    {
	my $version = &get_pass_version( $dbh, $run, $pass_index ); 
	$cdf_jobset = &get_jobset( $dbh, $run, 'PROD_PHYSICS_CDF', $version )
    }
    
    print &db_set_merge( $run, $cdf_jobset, $jobsets, $mode );
    push @patched_runs, $run;
}


open FILE, "> passcalibs.dat" or die "can't create passcalibs.dat";


if ( $pass_index ) # create passclibs.dat for entire run range
{
#    if ( $mode ne 'write' )  # this can take a while
#    {
#	print "make passcalibs.dat? (y/n)\n";
#	unless (<STDIN> =~ /y/i) {exit;}
#    }
#    
    my $new_pass_index = $opt{i};
    if (!$new_pass_index)  # guess what new pass index will be
    {
	my $sth = $dbh->prepare('select max(pass_index) from passes');
	$sth->execute();
        $new_pass_index = 1 + $sth->fetchrow;
    }
    
    print "writing passcalibs.dat with pass index $new_pass_index and new version of PROD_PHYSICS_CDF for patched runs and pass $opt{p} (pass_index $pass_index) version of all other runs\n";
    
    
#get a list of all the runs in current pass
    my $sql = "select lorun from passcalibs where pass_index = ?";
    
    my $sth = $dbh->prepare($sql);
    $sth->execute($pass_index);
    
    while ( my $run = $sth->fetchrow )
    {
	my $version;
	
	if ( grep( $_ == $run, @patched_runs ) ) 
	{
	    $version = &get_latest_version($dbh,$run,'PROD_PHYSICS_CDF'); 
	    my $date = &get_version_create_date($dbh, $run, $version, 'PROD_PHYSICS_CDF' );
	    print "using version $version (created $date) for run $run\n";
	}
	else
	{
	    $version = &get_pass_version($dbh,$run,$pass_index);
	}
	
	&write_passcalib(*FILE{IO},$new_pass_index,$run,$version);
    }
}
else  # creating passcalibs.dat for adding runs to current pass
{
    print "writing passcalibs.dat for pass_index $pass_index and new versions of PROD_PHYSICS_CDF\n";
    for my $run (@patched_runs)  
    {
	my $version = &get_latest_version($dbh,$run,'PROD_PHYSICS_CDF'); 
	&write_passcalib(*FILE{IO},$opt{i},$run,$version);
	my $date = &get_version_create_date($dbh, $run, $version, 'PROD_PHYSICS_CDF' );
	print "adding version $version (created $date) for run $run\n";
    }
    

}

close FILE;

print "finished\n";

exit;

