#!/usr/bin/perl -w

use CGI;

##   http://127.0.0.1/cgi-bin/Agonistics/list_messages_by_id.pl?&url=localhost&path=/Library/WebServer/Documents&archive=XYZ1

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

## Read the parameters
##
## archive = a string, the name of the directory in which the messages are stored
## 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
##
$request = new CGI;
@param_names = ( 'archive', 'path', 'url' );
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>';
    $html .= '<A href="list_messages_by_id.pl?&url='.$param{'url'}.'&path='.$param{'path'}.'&archive='.$param{'archive'}.'"><B>ID</B></A></TD>';
    $html .= "\n\t\t\t".'<TD class=headerfieldtext>';
    $html .= '<A href="list_messages_by_date.pl?&url='.$param{'url'}.'&path='.$param{'path'}.'&archive='.$param{'archive'}.'"><B>DATE</B></A></TD>';
    $html .= "\n\t\t\t".'<TD class=headerfieldtext>';
    $html .= '<A href="list_messages_by_subject.pl?&url='.$param{'url'}.'&path='.$param{'path'}.'&archive='.$param{'archive'}.'"><B>SUBJECT</B></A></TD>';
    $html .= "\n\t\t\t".'<TD class=headerfieldtext>';
    $html .= '<A href="list_messages_by_sender.pl?&url='.$param{'url'}.'&path='.$param{'path'}.'&archive='.$param{'archive'}.'"><B>SENDER</B></A></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,%html);
    my(@indices_and_html_table_row,$id,$msg_id);

    $end_of_record_marker = "\t\t".'</TR>'."\n";
    $standard_record_separator = $/;
    $id = 1;
    $next_index_file_name = $param{'path'}.'/Agonistics/'.$param{'archive'}.'/Interface/Messages/message_index_for_'.$id.'.txt';
    while (-e $next_index_file_name) {
	$/ = $end_of_record_marker;
	open(MSGINDEX,$next_index_file_name);
	@records = <MSGINDEX>;
	close(MSGINDEX);
	$/ = $standard_record_separator;
	foreach $record (@records) {
	    $html = '';
	    $msg_id = -1;
	    if ( $record !~ /^\s*$/ ) {
		@html_table_row = split /\n/,$record;
		$indices = shift @html_table_row;
		($smid,$addr,$sdt,$nsl,$spid) = split /\t/,$indices;
		if ( $smid ) { $msg_id = $smid; }
		$html .= join "\n",@html_table_row;
		$html .= "\n";
		$html{$msg_id} = $html;
	    }
	}
	$id++;
	$next_index_file_name = $param{'path'}.'/Agonistics/'.$param{'archive'}.'/Interface/Messages/message_index_for_'.$id.'.txt';
    }
    $html = &write_message_list_header('messages listed by id')."\n";
    foreach $smid (sort {$a <=> $b} keys %html) {
	$html .= $html{$smid};
    }
    $html .= "\n".&write_message_list_footer();    
    print "Content-Type: text/html; charset=utf-8\n\n";
    print $html."\n";
}

&write_message_list();

    
