#!/usr/bin/perl

my $pages;

@ud_pages = (
	  {
	      title => "Introduction",
	      body  => 'intro_body',
	      file  => introduction },
	  {
	      title => "Controls",
	      body  => 'controls_body',
	      file  => controls },
	  {
	      title => "Games",
	      body  => 'games_body',
	      file  => games },
	  {
	      title => "Authors",
	      body  => 'auth_body',
	      file  => authors }
	  );

@www_pages = (
	  {
	      title => "Introduction",
	      body  => 'intro_body',
	      file  => 'index' },
	  {
	      title => "Screenshots",
	      body  => 'sshots_body',
	      file  => 'sshots' },
	  {
	      title => "Download",
	      body  => 'down_body',
	      file  => 'download' },
	  {
	      title => "Authors",
	      body  => 'auth_body',
	      file  => 'authors' },
	  );

# table header
my $th = "<table bgcolor=\"#207e20\" border=0>
<tr><td>
<table bgcolor=\"#a0a0a0\" cellpadding=3 cellspacing=1 border=0>";

# table footer
my $tf = "</table>
</td></tr>
</table>";

# bold GTulpas
$bgt = "<b>GTulpas</b>";

sub
header
{
    my $t;
    my $tpage = shift @_;

    $t = "<html>
<title>GTulpas: $tpage->{title}</title>
<body bgcolor=\"#5e8e5f\" text=white link=\"#ffffb0\">
<table  bgcolor=#2ad92a border=0 cellspacing=0 cellpadding=0>
<tr>";
    for my $page (@$pages) {

	$t .= "<td background=\"left.png\"><font size=-2>&nbsp</font></td>
<td colspan=2><font size=-2>&nbsp</font></td>";
    }

    $t .= "<td bgcolor=black><img hspace=0 vspace=0 valign=bottom align=left src=\"reb.png\"></td><tr>\n";

    for my $page (@$pages) {
	if ($page != $tpage) {
	    $t .= "<td background=\"left.png\"><font size=-2>&nbsp</font></td>
<td><a href=\"$page->{file}.html\"><i>$page->{title}</i></a></td>
<td background=\"right.png\"><font size=-2>&nbsp</font></td>\n";
	} else {
	    $t .= "<td background=\"left.png\"><font size=-2>&nbsp</font></td><td><i>$page->{title}</i></td><td>&nbsp;</td>\n";
	}
    }

    $t .= "<td bgcolor=black><img hspace=0 vspace=0 valign=bottom align=left src=\"reb.png\"></td></tr>
<tr>
";

    for my $page (@$pages) {
	$t .= "<td valign=bottom><img hspace=0 vspace=0 valign=bottom align=right src=\"blc.png\"></td>
<td background=\"bl.png\"><font size=-2>&nbsp</font></td>\n
<td valign=bottom><img hspace=0 vspace=0 valign=bottom align=left src=\"blr.png\"></td>
";
    }

    $t .= "<td><img hspace=0 vspace=0 valign=bottom align=left src=\"re.png\"></td></tr>
</table>
<table border=0 cellpadding=5>
<tr><td rowspan=2><img src=\"icon.jpg\"></td><td>&nbsp;</td></tr><tr><td></td><td><h3>$tpage->{title}</h3></td></tr>
</table>
";
}

sub
footer
{
"
<p>
<hr size=1>
<div align=right><a href=\"mailto:gtulpas\@suse.cz\">gtulpas\@suse.cz</a>
</div>
</body>
</html>
";
}

sub
ctl_line
{
    "<tr><td>&nbsp;" . (shift @_) . "&nbsp;</td><td>&nbsp;" . (shift @_) . "&nbsp;</td></tr>";
}

sub
controls_body
{
    my $t;

    $t = "
<p>
<strong>Mouse & keyboard</strong><p></td></tr>
$th";

    $t .= ctl_line ("arrows", "rotate current view");
    $t .= ctl_line ("mouse-left", "rotate current view");
    $t .= ctl_line ("p & mouse-left", "rotate current view");
    $t .= ctl_line ("M-arrows", "pan current view");
    $t .= ctl_line ("mouse-middle", "pan current view");
    $t .= ctl_line ("+/-", "zoom current view in/out");
    $t .= ctl_line ("z & mouse-left", "zoom current view in/out");
    $t .= ctl_line ("s & mouse-left", "spinning on - while pressed use mouse to make spin");
    $t .= ctl_line ("v & mouse-left", "rotate cue");
    $t .= ctl_line ("c & mouse-left", "move cue");
    $t .= ctl_line ("m & mouse-left", "move ball (if allowed by game rules)");
    $t .= ctl_line ("h", "toggle hint line");
    $t .= ctl_line ("0 on keypad", "join view with cue");
    $t .= ctl_line ("1-9 on keypad", "predefined viewpoints");
    $t .= ctl_line ("C- (Control)", "finer movements (ie. pointing cue)");

    $t .= "$tf
";
}

