#!/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 =`echo $string | awk -F\",\" '{print \$2}'`;
$jobnet =~s/\s//g ;
$tmp_file="/tmp/${project}${jobnet}jobnetprof.tmp";

`cat $home/project/$project/$jobnet/jobnet.prof | sed -e \"s/=/:/g\" | sed -e \"s/\\"//g\" > $tmp_file`;
$schedule=`cat $tmp_file | grep SCHEDULE: | sed -e \"s/SCHEDULE://g\"`;
$job_order=`cat $tmp_file | grep JOB_ORDER: | sed -e \"s/JOB_ORDER://g\"`;
$mail_addr=`cat $tmp_file | grep MAIL_ADDR: | sed -e \"s/MAIL_ADDR://g\"`;
$input_message=`cat $tmp_file | grep INPUT_MESSAGE: | sed -e \"s/INPUT_MESSAGE://g\"`;
$output_message=`cat $tmp_file | grep OUTPUT_MESSAGE: | sed -e \"s/OUTPUT_MESSAGE://g\"`;
$execution_servers=`cat $tmp_file | grep EXECUTION_SERVERS: | sed -e \"s/EXECUTION_SERVERS://g\"`;
$spare_server=`cat $tmp_file | grep SPARE_SERVER: | sed -e \"s/SPARE_SERVER://g\"`;
$job_exec_user=`cat $tmp_file | grep JOB_EXEC_USER: | sed -e \"s/JOB_EXEC_USER://g\"`;


print "Content-type: text/html\n\n";
###############contents insert#########
system("cat ../elements/header");
print "<h4><a href=\"./set_batch_form.cgi?${project},${jobnet},new\">+Add Batch File</a></h4>";
print "<h4><a href=\"./set_job_form.cgi?${project},${jobnet}\">+Add Job</a></h4>";
print "<h3>${project}::${jobnet}</h3>";
print "<form method=\"post\" action=\"./set_jobnet.cgi\">";
print "  <input type=\"hidden\" name=\"project\" value=\"$project\">";
print "  <input type=\"hidden\" name=\"jobnet\" value=\"$jobnet\">";
print "      <div class=\"large-8 columns\">";
print "        <label>JOB_ORDER</label>";
print "        <input type=\"text\" name=\"job_order\" value=\"$job_order\" placeholder=\"job1 job2 job3 ...\"  pattern=\"[a-zA-Z0-9\. -_]+\" required>";
print "        <label>MAIL_ADDR</label>";
print "        <input type=\"text\" name=\"mail_addr\" value=\"$mail_addr\" placeholder=\"XX\@domain\"  pattern=\"[a-zA-Z0-9\.-_@]+\">";
print "        <label>INPUT_MESSAGE</label>";
print "        <input type=\"text\" name=\"input_message\" value=\"$input_message\" placeholder=\"set message if need\" pattern=\"[a-zA-Z0-9\.-_]+\">";
print "        <label>OUTPUT_MESSAGE</label>";
print "        <input type=\"text\" name=\"output_message\" value=\"$output_message\" placeholder=\"set message if need\" pattern=\"[a-zA-Z0-9\.-_]+\">";
print "        <label>SCHEDULE(same as cron entry)</label>";
print "        <input type=\"text\" name=\"schedule\" value=\"$schedule\" placeholder=\"minute hour day month week \" pattern=\"[0-9\*,- ]+\" required>";
print "        <label>EXECUTION_SERVERS</label>";
print "        <input type=\"text\" name=\"execution_servers\" value=\"$execution_servers\" placeholder=\"server1.hoge server2.hoge server3.hoge\" pattern=\"[a-zA-Z0-9\. -_]+\" required>";
print "        <label>SPARE_SERVER</label>";
print "        <input type=\"text\" name=\"spare_server\" value=\"$spare_server\" placeholder=\"set spare server name if need. spare server can execute job when execution server down\" pattern=\"[a-zA-Z0-9\. -_]+\">";
print "        <label>SSH_PORT</label>";
print "        <input type=\"text\" name=\"ssh_port\" value=\"\" placeholder=\"default port is 22. you have to install e-cron to execution server beforehand\" pattern=\"[0-9]+\">";
print "        <label>EXEC_USER</label>";
print "        <input type=\"text\" name=\"job_exec_user\" value=\"$job_exec_user\" placeholder=\"Default user is ejobmgr, If you want to use other user, please set sudo as well.\" pattern=\"[a-zA-Z0-9\.-_]+\">";
print "        <input type=\"submit\" class=\"small button\" value=\"Set Jobnet & Distribute to execution servers\">";
print "      </div>";
print "</form>";
print "<div class=\"large-4 columns\">";
print "&nbsp;&nbsp;&nbsp;<a href=\"./jobnet_list.cgi?${project}\">Back to Jobnet List</a>";
print "<br></br>";
print "&nbsp;&nbsp;&nbsp;<a href=\"./set_status_form.cgi?${project},${jobnet}\">Enable/Disable operation</a>";
print "<br></br>";
print "&nbsp;&nbsp;&nbsp;<a href=\"./execute_jobnet_form.cgi?${project},${jobnet}\">Execute Now</a>";
print "<br></br>";
print "&nbsp;&nbsp;&nbsp;<a href=\"./delete_jobnet_form.cgi?${project},${jobnet}\">Delete This Jobnet</a>";


