#!/usr/local/bin/perl

use CGI;
use GD::Graph::pie;
#Setting a Title for Piechart
use constant TITLE => "Comparing your choice with other choices using piechart";
#Assigning Flock parameters for Locking and Unlocking the files
$lock=2;
$sharedlock=1;
$unlock=8;
#Reading the User Input and storing it in form hash 
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs=split(/&/, $buffer);
foreach $pair (@pairs) {
     ($name,$value)= split(/=/, $pair);
      $value =~ tr/+/ /;
      $value =~ s/%([a-fA-F0-9])/pack("C", hex($1))/eg;
      $FORM{$name} =$value;
}

#opening data file data.txt using filehandler FILE

open FILE, "<data.txt";
#obtaining a shared Lock on  the File handler Using perl flock function
flock(FILE,$sharedlock);
#reading the contents of file in array lines
while(<FILE>){
    push @lines,$_;
 }
#unlocking the file
flock(FILE,$unlock);
#closing the file
close FILE;
#storing the data from data.txt in to DATA hash
foreach $line (@lines) {
chomp($line);
($sport,$number)= split(/,/, $line);
      $DATA{$sport} =$number;
}
#Checking the user input and incrementing the data according to the user input
#and storing the values in scalar variables  for computing the percentages 
foreach $key (keys(%FORM)) {
foreach $selection (keys(%DATA)) {
if($selection eq $FORM{$key})
{
$DATA{$selection}+=1;
}
if($selection eq "Cricket")
{
	$cricket=$DATA{$selection};
}
elsif($selection eq "Soccer")
{
	$soccer=$DATA{$selection};
}
elsif($selection eq "Tennis")
{
	$tennis=$DATA{$selection};
}
elsif($selection eq "Golf")
{
	$golf=$DATA{$selection};
}
elsif($selection eq "Hockey")
{
	$hockey=$DATA{$selection};
}

}
}
#calculating the percentage values for each choice after considering the user input
$total=$cricket+$soccer+$golf+$tennis+$hockey;
$cricket= sprintf "%.2f",($cricket/$total)*100;
$soccer= sprintf "%.2f",($soccer/$total)*100;
$tennis= sprintf "%.2f",($tennis/$total)*100;
$golf= sprintf "%.2f",($golf/$total)*100;
$hockey= sprintf "%.2f",($hockey/$total)*100;
#opening the file data.txt in   read,writ mode to  updated data
open FILE, "+<data.txt";
#locking the filehandler FILE with exclusive lock
flock(FILE,$lock);
#after obtaining the exclusive lock truncating the file 
seek(FILE,0,0);
truncate(FILE,0);
#writing the new data in to the data.txt
foreach $selection(keys(%DATA)){
print FILE "$selection,$DATA{$selection}\n";
}
#after updating the file releasing the lock on file data.txt
flock(FILE,$unlock);
#closing the filehandler
close FILE;


#creating the pie chart using the graphics module GD::Graph::pie
#creating the CGI object 
$q=new CGI;
$chart= new GD::Graph::pie(600,600);
#providing the data to pie chart from values computed above  
@data=(
["cricket=$cricket%", "soccer=$soccer%", "golf=$golf%", "tennis=$tennis%", "hockey=$hockey%"],
[ $cricket,$soccer,$golf,$tennis,$hockey ] 
);


$chart->set(
title  =>TITLE,
'3d' =>0,label =>"piechart"
);
#ploting  the graph and the storing the image object in gd_image
$gd_image =$chart->plot( \@data);
print $q->header(-type=>"image/png", -expires=>"-1d");

print $gd_image->png;
