#!/usr/bin/perl

if ($ENV{'REQUEST_METHOD'} eq 'GET') {
    @pairs = split(/&/,$ENV{'QUERY_STRING'});
}
elsif ($ENV{'REQUEST_METHOD'} eq 'POST') {
    read (STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
    @pairs = split(/&/, $buffer);
}
else {
    print "Content-type: text/html\n\n";
    print "<P>Use Post or Get";
}

foreach $pair (@pairs) {
    ($key, $value) = split (/=/, $pair);
    $key =~ tr/+/ /;
    $key =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
    $value =~ tr/+/ /;
    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
    $value =~ s/<!--(.|\n)*-->//g;
    if ($formdata{$key}) {
        $formdata{$key} .= ", $value";
    }
    else {
        $formdata{$key} = $value;
    }
}

$loghand = 'ncaalog400';
$reshand = 'ncaaresults';

if (-e "football/hoopla/$loghand") {
    $count = 0;
    open (LOG, "football/hoopla/$loghand") or die "Can't open file: $!\n";
    while ($line = <LOG>) {
        chop $line;
        ($first[$count], $last[$count], $bet[$count]) = split /\t/, $line;
	if (($first[$count] ne '') and ($last[$count] ne '')) {
        $count += 1;
    }
    }
}
else {
    print "Content-type: text/html\n\n";

    print <<"HTML code";

<html>
<head>
<title>HooPLA 2003 Leaderboard</title>
<head>
<body>

Sorry, no one has made picks for this week yet.

</body>
</html>

HTML code
}

if (-e "football/hoopla/$reshand") {
    open (RES, "football/hoopla/$reshand") or die "Can't open file:$!\n";
    $count = 1;
    while ($line = <RES>) {
	chop $line;
	($Game[$count], $loser[$count]) = split /\t/, $line;
	$count += 1;
    }
}

for $x (0..$#first) {
    $raw[$x]{name} = join ' ',$first[$x],$last[$x];
    if ($bet[$x] eq 'one') {
	$raw[$x]{wage} = '$';
    }
    else {$raw[$x]{wage} = '';}
}

$ext = '300';

for $x (0..$#first) {
    $first[$x] =~ tr/A-Z/a-z/;
    $last[$x] =~ tr/A-Z/a-z/;
    $handle[$x] = join '',$first[$x],$last[$x],$ext,$bet[$x];
}

for $x (0..$#handle) {
    $raw[$x]{handle} = $handle[$x];
}

for $x (0..$#handle) {
    open (HAND, "football/hoopla/$handle[$x]") or die "Can't open file:$!\n";
    $count = 1;
    while ($line = <HAND>) {
        chop $line;
        $pick[$x][$count] = $line;
        $count += 1;
    }
}

for $x (0..$#first) {
    $raw[$x]{total} = 0;
    $raw[$x]{max} = 158;
    for $y (1..32) {
	if ($pick[$x][$y] eq $Game[$y]) {
	    $raw[$x]{total} += 1;
	}
    }
    for $y (33..48) {
	if ($pick[$x][$y] eq $Game[$y]) {
	    $raw[$x]{total} += 2;
	}
    }
    for $y (49..56) {
	if ($pick[$x][$y] eq $Game[$y]) {
	    $raw[$x]{total} += 4;
	}
    }
    for $y (57..60) {
	if ($pick[$x][$y] eq $Game[$y]) {
	    $raw[$x]{total} += 7;
	}
    }
    for $y (61..62) {
	if ($pick[$x][$y] eq $Game[$y]) {
	    $raw[$x]{total} += 10;
	}
    }
    if ($pick[$x][63] eq $Game[63]) {
	$raw[$x]{total} += 14;
    }
    for $y (1..63) {
        if ($loser[$y] eq $pick[$x][63]) {
            $raw[$x]{max} -= 14;
        }
    }
    for $y (1..62) {
        for $z (61..62) {
            if ($loser[$y] eq $pick[$x][$z]) {
                $raw[$x]{max} -= 10;
            }
        }
    }
    for $y (1..60) {
        for $z (57..60) {
            if ($loser[$y] eq $pick[$x][$z]) {
                $raw[$x]{max} -= 7;
            }
        }
    }
    for $y (1..56) {
        for $z (49..56) {
            if ($loser[$y] eq $pick[$x][$z]) {
                $raw[$x]{max} -= 4;
            }
        }
    }
    for $y (1..48) {
        for $z (33..48) {
            if ($loser[$y] eq $pick[$x][$z]) {
                $raw[$x]{max} -= 2;
            }
        }
    }
    for $y (1..32) {
        for $z (1..32) {
            if ($loser[$y] eq $pick[$x][$z]) {
                $raw[$x]{max} -= 1;
            }
        }
    }
}

for $x (0..$#raw) {
    $switch = 0;
    if ($x == 0) {%{$sort[0]} = %{$raw[$x]};}
    for ($y = $#sort; $y > -1; $y -= 1) {
        if ($raw[$x]{total} > $sort[$y]{total}) {
            $z = $y + 1;
            $tt = $sort[$y]{total};
	    $tn = $sort[$y]{name};
	    $th = $sort[$y]{handle};
            $tw = $sort[$y]{wage};
            $tm = $sort[$y]{max};
	    $sort[$y]{total} = $raw[$x]{total};
	    $sort[$y]{name} = $raw[$x]{name};
	    $sort[$y]{handle} = $raw[$x]{handle};
            $sort[$y]{wage} = $raw[$x]{wage};
            $sort[$y]{max} = $raw[$x]{max};
	    $sort[$z]{total} = $tt;
	    $sort[$z]{name} = $tn;
	    $sort[$z]{handle} = $th;
            $sort[$z]{wage} = $tw;
            $sort[$z]{max} = $tm;
	    $switch = 1;
        }
    }
    if (($switch == 0) and ($x != 0)) {
        $z = $#sort + 1;
        %{$sort[$z]} = %{$raw[$x]};
    }
}

print "Content-type: text/html\n\n";

print <<"HTML code";

<html>
<head>
<title>HooPLA 2003 Leaderboard</title>
<head>
<body>

<h2>HooPLA 2003 Leaderboard</h2>

Note: The Max totals denote the maximum number of points that participant<br>
can score, based on the teams they still have remaining in the tournament.<br>
<p>

HTML code

    print "<TABLE>\n";
print "<TR><TD><b>Rank<b><TD><b>Name<b><TD><b>Score<b><TD><b>Max</b>\n";

    for $x (0..$#sort) {
	$y = $x + 1;
	$z = $x - 1;
	if (($sort[$x]{total} == $sort[$z]{total}) and ($x > 0)) { 
	    print <<"HTML code"

<TR><TD><TD><a href="http://ling.ohio-state.edu/~swinters/cgi-bin/bracket.pl?handle=$sort[$x]{handle}">$sort[$x]{name}</a><TD>$sort[$x]{total}<TD>($sort[$x]{max})<TD>$sort[$x]{wage}

HTML code
	}
	else {
	    print <<"HTML code"

<TR><TD>$y<TD><a href="http://ling.ohio-state.edu/~swinters/cgi-bin/bracket.pl?handle=$sort[$x]{handle}">$sort[$x]{name}</a><TD>$sort[$x]{total}<TD>($sort[$x]{max})<TD>$sort[$x]{wage}

HTML code
	}
    }

print "</TABLE>\n";

print "</body>\n";
print "</html>\n";








