#!/bin/perl

# 27-Jul-2007 DPB
# A small script to concatenate passcalib files
#
use strict;
use Getopt::Std;

my $doc_opts = 
"   -f  Ouput file with which to fill PASSCALIBS (defaults to passcalibs.dat)
   -P  passcalibs.dat file for old runs (e.g. pass 17)
   -A  passcalibs.dat file for new runs (e.g. merging tables into 17A)
   -r  Closed lower bound for new runs (open upper bound for old runs)
   -p  New pass index (default will guess from input)
   -v  Verbose mode (pipe to a log file)
   -h  Display this help
";
my $doc = "cat_passcalibs.pl - DPB 27-Jul-2007 - beecher\@fnal.gov

Concatenates two passcalibs.dat files to create the final passcalibs.dat file for
filling a 17TEST1.  Can also be used to join two PASSCALIBS files where runs need
to be ignored and a simple cat not sufficient.

The file for filling PASSCALIBS from merge.pl (-A) is trimmed by setting the lowest run
in the run range (-r) and added to the calibrated runs from the current pass 17 (-P).

Command line options:
".$doc_opts;

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

my %opt;
getopts('P:A:r:f:p:hv',\%opt) or die $doc;

# If help is requested
die $doc if $opt{h};

# Set the output file
my $file_out = "passcalibs.dat";
$file_out = $opt{f} if $opt{f};

# Get the input files
my $file_in_a = $opt{P} or die "Need to set: -P \n".$doc_opts;
my $file_in_b = $opt{A} or die "Need to set: -A \n".$doc_opts;
# Set the lowerst run
my $lowest_run = $opt{r} or die "Need to set: -r\n".$doc_opts;

my $timenow = localtime;

# Options are set, lets read some files!
print "----------------------------------------------------------------------
 PASSCALIBS : $timenow
----------------------------------------------------------------------
Concatenating old runs with new runs PASSCALIBS to form $file_out for filling next pass
Old runs : $file_in_a
New runs : $file_in_b (runs beginning at $lowest_run)
" if $opt{v};

# Open files
open FILE_IN_A, "<$file_in_a" or die "Cannot open $file_in_a for reading.\n";
open FILE_IN_B, "<$file_in_b" or die "Cannot open $file_in_b for reading.\n";
open FILE_OUT , ">$file_out"  or die "Cannot open $file_out for writing.\n";

my $pass_a;
print "----------------------------------------------------------------------
 Old runs PASSCALIBS entries 
 $file_in_a
----------------------------------------------------------------------
" if $opt{v};
while ( my $line = <FILE_IN_A> ) {
  ($pass_a, my $lorun, my $lorunb, my $version) = split(' ',$line);
  if ($lorun < $lowest_run) {
    $pass_a = $opt{p} if $opt{p};
    &write_passcalib(*STDOUT{IO},$pass_a,$lorun,$version) if $opt{v};
    &write_passcalib(*FILE_OUT{IO},$pass_a,$lorun,$version);
  }
}

   $pass_a = $opt{p} if $opt{p};
my $pass_b;

print "----------------------------------------------------------------------
 New runs PASSCALIBS entries
 $file_in_b
----------------------------------------------------------------------
" if $opt{v};

my $warned = 0;

while ( my $line = <FILE_IN_B> ) {
  ($pass_b, my $lorun, my $lorunb, my $version) = split(' ',$line);
  if ($lorun >= $lowest_run) {
    $pass_b = $opt{p} if $opt{p};
    if ( $pass_a != $pass_b && !$warned ) {
      print "WARNING: Pass indicies do not match! Assuming $pass_a is correct\n";
      $warned = 1;
    }
    &write_passcalib(*STDOUT{IO},$pass_a,$lorun,$version) if $opt{v};
    &write_passcalib(*FILE_OUT{IO},$pass_a,$lorun,$version);
  }
}

close FILE_IN_A;
close FILE_IN_B;
close FILE_OUT;
