

my $documentation = 
"
Prints pass informatiion to file.  
Default format: PASS_INDEX RUN RUN PROC_CALIB_VERSION

-f filename     default is runlist.dat 
-p pass        
-i pass_index
-a              specify an alternative pass_index for file
-s              short output.  only prints run number

-r lorun        specify a start run
-r lorun-hirun  specify a run range
-r lorun-last   specify a start run
";


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

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


my %opt;
getopts('f:p:i:r:sa:h',\%opt) or die $documentation;

if ($opt{h})
{
  print "$documentation\n";
  exit;
}

die $documentation if ( $#ARGV >= 0 
                    || $opt{i} && $opt{p}
		    || ! ($opt{i} || $opt{p}) );

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

print $opt{i}."\n";
my $file = "runlist.dat";    
my $pass_index;

if ($opt{p})
{
  $pass_index = &get_pass_index($dbh,$opt{p});
}
elsif ($opt{i}) 
{
  $pass_index = $opt{i};
}
else {die $documentation;}

my $new_pass_index = $pass_index;

$file = $opt{f} if $opt{f};
$new_pass_index = $opt{a} if $opt{a};




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

#get a list of all the runs in current pass
my $sql = "select lorun from passcalibs where pass_index = ?
           and lorun between ? and ?
           order by lorun asc";

my ($start_run,$end_run);

print "writing $file for pass_index $pass_index\n";
print &get_run_range($dbh, \$start_run, \$end_run, $opt{r}) . "\n";

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

while ( my $run = $sth->fetchrow )
{
  my $version = &get_pass_version($dbh,$run,$pass_index);

  if ($opt{s})
  {
    print FILE "$run\n";
  }
  else
  {
    &write_passcalib(*FILE{IO},$new_pass_index,$run,$version);
  }
}


close FILE;

print "finished\n";

exit;

