#!perl
#
#

use strict;
use warnings;
use File::Slurp;
use File::Copy;
use File::Temp;
use File::Path;
use Getopt::Long;
use Cwd;

print "assembleknots 1.0\n";

my ($knotfile,$knots);
my @knotlist;
my @rawknotlist;
my $cwd;
my $tag;
my $pattern;

$cwd = getcwd();

GetOptions ( 'infile=s' => \$knotfile, 'knots|Knots=s' => \$knots, 'tag|Tag=s' => \$tag , 'pattern=s' => \$pattern);

if ($knotfile) {
  open( FH, $knotfile) or die("Couldn't open knotfile $knotfile.\n");
  @knotlist = <FH>;
  foreach (@knotlist) { chomp($_); }

} elsif ($knots) {
  $knots =~ s/\s//g;
  @knotlist = split( /,/ , $knots);

} elsif ($pattern) {

    my @tars;

    @tars = <${cwd}/tars/*.tar.gz>; 

    foreach (@tars) {

	if ($_ =~ m/$pattern/) {

	    push @knotlist, $_;

	}

    }
    
    print "Pattern $pattern matches ".scalar @knotlist." files in ".scalar @tars." files in${cwd}/tars. Cleaning to knot types.\n";
    foreach (@knotlist) {

	$_ =~ s/${cwd}\/tars\///g;
	$_ =~ s/kl_//g;
	$_ =~ s/.tar.gz//g;
	
    }
    
} else {
  print "Usage: assembleknots --infile=<knotfiles> or assembleknots --knots=<knots> --tag=<tag> --pattern=<pattern> \n";
  print "       We expect the knotfiles file or <knots> list to contain a list of comma-separated knot types or pattern to contain a regex to match against tar files in the ./tars subdirectory\n";
  die();
}

unless (defined $tag) {
  print "Usage: assembleknots --infile=<knotfiles> or assembleknots --knots=<knots> --tag=<tag>\n";
  print "       We expect the knotfiles file or <knots> list to contain a list of comma-separated knot types.\n";
  die();
}

print "Assembling list of ".scalar @knotlist." knot types.\n";
print "Opening KnotDB.pl\n";

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

my $thisknot;
my @filelist;

if (-d "${cwd}/data/assembled") { rmtree("${cwd}/data/assembled") or die("Couldn't remove ${cwd}/data/assembled"); }
mkpath("${cwd}/data/assembled") or die("Couldn't make ${cwd}/data/assembled");

foreach $thisknot ( @knotlist ) {

    if ($thisknot !~ /vect/) { # this is a knot type and not a filename.

	unless(defined $knots{$thisknot}) { die("Can't find $thisknot in the KnotDB\n"); }
	unless(defined $knots{$thisknot}{filename}) { die("$thisknot is a knot type but has no winning filename.\n"); }

	push(@filelist, $knots{$thisknot}{filename});
	
	my $tarfile;                           
	my $sourcename;
	
	chdir("${cwd}/tars") or die("Couldn't chdir to ${cwd}/tars");
	
	$tarfile = "kl_${thisknot}.tar.gz";
	print "tar -xzf ${cwd}/tars/$tarfile \n";
	print `tar -xzf ${cwd}/tars/$tarfile`;
	$sourcename = "${cwd}/tars/kl_${thisknot}/$knots{$thisknot}{filename}";
	
	chdir("${cwd}/data/") or die("Couldn't chdir back to ${cwd}/data");
	
	print "$thisknot -> $knots{$thisknot}{filename} -> ${cwd}/data/assembled/kl_".$thisknot."_${tag}.vect\n";
	copy($sourcename,"./assembled/kl_".$thisknot."_${tag}.vect") or die("Couldn't copy!\n");
	
	chdir("${cwd}") or die("Couldn't chdir back to ${cwd}\n");
	
	rmtree("${cwd}/tars/kl_${thisknot}/") or die("Couldn't rmtree ${cwd}/tars/kl_${thisknot}/ \n");

    } elsif (defined $knots{$thisknot}) { # this is a file and not a knottype

      my $tarfile;                           
      my $sourcename;
      my $ktype;

      $thisknot =~ /kl_(.*)_(!(m|mr|r)).vect/;
      $ktype = $1;
  
      unless (defined $ktype) { die("Couldn't knot type $ktype extracted from filename $thisknot.\n"); }

      $tarfile = "kl_${ktype}.tar.gz";
      print "tar -zf ${cwd}/tars/$tarfile --get $knots{$thisknot}\n";
      print `tar -zf ${cwd}/tars/$tarfile --get $knots{$thisknot}`;
      $sourcename = "${cwd}/tars/kl_${ktype}/$knots{$thisknot}";

      print "$thisknot -> $knots{$thisknot} -> ./assembled/${thisknot}\n";
      copy($sourcename,"${cwd}/data/assembled/${thisknot}") or die("Couldn't copy!\n");

      rmtree("${cwd}/tars/kl_${thisknot}/") or die("Couldn't rmtree ${cwd}/tars/kl_${thisknot}/ \n");

  } else {
      
      print "$thisknot -> NOT REPRESENTED IN DB.\n";
     
  }

}

print "Now creating assembled.tgz.\n";
chdir("${cwd}/data") or die("Couldn't chdir to ${cwd}/data");
print `tar -cvzf assembled.tgz ./assembled/`;
chdir("${cwd}") or die("Couldn't chdir back to ${cwd}");
    
  
