#!perl
#
#
# topknot: generates an arxiv of the shortest n knots and links


use strict;
use warnings;
use Lingua::EN::Numbers qw(num2en num2en_ordinal);
use File::Slurp;
use File::Copy;
use File::Temp;
use File::Path;
use Data::Dumper;
use Getopt::Long;
use Cwd;

require './bin/tightknot_common.pl';

my $cwd;
$cwd = getcwd;

my $nKnots = 150;

print STDERR "topknot 1.0: Generates tables of shortest knots from KnotDB.pl.\n";

my $include_composites= '';

#open(DB,'KnotDB.pl');
my $DBFILE = read_file("${cwd}/knotdb/KnotDB.pl") or die("Couldn't open ${cwd}/knotdb/KnotDB.pl");
my %knots;
eval($DBFILE);

#close(DB);

my $numkeys = keys ( %knots );
print "We have $numkeys entries in the original database. Looking for knot types.\n";

# first, a pass to remove files from the hash

my $key;
my $nconsidered;
my $ndeleted;

$nconsidered = 0;

foreach $key ( keys %knots ) {

    $nconsidered++;

    if ($key =~ /vect/) {

	delete $knots{$key};
	#print "Deleted $key.\n";
	if (defined $knots{$key}) { die("I tried to delete $key, but it has value $knots{$key}.\n"); }
	$ndeleted++;

    }

  }

# now we sort by ropelength 

my @knotlist = sort{ $knots{$a}{upperbound} <=> $knots{$b}{upperbound} } keys( %knots );

open(CLR,">${cwd}/data/topknot_report.txt") || die("Couldn't open ${cwd}/data/topknot_report.txt\n");

my $n;
my ($k,$ub,$rso,$res);

my @addtoTar;

foreach $n (0..$nKnots) {

  $k = $knotlist[$n];
  $ub = $knots{$k}{upperbound};
  $rso = $knots{$k}{edges}/$knots{$k}{upperbound};
  $res = $knots{$k}{residual};

  push(@addtoTar,$k);

  write CLR;

}

close(CLR);

print "Now recreating ./topknots directory of $nKnots knot types with lowest ropelength.\n";

my $tempdir;
$tempdir = File::Temp->newdir();
mkdir("$tempdir/topknots") or die("Couldn't make $tempdir/topknots");
chdir("${cwd}/tars") or die("Couldn't chdir to tars.\n");

my $sourcename;
my $targetname;


foreach $key (@addtoTar) {
    
    my $tarfile;
    $tarfile = "kl_${key}.tar.gz";
#    print "tar -xzf ${cwd}/tars/$tarfile \n";
    print `tar -xzf ${cwd}/tars/$tarfile`;
    $sourcename = "${cwd}/tars/kl_${key}/$knots{$key}{filename}";
    $targetname = "$tempdir/topknots/kl_".$key."_best.vect";

    #print "$sourcename -> $targetname ...";
    copy($sourcename,$targetname) or die("failed to copy $sourcename -> $targetname\n");
    #print "ok\n";

    if ($sourcename =~ /temp/) { unlink($sourcename) or die("Couldn't unlink $sourcename.\n"); }
    rmtree("${cwd}/tars/kl_${key}/") or die("Couldn't rmtree ${cwd}/tars/kl_${key}/ \n");

}

chdir($tempdir) or die("Couldn't chdir to $tempdir\n");
`tar -czf topknots.tar.gz topknots`;
chdir($cwd) or die("Couldn't change back to $cwd");
move("$tempdir/topknots.tar.gz","${cwd}/data/topknots.tar.gz") or die("Couldn't move topknots.tar.gz back to ${cwd}/data/");

print "Tar'd results to ${cwd}/data/topknots.tar.gz\n";

format CLR_TOP = 

     Links by Ropelength (summary table)

     Link Type           Ropelength       Resolution      Residual
     ------------------- ---------------  --------------  --------------
.

format CLR =
     @<<<<<<<<<<<<<<<<<< @####.#########  @####.########  @####.########
     $k,                 $ub,             $rso,           $res
.



