#!/usr/bin/perl

require "/home/oc/cgi-bin/CGI.pl";
use Date::Format;               # For time2str()

$RESULTS="/home/oc/vote/results";
$NEW_RES="/home/oc/vote/results.new";
$DATA_DIR="/home/oc/vote";

$VOTE_FILE1="/home/oc/www/vote/vote1.shtml";
$VOTE_FILE2="/home/oc/www/vote/vote2.shtml";

$ERROR_FILE="/home/oc/www/vote/error";

$TMP="/home/oc/www/tmp/vote";
$TMP_WWW="/tmp/vote";

$VOTE_FOR;	# we need these two to be global
$VOTE_ITEM;

sub Error {
        $out_file=time2str("%T.shtml",time());
        $output="$TMP/$out_file";
        open(NEW,">$output");
        open(HTML,"$ERROR_FILE$_[0].shtml") or die("cannot open error file");
        while ($line=<HTML>) {
                $line=~s/VOTE_FOR/$VOTE_FOR/g;
                $line=~s/VOTE_ITEM/$VOTE_ITEM/g;
                $line=~s/RESULTS/$RESULTS/g;
                print NEW $line;
        }
        close(HTML);
        close(NEW);

	print "Location: $TMP_WWW/$out_file\n\n";
	exit;	
};

sub Define_Vote_Vars {
	if ((!defined $::FORM{'for'}) || (!defined $::FORM{'item'})) {
		Error(3);
	}
	$VOTE_FOR=$::FORM{'for'};
	$VOTE_ITEM=$::FORM{'item'};		
}

sub Vote {
	Get_Cookies();
	Define_Vote_Vars(0);
	if (defined $::COOKIES{$VOTE_FOR} || defined $::COOKIES{"for_".$VOTE_FOR}) {
		Error(1);	# already vote 
	}
	system("rm $TMP/*");
	$out_file=time2str("%T.shtml",time());
	$output="$TMP/$out_file";
	open(NEW,">$output");
	open(HTML,$VOTE_FILE1);
	while ($line=<HTML>) {
		$line=~s/VOTE_FOR/$VOTE_FOR/g;
		$line=~s/VOTE_ITEM/$VOTE_ITEM/g;
		print NEW $line;
	}
	close(NEW);
	close(HTML);
	print "Location: $TMP_WWW/$out_file\n";	
	Set_Cookies("for_".$VOTE_FOR,"yes");
	print "\n\n";
}

sub Finish_Vote {
	Get_Cookies();
	Define_Vote_Vars(1);
	if ((!defined $::COOKIES{"for_".$VOTE_FOR}) ||
	    (defined $::COOKIES{$VOTE_FOR})) {
		Error(2);
	}
	if (index($::FORM{"email"},'@')>0 && index($::FORM{"email"},'.')>2) {
		open(EARC,">>$DATA_DIR/$VOTE_FOR.emails");
		print EARC "$::FORM{'email'}\n";
		close(EARC);
	}
	$writen=0;
	open(NEW_RES,">$NEW_RES");
	open(RESULTS,$RESULTS);
	while ($line=<RESULTS>) {
		$line=~s/\n//g;
		($what,$item,$count)=split("\t",$line);
		if (lc($what) eq lc($VOTE_FOR) && $item==$VOTE_ITEM) {
			$writen=1;
			$count++;
		}
		print NEW_RES "$what\t$item\t$count\n";
	}
	if ($writen!=1) {
		$count=1;
		print NEW_RES "$VOTE_FOR\t$VOTE_ITEM\t$count\n";
	}  
	close(RESULTS);
	close(NEW_RES);
	system("mv $NEW_RES $RESULTS");

	$out_file=Print_Results();
	print "Location: $TMP_WWW/$out_file\n";
	if (!SetCookieExpDate("Wdy, 01-Jan-2009 01:01:01 GMT")) {
		exit;
	};
	Set_Cookies($VOTE_FOR,"yes");
	print "\n\n";
}

sub Print_Results {
	
	$VOTE_FOR=$::FORM{'for'};	
	open(RES,$RESULTS);
	while ($line=<RES>) {
		($for,$item,$count)=split("\t",$line);
		if (lc($for) eq lc($VOTE_FOR)) {
			$RES{$item}->{'COUNT'}=$count;
		}
	}
	close(RES);

	open(CONF,"$DATA_DIR/$VOTE_FOR.conf") or die("Can not open $VOTE_FOR.conf file");
	$RES{'MAX_ROWS'}=0;
	while ($line=<CONF>) {
		if (index($line,"\t")>0) {
			($item,$author,$desc)=split("\t",$line);
			$RES{$item}->{'AUTHOR'}=$author;
			$RES{$item}->{'DESC'}=$desc;
			if ($item>$RES{'MAX_ROWS'}){
				$RES{'MAX_ROWS'}=$item;
			}
		}
	}
	close(CONF);

	$RESULTS ="<table cellpadding=2 cellspacing=1 border=0 bgcolor=#ffffff>";
	$RESULTS.="<tr bgcolor=#f0f0f0><td>Item</td> <td>Author</td> <td>Link</td> <td>Count</td> </tr>";
	($color1,$color2)=("#ffffff","#f8f8f0");
#	foreach $i (keys %RES) {
	for ($i=1; $i<=$RES{'MAX_ROWS'}; $i++) {
		($color1,$color2)=($color2,$color1);
		if (!defined $RES{$i}->{'COUNT'}) {
			$RES{$i}->{'COUNT'} = "0";
		}
		$RESULTS.="<tr bgcolor=$color1><td align=right>$i&nbsp;</td><td>$RES{$i}->{'AUTHOR'}&nbsp;</td> <td>$RES{$i}->{'DESC'}&nbsp;</td> <td><center>$RES{$i}->{'COUNT'}</td> </tr>";
	}
	$RESULTS.="</table>";
	if ($::FORM{'redirect'} eq 'no') {
 		print "Content-Type: text/html\n\n\n";
		print $RESULTS;
		return;
        }
	$out_file=time2str("%T.shtml",time());
        $output="$TMP/$out_file";
        open(NEW,">$output");
	open(HTML,$VOTE_FILE2);
	while ($line=<HTML>) {
		$line=~s/VOTE_FOR/$VOTE_FOR/g;
		$line=~s/VOTE_ITEM/$VOTE_ITEM/g;
		$line=~s/RESULTS/$RESULTS/g;
		print NEW $line;
	}
	close(HTML);
	close(NEW);

	return $out_file;
}

sub Results {
	$out_file=Print_Results();
        if ($::FORM{'redirect'} ne 'no') {
    	    print "Location: $TMP_WWW/$out_file\n\n\n";
	}
}

sub Main {
	Vote() if (lc($::FORM{'action'}) eq "vote");
	Finish_Vote() if (lc($::FORM{'action'}) eq "finish_vote");
	Results() if (lc($::FORM{'action'}) eq "results");
}

Main();