#!/usr/bin/perl -w

use CGI;

##   http://127.0.0.1/cgi-bin/Agonistics/forward_to_latest_frame.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);
}

sub find_last_interface_file
{
    my($interface_dir,@file_names,$max_frame,$fn,$frame_number);

    $interface_dir = $param{'path'}.'/Agonistics/'.$param{'archive'}.'/Interface';
    opendir(ID,$interface_dir);
    @file_names = readdir(ID);
    close(ID);
    $max_frame = 0;
    foreach $fn (@file_names) {
	if ( $fn =~ /^agon(\d+)\./ ) {
	    $frame_number = $1;
	    if ( $frame_number > $max_frame ) { $max_frame = $frame_number; }
	}
    }
    return($max_frame);
}


sub write_html
{
    my($max_frame,$html);

    $max_frame = &find_last_interface_file();
    $html = '<HTML>
<HEAD>
<META http-equiv="Refresh" content="1; URL=http://'.$param{'url'}.'/Agonistics/'.$param{'archive'}.'/Interface/agon'.$max_frame.'.html">
<TITLE>agonistics</TITLE>
<LINK type="text/css" rel="stylesheet" href="message_style.css">
</HEAD>
<BODY>
&nbsp;
</BODY>
</HTML>';
    print "Content-Type: text/html; charset=utf-8\n\n";
    print $html."\n";
}

&write_html();

    
