#!/usr/bin/perl
#
# Copyright (c) 2000 - E.L.Willighagen.
#
# 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.
# 
# Author: E.L.Willighagen
#
# Contributers:
#	  Stein Vrale (testing)
#	  Felis (DHTML)
#
# Major Modifications for Terminus use:
#	G'Kar ( d99msr@efd.lth.se )
#

use strict;
use CGI qw(:standard);

print header;

my $VERSION = 0.4;

my $id = $ARGV[0];
# these parameters need configuration
#
#  where is the db info located?
#
my $base_url = "http://xsw.terminator.net";
my $base_dir = "/home/swserv";

my $opmfile = "$base_dir/universe/db/terminus.opm";
my $picturedir = "$base_dir/public_html/images";
my $scripturl = $ENV{'SCRIPT_NAME'};
my $css_url = "$base_url/css/xsw.css";

#  where are the results located?
#
#my $gifext = "gif";
my $gifext = "png";
my $htmlgifdir = "$base_url/images/"; 
my $universe = "XSW Terminus";

# read code2picture mapping
my %pictures;
if (open(FILE, "$picturedir/default.isr")) {
	my $picture = "";
	my $code = "";
	while (<FILE>) {
		if (/BeginISRefEntry\s*=\s*(.*)/i) {
			$code = $1;
			$picture = "";
		} elsif (/FileName\s*=\s*(.*)/i) {
			$picture = $1;
		} elsif (/EndISRefEntry/i) {
			if ($picture ne "") {
				$picture =~ s/(.*?)\..*/$1.$gifext/;
				$pictures{$code} = $picture;
			}
		}
	}
} else {
	print "Can't locate picturedir ($picturedir)\n";
	exit(1);
}	  

