#!/usr/bin/perl -w

use IO::Socket;
use strict;
use FileHandle;

use CGI;
my $q = new CGI;

#use CGI::Pretty;
#my $q = new CGI::Pretty;

$| = 1;

print $q->header(-type => 'text/html');
print <<END_OF_HTML;
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
  <head>
    <title>Boggle Solver, Wordracer Helper</title>
    <meta name="description" content="Boggle Helper, Boggle Word Builder, Wordracer Help">
    <meta name="keywords" content="boggle helper, boggle, bogle, wordracer, game helper, word builder, word finder">

    <link href="../css/style.css" rel="stylesheet" type="text/css">
  </head>

<body bgcolor="#ffffff" background="../images/col.gif" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<table width="100%" height="500px" border=0 cellpadding=0 cellspacing=0>
  <tr>
    <td width="79" height="130px"><img alt="Scrabble Helper" src="../images/test_01.gif" width=79 height=130 usemap="#Map_left" border="0"></td>
    <td width="245" height="130px"><img alt="Scrabble Helper" src="../images/test_02.gif" width=245 height=130 usemap="#Map_right" border="0"></td>
    <td width="426" background="../images/row.gif" height="130px">&nbsp;</td>
    <td background="../images/row.gif" bgcolor="#ffffff">&nbsp;</td>	
	</tr>
  <tr>
    <td valign="top" bgcolor="#7989ac"><img src="../images/test_04.gif" width=79 height=370></td>
    <td colspan=2 valign="top" bgcolor="#ffffff">&nbsp;</td>
    <td bgcolor="#ffffff">&nbsp;</td>	
  </tr>
</table>
<!-- menu -->
<div id="menu" style="position:absolute; left:319px; top:83px; width:456px; height:20; z-index:2"> 
  <span class="dots">&#8226;</span><span class="menu"><a href="http://www.wineverygame.com/scrabble-word-list.php" class="a2">Word Lists </a></span> 
  <span class="dots">&#8226;</span><span class="menu"><a href="http://www.wineverygame.com/" class="a2">Scrabble Solver </a></span> 
  <span class="dots">&#8226;</span><span class="menu"><a href="http://www.wineverygame.com/memory/index.php" class="a2">Memory Game </a></span> 
</div>
<!-- end of menu -->


<!-- banner -->

<div id="content" style="position:absolute; left:257px; top:110px; width:475px; z-index:19;">
<h1><a href="http://wineverygame.com/boggle/helper.pl">Boggle and Wordracer Solver</a></h1>
END_OF_HTML

#print $q->header(-type => 'text/html'),
#    $q->start_html(-bgcolor => '#ffffff', -title => 'Boggle Solver, Wordracer Helper');

my $op = $q->param('op') || "unspecified";
my $board;
my $i;
my %mode_labels;
my %format_labels;

#print $q->h1("Boggle and Wordracer Solver");

print <<END_OF_HTML;
END_OF_HTML


if ($op eq "solve") {

    my ($remote, $line);

    $remote = IO::Socket::INET->new(
				    Proto    => "tcp",
				    PeerAddr => "localhost",
				    PeerPort => "12345",
				    )
	or die "sorry - cannot connect to boggle server.  try again later.";

    $remote->autoflush(1);

    # collapse the entire board into one line
    $board = "";
    foreach $i (split /\W+/, $q->param('board')) {
	$board .= $i;
    }

    # set min length to 3 if the mode is a wordracer mode
    if ($q->param('mode') ne "normal" && $q->param('minlength') eq "Default") {
	$q->param('minlength' => 3);
    }

    my $message = "";
    $message .= sprintf "minlength=" . $q->param('minlength') . "\n";
    $message .= sprintf "mode=" . $q->param('mode') . "\n";
#    $message .= sprintf "format=" . $q->param('format') . "\n";
    $message .= sprintf "board=$board\n";
    $message .= sprintf "%%\n";

    my $date = `date`;
    chomp $date;
    open(BOGLOG, ">>/tmp/boglog");
    print BOGLOG "$date:\n$message\n\n";
    close BOGLOG;

    print $remote "$message";

    print qw!<pre>!;
    while ($line = <$remote>) {
	print $line;
    }
    print qw!</pre>!;
    $q->param('board' => "");
}

print $q->p, $q->hr, $q->p;

print $q->p("Enter your desired boggle board in the box " .
"below, press 'solve', and all possible boggle responses will be" .
" displayed!");

#my $boglog = "<b>Click <a href=\"boglog.pl\">here</a> for a list of recent uses of this solver</b>";

#print $q->p($boglog);
print $q->p("Use 'Q' (capital Q) for the 'Qu' combination");
print $q->p("Use 'q' (lowercase q) for plain 'q'");

%mode_labels = ('normal' => 'Normal (square)',
		'wr1' => 'Wordracer Round 1 (4x4 square)',
		'wr2' => 'Wordracer Round 2 (diamond)',
		'wr3' => 'Wordracer Round 3 (double square)',
		'wr4' => 'Wordracer Round 4 (picture frame)');

#%format_labels=('plain' => 'Plain Word List',
#		'augmented' => 'With definitions and word locations',
#		'irc' => 'Formatted for IRC Boggle');

#print $q->start_form( -method => 'POST', -action => $q->url()) ,
print $q->start_form( -method => 'POST', -action => 'helper.pl') ,
    $q->textarea ( -name => 'board', -rows => '8', -columns => '18',
		   -default=> "" ),
    $q->submit( -name => 'op', -value => 'solve'),

    $q->p,
    "Minimum Word Length: ",
    $q->popup_menu( -name=> 'minlength',
		    -values=>['Default','1','2','3','4','5','6','7','8']),

    $q->p,
    "Board shape: ",
    $q->popup_menu( -name => 'mode',
		    -values =>['normal', 'wr1', 'wr2', 'wr3', 'wr4'],
		    -labels => \%mode_labels),
    $q->p,
#    "Output format: ",
#    $q->popup_menu( -name => 'format',
#		    -values =>['plain', 'augmented', 'irc'],
#		    -labels => \%format_labels),

    $q->end_form();

#print $q->p, $q->hr, $q->p("You can find out " .
#      $q->a({href=>""},
#	  "more information about this program") . ", or its " .
#      $q->a({href=>""}, "creator") . ".");
#
#
print $q->p, $q->hr, $q->p(" " .
      $q->a({href=>"http://www.wineverygame.com/"},
	  "Scrabble Helper"));

print $q->p, $q->p(" " .
      $q->a({href=>"http://www.wineverygame.com/letterpress-cheat/"},
	  "Letterpress Helper"));
print <<END_OF_HTML;
</div>
END_OF_HTML
print $q->end_html();
    
