#!/bin/perl

# This program searches the KnotDB database for knot types without alternating/nonalternating data.

use strict;
use warnings;
use File::Slurp;
use File::Path;
use Data::Dumper;
use Getopt::Long;
use Cwd;

my $cwd;
$cwd = getcwd;

require "${cwd}/bin/tightknot_common.pl";

print STDERR "addAlternating.pl: Adds alternating/nonalternating data to the KnotDB.pl database.\n";

my $force = '';

GetOptions ( 'force' => \$force );

# We start by loading the database.

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

my %wknots = %knots;

my $numkeys = keys ( %wknots );
print "We have $numkeys entries in the original database. Looking for files without alternating...\n";

my $key;

foreach $key (keys %wknots) {

  if ($key =~ /vect/) {

    delete $wknots{$key};

  }

}

unless ($force) {

  # first, a pass to remove files with alternating from the hash
 
  foreach $key ( sort(keys %wknots) ) {
    
    if (defined($wknots{$key}{alternating})) {
      
      delete $wknots{$key};
      
    }
    
  }
  
  $numkeys = keys ( %wknots );
  print "There seem to be $numkeys knot types in the database without alternating data.\n";
  print "Generating data now.\n";

} else {

  print "Force mode means we will recompute alternating for all ".keys ( %wknots )." files.\n";

}
  
my $knot_type;

foreach $key ( sort {$knots{$a}{sortorder} <=> $knots{$b}{sortorder} } keys %wknots ) {
  
  if (!defined($knots{$key}{summands})) {
    
    print "$key -> ";

    if ($knots{$key}{comp} == 1) {
      
      if ($knots{$key}{cr} < 8) {
	
	$knots{$key}{alternating} = 1;
	
      } elsif ($knots{$key}{cr} == 8) {
	
	$knots{$key}{alternating} = 
	  !($knots{$key}{index} >= 19 && $knots{$key}{index} <= 21);
	
      } elsif ($knots{$key}{cr} == 9) {
	
	$knots{$key}{alternating} = 
	  !($knots{$key}{index} >= 42 && $knots{$key}{index} <= 49);
	
      } elsif ($knots{$key}{cr} == 10) {
	
	$knots{$key}{alternating} = 
	  !($knots{$key}{index} >= 124 && $knots{$key}{index} <= 165);
	
      }
      
    } elsif ($knots{$key}{comp} == 2) {
      
      if ($knots{$key}{cr} < 7) {
	
	$knots{$key}{alternating} = 1;
	
      } elsif ($knots{$key}{cr} == 7) {
	
	$knots{$key}{alternating} = 
	  !($knots{$key}{index} >= 7 && $knots{$key}{index} <= 8);
	
      } elsif ($knots{$key}{cr} == 8) {
	
	$knots{$key}{alternating} = 
	  !($knots{$key}{index} >= 15 && $knots{$key}{index} <= 16);
	
      } elsif ($knots{$key}{cr} == 9) {
	
	$knots{$key}{alternating} = 
	  !($knots{$key}{index} >= 43 && $knots{$key}{index} <= 61);
	
      }
      
    } elsif ($knots{$key}{comp} == 3) {
      
      if ($knots{$key}{cr} == 6) {
	
	$knots{$key}{alternating} =  
	  !($knots{$key}{index} >= 3 && $knots{$key}{index} <= 3);;
	
      } elsif ($knots{$key}{cr} == 7) {
	
	$knots{$key}{alternating} = 1;
	
      } elsif ($knots{$key}{cr} == 8) {
	
	$knots{$key}{alternating} = 
	  !($knots{$key}{index} >= 7 && $knots{$key}{index} <= 10);
	
      } elsif ($knots{$key}{cr} == 9) {
	
	$knots{$key}{alternating} = 
	  !($knots{$key}{index} >= 13 && $knots{$key}{index} <= 21);
	
      }
      
    } elsif ($knots{$key}{comp} == 4) {
      
      if ($knots{$key}{cr} == 8) {
	
	$knots{$key}{alternating} = 
	  !($knots{$key}{index} >= 2);
	
      } elsif ($knots{$key}{cr} == 9) {
	
	$knots{$key}{alternating} = 
	  !($knots{$key}{index} >= 2);
	
      }
      
    }

    if ($knots{$key}{alternating}) {
      
      print "alternating.\n";
      
    } else {
      
      print "NONalternating.\n";
      
    }
    
  } else {
    
    $knots{$key}{alternating} = undef;
    
  }

}

print "Writing updated database...\n";
rename "${cwd}/knotdb/KnotDB.pl","${cwd}/knotdb/KnotDB.bak.pl" or die("Couldn't rename the old knot database.\n");

open(KNOTDB,">${cwd}/knotdb/KnotDB.pl") or die "Unable to create Knot database.";
print KNOTDB Data::Dumper->Dump([\%knots],['*knots']),"\n";
close KNOTDB;

