#!/usr/bin/perl

use DBI;

if( $^O=~/linux/ ) {
    require "/usr/local/apache/cgi-bin/games2000/location.set";
} else {
    require "/bhome/part2/01/vh01002/vcgi/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. Tell 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/games2000_res_fn.shtml") || die "Cannot open template: $tplpath/games2000_res_fn.shtml";
    $tpl=<T>; close(T);


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

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


    ($resultsblock) = $tpl=~/::resultsloop(.+?)resultsloop::/s;
    $resultsfiller="";

    $searchstring = "";
    if( $formdata{mysport} ne "" ) { $searchstring.= " and sportday_sport=$formdata{mysport}"; }
    if( $formdata{mydate} ne "" ) { $searchstring.= " and sportday_date=$formdata{mydate}"; }
    if( $formdata{mygender} ne "" ) { $searchstring.= " and sportday_gender=$formdata{mygender}"; }
    if( $formdata{mystage} ne "" ) { $searchstring.= " and sportday_final=$formdata{mystage}"; }
    if( $formdata{mycountry} ne "" ) { $searchstring.= " and sportday_results like '%$formdata{mycountry}%'"; $searcher = "($formdata{mycountry}|::al::)"; }



    $sth = $dbh->prepare("select * from sportday, sports, dates where sportday_sport=sport_id and sportday_date=date_id and sportday_results<>'' $searchstring  order by sportday_date, sportday_sport, sportday_name");
    $sth->execute();
    
    @genderbender = ( "мужчины", "женщины", "микст" );

    if( $formdata{mycountry} ne "" ) {
	for( $i=0; $i<$sth->rows; $i++) {
	    $row = $sth->fetchrow_hashref;

	    $resultscopy = $resultsblock;

	    ($myyear, $mymonth, $myday ) = split/\D/, $row->{date_date};
	    $resultscopy =~s/::resultsdate::/$myday\.$mymonth\.00/;
	    $resultscopy =~s/::resultssportname::/$row->{sport_name}/;
	    $resultscopy =~s/::resultssportdayname::/$row->{sportday_name}/;
	    $resultscopy =~s/::resultsgender::/$genderbender[$row->{sportday_gender}]/;

	    @resultlines = split/\n/, $row->{sportday_results};
	    $myliner="";
	    for( $j=0; $j < scalar(@resultlines); $j++) {
		if( $resultlines[$j]=~s/$searcher//g ) { 
		    $myliner.="$resultlines[$j]<br>"; 
		}
	    }
	    $resultscopy =~s/::resultstext::/$myliner/;
	    $resultsfiller.=$resultscopy;
	}
    } else {
	for( $i=0; $i<$sth->rows; $i++) {
	    $row = $sth->fetchrow_hashref;

	    $resultscopy = $resultsblock;

	    ($myyear, $mymonth, $myday ) = split/\D/, $row->{date_date};
	    $resultscopy =~s/::resultsdate::/$myday\.$mymonth\.00/;
	    $resultscopy =~s/::resultssportname::/$row->{sport_name}/;
	    $resultscopy =~s/::resultssportdayname::/$row->{sportday_name}/;
	    $resultscopy =~s/::resultsgender::/$genderbender[$row->{sportday_gender}]/;
	    
	    $row->{sportday_results} =~s/\n/<br>/g;
	    $row->{sportday_results} =~s/::\w\w:://g;
	    $resultscopy =~s/::resultstext::/$row->{sportday_results}/;
	    $resultsfiller.=$resultscopy;
	}
    }
    
    $tpl=~s/::sportsel$formdata{mysport}::/selected/;
    $tpl=~s/::sportsel\d+:://g;

    $tpl=~s/::datesel$formdata{mydate}::/selected/;
    $tpl=~s/::datesel\d+:://g;

    $tpl=~s/::finalsel$formdata{mystage}::/selected/;
    $tpl=~s/::finalsel\d+:://g;

    $tpl=~s/::gendersel$formdata{mygender}::/selected/;
    $tpl=~s/::gendersel\d+:://g;

    $tpl=~s/::countrysel$formdata{mycountry}::/selected/;
    $tpl=~s/::countrysel\d+:://g;


    $tpl=~s/::resultsloop(.+?)resultsloop::/$resultsfiller/s;

    open(T,"$htmldir/scrollah.shtml") || die "Cannot open template: $htmldir/scrollah.shtml";
    $atpl=<T>; close(T);

    $tpl=~s/::ssigoeshere::/$atpl/;

    print $tpl;

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


