#!/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") || exit 1;

$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(%sub2judg))) {
    next if ( $sub2time{$sub} < $starttime );
    next if ( $sub2time{$sub} > $starttime+60*$length );
    next unless $sub2judg{$sub};
    if ( $sub2judg{$sub} eq "s" ) {
        $teamprob2score{$sub2team{$sub}.":".$sub2prob{$sub}} = int(($sub2time{$sub} - $starttime)/60);
    } elsif ( $sub2judg{$sub} ne "g" ) {
        if( $teamprob2score{$sub2team{$sub}.":".$sub2prob{$sub}} 
        &&  $teamprob2score{$sub2team{$sub}.":".$sub2prob{$sub}} >= 0 ) {
            $teamprob2score{$sub2team{$sub}.":".$sub2prob{$sub}} += 20;
        } else {
            $teamprob2score{$sub2team{$sub}.":".$sub2prob{$sub}} -= 1;
        }
    }
}

foreach $teamprob (keys(%teamprob2score)) {
    $teamprob =~ /^([^:]*):([^:])$/;
    $team = $1;
    $prob = $2;
    if( $teamprob2score{$teamprob} >= 0 ) {
        $team2probs{$team}+=1;
        $team2score{$team}+=$teamprob2score{$teamprob};
    }
}

if( $starttime && $starttime > 0 ) {
    $ENDTIME=$starttime+60*$length;
    $TIME_TOT=$length*60;
    $TIME_REM=$ENDTIME-$CURR_TIME;
    $TIME_EL=$CURR_TIME-$starttime;
    if( $TIME_REM >= 0 ) {
    printf "Total time: %3u:%02u:%02u    Time elapsed: %3u:%02u:%02u    Time remaining: %3u:%02u:%02u\n",
    $TIME_TOT/3600, ($TIME_TOT/60)%60, $TIME_TOT%60,
    $TIME_EL/3600, ($TIME_EL/60)%60, $TIME_EL%60,
    $TIME_REM/3600, ($TIME_REM/60)%60, $TIME_REM%60;
    } else{
        printf "Total time: %3u:%02u:%02u    Time elapsed: %3u:%02u:%02u    CONTEST HAS ENDED\n",
        $TIME_TOT/3600, ($TIME_TOT/60)%60, $TIME_TOT%60,
        $TIME_EL/3600, ($TIME_EL/60)%60, $TIME_EL%60;
    }
} else {
        printf "CONTEST HAS NOT YET STARTED\n";
}

printf "%-25s", "Team";
foreach $prob (split //,$problems) {
    printf "%5s", $prob;
}
printf "%10s\n", "  Prob Time";
print "==============================================================================\n";

#foreach $team (sort {$team2probs{$b} <=> $team2probs{$a}} (sort {$team2score{$a} <=> $team2score{$b}} (keys %teamprob2score))) {
#foreach $team (keys %team2score) {
foreach $team (sort {$team2probs{$b} <=> $team2probs{$a}} (sort {$team2score{$a} <=> $team2score{$b}} (keys %team2score))) {
    $name = $team2name{$team} || $team;
    printf "%-25s", $name;
    foreach $prob (split //,$problems) {
        printf "%5s", $teamprob2score{$team.":".$prob};
    }
    printf "%3s", $team2probs{$team};
    printf "%7s\n", $team2score{$team};
}
