#!/usr/bin/perl -w

use CGI;

##   http://127.0.0.1/cgi-bin/Agonistics/index_for_player.pl?id=1&url=localhost&path=/Library/WebServer/Documents&archive=XYZ1&msg=16

my($request,$param_name,@param_names,%param);

## Read the parameters
##
## archive = a string, the name of the directory in which the messages are stored
## id =      an integer, the simplified author id 
## path =    a string, the absolute path to the documents directory of the Web server
## url =     a string, the URL (excluding the "http://" prefix) of the server that contains the messages
## msg =     an integer, the current simplified message id
##
$request = new CGI;
@param_names = ( 'id', 'archive', 'path', 'url', 'msg' );
foreach $param_name (@param_names) {
    $param{$param_name} = $request->param($param_name);
}


## WRITE_MESSAGE_LIST_HEADER
##
sub write_message_list_header
{
    my($title) = @_;

    my($html);
    $html = '<HTML>'."\n\t".'<HEAD>';
    $html .= "\n\t\t".'<META HTTP-EQUIV="Content-Type" content="text/html; charset=utf-8">';
    $html .= "\n\t\t".'<TITLE>'.$title.'</TITLE>';
    $html .= "\n\t\t".'<LINK type="text/css" rel="stylesheet" href="'.'http://'.$param{'url'}.'/Agonistics/'.$param{'archive'}.'/Interface/Messages/message_style.css">'."\n\t".'</HEAD>';
    $html .= "\n\t".'<BODY>'."\n";
    $html .= "\n\t".'<TABLE>'."\n\t\t".'<TBODY>';
    $html .= "\n\t\t".'<TR align="left">';
    $html .= "\n\t\t\t".'<TD class=headerfieldtext><B>ID</B></TD>';
    $html .= "\n\t\t\t".'<TD class=headerfieldtext><B>DATE</B></TD>';
    $html .= "\n\t\t\t".'<TD class=headerfieldtext><B>SUBJECT</B></TD>';
    $html .= "\n\t\t\t".'<TD class=headerfieldtext><B>SENDER</B></TD>';
    $html .= "\n\t\t".'</TR>';
    return($html);
}


## WRITE_MESSAGE_LIST_FOOTER
##
sub write_message_list_footer
{
    my($html);

    $html = "\n\t\t".'</TBODY>'."\n\t".'</TABLE>'."\n\t".'<BR><BR>';
    $html .= "\n\t".'</BODY>'."\n".'</HTML>';
    return($html);
}


## WRITE_MESSAGE_LIST
##
sub write_message_list
{
    my($end_of_record_marker,$standard_record_separator,$record,@records);
    my($smid,$addr,$sdt,$nsl,$indices,@html_table_row,$html);
    my(@indices_and_html_table_row);

    $end_of_record_marker = "\t\t".'</TR>'."\n";
    $standard_record_separator = $/;
    $/ = $end_of_record_marker;
    open(MSGINDEX,$param{'path'}.'/Agonistics/'.$param{'archive'}.'/Interface/Messages/message_index_for_'.$param{'id'}.'.txt');
#    open(MSGINDEX,'/Library/WebServer/Documents/Agonistics/'.$param{'archive'}.'/Interface/Messages/message_index_for_'.$param{'id'}.'.txt');
    @records = <MSGINDEX>;
    close(MSGINDEX);
    $/ = $standard_record_separator;
    foreach $record (@records) {
	if ( $record !~ /^\s*$/ ) {
	    @html_table_row = split /\n/,$record;
	    $indices = shift @html_table_row;
	    ($smid,$addr,$sdt,$nsl) = split /\t/,$indices;
	    if ( $smid <= $param{'msg'} ) {
		$html .= join "\n",@html_table_row;
		$html .= "\n";
	    }
	    else { last; }
	}
    }
    $html = &write_message_list_header($addr)."\n".$html."\n".&write_message_list_footer();
    print "Content-Type: text/html; charset=utf-8\n\n";
    print $html."\n";
}

&write_message_list();

    
