#!/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( $ENV{SIMPCONT_SERVER} ) {
    system "stty cbreak </dev/tty >/dev/tty 2>&1";
    system "stty", '-icanon', 'eol', "\001";
    $ret = system( "ssh $ENV{SIMPCONT_SERVER} \"TERM=xterm watch $ENV{SIMPCONT_REMOTE_DIR}\"" );
    system "stty -cbreak </dev/tty >/dev/tty 2>&1";
    system "stty", 'icanon', 'eol', "\001";
    exit $ret;
}
if( $ENV{SIMPCONT_REMOTE_DIR} ) {
    $MESSAGE_DIR="$ENV{SIMPCONT_REMOTE_DIR}";
    system "stty cbreak </dev/tty >/dev/tty 2>&1";
    system "stty", '-icanon', 'eol', "\001";
    $SCOREBOARD="$MESSAGE_DIR/scoreboard";
} elsif( $ARGV[0] ) {
    $MESSAGE_DIR="$ARGV[0]";
    $SCOREBOARD="$MESSAGE_DIR/scoreboard";
} else {
    $IS_JUDGE=1;
    system "stty cbreak </dev/tty >/dev/tty 2>&1";
    system "stty", '-icanon', 'eol', "\001";
    $SCOREBOARD="$CONTEST_DIR/var/scoreboard";
}

$MODE="scoreboard";
$SCROLL=0;


$timeout=0;
while(1) {
    $nfound = 0;
    $rin='';
    vec($rin,fileno(STDIN),1)=1;
    $nfound = select( $rin, undef, undef, $timeout );
    $timeout=3;

    $SB_TEXT=`cat $SCOREBOARD 2> /dev/null`;
    if( $IS_JUDGE ) {
        $SUB_TEXT=`$CONTEST_DIR/bin/to_be_judged`;
        $MES_TEXT=`cat $CONTEST_DIR/var/allmsgs/msg* 2> /dev/null`;
    } else {
        $SUB_TEXT=`cat $MESSAGE_DIR/sub* 2> /dev/null`;
        $MES_TEXT=`cat $MESSAGE_DIR/msg* 2> /dev/null`;
    }

    if( $nfound > 0 ) {
        $key='';
        sysread(STDIN,$key,1);
        $MODE="scoreboard", $SCROLL=0 if $key eq "1";
        $MODE="submissions", $SCROLL=0 if $key eq "2";
        $MODE="messages", $SCROLL=0 if $key eq "3";
        $SCROLL+=1 if $key eq "\n";
        $SCROLL-=1 if $key eq "-";
        $SCROLL+=20 if $key eq " ";
        $SCROLL+=20 if $key eq "d";
        $SCROLL-=20 if $key eq "u";
        $SCROLL-=20 if $key eq "b";
        $SCROLL=0 if $SCROLL<0;
    } else {
        $MODE="messages", $SCROLL=0 if $MES_TEXT ne $OLD_MES_TEXT;
        $MODE="submissions", $SCROLL=0 if $SUB_TEXT ne $OLD_SUB_TEXT;
        $OLD_MES_TEXT=$MES_TEXT;
        $OLD_SB_TEXT=$SB_TEXT;
        $OLD_SUB_TEXT=$SUB_TEXT;
    }

    $SB="scoreboard";
    $SUB="submissions";
    $MES="messages";
    $SB =~ tr/a-z/A-Z/ if $MODE eq "scoreboard";
    $SUB =~ tr/a-z/A-Z/ if $MODE eq "submissions";
    $MES =~ tr/a-z/A-Z/ if $MODE eq "messages";

    system("clear");
    print "1 - $SB  2 - $SUB  3 - $MES  u/d - scroll up/down\n";
    
    $SCREEN=$SB_TEXT if $MODE eq "scoreboard";
    $SCREEN=$SUB_TEXT if $MODE eq "submissions";
    $SCREEN=$MES_TEXT if $MODE eq "messages";

    for( $i=0; $i<$SCROLL; $i++ ) { $SCREEN=~ s/[^\n]*\n//m };
    $SCREEN=~ s/(([^\n]*\n){0,20})(.*)/$1/s;
    print $SCREEN;
    print "Hit enter, space, or d, to scroll down to see the rest.\n" if length($3) > 0;
}
