#!/usr/bin/perl

use DBI;

require "/usr/local/apache/cgi-bin/games2000/location.set";


###### getting form data

%formdata=();
%filenames=();

if( $ENV{'CONTENT_TYPE'}=~/multipart/ )
{
    binmode STDIN;
    read(STDIN, $query, $ENV{'CONTENT_LENGTH'});

    $query=~/\A(.+?)(\015|\012)/;
    $boundary = $1;

    (@allkeys) = split/$boundary/,$query;

    foreach $mykey (@allkeys) {

        if($mykey=~/name="(.+?)"/) {
            $keyname=$1;
            $mykey=~s/\A(.+?)\015{0,1}\012\015{0,1}\012//s;
            $keyinfo=$1;

            if( $keyinfo=~/filename="(.+?)"/ ) {
                $filename = $1;
            } else { $filename=""; }

            $mykey=~s/\015{0,1}\012\Z//;

            $formdata{$keyname}=$mykey;
            $filenames{$keyname}=$filename;
        }
    }
} else {
if( $ENV{'REQUEST_METHOD'} eq 'POST' )
{
    read(STDIN, $query, $ENV{'CONTENT_LENGTH'});

    @params=split(/&/, $query);
    foreach $keyvalueparam (@params) {
        ($keyparam, $valueparam) = split(/=/, $keyvalueparam);
        $valueparam=~tr/+/ /;
        $valueparam=~ s/%([\dA-Fa-f][\dA-Fa-f])/pack ("C", hex ($1))/eg;
        $formdata{$keyparam}=$valueparam;
    }
} else
{
    $query = $ENV{'QUERY_STRING'};

    @params=split(/&/, $query);
    foreach $keyvalueparam (@params) {
        ($keyparam, $valueparam) = split(/=/, $keyvalueparam);
        $valueparam=~tr/+/ /;
        $valueparam=~ s/%([\dA-Fa-f][\dA-Fa-f])/pack ("C", hex ($1))/eg;
        $formdata{$keyparam}=$valueparam;
    }
}
}

######  analyzing parameters

    
    $action = $formdata{'action'};
    $recordid = $formdata{recordid};

    $jsstart = "<script language='javascript'><!--\n";
    $jsend = "\n//--></script>";

    if( ($action eq "") or ($action eq "main") ) { show_main(); exit;}
    if( $action eq "update" ) { update(); exit;}

    print $standardheader;
    print "Query error. Fuck jager.";
    exit;


sub parse_client_cookies
{
    local (*COOKIE_DATA) = @_;
        
    local (@key_value_pairs, $key_value, $key, $value);
    @key_value_pairs = split (/;\s/, $ENV{'HTTP_COOKIE'});
    foreach $key_value (@key_value_pairs) {
        ($key, $value) = split (/=/, $key_value);
        $key   =~ tr/+/ /;
        $value =~ tr/+/ /;
        $key   =~ s/%([\dA-Fa-f][\dA-Fa-f])/pack ("C", hex ($1))/eg;
        $value =~ s/%([\dA-Fa-f][\dA-Fa-f])/pack ("C", hex ($1))/eg;
        if (defined($FORM_DATA{$key})) {
            $FORM_DATA{$key} = join ("\0", $FORM_DATA{$key}, $value);
        } else {
	    $FORM_DATA{$key} = $value;
	}
    }
}
													    
													    








