#!/bin/perl
# Dan's test perl script

# Documentation
my $long_doc = 
"ref_check.pl v1.0 - DPB 27-Jul-2007 - beecher\@fnal.gov

This script serves to extract the properties of the PROD_PHYSICS_CDF jobset given
a specific run or jobset.
  -j  Jobset ID
  
  -c  PROD_PHYSICS_CDF version number (get latest if not used)
  -r  Run number
  -l  Only list JOBSET
" ;
my $short_doc = $long_doc ;

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

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

# Get input from command line
my %opts;
getopts('lhr:j:c:',\%opts) or die $short_doc;

die $long_doc if $opts{h};

# Connect to the database
$ENV{'ORACLE_HOME'} = '/home/cdfsoft/products/oracle_client/v9_2_0_1a/Linux+2';
$ENV{'TNS_ADMIN'} = '/home/cdfsoft/dist/oracle_tnsnames';

my $dbh = DBI->connect('dbi:Oracle:cdfonprd','cdf_reader','reader') or die "Error connecting $DBI::errstr\n";
my $run;
my $jobset;
my $version;
my $date;
if ( $opts{r} ) {
  $run = $opts{r};
# Get latest version of PROD_PHYSICS_CDF for the run
  $version = &get_latest_version($dbh,$run,"PROD_PHYSICS_CDF");
  $version = $opts{c} if $opts{c};
  my $sql = $dbh->prepare("select jobset,create_date from used_sets where process_run = ? and proc_calib_version = ?");
  $sql->execute($run,$version);
  my @row = $sql->fetchrow_array();
  $jobset = $row[0];
  $date   = localtime($row[1]);
  #$jobset  = &get_jobset($dbh,$run,"PROD_PHYSICS_CDF",$version);
}
elsif ( $opts{j} ) {
  $jobset = $opts{j};
  my $sql = $dbh->prepare("select process_run,proc_calib_version,create_date from used_sets where jobset = ?");
  $sql->execute($jobset);
  my @row = $sql->fetchrow_array();
  $run = $row[0];
  $version = $row[1];
  $date = localtime($row[2]);
}

if ( $opts{l} ) {
print "${jobset}
";
} else {
print "The latest version of PROD_PHYSICS_CDF for the given run or jobset:
Run    : ${run}
Jobset : ${jobset} version ${version} (created ${date})
";
}
