#!/bin/perl

# This program searches the KnotDB database for knot types without a mugshot
# then generates corresponding povray views of them in the /mugshots directory.

use strict;
use warnings;
use File::Slurp;
use File::Path;
use Data::Dumper;
use File::Copy;

# We start by loading the database.

my $DBFILE = read_file('KnotDB.pl');
my %knots;
eval($DBFILE);

my %wknots = %knots;

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

# first, a pass to remove files from the hash

my $key;

foreach $key ( sort(keys %wknots) ) {

    if ($key =~ /.vect/) {

	delete $wknots{$key};

    } 

}

foreach $key (keys %wknots) { 

 if ($key =~ m/^\d{1,2}_\d{1,3}$/) {
      
      print "$key \n";

      delete $wknots{$key};

    }

}

$numkeys = keys ( %wknots );
print "Found $numkeys link types in the database.\n";
print "Renaming best example of each and copying to ./links.\n";

my $newname;

foreach $key ( keys %wknots ) {

    $newname = "./links/".$wknots{$key}{filename};
    copy("./vects/".$wknots{$key}{filename},$newname) or die("File can't be copied to ".$newname);

}

print "Refreshed ./links directory.\n";

die("done");
