#!/usr/bin/perl -w

use CGI;

##   http://127.0.0.1/cgi-bin/Agonistics/list_messages_by_thread.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);
}
%smid2sdt = ();
%smid2spid = ();
%spid2smids = ();
%html = ();


## 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,$spid,$indices,@html_table_row,$html);
    my(@indices_and_html_table_row,$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 = '';
	    if ( $record !~ /^\s*$/ ) {
		@html_table_row = split /\n/,$record;
		$indices = shift @html_table_row;
		($smid,$addr,$sdt,$nsl,$spid) = split /\t/,$indices;
		if ( $spid eq 'no_parent' ) { push(@roots,$smid); }
		$smid2spid{$smid} = $spid;
		$smid2sdt{$smid} = $sdt;
		$html .= join "\n",@html_table_row;
		$html .= "\n";
		$html{$smid} = $html;
	    }
	}
	$id++;
	$next_index_file_name = $param{'path'}.'/Agonistics/'.$param{'archive'}.'/Interface/Messages/message_index_for_'.$id.'.txt';
    }
    foreach $smid (keys %smid2spid) {
	if ( $smid2spid{$smid} ne 'no_parent' ) { push(@{$spid2smids{$smid2spid{$smid}}},$smid); }
    }
    $html = &write_message_list_header('messages listed by thread')."\n";
    foreach $root (sort { $smid2sdt{$a} <=> $smid2sdt{$b} } @roots) {
	&rewrite_html_for_thread($root,0);
	$html .= &assemble_html_for_thread($root);
    }
    $html .= "\n".&write_message_list_footer();    
    print "Content-Type: text/html; charset=utf-8\n\n";
    print $html."\n";
}


sub create_indent 
{
    my($depth) = @_;
    
    my($indent);
    
    if ( $depth <= 0 ) { return(''); }
    else { 
	$indent = &create_indent($depth-1);
	return('&nbsp;&nbsp;&nbsp;'.$indent);
    }
}


sub rewrite_html_for_thread
{
    my($smid,$depth) = @_;

    my($indent,$html,$child);

    $indent = &create_indent($depth);
    $html = $html{$smid};
    $html =~ s/<A href/$indent<A href/;
    $html{$smid} = $html;
    foreach $child (@{$spid2smids{$smid}}) {
	&rewrite_html_for_thread($child,$depth+1);
    }
}


sub assemble_html_for_thread
{
    my($smid) = @_;
    
    my($html,$child);

    $html = $html{$smid};
    foreach $child (sort { $smid2sdt{$a} <=> $smid2sdt{$b} } @{$spid2smids{$smid}}) {
	$html .= &assemble_html_for_thread($child);
    }
    return($html);
}


&write_message_list();

    