if (open(FILE, "<$opmfile")) {
	print qq[<HTML>\n];
	print qq[<HEAD>\n];
	print qq[<LINK REL="stylesheet" HREF="$css_url">\n];
	print qq[</HEAD>\n];

	if (param('action') ne 'image') {
		print qq[<body>\n];
		print qq[<SCRIPT language="javascript">\n];
		print qq[<!--\n];
		print qq[function openWindow (URL,Name,Attr) {\n];
		print qq[window.open (URL,Name,Attr);\n];
		print qq[}\n];
		print qq[-->\n];
		print qq[</SCRIPT>\n];
        print qq[<center><BR>\n];
#		print qq[<table width="90%" border=1><TR><TD><BR><h1>$universe</h1></TD><TR></TABLE><BR><BR>\n];
#		print qq[<TABLE BORDER=1 WIDTH="90%"><TR><TD><BR>\n];
		print qq[<table align=center width="90%" border="0">\n];
	}
	
	# parse opm file
	my $name = "";
	my $picture = "";
	my $empire = "";
	my $scanrange = "";
	my $velmax = "";
	my $type = "";
	my $cloak = "";
	my $damagecontrol = "";
	my $hitmax = "";
	my $turnrate = "";

	# parse opm file
	my $isobject = 0;
	my $isuniv = 0;
	my $current_id = "";
	my %params;	 

	while (<FILE>) {
		if (/BeginXSWObject\s*=\s*(.*)/i) {
			$isobject = 1;
			$current_id = $1;
			%params = ();
			#print "Detected object $current_id$/";
		} elsif (/BeginHeader/i) {
			$isuniv = 1;
		} elsif (/EndHeader/i) {		
			$isuniv = 0;
		} elsif (/EndXSWObject/i) {
			$isobject = 0;

			if ( $params{'Type'} == 4 && $params{'Name'} ne 'Guest' && $params{'Name'} ne 'New Player' && $params{'Name'} ne 'Player Wreck') {

				if (param('id') ne '')
				{ # Dump ship 
					if (param('id') eq $current_id ) {
						if (param('action') eq "image") {
							print qq[<img src="$htmlgifdir$pictures{$params{'ImageSet'}}">];
						}
						else{				
							print qq[<tr><td><a href="$ENV{'SCRIPT_NAME'}?id=$current_id">$params{'Name'}</A></td><td>&nbsp;</td><td>&nbsp;</td></tr>\n];
							print qq[<tr>\n];
							if ($params{"ImageSet"} ) {
								# it has an picture
								print qq[<td>];
								print qq[<img src="$htmlgifdir$pictures{$params{'ImageSet'}}">];		
								print qq[</td><td>&nbsp;</td><td>&nbsp;</td>\n];
							} else {
								print "<td><b>No picture</b></td>";
							}
							print qq[</TR>\n];
							print qq[<TR><TD>General</TD>];
							print qq[<TD>Size</TD><TD>$params{'Size'}</TD></TR>\n];						
							print qq[<TR><TD>&nbsp;</TD><TD>Hitpoints</TD><TD>$params{'HitPointsMax'}</TD></TR>\n];
							print qq[<TR VALIGN=TOP><TD>&nbsp;</TD><TD>Antimatter</TD><TD>$params{'AntimatterMax'}<BR><BR></TD></TR>\n];

							print qq[<TR><TD>Movement</TD>\n];
						
							print "<TD>Speed</TD><TD>$params{'VelocityMax'}</TD></TR>\n";
						print "<TR><TD>&nbsp;</TD><TD>Acceleration</TD><TD>$params{'ThrustPower'}</TD></TR>\n";
						print "<TR VALIGN=TOP><TD>&nbsp;</TD><TD>Maneuverability</TD><TD>$params{'TurnRate'}<BR><BR></TD></TR>\n";

						print "<TR><TD>Generator</TD>\n";
						print "<TD>Power</TD><TD>$params{'PowerMax'}</TD></TR>\n";
						print "<TR><TD>&nbsp;</TD><TD>CoreEfficency</TD><TD>$params{'CoreEfficency'}</TD></TR>\n";
						print "<TR VALIGN=TOP><TD>&nbsp;</TD><TD>PowerPurity</TD><TD>$params{'PowerPurity'}<BR><BR></TD></TR>\n";

						print "<TR><TD>Equipment</TD>\n";
						print "<TD>ScannerRange</TD><TD>$params{'ScannerRange'}</TD></TR>\n";
						print "<TR><TD>&nbsp;</TD><TD>Cloaking</TD><TD>$params{'CloakStrength'}</TD></TR>\n";
						print "<TR><TD>&nbsp;</TD><TD>Visibility</TD><TD>$params{'Visibility'}</TD></TR>\n";
						print "<TR><TD>&nbsp;</TD><TD>TotalWeapons</TD><TD>$params{'TotalWeapons'}</TD></TR>\n";	
						}
					}
				}
				else
				{
					# Index
					print qq[<TR>$/];
					print qq[<TD ALIGN=CENTER WIDTH="50%"><img width="50" heigth="50" src="$htmlgifdir$pictures{$params{'ImageSet'}}"></TD>];
					print qq[<TD ALIGN=CENTER WIDTH="50%"><A HREF="javascript:openWindow('$scripturl?id=$current_id','ShipID$current_id','width=600,height=620')">$params{'Name'}</A></BR><BR>];
					print qq[</TD></TR>$/];
				}
			}
		} elsif (/BeginEcoData/i) {
			$isobject = 0;
		} elsif (/EndEcoData/i) {
			$isobject = 1;
		} elsif (/BeginEcoProduct/i) {
		} elsif (/EndEcoProduct/i) {
		} elsif (/BeginWeapon/i) {
			$isobject = 0;
		} elsif (/EndWeapon/i) {
			$isobject = 1;
		} elsif ($isobject || $isuniv) {
			# do some general parsing of parameters
			my ($param, $value) = parseLine($_);
			# print "  $param -> $value$/";
			$params{$param} = $value;
		}
	}
		
	print "</table>$/";
#	print "<BR></TD></TR></TABLE>$/";
	print "</center><BR>$/";
	print "</body>$/";
	print "</html>$/";
	} else {
	print "Can't locate OPM file ($opmfile)\n";
}

sub parseLine {
	my $line = shift @_;
	$line =~ /^\s*(.*?)\s*=\s*(.*)\s*/;

	my $param = $1;
	my $val = $2;
	return ($param, $val);
}





