#!/usr/bin/perl
#
# Copyright (C) 2003 Ondrej Lhotak
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
$CONTEST_DIR=`dirname $0`;
chomp $CONTEST_DIR;
$CONTEST_DIR.="/..";

if( open(NAMES, "<", "$CONTEST_DIR/etc/names") ) {
    while( <NAMES> ) {
        chomp;
        if( /([^:]*):([^:]*)/ ) {
            $team2name{$1}=$2;
        }
    }
}

open(LOG, "<", "$CONTEST_DIR/var/log") || die "Can't open $CONTEST_DIR/var/log";

$CURR_TIME=`$CONTEST_DIR/bin/timestamp`;

while( <LOG> ) {
    chomp;
    if( /^([^:]*):B$/ ) {
        $starttime=$1;
    }
    if( /^([^:]*):E:([^:]*)$/ ) {
        $length=$2;
    }
    if( /^([^:]*):S:([^:]*):([^:]*):(.)/ ) {
        $sub2time{$2}=$1;
        $sub2team{$2}=$3;
        $sub2prob{$2}=$4;
        $team2score{$3}=0;
    }
    if( /^([^:]*):J:([^:]*):(.)$/ ) {
        $sub2judg{$2}=$3;
    }
    if( /^([^:]*):U:([^:]*)$/ ) {
        $sub2judg{$2}=undef;
    }
    if( /^([^:]*):P:([^:]*)$/ ) {
        $problems=$2;
    }
}

foreach $sub (sort {$sub2time{$b} <=> $sub2time{$a}} (keys(%sub2time))) {
    next if ( $sub2time{$sub} < $starttime );
    next if ( $sub2time{$sub} > $starttime+60*$length );
    next if $sub2judg{$sub};
    print "$sub $sub2team{$sub} $team2name{$sub2team{$sub}} $sub2prob{$sub}\n";
}
