#!/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 ;
$jobname =`echo $string | awk -F\",\" '{print \$3}'`;
$jobname =~s/\s//g ;

$tmp_file = "/tmp/${project}${jobnet}${jobname}.tmp";

`cat $home/project/$project/$jobnet/$jobname | sed -e \"s/=/:/g\" | sed -e \"s/\\"//g\" > $tmp_file`;
$execute_command=`cat $tmp_file | grep EXECUTE_COMMAND: | sed -e \"s/EXECUTE_COMMAND://g\"`;
$recovery_command=`cat $tmp_file | grep RECOVERY_COMMAND: | sed -e \"s/RECOVERY_COMMAND://g\"`;
$exclusive_resource=`cat $tmp_file | grep EXCLUSIVE_RESOURCE: | sed -e \"s/EXCLUSIVE_RESOURCE://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 "<h3>${project}::${jobnet}</h3>";
print "<form method=\"post\" action=\"./set_job.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_Name</label>";
print "  <input type=\"text\" name=\"jobname\" value=\"$jobname\" placeholder=\"\joname\" pattern=\"[a-zA-Z0-9\. -_]+\" required>";
print "  <label>Execute_Comand</label>";
print "  <input type=\"text\" name=\"execute_command\" value=\"$execute_command\" placeholder=\"\${JOBNET_HOME}/bat/execute.sh\" pattern=\"[a-zA-Z0-9\. -_\${}\;|]+\" required>";
print "  <label>Recovery_Command</label>";
print "  <input type=\"text\" name=\"recovery_command\" value=\"$recovery_command\" placeholder=\"\${JOBNET_HOME}/bat/recovery.sh\" pattern=\"[a-zA-Z0-9\. -_\${}\;]+\" >";
print "  <label>Exclusive_resource</label>";
print "  <input type=\"text\" name=\"exclusive_resource\" value=\"$exclusive_resource\" placeholder=\"set lock resouce name if execute job exclusively\"  pattern=\"[a-zA-Z0-9\.-_]+\" >";
print "  <input type=\"submit\" class=\"small button\" value=\"Set Job\">";
print "  </div>";
print "</form>";
print "<div class=\"large-4 columns\">";
print " &nbsp;&nbsp;&nbsp;<a href=\"./set_jobnet_form.cgi?${project},${jobnet}\">Back to jobnet</a>";
print "<br></br>";
print " &nbsp;&nbsp;&nbsp;<a href=\"./delete_job.cgi?${project},${jobnet},${jobname}\">Delete This Job</a>";
print "  </div>";

## JOB LIST 
print "<div class=\"large-12 columns\">";
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 "</div>";
system("cat ../elements/footer");
system("rm $tmp_file");
