#! /usr/bin/perl -w

use DBI;
use Getopt::Long qw( GetOptions :config auto_help auto_version gnu_getopt );
use PS::IPP::Config 1.0.1 qw( :standard );

my $ipprc =  PS::IPP::Config->new(); # IPP Configuration
my $siteConfig = $ipprc->{_siteConfig};

my $dbname = 'gpc1';

GetOptions(
    'dbname=s'    => \$dbname,
    );

my $date = shift(@ARGV);
unless(defined($date)) {
    my @time = gmtime;
    $date = sprintf("%4d-%02d-%02d",$time[5] + 1900,$time[4] + 1,$time[3]);
}
#print "Date: $date\n";

#print "CMD: regtool -checkstatus -date $date -class_id ota33 -dbname gpc1 -simple\n";
chomp(my $last_exp = `regtool -checkstatus -date $date -class_id ota33 -dbname gpc1 -simple | grep 'stop stop full full' | tail -1`);
my @row2 = split /\s+/, $last_exp;
if ($#row2 == 18) {
#    print "     exp_name uri registered Nimfile summit_fault download_state newExp_state rawExp_state imfile_state exp_id exp_type dateobs obs_mode object burntool_state class_id summit_class_id is_downloaded is_registered\n";

    print "last registered: $row2[0]\n"
}
elsif ($#row2 == -1) {
    print "\nSomething is wrong\n";
    exit(0);
}


#print "CMD: regtool -checkstatus -date $date -class_id ota33 -dbname gpc1 -simple\n";
chomp(my $bad_exp = `regtool -checkstatus -date $date -class_id ota33 -dbname gpc1 -simple | grep 'stop run' | head -1`);
my @row = split /\s+/, $bad_exp;
if ($#row == 18) {
    print "stuck on reg: $row[0]\n";
}
elsif ($#row == -1) {
    print "\nEverything looks good.\n";
    exit(0);
}
else {
    print "     exp_name registered Nimfile summit_fault download_state newExp_state exp_id exp_type obs_mode burntool_state summit_class_id is_downloaded is_registered\n";
}
print "CheckStatus: $bad_exp\n";
$bad_exp = (split /\s+/, $bad_exp)[0];
print "Exposure: $bad_exp is the current pending exposure.\n";

