use strict;

#$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;

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


printMPGoodRuns($ARGV[0],$ARGV[1]);

sub printMPGoodRuns()
{
    my ($minrun,$maxrun) = @_;

 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 '%Phyiscs' 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 >= ?
    and rc.runnumber <= ?
  order by rc.runnumber";

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

    $sth->execute($minrun,$maxrun);

   while (my $row = $sth->fetchrow_array)
   {
       print("$row\n");
   }


}