sub
auth_body
{
    my $t;
    open (AUTH, "< ../AUTHORS");
    $t = "<p>Authors of GTulpas<p>
$th
";

    while (<AUTH>) {
	chop;
	($first, $last, $addr) = /^([^\t ]+) ([^\t ]+)[ \t]+(.*)$/;
#	$addr =~ s/\@/\\\@/;
	print STDERR "$first $last : $addr\n";
	$t .= "<tr><td>&nbsp;$first&nbsp;$last&nbsp;</td><td>&nbsp;<a href=\"mailto:$addr\">$addr</a>&nbsp;</td></tr>";
    }

    close (AUTH);

    $t .= "$tf
<p>
$bgt was developed in cooperation with <a href=http://www.suse.cz>SuSE CR</a>.
";
}

sub
intro_body
{
    "
<p>
The Gnome Tulpas is a pool game. It could play pool games written in Guile language. Current release
contains Carom, Snooker and 9-Ball. It has nice 3D graphic thanks to Mesa/GL
<p>
Its main features are:
<ul>
<li>play pool games
<li>nice 3D graphics - uses Mesa/GL
<li>realistic physical engine
<li>extensible - you could write your own games in guile
<li>multiplayer game - you could play with your friend(s)
</ul>
";
}

sub
do_game
{
    my $fname = shift @_;
    my @author;
    my @link;
    my $name;
    my $min_pl, $max_pl;
    my $head=1;
    my $t;

    open (GAME, "< $fname");

    while (<GAME>) {
	chop;
	if ($head) {
	    if (/^name/) {
		($name) = /^name (.*)$/;
	    }
	    if (/^players/) {
		($min_pl) = /^players (.*)$/;
		if ($min_pl =~ /,/) {
		    ($max_pl) = /.*,(.*)$/;
		    $min_pl =~ s/,.*//;
		}
	    }
	    if (/^link/) {
		($link[$#link+1]) = /^link (.*)$/;
	    }
	    if (/^author/) {
		($author[$#author+1]) = /^author (.*)$/;
	    }
	    if (/^rules/) {
		$head=0;
		$t = "<table border=0><tr><td colspan=2><b>Players</b></td></tr><tr><td></td><td>$min_pl";
		if ($max_pl) {
		    $t .= " - $max_pl";
		}
		$t .= "</td></tr><tr><td colspan=2><br><b>Rules</b></td></tr><tr><td>&nbsp;&nbsp;</td><td>\n";
	    }
	} else {
	    $t .= $_;
	}
    }

    $t .= "</td></tr>\n";


    if ($#author>=0) {

	my $aname,$aaddr;

	$t .= "<tr><td colspan=2><br><b>Authors</b></td></tr>";
	for (@author) {
	    ($aname,$aaddr) = /^([^,]*),(.*)$/;
	    $t .= "<tr><td></td><td>$aname&nbsp;&nbsp;<a href=\"mailto:$aaddr\">($aaddr)</a></td></tr>\n";
	}
    }

    if ($#link>=0) {

	my $aname,$aaddr;

	$t .= "<tr><td colspan=2><br><b>Links</b></td></tr>";
	for (@link) {
	    ($aname,$aaddr) = /^([^,]*),(.*)$/;
	    $t .= "<tr><td></td><td><a href=\"$aname\">$aaddr</a></td></tr>\n";
	}
    }

    $t .= "</table>\n";

    close (GAME);

    $fname =~ s/\.\.\/rules\///;
    $fname =~ s/\.def//;

    my $page = { title => $name };

    open (GAME, "> game_$fname.html");
    print GAME header($page);
    print GAME $t;
    print GAME footer;
    close (GAME);

    "&nbsp;&nbsp;&nbsp;<strong><a href=\"game_$fname.html\">$name</a></strong><br>";
}

sub
games_body
{
    my $t="<p>The following games could be played with GTulpas<p>";

    open (LS, "ls ../rules/*.def |");
    while (<LS>) {
	print STDERR $_;
	chop;
	$t .= do_game ($_);
    }
    close (LS);

    $t .= "<p>Follow links above to learn more about games rules, authors and other info.";
}

sub
down_line
{
    my $url = shift @_;
    my $text = shift @_;
    my $spec = shift @_;
    my $size = shift @_;


    "<a href=\"$url\">$text</a> (" . ($size >> 10) . " kB, $spec)";
}

sub
down_body
{
    "
<p>
Latest release: " .
down_line ("ftp://www.suse.com/fill_path_here/gtulpas-1.0.0.tar.gz", "gtulpas-1.0.0.tar.gz", "source tarball", 563287) .
"";
}

sub
sshots_body
{
    "";
}

my $ud = $ARGV[0] eq "ud" ? 1 : 0;

if ($ud) {
    print "making User doc pages\n";
    open (TOPIC, "> topic.dat");
    $pages = \@ud_pages;
} else {
    print "making WWW pages\n";
    $pages = \@www_pages;
}

for my $page (@$pages) {
    print "$page->{file} $page->{body}\n";
    open (FILE, "> $page->{file}.html");
    print FILE header($page);
    print FILE &{$page->{body}};
    print FILE footer;
    if ($ud) { print TOPIC "$page->{file}.html\t$page->{title}\n"; }
    close (FILE);
}

if ($ud) {
    close (TOPIC);
}
