
unless ( $Executable_Directory ) { exit }
unless ( -e $Executable_Directory ) { exit }
unless ( $Executable_File ) { exit }
unless ( -e $Executable_File ) { exit }
chdir $Executable_Directory;
$Run_File = "$Executable_File.run";
$Submit_File = "$Executable_File.cmd";

#-------------------------------------------------------------------------------
# linux submit file
#-------------------------------------------------------------------------------

$Submit_Command = " ";
unless ( -e $Submit_File ) {
# create submit file
 open OUT, ">$Submit_File";
 print OUT "#!/bin/sh\n";
 print OUT "$Run_File\n";
 close OUT;
}

#-------------------------------------------------------------------------------
# end of linux submit file
#-------------------------------------------------------------------------------

# resubmit if argument is "q" or stop if argument is "s"
if ( $ARGV[0] ) {
 system "chmod +x $Submit_File"; 
 if ( $ARGV[0] eq "q" ) { system "$Submit_Command $Submit_File" }
 if ( $ARGV[0] eq "s" ) { unlink $Run_File }
 exit;
}

unless ( $Output_File ) { exit }
unless ( $Input_File ) { exit }
unless ( -e $Input_File ) { exit }
use File::Copy;

undef @in;
open IN,"$Input_File";
while (<IN>) {
  chomp $_;
# read the runstep and runlen from the input file
 if ( /runstep/ ) { 
  $runstep = $_; $runstep =~ s/.*runstep//; $runstep =~ s/=//; $runstep =~ s/^\s+//; 
  $runstep =~ s/,.*//; $runstep =~ s/ .*//; $runstep =~ s/\/.*//;
 }
 if ( /runlen/ ) {
  $runlen = $_; $runlen=~ s/.*runlen//; $runlen =~ s/=//; $runlen =~ s/^\s+//; 
  $runlen =~ s/,.*//; $runlen =~ s/ .*//; $runlen =~ s/\/.*//;
 }
 push ( @in, "$_\n" );
}
close IN;

if ( $runlen > 0) {

# modify the control file to always write restarts

 if ( $runlen > $runstep ) {
  $a = "$Input_File.run";
  if ( ! -e $a ) { copy $Input_File, $a }
  open OUT, ">$Input_File";
  foreach (@in) {
   s/ restrt=\.false\./ restrt=\.true\./;
   s/,restrt=\.false\./,restrt=\.true\./;
   print OUT "$_";
  }
  close OUT;
 }
 
# if a step_start_file exists then run it
 if ( $Step_Start_File ) {
  if ( -e $Step_Start_File ) {
   system "chmod +x $Step_Start_File";
   system "$Step_Start_File";
  }
 }

# run the executable
 system "rm -f  $Output_File";
 system "$Executable_File > $Output_File 2>&1";

# if a step_end_file exists then run it
 if ( $Step_End_File ) {
  if ( -e $Step_Start_File ) {
   system "chmod +x $Step_End_File";
   system "$Step_End_File";
  }
 }

# add output_file to output_file.run
 undef @in;
 open IN,"$Output_File";
 while (<IN>) { push ( @in, "$_" ) };
 close IN;
 open OUT, ">>$Output_File.run";
 foreach (@in) { $a = print OUT "$_"; if ($a == 0) { exit } };
 close OUT;

 if ( $runlen - $runstep > 0 ) {
 # modify input file for next step
  undef @in;
  open IN,"$Input_File"; while (<IN>) { push ( @in, "$_" ) }; close IN;
 
  $a = "$Input_File.run";
  if ( ! -e $a ) { copy $Input_File, $a }
  open OUT, ">$Input_File";
  foreach (@in) {
   s/ init=\.true\./ init=\.false\./;
   s/,unit=\.true\./,init=\.false\./;
   $a = $runlen - $runstep;
   s/runlen=$runlen/runlen=$a/;
   print OUT "$_";
  }
  close OUT;
# resubmit the job
  system "$Submit_Command $Submit_File"
 }
 else {
# clean up files
  if ( -e "$Input_File.run" ) { system "mv $Input_File.run $Input_File" }
  if ( -e "$Output_File.run" ) { system "mv $Output_File.run $Output_File" }
  if ( -e "$Run_File" ) { unlink $Run_File }
 }
}
