#!/usr/bin/perl
use lib '.';
require "./cgi-lib.pl";
header();
init();
body();
footer();

sub header {
  print "Content-type: text/html\n\n";
  print "<html>\n<head><title>Smacker Word Search</title></head>\n";
  print "<body bgcolor=#ffffff>\n";
}

sub init {
  ReadParse(*in);
  $width = $in{width};
  $height = $in{height};
  $num = $in{num};
  if($width < 3) { $width = 20; }
  if($height < 3) { $height = 20; }
  if($num < 1) { $num = 20; }
  if($in{puzz}) {
    if($in{type} eq "include") {
	$words = $in{words};
	$words =~ s/[^\w^\s]/ /g;
    }
    elsif($in{type} eq "random") {
	$in{words} =~ s/[^\w^\s]/ /g;
	$words = "-r $in{words}";
    }
    else { $words = ""; }
    $words =~ s/\s+/ /g;
    #@resp = split /\n/,`/home/dave/ws.pl -q -a -n $num -h $height -w $width $words`;
    @resp = split /\n/,`/home/dave/ws.pl -q -a -e -n $num -h $height -w $width $words`;
    @array = ();
    LINE: for($i=0;$i<@resp;$i++) {
	next LINE if($resp[$i] =~ /^-+$/);
	if($resp[$i] =~ /^\| ([\w ]+) \|$/) {
	  @row = split /\s/,$1;
	  for($j=0;$j<@row;$j++) { $array[$j][$crow] = $row[$j]; }
	  $crow++;
	  next LINE;
	}
	if($resp[$i] =~ /\w\w/) {
	  foreach (split /\s+/,$resp[$i]) {
	    if(/^(\w+)$/) { $wswords[@wswords] = $1; }
	  }
	}
    }
  }
}

sub body {
  if($in{puzz}) {
  print "<script language=\"Javascript\">\n";
  print "function reveal() {\n";
  print "  if(document.bgColor == \"#cccccc\") {\n";
  print "    document.bgColor = \"#ffffff\";\n";
  print "    document.forms[0].revealb.value = \"Reveal Answers\";\n";
  print "  }\n";
  print "  else {\n";
  print "    document.bgColor = \"#cccccc\";\n";
  print "    document.forms[0].revealb.value = \"Hide Answers\";\n";
  print "  }\n";
  print "}\n";
  print "</script>\n";
  }
  print "<form>\n";
  print "Generate new word search<br>\n";
  print "<select name=type size=1>\n";
  print "<option value=\"standard\">Search for random words (ignore below words)\n";
  if($in{type} eq "random") { $foo = " selected"; }
  else { $foo = ""; }
  print "<option$foo value=\"random\">Search for random words based on these words\n";
  if($in{type} eq "include") { $foo = " selected"; }
  else { $foo = ""; }
  print "<option$foo value=\"include\">Include these words\n";
  print "</select><br>\n";
  print "<textarea name=words cols=40 rows=4>$in{words}</textarea><br>\n";
  print "Width: <input name=width type=text size=3 value=\"$width\">\n";
  print "Height: <input name=height type=text size=3 value=\"$height\">\n";
  print "Max # of Words: <input type=text name=num size=3 value=\"$num\">\n";
  print "<br><input type=hidden name=puzz value=1>\n";
  print "\&nbsp;\&nbsp;&nbsp;<input type=submit value=\"Generate Word Search\"><br>\n";
  if($in{puzz}) {
    print "\n\n";
    print "<table border=0 cellpadding=1 cellspacing=0>";
    for($y=0;$y<$height;$y++) {
      print "<tr>\n";
      for($x=0;$x<$width;$x++) {
	print "<td align=center width=\"20px\" style=\"border: 1px solid #CCCCCC;\"";
	if($array[$x][$y] eq (uc $array[$x][$y])) { print " bgcolor=#ffffff"; }
	print ">" . (uc $array[$x][$y]) . "</td>";
      }
      print "</tr>\n";
    }
    print "</table>\n";
    print "<table cellspacing=3 cellpadding=5>\n";
    $i=0;
    foreach (@wswords) {
      if($i == 0) { print "<tr>\n"; }
      print "<td>&nbsp;$_&nbsp;</td>\n";
      $i++;
      if($i > 4) { print "</tr>\n"; $i=0; }
    }
    print "</table>\n";
    print "<input type=button name=revealb onclick=\"Javascript:reveal()\" value=\"Reveal Answers\">\n";
  }
  print "</form>\n";
}

sub footer {
  print "</body>\n";
  print "</html>\n";
}

