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

$home="/usr/local/e-cron";
$string = $ENV{'QUERY_STRING'};
$string =~s/\///g ;
$project =`echo $string | awk -F\",\" '{print \$1}'`;
$project =~s/\s//g ;

@jobnet_list=();
@jobnet_list=`ls /usr/local/e-cron/project/$project`;

print "Content-type: text/html\n\n";
###############contents insert#########
system("cat ../elements/header");
print "<h3>${project}</h3>";

unless(@jobnet_list){
  print "<a href=\"./jobnet_list.cgi?${project}\">Back to Jobnet List</a></p>";
  print "<h6>This project did not distributed to any server</h6>";
  print "<form method=\"post\" action=\"./delete_project.cgi\" class=\"custom\">";
  print "  <input type=\"hidden\" name=\"project\" value=\"$project\">";
  print "<input type=\"submit\" class=\"button\" value=\"Delete This Project\">";
  print "</form>";
  system("cat ../elements/footer");
  exit 1
}  

print "<form method=\"post\" action=\"./delete_project.cgi\" class=\"custom\">";
print "<input type=\"hidden\" name=\"project\" value=\"$project\">";
print "<div class=\"large-8 columns\">";

foreach $jobnet ( @jobnet_list ) {
  $jobnet =~ s/\n//;
  @server_list=();
  @server_list=`sudo -u ejobmgr ${home}/bin/distributed_servers $project/$jobnet| sed -e \"s/        version:/ /g\" | sed -e \"s/ /,/g\"`;

  print "<h4>$jobnet</h4>";
  print "<table>";
  print "  <thead>";
  print "    <tr>";
  print "      <th>Server_Name</th>";
  print "      <th>Status</th>";
  print "      <th>Job and Batch file version</th>";
  print "    </tr>";
  print "  </thead>";
  print "  <tbody>";

    while (<@server_list>){
      @distributed_server=();
      @distributed_server=split(/,/, $_);
      print "    <tr>";
      print "       <td>$distributed_server[0]</td>";
      print "       <td>$distributed_server[1]</td>";
      print "       <td>$distributed_server[2]</td>";
      print "    </tr>"; 
    }
  print "  </tbody>";
  print "</table>";
  print "<br>";
}
print "<input type=\"submit\" class=\"button\" value=\"Delete This Project\">";
print "</form>";
print "</div>";
print "<div class=\"large-4 columns\">";
print "<a href=\"./jobnet_list.cgi?${project}\">Back to Jobnet List</a></p>";
print "</div>";
system("cat ../elements/footer");
