#!/usr/bin/perl
##print "Content-type: text/html\n\nData Data<br>";

##
## ascii2sgfa.pl
## CGI-script that converts ASCII-go diagrams to SGF format.
## See my web page at http://www.dse.nl/~toni/go/ascii2sgf/
## for more information.
##
## Copyright (C) 1999  Toni Cornelissen (toni@dse.nl)
##
## This program is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public License
## as published by the Free Software Foundation; either version 2
## of the License, or (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
##
## This script (with the name ascii2sgf.pl) was written by Toni Cornelissen.
## It was then modified so much by Nick Wedd that it is not worth indicating
## the modifications separately.
##

require 'cgi-utils.pl';
require 'tc-utils.pl';
require 'go-utils.pl';

%param = &get_query();

$URLImage = "http://www.weddslist.com/g";

$edgelist="";
if ( $param{'topedge'}    eq 'on' ) { $edgelist = $edgelist . 't'; }
if ( $param{'bottomedge'} eq 'on' ) { $edgelist = $edgelist . 'b'; }
if ( $param{'leftedge'}   eq 'on' ) { $edgelist = $edgelist . 'l'; }
if ( $param{'rightedge'}  eq 'on' ) { $edgelist = $edgelist . 'r'; }
$labellist="";
if ( $param{'toplabel'}    eq 'on' ) { $labellist = $labellist . 't'; }
if ( $param{'bottomlabel'} eq 'on' ) { $labellist = $labellist . 'b'; }
if ( $param{'leftlabel'}   eq 'on' ) { $labellist = $labellist . 'l'; }
if ( $param{'rightlabel'}  eq 'on' ) { $labellist = $labellist . 'r'; }

@board = make_board(clean_board($param{'ascii'}),
         "pl=$param{'pl'}", "edge=$edgelist", "sq=$param{'sq'}", "label=$labellist");
$sgf   = board2sgf(@board);

if ($param{'out'} eq "sgf") {
  print "Content-type: application/x-go-sgf\n\n";
  print $sgf;
  exit(0);
}

$size = 15;
if ($param{'size'}) {
  $size = $param{'size'};
}

print "Content-type: text/html\n\n";
#print $head;
print"<html>\n";
print"<head>\n";
print"<title>\n";
print"Goban presented by Toni's script\n";
print"</title>\n";
print"</head>\n";
print"\n";
print"<BODY background=\"http://www.weddslist.com/goban/wd-back.gif\">\n";
print"\n";
print"<h1>Goban presented by Toni's script</h1>\n";

print"<!------\n";
print"This HTML code was generated by a script written by\n";
print"Toni Cornelissen <toni\@dse.nl> and modified by Nick Wedd.\n";
if ( $size < 0 ) { print"It uses graphics which were drawn by Nick Wedd and Andrew Grant.\n"; }
            else { print"It uses graphics which were drawn by Andrew Grant.\n"; }
print"Any defects should be reported to Nick Wedd <nick\@maproom.co.uk>.\n";
print"The scripts and graphics are hosted from Nick Wedd's web space.\n";
print"\nDon't insert line breaks in this code, line breaks may\n";
print" cause blank horizontal lines.\n";
print"------->\n";

print "The diagram:<p>\n";

print"<!------------ CUT HERE ----------->\n";
if ($param{'out'} eq "ascii") {
  print "<pre>" . &board2ascii(@board) . "</pre>";  ## <br> added here
}
else {
  ## set img attributes
  $abssize = $size;
  if ( $abssize<0 ) { $abssize = -$abssize; }
  $board[0]{'width'}  = $abssize;
  $board[0]{'height'} = $abssize;
  $board[0]{'alt'}    = "";

  if ( $size < 0 ) { print &board2html("${URLImage}/", @board); }
              else { print &board2html("${URLImage}/${size}/", @board); }
}
print"\n<!------------ CUT HERE ----------->\n";

print "\n<p>\nIs converted to the following SGF:<br>\n";
print "<pre>$sgf</pre>\n";

#print $foot;
print"<p>\n";
print"<a href=\"http://www.weddslist.com/goban/index.html#diaga\">. . back</a>\n";
print"\n";
print"</body>\n";
print"</html>\n";