## JOB LIST 
print "<div class=\"large-12 columns\">";
print "<br></br>";
print "<h3>#Jobnet infomation</h3>";
print "<table>";
print "  <thead>";
print "    <tr>";
print "      <th>Available Job list</th>";
print "    </tr>";
print "  </thead>";
print "  <tbody>";
@JOBS=`ls -F /usr/local/e-cron/project/$project/$jobnet | grep -v /\$ | grep -v controller.sh | grep -v jobnet.prof | grep -v _controller.sh | grep -v sample_job | sed -e \"s/\*//g\" `;
while (<@JOBS>){

  if($_){
  print "    <tr>";
  print "       <td><a href=\"./set_job_form.cgi?${project},${jobnet},$_\">$_</a></td>";
  print "       </td>";
  print "    </tr>";
 }

}
print "  </tbody>";
print "</table>";
print "<table>";
print "  <thead>";
print "    <tr>";
print "      <th>Available batch list</th>";
print "    </tr>";
print "  </thead>";
print "  <tbody>";
@BATCHS=`ls /usr/local/e-cron/project/$project/$jobnet/bat`;
while (<@BATCHS>){

  if($_){
  print "    <tr>";
  print "       <td><a href=\"./set_batch_form.cgi?${project},${jobnet},$_\">\${JOBNET_HOME}/bat/$_</a></td>";
  print "       </td>";
  print "    </tr>";
 }

}
print "  </tbody>";
print "</table>";

print "<table>";
print "  <thead>";
print "    <tr>";
print "      <th>Jobnet Distributed Server</th>";
print "      <th>Status</th>";
print "      <th>Job and Batch file version</th>";
print "    </tr>";
print "  </thead>";
print "  <tbody>";

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

while (<@server_list>){
 @distributed_server=();
 @distributed_server=split(/,/, $_);
  print "    <tr>";
  if ( $distributed_server[3] eq spare ){
    print "       <td><a href=\"./server_info.cgi?$distributed_server[0]\">$distributed_server[0] *spare</a></td>";
  }else{
    print "       <td><a href=\"./server_info.cgi?$distributed_server[0]\">$distributed_server[0]</a></td>";
  }
   print "       <td>$distributed_server[1]</td>";
   print "       <td>$distributed_server[2]</td>";
  print "    </tr>";
}

print "  </tbody>";
print "</table>";

print "</div>";
print "</div>";
system("cat ../elements/footer");
system("rm $tmp_file");
