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

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

# the email addresses
my $svx_email = 'nielsenj@fnal.gov';
my $cot_email = 'johnpaul@fnal.gov';
my $beamlines_email = 'boj@fnal.gov';
my $calib_email = 'tvine@fnal.gov';

# the time offset
my $beamlines_time = 48;
my $cot_time = 2;
my $svx_time = 24;

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

use DBI;

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

my $svx_txt = '';
my $cot_txt = '';
my $beamlines_txt = '';
my $calib_txt = '';

my @runs;
&get_missing_good_runs( $dbh, 190000, \@runs);

foreach my $run (@runs)
{
    
    my $txt = &check_pass( $dbh, $run, '17A', 'PAD_PHYSICS_SVXBEAM', $beamlines_time )
	    . &check_pass( $dbh, $run, '17A', 'PAD_PHYSICS_COTBEAM', $beamlines_time );  
    if ( $txt ) 
    {
	$beamlines_txt .= $txt;
	$calib_txt .= $txt;
    }
    elsif ( &get_run_age( $dbh, $run ) > $beamlines_time + 1 )
    {
	$calib_txt .=  &check_pass( $dbh, $run, '17A' );
    }

    $txt = &check_pass( $dbh, $run, '10A', 'PROD_PHYSICS_SVX', $svx_time ); 
    if ( $txt )
    {
	$svx_txt .= $txt;
	$calib_txt .= $txt;
    }
    elsif ( &get_run_age( $dbh, $run ) > $svx_time + 1 )
    {
	$calib_txt .=  &check_pass( $dbh, $run, '10A' );
    }
    $txt = &check_pass( $dbh, $run, '10A', 'PROD_PHYSICS_COT', $cot_time );
    $cot_txt .= $txt;
    $calib_txt .= $txt;
}

print $calib_txt;

my $subject = 'missing calibrations';

if ( $cot_txt ) { &send_mail($cot_email, $cot_txt, $subject) } 
if ( $calib_txt ) { &send_mail($calib_email, $calib_txt, $subject) }
if ( $svx_txt ) { &send_mail($svx_email, $svx_txt, $subject) }
if ( $beamlines_txt ) { &send_mail($beamlines_email, $beamlines_txt, $subject) } 

$dbh->disconnect;

exit;

# ---------------------------------------------------
sub send_mail
{
#Arguments 1) email addresses separated my commas 2) content 3) subject
    my $mail = '/bin/mail';
    open MAIL, "| $mail -s \"cdfcalib monitoring: $_[2]\" $_[0]";
    print MAIL $_[1];
    close MAIL;
}

# -------------------------------------------------------------
sub check_pass
{
    #i) db handle ii) run iii) pass name vi) jobset name v) time offset in hours 
    
    # this checks that a run is not in a pass AND (if specified) is missing a jobset
    # returns a string if missing
    
    my ( $dbh, $run, $pass, $process_name, $time_offset ) = @_;

    my $age = &get_run_age( $dbh, $run );
    if ( $time_offset &&  $age < $time_offset ) { return }

    my $pass_index = &get_pass_index( $dbh, $pass );

    my $sth = $dbh->prepare( "select lorun from passcalibs 
                          where lorun = ? and pass_index = ?" );

    $sth->execute( $run, $pass_index );
    if ( $sth->fetchrow ) { return }

    if ( &get_latest_version( $dbh, $run, $process_name ) ) { return }

    if ( $process_name ) { $process_name  = " missing $process_name" }

    return "$run ($age hours old) not in $pass $process_name\n";
}
