use strict;
use lib '/cdf/scratch/cdfopr/danielw/CommonLibs';

#my $status_dir = "/cdf/www/offline/calibrations/Status";
my $status_dir = "/cdf/prdwww/cdfprd_cal/Status";



sub getRunCalibPending()
{
    my ($run) = @_;
    return getRunStatus($run,"Calib","Pending");
}

sub checkSubmittedRungroup()
{
    my ($rung) = @_;
    my @submittedraw = `cd $status_dir/Valid_Submitted/$rung/; ls -d *`;  
    my @submitted;
    my $f;
    my $found=0;
    for $f (@submittedraw)
    {
	chop($f);
	$submitted[$found++] = $f;
    }

    return @submitted;

}


# list of defined rungroups which have not yet had their validation samples submitted
sub getReadyRungroups()
{
    my $stage = "Calib";
    my $status = "Pending";

    my @readyr;
    my $found=0;
    my @runslist = `cd  $status_dir/${stage}_${status}; ls -d *`;


	
    my $r;
#    printf("Rungroups are: @runslist\n");
    foreach $r (@runslist)
    {
	chop($r);
#	printf("$r is ready to be submitted [ may already have been ]\n");
	$readyr[$found++] = $r;
	
    }
    
    return @readyr;	
}


# calib pending = y
# valid submitted = y
# valid finished = n
sub getToSubmitRungroups()
{
    my $stage = "Calib";
    my $status = "Pending";

    my @readyr;
    my $found=0;
    my @runslist = `cd  $status_dir/${stage}_${status}; ls -d *`;

    my $submitted = `cd $status_dir/Valid_Submitted/; ls -d *`;
    
    my $r;
    printf("Rungroups are: @runslist\n");
    foreach $r (@runslist)
    {
	chop($r);
	my $s = $submitted =~ /$r/g;
	printf("This rungroup $r [submitted? $s]\n");
	if (!$s)
	{
	    printf("$r unsubmitted\n");
	    $readyr[$found++] = $r;
	}
	else
	{
	    printf("$r already submitted\n");
	}
    }
    
    return @readyr;	
}

sub getSubmittedRungroups()
{
    my $stage = "Calib";
    my $status = "Pending";

    my @readyr;
    my $found=0;
    my @runslist = `cd  $status_dir/${stage}_${status}; ls -d *`;

    my $submitted = `cd $status_dir/Valid_Submitted/; ls -d *`;
    my $validated = `cd $status_dir/Valid_Plots/; ls -d *`;
    
    my $r;
    printf("Rungroups are: @runslist\n");
    foreach $r (@runslist)
    {
	chop($r);
	my $s = $submitted =~ /$r/g;
	my $v = 0; #$validated =~ /$r/g;
	printf("This rungroup $r [submitted? $s validated? $v]\n");
	if ($s && !$v)
	{
	    printf("$r submitted and unvalidated\n");
	    $readyr[$found++] = $r;
	}
	else
	{
	    printf("$r needs to be submitted or already validated\n");
	}
    }
    
    return @readyr;	
}

sub getRuns()
{
    my ($rung) = @_;

	
    # look in first place (run <-> rungroup assoc is made)
    my $stage = "Calib";
    my $status = "Pending";

    my @runslist = `cat  $status_dir/${stage}_${status}/$rung/runs.list`;

    my @retlist;
    my $r;
    my $rn=0;
    for $r (@runslist)
    {
	chop($r);
	$retlist[$rn++] = $r;
    }
    return @retlist;
}

sub getRunGroup()
{
    my ($run) = @_;

    # look in first place (run <-> rungroup assoc is made)
    my $stage = "Calib";
    my $status = "Pending";

    my @runslist = `cd  $status_dir/${stage}_${status}; ls -d *`;

    my $r;
    foreach $r (@runslist)
    {

	chop ($r);
#	printf("<!-- Looking for run $run in group $r --!>\n");
	my @runlist = `cat $status_dir/${stage}_${status}/$r/runs.list`;
	my $rc;
	foreach $rc (@runlist)
	{
	    chop($rc);
	    if ($rc == $run) { return $r; }
	}
    }
    return 0;
}

sub getRunStatus()
{
    my ($run,$stage,$status) = @_;
    my @runlist = `cat $status_dir/${stage}_${status}/*/*/runs.list`;
    my $r;
    foreach $r (@runlist)
    {
	chop($r);
	if ($r == $run) { return 1; }
    }
    return 0;
}


sub getRejectedRuns()
{
    my ($stage) = @_;
    return &getStatusRuns($stage,"Rejected");
}

sub getRejectedRunsProc()
{
    my ($stage,$proc) = @_;
    return &getStatusRuns($stage,$proc,"Rejected");
}

sub getPendingRuns()
{
    my ($stage) = @_;
    return &getStatusRuns($stage,"Pending");
}

sub getPendingRunsProc()
{
    my ($stage,$proc) = @_;
    return &getStatusRunsProc($stage,$proc,"Pending");
}

sub getSubmittedRuns()
{
    my ($stage) = @_;
    return &getStatusRuns($stage,"Submitted");
}


sub getStatusRungroupProc()
{

    my ($stage,$status,$rungroup,$proc) = @_;
#    printf("Getting run status $status stage $stage\n");
    my $res = `cat $status_dir/${stage}_${status}/$rungroup/$proc/status.txt`;
    return $res;
}


sub getStatusRuns()
{

    my ($stage,$status) = @_;
    printf("Getting run status $status stage $stage\n");
    my @runlist = `cat $status_dir/${stage}_${status}/*/*/runs.list`;
    my $r;
    my %rlist;
    foreach $r (@runlist)
    {
	chop($r);
#	printf(" - run $r\n");
	$rlist{$r} = 1;
    }
    return %rlist;
}

sub getStatusRunsProc()
{

    my ($stage,$proc,$status) = @_;
    printf("Getting run status $status proc $proc stage $stage\n");
    my @runlist = `cat $status_dir/${stage}_${status}/*/${proc}/runs.list`;
    my $r;
    my %rlist;
    foreach $r (@runlist)
    {
	chop($r);
#	printf(" - run $r\n");
	$rlist{$r} = 1;
    }
    return %rlist;
}
