
#!/usr/bin/perl 

##for ($iii=0;$iii<=0;$iii++)
##{



$starter = 0;
print $starter."\n";
$ender = 1288;
print $ender."\n";

open(IF,'<12alternating.txt');
open(OF,'>rerun12output'.$starter.'.txt');
open(OF3,'>rerun12errorcatcher'.$starter.'.txt');
open(OF4,'>rerun12countcatcher'.$starter.'.txt');

$successcount = 0;
$errorcount = 0;
$cntr = 0;

while (<IF>)
 {

m/([\d]+)[\s]+([\d]+)[\s]+([\d]+)[\s]+([\d]+)[\s]+([\d]+)[\s]+([\d]+)[\s]+([\d]+)[\s]+([\d]+)[\s]+([\d]+)[\s]+([\d]+)[\s]+([\d]+)[\s]+([\d]+)[\s]+([\d]+)[\s]+([\d]+)/;
## print "here",$1,"\n";

$cntr = $2;
print $cntr."\n";
if ($cntr > $starter)
{
if ($cntr <= $ender)
{
print "good last run: ",$successcount,", errors to now: ",$errorcount,"\n";
print OF4 $cntr-1,",",$successcount,",",$errorcount,"\n";
$successcount = 0;

 print $_;
print OF "\n";

foreach my $i1 (0..1)
{
foreach my $i2 (0..1)
{
foreach my $i3 (0..1)
{
foreach my $i4 (0..1)
{
foreach my $i5 (0..1)
{
foreach my $i6 (0..1)
{
foreach my $i7 (0..1)
{
foreach my $i8 (0..1)
{
foreach my $i9 (0..1)
{
foreach my $i10 (0..1)
{
foreach my $i11 (0..1)
{
if ($i1 == 0) {$j1 = '';} else {$j1 = '-';}
if ($i2 == 0) {$j2 = '';} else {$j2 = '-';}
if ($i3 == 0) {$j3 = '';} else {$j3 = '-';}
if ($i4 == 0) {$j4 = '';} else {$j4 = '-';}
if ($i5 == 0) {$j5 = '';} else {$j5 = '-';}
if ($i6 == 0) {$j6 = '';} else {$j6 = '-';}
if ($i7 == 0) {$j7 = '';} else {$j7 = '-';}
if ($i8 == 0) {$j8 = '';} else {$j8 = '-';}
if ($i9 == 0) {$j9 = '';} else {$j9 = '-';}
if ($i10 == 0) {$j10 = '';} else {$j10 = '-';}
if ($i11 == 0) {$j11 = '';} else {$j11 = '-';}


$knotinfo = $1.' '.$2.' '.$3.' '.$j1.$4.' '.$j2.$5.' '.$j3.$6.' '.$j4.$7.' '.$j5.$8.' '.$j6.$9.' '.$j7.$10.' '.$j8.$11.' '.$j9.$12.' '.$j10.$13.' '.$j11.$14;
  open(OF2,'>hhh.txt');
  print OF2 $knotinfo,"\n";
## print $knotinfo,"\n";
  close(OF2);


## knotfind can hang! This code sets a timer of 10 seconds for knotfind to terminate.
## If it doesn't, the alarm causes an error to be printed, and a kill order for 'knotfind' to run
## The kill order is needed because otherwise the knotfind call will run forever;
## multiple such calls will seriously degrade performance!

eval {
    local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required
    alarm 10;
    system("\.\/knotfind hhh.txt kkk.txt");
    alarm 0;

  open(IF2,'<kkk.txt');
  $successcount++;
  while(<IF2>)
   {
    if (m/unknot/)
    {
     print OF "unknot   ";
    }
    else
    {
     print OF $_,"   ";
    }    print OF $knotinfo,"\n";

   }
  close(IF2);

};
if ($@) {
    die unless $@ eq "alarm\n";   # propagate unexpected errors
    # timed out
system("pkill -9 knotfind");
$errorcount++;
print OF3 "error: ",$knotinfo,"\n";
print "error: ",$knotinfo,"\n";
print OF "error:  ",$knotinfo,"\n";
print OF4 "error:  ",$knotinfo,"\n";
}
else {
    # didn't
}

  


}
}
}
}
}
}
}
}
}
}
}
}
}
 }

print "good last run: ",$successcount,",errors to now: ",$errorcount,"\n";
print OF4 $cntr-1,",",$successcount,",",$errorcount,"\n";



close(IF);
close(OF);

close(OF3);
close(OF4);

system("gzip rerun12output".$starter.".txt");

##}
