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

#############
#
#  ValidRequester.pl
#
# Makes validation plots for runs which
#      - have pass 17test1 defined
#      - have validation ntuples available [ see "CalibValidFiles.pl" ]
#      - have not had validation plots made yet
#

use strict;
use lib '/cdf/scratch/cdfopr/danielw/CommonLibs/';
require "db_queries.pl";
require "CalibValidFiles.pl";
require "CalibStatus.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;

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

# arbitrary
my $firstrun = 217989;


# some definitions -> put these in a common header later
my $status_dir = "/cdf/home/danielw/public_html/Calibrations/Status";
my $status_html = "http://www-cdf.fnal.gov/~danielw/Calibrations/Status";

my $val_output_dir = "/export/data1/cdfopr/validation/";

my $submit_dir = "/cdf/scratch/cdfopr/danielw/Validation/Submit/";
my $plots_dir = "/cdf/scratch/cdfopr/danielw/Validation/Plots/";
my @strips = ("zee");#"wen","wmn","zee");

# associate process with user
my %emails;
$emails{"wen"} = $emails{"wmn"} = $emails{"zee"}    = "danielw\@FNAL.GOV";

my %runsv = &get_unvalidated_runs($dbh,$firstrun);

my $k;

my $stage = "Valid";

# get list of ready rungroups [ submitted but not validated ]
my @rungroups = &getSubmittedRungroups();

my %vfiles;

# this should loop over rungroups, and check whether the rungroup has been submitted and pass 17test1 exists

my $rg;
foreach $rg (@rungroups)
{
    printf("Rungroup $rg need validating. Checking run status...\n");
    my @runs = &getRuns($rg);
    my $rs = 1;

 
    # check to make sure all are pass17test1 and not pass17
    my $run;
    foreach $run (@runs)
     {	
	 printf("[run $run=$rs] ");
       $rs = $rs && ($runsv{$run});
     }
    printf("[ status = $rs ]\n");


    if ($rs)
    {

	printf("[ looking for files for $rg ]\n");
	my $strip;
	my %vfiles_run;
	my $validavail = 1;
	my $missing ="";
	    
	foreach $strip (@strips)
	{
	    
	    # look for validation files
	    my @vfiles_run_strip = &getValidNtuples($strip,$rg);
	    
	    printf(" Files available for $strip in rungroup $rg:".(scalar @vfiles_run_strip)."\n");
	    if ((scalar @vfiles_run_strip) > 0)
	    {
		$vfiles_run{$strip} = [ @vfiles_run_strip ];
	    }
	    else
	    {
		$validavail = 0;
		$missing = $missing." ".$strip;
	    }
	}
	
	if ($validavail)
	{
	    foreach $strip (@strips)
	    {
		my $vnew = $vfiles_run{$strip};
		my @vnewa = @$vnew;
		my $vold = $vfiles{$strip};
		if ($vold eq "")
		{
		    $vfiles{$strip} = [ @vnewa ];
		}
		else
		{
		    my @volda = @$vold;
		    $vfiles{$strip} = [ (@volda,@vnewa) ];
#		    printf(" New files are : @vnewa\nOld files are: @volda\n\n");
		    
		}
	    }
	    printf(" All files available for validation!\n");

	    doValid($rg);

	    requestApproval($rg);
	}
	else
	{
	    printf("Missing files in rungroup $rg for strips $missing\n");
	}
	
    }
   
}



exit(0);

sub doValid()
{
    my ($rg) = @_;
    printf("performing calibs for [ $rg ]\n");
    my $strip;


    my $cdir = "${status_dir}/${stage}_Plots/$rg/";
    printf("Making $cdir\n");
    `mkdir $cdir`;
    foreach $strip (@strips)
    {
	my $vfiles_strip = $vfiles{$strip};
	my @vfiles_strip_arr = @$vfiles_strip;
	printf("Strips ${strip}:".@vfiles_strip_arr." files available\n");
	my $pdir = $cdir."/$strip";
	`mkdir $pdir`;

	printf("Writing file list to ${pdir}/files.list\n");

	open(FLOUT,">${pdir}/files.list");
	my $cf;
	foreach $cf ( @vfiles_strip_arr)
	{
	    print FLOUT "$cf\n";
	}
	close(FLOUT);

	open(RGOUT,">${plots_dir}/${strip}/rungroup.txt");
	print RGOUT "$rg\n";
	close(RGOUT);

	my $cmd = "cp ${pdir}/files.list ${plots_dir}/${strip}; cd ${plots_dir}/${strip}; ./run.sh ./files.list";
	printf("Running: $cmd\n");
	my @cmdres = `$cmd | grep "ResultsFile" | awk '{print \$2}'`;
	printf("Result: @cmdres\n");
	my $rf;
	foreach $rf (@cmdres)
	{
	    chop($rf);
	    my $cmd2 = "mv ${plots_dir}/${strip}/$rf ${pdir}";
	    printf("Moving result file $cmd2\n");
	    `$cmd2`;
	}

	# indicate the status
	open(SOUT,">${pdir}/status.txt");
	print SOUT "Ready";
	close (SOUT);

    }
}

sub requestApproval()
{
    my ($rg) = @_;
    printf("requesting approval  for for [ $rg ]\n");
    my $strip;


    my $cdir = "${status_html}/${stage}_Plots/${rg}";
    my $cdirl = "${status_dir}/${stage}_Plots/${rg}";
    

    foreach $strip (@strips)
    {
        my $pdir = $cdir."/$strip";
	my $message = "Please review the new validations for $strip covering runs ($rg):\n$pdir\n";
	my $recip = $emails{$strip};
	open(MOUT,">.mail.${strip}.$$");
	print MOUT $message;
	close(MOUT);
	my $res = `mail -s \"Please review $strip validations ($rg)" $recip < .mail.${strip}.$$`;
	`rm .mail.${strip}.$$`;
	printf("Sent \"$message\" to $recip with result $res\n");
	
    }
}

# same as ValidationSubmitter.pl
sub get_unvalidated_runs
{
# Arguments: 1) database handle 2) start run 3 array to return runs
# fills hash with key = run and value = run terminate time

    my ( $dbh, $start_run) = @_;
    my %unvruns;

  my $sql =
  "select rc.runnumber
  from runconfigurations rc, run_status rs, physicstables pt
  where rs.runcontrol_status != 0
    and rs.shiftcrew_status != 0
    and rc.b0integratedlivelumi > 10.0
    and rc.csl_neventsoutput > 1000
    and not pt.physicstablename like '%TEST%'
    and (pt.physicstablename like '%Physics' or pt.physicstablename like '%PHYSICS%')
    and rc.runnumber in (select lorun from passcalibs where pass_index =
  ( select pass_index from passes where passname like '10A' and retired is null) )
    and pt.runnumber = rc.runnumber
    and rs.runnumber = rc.runnumber
    and rc.runnumber > ?
  order by rc.runnumber";

#    and rc.runnumber not in (select lorun from passcalibs where pass_index =
#  ( select pass_index from passes where passname like '17' and retired is null) )


    my $sth = $dbh->prepare($sql);
    $sth->execute($start_run);

    while (my $row = $sth->fetchrow) { $unvruns{$row} = 1; }

    return %unvruns;
}
