#!/usr/bin/perl

$mode    = $ARGV[0];
open (IN,"/usr/local/etc/.domhub_name");
@domhubname=(<IN>);
close(IN);
foreach (@domhubname) {
    $line = $_;
    chop $line;
    $domhub = $line;
    ($junk,$string) = split("HUB",$line);
    if ($string < 10) { $string = "0"."$string" };
}  
printf("-------------------------------------------------------------------------------\n");
printf ("$domhub domhubApp SUMMARY:\n\n");
                        
      
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdat) = localtime(time());
$str = "$year"."$mon"."$mday"."$hour"."$min"."$sec";

system("ps -efl > /tmp/processes.$str.txt");
  open (IN,"/tmp/processes.$str.txt");
  @processes = (<IN>);
  close (IN);
  system("rm /tmp/processes.$str.txt");


  $domservs = 0;
  $domhubapps = 0;

  foreach (@processes) {
#   print $_;
   if (/java icecube.daq.domhub.DOMHub/) {
     $domhubapps = $domhubapps + 1;
   }   
   if (/domserv/) {
      $domservs = $domservs + 1;
   }
#   print $_;
  }


print "\n";
$message = "";
#if ($domhubapps > 0)  { 
  $message = $message." $domhubapps domhubapps;";
#}

#if ($domservs > 0)  { 
$message = $message." $domservs domservs;";
#};


if ($message ne "") {
  print "software: $message \n\n";
} 

$parse = "";
if ( ($domhubapps > 0)) {
 open (IN,"/mnt/data/testdaq/domhubapp.log");
# @domhubapplog=(<IN>);
# close(IN);
  
 $lines = 0;
 $rmicalls = 0;
 $gettingDrivers = 0;
 $softbooting = 0;
 $discovery = 0;
 $getDOMStatusList = 0;
 $reservedDOMs = 0;
 $truncated = 0;
my $DLS_flag = 0; 
# foreach (@domhubapplog) { 
 while (<IN>) {
   $lines = $lines + 1;
   $line = $_;
   chop $line;
#   print "$line\n";

   if (/Waiting for RMI method calls/) { $rmicalls = 1; };   
   if (/Getting drivers for/) { $gettingDrivers = 1; };
   if (/About to softboot all non excluded DOMs/) { $softbooting = 1; };
   if (/Discovery: sleeping/) { $discovery = 1; };
   if (/Received call to getDOMStatusList/) { 
	if ($DSL_flag) {
		$getDOMStatusList = 1; 
	} else {
		$DLS_flag = 1;
	}
   }
   if (/Received call to reserveDOM for DOM/) { $reservedDOMs = 1; };

   if ($lines == 1000) { 
     $truncated = 1;
     last;
   }

 }
close(IN);

print "Waiting for RMI method calls            = $rmicalls \n";
print "Getting drivers                         = $gettingDrivers \n";
print "Softbooting all non-excluded DOMs       = $softbooting \n";
print "Discovery phase                         = $discovery \n";
print "DOMStatusList - waiting for other hubs  = $getDOMStatusList \n";
print "reserveDOM - hub should be taking data  = $reservedDOMs \n";
if (($reservedDOMs == 0) && ($truncated == 1)) {
print "Truncated file read - Oh Oh ...         = $truncated \n";
}
}

printf("-------------------------------------------------------------------------------\n");


