#! perl -w

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

use DBI;

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


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

if ( @runs == 0 )
{
  print "pass 10A and 17A are up-to-date\n";
  exit;
}

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

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

foreach my $run (@runs)
{
  my $age = &get_run_age( $dbh, $run );

  $sth->execute( $run, $index10A );
  if ($sth->fetchrow) # then only 17A is missing
  {
    my $missing = '';

    if ( !&get_latest_version( $dbh, $run, 'PAD_PHYSICS_SVXBEAM') )
    {
      $missing .= ' PAD_PHYSICS_SVXBEAM';
    }

    if ( !&get_latest_version( $dbh, $run, 'PAD_PHYSICS_COTBEAM') )
    {
      $missing .= ' PAD_PHYSICS_COTBEAM';
    }

    $sth->execute( $run, $index10 );
    if (!$sth->fetchrow && !$missing )
    {
      $missing .= ' Pass 10';
    }	

    if ( $missing ) { $missing = "missing" . $missing }
    print "$run ($age hours old) not in 17A $missing\n";
  }
  else
  {   

    my $missing = '';

    if ( !&get_latest_version( $dbh, $run, 'PROD_PHYSICS_SVX') )
    {
      $missing .= ' PROD_PHYSICS_SVX';
    }

    if ( !&get_latest_version( $dbh, $run, 'PROD_PHYSICS_COT') )
    {
      $missing .= ' PROD_PHYSICS_COT';
    }

    if ( $missing ) { $missing = "missing" . $missing }
    print "$run ($age hours old) not in 10A $missing\n"; 
  }

}


$sth->finish;
$dbh->disconnect;

exit;