sub show_main() {
    print $standardheader;

    local $/=undef;
    open(T,"$tplpath/medals_menu.html") || die "Cannot open template: $tplpath/medals_menu.html";
    $tpl=<T>; close(T);

    open(T,"$tplpath/navigation") || die "Cannot open template: $tplpath/navigation";
    $navigation=<T>; close(T);
    ($navigationselected) = $navigation=~/::selected(.+?)selected::/s; $navigation=~s/::medals::/$navigationselected/; $navigation=~s/::(.+?):://g;
    $tpl=~s/::navigation::/$navigation/;

    ($jsblock)=($tpl=~/::jscountry(.+?)jscountry::/s);
    ($selectblock)=($tpl=~/::countrylist(.+?)countrylist::/s);

    $dbh = DBI->connect("DBI:mysql:$database:$hostname", $username, $password);
    if( !$dbh ) {
        print "Unable to open database\n";
        exit;
    }

    $sth = $dbh->prepare("select * from countrys order by country_name");
    $sth->execute();


    $jsblockx="";
    $selectblockx="";

    if($formdata{selector} ne "") { $selected=$formdata{selector}; } else {$selected=-1;};

    for( $i=0; $i<$sth->rows; $i++) {
	$row = $sth->fetchrow_hashref;
	$jsblockc=$jsblock;
	$selectblockc=$selectblock;
	
	$jsblockc=~s/::id::/$row->{country_id}/;
	$jsblockc=~s/::gold::/$row->{country_gold}/;
	$jsblockc=~s/::silver::/$row->{country_silver}/;
	$jsblockc=~s/::bronze::/$row->{country_bronze}/;
	$jsblockc=~s/::oldgold::/$row->{country_oldgold}/;
	$jsblockc=~s/::oldsilver::/$row->{country_oldsilver}/;
	$jsblockc=~s/::oldbronze::/$row->{country_oldbronze}/;
	
	$selectblockc=~s/::id::/$row->{country_id}/;
	$selectblockc=~s/::countryname::/$row->{country_name}/;

        if( ($selected==$row->{country_id}) or (($selected==-1) and ($i==0))) { $isselected=" selected "; } else { $isselected="" };

	$jsblockx.=$jsblockc;
	
	$selectblockc=~s/::selected::/$isselected/;
	$selectblockx.=$selectblockc;

    }
    
    $tpl=~s/::jscountry(.+?)jscountry::/$jsblockx/s;
    $tpl=~s/::countrylist(.+?)countrylist::/$selectblockx/s;
    print $tpl;

    $sth->finish;
    $dbh->disconnect;
}



sub update() {
    print $standardheader;


    $dbh = DBI->connect("DBI:mysql:$database:$hostname", $username, $password);
    if( !$dbh ) {
        print "Unable to open database\n";
        exit;
    }

    $sth = $dbh->prepare("update countrys set country_gold=?, country_silver=?, country_bronze=?, country_oldgold=?, country_oldsilver=?, country_oldbronze=? where country_id=?");
    $sth->execute($formdata{mygold},$formdata{mysilver},$formdata{mybronze},$formdata{myoldgold},$formdata{myoldsilver},$formdata{myoldbronze},$formdata{selector});
    $sth->finish;

    local $/=undef;
    open(T,"$tplpath/games2000_medals.shtml") || die "Cannot open template";
    $tpl=<T>; close(T);


    $medalsfiller = "";
    $sth = $dbh->prepare("select *, (country_oldgold*3+country_oldsilver*2+country_oldbronze) as totalla from countrys order by totalla desc");
    $sth->execute;
    
    for( $i=0; $i<9; $i++) {
	$row = $sth->fetchrow_hashref;
	$medalsblockcopy = $tpl;
    
	$medalsblockcopy =~s/::country::/$row->{country_name}/;
	$medalsblockcopy =~s/::gold::/$row->{country_oldgold}/;
	$medalsblockcopy =~s/::silver::/$row->{country_oldsilver}/;
	$medalsblockcopy =~s/::bronze::/$row->{country_oldbronze}/;
	$medalsblockcopy =~s/::total::/$row->{totalla}/;
	$medalsblockcopy =~s/::counter::/$i+1/e;

	$medalsfiller.=$medalsblockcopy;
    }
    $sth->finish;

    open(MYCLOWN, ">$htmldir/includes/games2000_medals1.ssi");
    print MYCLOWN $medalsfiller;
    close(MYCLOWN);
    chmod 0666, "$htmldir/includes/games2000_medals1.ssi";


    $medalsfiller = "";
    $sth = $dbh->prepare("select *, (country_oldgold+country_oldsilver+country_oldbronze) as totalla from countrys order by totalla desc");
    $sth->execute;
    
    for( $i=0; $i<9; $i++) {
	$row = $sth->fetchrow_hashref;
	$medalsblockcopy = $tpl;
    
	$medalsblockcopy =~s/::country::/$row->{country_name}/;
	$medalsblockcopy =~s/::gold::/$row->{country_oldgold}/;
	$medalsblockcopy =~s/::silver::/$row->{country_oldsilver}/;
	$medalsblockcopy =~s/::bronze::/$row->{country_oldbronze}/;
	$medalsblockcopy =~s/::total::/$row->{totalla}/;
	$medalsblockcopy =~s/::counter::/$i+1/e;

	$medalsfiller.=$medalsblockcopy;
    }
    $sth->finish;

    open(MYCLOWN, ">$htmldir/includes/games2000_medals2.ssi");
    print MYCLOWN $medalsfiller;
    close(MYCLOWN);
    chmod 0666, "$htmldir/includes/games2000_medals1.ssi";




    $dbh->disconnect;
    
    $magicteleport =~s/::whereto::/edit_medals.pl?action=main&recordid=$lastinsertid/;
    print $magicteleport;

#    show_main();
}



