#!/usr/bin/perl -w

# List of CSW15 *racks* arrives via the usual channel
# Use by piping output into AnyMemo/txt2db.pl


#  533  cat WordLists/csw12-only.txt | bin/srack.pl | sort -u | grep -v ....  | bin/csw2anymemo.pl 
#  534  history | tail -3
#  535  history | tail -3 > /tmp/t.t

use strict;

use Getopt::Std;

use vars qw ( $opt_t );

if ( ! getopts( 't' ) )
{
  die "Usage:  csw2anymemo-defs.pl [-t]";
}


require "/home/gvc/Dropbox/XWords2015/bin/dawg.pl";

sub rack_order {
  if ( $a !~ /[a-zA-Z]/) {return 1;}
  if ( $b !~ /[a-zA-Z]/) {return -1;}
  return $a cmp $b;
}

sub srack {
  my ($rack) = @_;
  return (join "", sort rack_order split//,$rack);
}

#sub color {
#  my ($text) = @_;
#  return "<font color=\"#FF0000\">$text</font>";
#}

$ENV{XWORDS_ROOT} = "/home/gvc/Dropbox/XWords2015/";


my %CSW19;
my %CSW19_WORDS;
my %CSW15;
my %CSW15_WORDS;
my %CSW12;
my %CSW12_WORDS;
my %OWL;
my %OWL_WORDS;
my %DECK;

my @LETTERS = split "", "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

&dawg'open(*CSW2019, "$ENV{XWORDS_ROOT}/lib/words/csw2019.dwg");

sub color_based_on {
  my ($text, $basis) = @_;
  $opt_t && do {return $text;};
  # Assumes $basis is legal at least in CSW19
  if ( ! $OWL_WORDS{ $basis } )
    {
      if ( ! $CSW12_WORDS{ $basis } )
	{
        if ( ! $CSW15_WORDS{ $basis } )
          {
	    return "<font color=\"#0077DD\">$text</font>";
          }
        else
          {
	    return "<font color=\"#00CC00\">$text</font>";
	  }
        }
      else
	{
	  return "<font color=\"#FF0000\">$text</font>";
	}
    }
  else
    {
      return $text;
    }
}


if (! $opt_t) 
{
open OWL, "<$ENV{XWORDS_ROOT}/WordLists/owl2.txt" or die "Failed to open OWL2 wordlist";

while (<OWL>)
  {
     chomp;
     push @{$OWL{&srack( $_ )}}, $_;
     $OWL_WORDS{$_} = 1;
  }

close OWL;

open CSW12, "<$ENV{XWORDS_ROOT}/WordLists/csw12.txt" or die "Failed to open CSW wordlist";

while (<CSW12>)
  {
     chomp;
     push @{$CSW12{&srack( $_ )}}, $_;
     $CSW12_WORDS{$_} = 1;
  }

close CSW12;

# my %CSW12DEFS;
# 
# open CSW12DEFS, "<$ENV{XWORDS_ROOT}/WordLists/csw12-defs.txt" 
#    or die "Failed to open csw12-defs ($!)";
# while (<CSW12DEFS>)
#   {
#      chomp;
#      /(\S+)\s+(.*)/;
#      if (defined $1)
#        {
# 	 my $def = $2;
# 	 my $word = $1;
# 	 $def =~ s/\>/\&gt;/g;
# 	 $def =~ s/\</\&lt;/g;
# 	 $CSW12DEFS{$word} = $def;
#        }
#      else
#        {
# 	 $CSW12DEFS{$_} = "";
#        }
#   }
# 
# close CSW12DEFS;
# 
my %CSW15DEFS;

open CSW15DEFS, "<$ENV{XWORDS_ROOT}/WordLists/csw15-defs.txt" 
   or die "Failed to open csw15-defs ($!)";
while (<CSW15DEFS>)
  {
     chomp;
     /(\S+)\s+(.*)/;
     if (defined $1)
       {
	 my $def = $2;
	 my $word = $1;
	 $def =~ s/\>/\&gt;/g;
	 $def =~ s/\</\&lt;/g;
	 $CSW15DEFS{$word} = $def;
	 push @{$CSW15{&srack( $word )}}, $word;
	 $CSW15_WORDS{$word} = 1;
       }
     else
       {
	 $CSW15DEFS{$_} = "";
       }
  }

close CSW15DEFS;
} # ! $opt_t (text-only?)

my %CSW19DEFS;

open CSW19DEFS, "<$ENV{XWORDS_ROOT}/WordLists/csw19-defs.txt" 
   or die "Failed to open csw19-defs ($!)";
while (<CSW19DEFS>)
  {
     chomp;
     /(\S+)\s+(.*)/;
     if (defined $1)
       {
	 my $def = $2;
	 my $word = $1;
	 $def =~ s/\>/\&gt;/g;
	 $def =~ s/\</\&lt;/g;
	 $CSW19DEFS{$word} = $def;
	 push @{$CSW19{&srack( $word )}}, $word;
	 $CSW19_WORDS{$word} = 1;
       }
     else
       {
	 $CSW19DEFS{$_} = "";
       }
  }

close CSW19DEFS;

warn "Defs loaded...";

while (<>)
  {
    chomp;
    print "$_";
    if (!$opt_t) { print "|"; } else { print " "; }
    my %WORD_SEEN;
    foreach my $rack_word (&dawg'list2( *CSW2019, lc $_, 1 ))
      {
      $rack_word = join "", sort split//,uc $rack_word;
       if ($WORD_SEEN{$rack_word}) {next;}
      $WORD_SEEN{$rack_word} = 1;

      foreach my $plain_word (@{$CSW19{$rack_word}})
        {
  	my $word = &color_based_on( $plain_word, $plain_word );

  	my $fronts = "";
  	my $backs = "";
  	foreach my $l (@LETTERS)
  	  {
  	    my $hook = "";
  	    if ( defined $CSW19_WORDS{ $l . $plain_word } )
  	      {
  		$hook = &color_based_on( $l, $l . $plain_word );
  		$fronts .= $hook;
  	      }
  	    $hook = "";
  	    if ( defined $CSW19_WORDS{ $plain_word . $l} )
  	      {
  		$hook = &color_based_on( $l, $plain_word . $l );
  		$backs .= $hook;
  	      }
  	  }
  	my $front_inner = substr $plain_word, 1;
  	if ( ! $opt_t && defined $CSW19_WORDS{ $front_inner } )
  	  {
  	    $front_inner = &color_based_on( "-", $front_inner );
  	  }
  	else
  	  {
  	    $front_inner = "";
  	  }
  	my $back_inner = substr $plain_word, 0, -1;
  	if ( ! $opt_t && defined $CSW19_WORDS{ $back_inner } )
  	  {
  	    $back_inner = &color_based_on( "-", $back_inner );
  	  }
  	else
  	  {
  	    $back_inner = "";
  	  }
  	if ( $fronts )
  	  {
  	    print "$fronts/";
  	  }
  	print $front_inner;
  	print $word;
  	print $back_inner;
  	if ( $backs )
  	  {
  	    print "/$backs";
  	  }
	  if ($opt_t)
	  {
	    print " ";
	  }
	  else
	  {
            print " $CSW19DEFS{$plain_word}";
  	    print "<br>";
	  }
        }
    }
    print "\n";
  }




