#!/usr/bin/perl

my ($IDfile, $outfile) = @ARGV;

if (!$IDfile) {
  print "Usage: GetStatus.pl <jobIDfile>\n";
  exit 1;
}

if (!$outfile) {
  $outfile = "$IDfile.status";
}

system("edg-job-status -i $IDfile -o $outfile --noint");

my $Total = `less $outfile | grep  'Current Status' | wc -l`;
my $Submitted = `less $outfile | grep  'Submitted' | wc -l`;
my $Waiting = `less $outfile | grep  'Waiting' | wc -l`;
my $Ready = `less $outfile | grep  'Ready' | wc -l`;
my $Scheduled = `less $outfile | grep  'Scheduled' | wc -l`;
my $Running = `less $outfile | grep  'Running' | wc -l`;
my $Donesuccess = `less $outfile | grep  -A1 'Done (Success)' | grep "Exit code" |grep 0 | wc -l`;
my $Donesuccessnonzero = `less $outfile | grep  -A1 'Done (Success)' | grep "Exit code" | grep -v 0 | wc -l`;
my $Donefailure = `less $outfile | grep  'Done (Failure)' | wc -l`;
my $Aborted = `less $outfile | grep  'Aborted' | wc -l`;
my $Cancelled = `less $outfile | grep  'Cancelled' | wc -l`;
my $Cleared = `less $outfile | grep  'Cleared' | wc -l`;

chomp($Total);
chomp($Submitted);
chomp($Waiting);
chomp($Ready);
chomp($Scheduled);
chomp($Running);
chomp($Donesuccess);
chomp($Donesuccessnonzero);
chomp($Donefailure);
chomp($Aborted);
chomp($Cancelled);
chomp($Cleared);

print "Out of $Total files:\n";
print "$Submitted Submitted\n";
print "$Waiting Waiting\n";
print "$Ready Ready\n";
print "$Scheduled Scheduled\n";
print "$Running Running\n";
print "$Donesuccess Done(Success)\n";
print "$Donesuccessnonzero Done(Success) but non-zero exit code\n";
print "$Donefailure Done(Failure)\n";
print "$Aborted Aborted\n";
print "$Cancelled Cancelled\n";
print "$Cleared Cleared\n";

