#Generator jdl datoteke za distribuirano renderiranje video sekvenci

open(JDL, "> zadatak.jdl") or
  die("Failed creating JDL script: $!");

## Resolution settings ##
$Resx=1024;
$Resy=768;


#$footer = <<EOF;
#EOF



if ($ARGV[3] eq "animation") {

$header = <<EOH;
[
Type = "dag";
VirtualOrganisation = "seegrid";
InputSandbox = {"povr.tgz", "animacija.tgz", "include.tgz", "skripta.sh" };
Requirements = RegExp("irb.hr", other.GlueCEUniqueID);
nodes = [
EOH

print JDL $header;


$nFrames = $ARGV[0];
$nJobs = $ARGV[1];
$FileName = $ARGV[2];
$Div=$nFrames/$nJobs;
$Auxs=0;
$Counter=0;

foreach $Frame (1..$nJobs) {
$Auxe+=$Div;
$Counter+=1;
print JDL<<EOF;
  node$Frame = [
     description = [
     Executable = "skripta.sh";
     Arguments = "$nFrames $Auxs $Auxe $Counter $FileName";
     StdOutput = "node$Counter.out";
     StdError = "node$Counter.err";
     OutputSandbox = {"node$Counter.out","node$Counter.err","img$Counter.tgz"};
     ];
];
EOF
$Auxs+=$Div;
}

print JDL<<EOF;
];
EOF

print JDL<<EOF;
dependencies =  {
EOF
print JDL "{ {";
foreach $Job (1..$nJobs-1){
if ($Job>1){
print JDL ", ";
}
print JDL "node$Job";
}
print JDL " } , { node$nJobs } ";

print JDL "}\n";

print JDL <<EOF;
};
];
EOF


}
elsif ($ARGV[3] eq "single") {

$header = <<EOH;
[
Type = "dag";
VirtualOrganisation = "seegrid";
InputSandbox = {"povr.tgz", "animacija.tgz", "skripta_static.sh" };
Requirements = RegExp("irb.hr", other.GlueCEUniqueID);
nodes = [
EOH

print JDL $header;

$nX = $Resx/$ARGV[0];
$nY = $Resy/$ARGV[1];
$FileName = $ARGV[2];

foreach $Auxy (0..$ARGV[0]-1){
  foreach $Auxx (0..$ARGV[1]-1) {
  
$Arg1=$nX*$Auxx;
$Arg2=$nX*$Auxx+$nX;
$Arg3=$nY*$Auxy;
$Arg4=$nY*$Auxy+$nY;
$Arg5=$Auxy*$ARGV[1]+$Auxx;
  
print JDL<<EOF;
node$Arg5 = [
description = [
Executable = "skripta_static.sh";
Arguments = "$Arg1 $Arg2 $Arg3 $Arg4 $Arg5 $FileName";
StdOutput = "node$Arg5.out";
StdError = "node$Arg5.err";
OutputSandbox = {"node$Arg5.out","node$Arg5.err","img$Arg5.tgz"};
       ];
];
EOF
                          }
                        }              
print JDL<<EOF;
];
EOF
                             
                               
#dag segment za prikaz grafa ovisnosti

$Aux=$ARGV[0]*$ARGV[1]-1; 
print JDL<<EOF;
dependencies =  {
EOF
print JDL "{ {";
foreach $Job (0..$Aux-1){
if ($Job>0){
print JDL ", ";
}
print JDL "node$Job";
}
print JDL " } , { node$Aux } ";

print JDL "}\n";

print JDL <<EOF;
};
];
EOF

                          }

close JDL;
print "Initalization successful!"; 