#!/usr/bin/perl -wT

use strict;
use lib '.';
use Dev2;
use CGI;
my $q = new CGI;

my $ftp_root = '/home/ftp/pub/CircleMUD';
my $changes = "$ftp_root/Changes";
my $index_name = '00Index';

if (!open(CHANGES, "<$changes")) {
  warn "$changes: $!\n";
  print	$q->header( -type => 'text/plain' ),
	'Fatal error opening Changes file.';
  exit;
}

my @changes;
my ($month, $day, $year, $maintainer, $directory, $filename, $action) = ('') x 7;
my $counter = 10;
while (<CHANGES>) {
  chomp;
  if (/^(\w+) (\d+), (\d+) -- (\w+)$/) {
    $month = $1;
    $day = $2;
    $year = $3;
    $maintainer = $4;

    last if (not --$counter);
  } elsif (m!^\s+(/[\w/]+)$!) {
    $directory = $1;
  } elsif (m!^\s+([^/][^\s]+)\s+\((\w+)\)$!) {
    $filename = $1;
    $action = $2;

    push(@changes, [ $month, $day, $year, $maintainer, $action eq 'Archived' ? "$directory/OLD" : $directory, $filename, $action ])
	if (not $filename =~ /\.README$/);
  }
}

my %index;

my @classes = ( 'latest-bg1', 'latest-bg2' );
my $class_now = 0;

my $url = $q->script_name();
$url =~ s!/[^/]+$!!;

print	$q->table({ -width => '100%', -cellpadding => 3, -cellspacing => 0 }, [ $q->Tr([ map {
		my ($m, $d, $y, $t, $r, $f, $a) = @{$_};
		my $class = $classes[$class_now++ % @classes];
		$index{$r} ||= Dev2::parse_index($index_name, "$ftp_root$r/$index_name");
		( $q->td({ -class => $class }, [
			substr($m, 0, 3) . "\&nbsp;$d,\&nbsp;$y",
			$q->a({ -href => ($url . "$r/$f") }, $f),
			$q->a({ -href => ($url . $r . '/') }, $r),
			$a
		  ]),
		  $q->td({ -class => $class }, [ '&nbsp;' ]) .
		  $q->td({ -class => $class, -colspan => 3 }, [ $index{$r}{$f} || 'No description available.' ])
		);
	} (@changes) ]) ]);
