#!/usr/bin/perl
use CGI qw(param);

$word = param("word");
$home = "/usr/local/e-cron";

print "Content-type: text/html\n\n";
###############contents insert#########
system("cat ../elements/header");
@STATUS_LIST = ();
@STATUS_LIST=`/usr/local/e-cron/bin/jobstat | egrep "$word" | grep -v addons  | grep -v Notice: | grep -v \"^If you\" | grep : `;

$secondary_check=`grep MASTER_SERVER ${home}/global.conf`;

if ($secondary_check and ! -e "${home}/utilities/.master_dead.flg"){
  print "<h4>This is secondary server</h4><h4>Please access master server</h4>";
  print "</div>";
  system("cat ../elements/footer");
  exit 0
}

unless(@STATUS_LIST){

 unless($word){
    print "<h4>No jobnet is running now</h4><h4>Please define job from Projects menu</h4>";
    print "</div>";
    system("cat ../elements/footer");
    exit 0
  }else{
    print "<h4>No search result</h4>";
    print "<a href=\"./jobstat.cgi\">back to jobnet status</a><h6>";
    print "</div>";
    system("cat ../elements/footer");
    exit 0
  }
}

system("cat ../elements/search_jobstat");
print "<h6>status will be updated 1-5min, please refresh this page</h6>";
print "<table  style=\"border-style:none\">";
print "  <thead>";
print "    <tr>";
print "      <th>Date</th>";
print "      <th>Time</th>";
print "      <th>Project</th>";
print "      <th>Jobnet</th>";
print "      <th>Status</th>";
print "      <th>Status Detail</th>";
print "      <th>Execution Server</th>";
print "      <th>Log</th>";
print "    </tr>";
print "  </thead>";
print "  <tbody>";

foreach $status (@STATUS_LIST){
 $status =~ s/  / /g;
 @status = split(/ /, $status);
 print "    <tr>";
 print "<td>$status[0]</td>";
 print "<td>$status[1]</td>";
 print "<td>$status[2]</td>";
 print "<td><a href=\"./set_jobnet_form.cgi?$status[2],$status[3]\">$status[3]</a></td>";
 
 if ( $status[4] eq SUCCESS ){
   print "<td><span class=\"success label\">$status[4]</span></td>";
 }elsif ( $status[4] eq ERROR ){
   print "<td><span class=\"alert label\">$status[4]</span></td>";
 }elsif ( $status[4] eq WARN ){
   print "<td><span class=\"secondary label\">$status[4]</span></td>";
 }elsif ( $status[4] eq INFO ){
   print "<td><span class=\"label\">$status[4]</span></td>";
 }else{
   print "<td>$status[4]</td>"; 
 }
 print "<td>$status[5]</td>";
 print "<td>$status[6]</td>";
 print "<td><a href=\"./ls_log.cgi?$status[2],$status[3],$status[6]\">logpage</a></td>";
 print "    </tr>";
} 
print "</tbody>";
print "</table>";
print "</div>";
system("cat ../elements/footer");
