#!/usr/bin/perl

my $wordcount=2360;
my $finish=50000;
my $pacerstart=1009861200;
my $pacerend=1012539600;
my $pacerlength=$pacerend-$pacerstart;
my $stamp=time();
my $pacerpos=$stamp-$pacerstart;
my $pacercount=int($finish*$pacerpos/$pacerlength);
if ($pacercount>$finish) {$pacercount=$finish;}
my $realwordcount=$wordcount;
if ($wordcount>$finish) {$wordcount=$finish;}

my $q=$ENV{'QUERY_STRING'};
my @people;
my $head='';
my $foot='';

if ($q=~s/^Update//i) {
 my ($user,$words,$pass);
 if ($q=~/^\&(.*?)=(.*?)\&([^=]*?)$/i) {
  ($user,$words,$pass)=($1,$2,$3);
 } else {
  my @parts=split(/&/,$q);
  my $part;
  foreach $part (@parts) {
   my ($a,$b)=split(/=/,$part);
   if ($a=~/username/i) {$user=$b;}
   elsif ($a=~/wordcount/i) {$words=$b;}
   elsif ($a=~/password/i) {$pass=$b;}
  }
 }
 print "Content-type: text/html\n";
 if ($pass) {
  $pass=~s/[^a-zA-Z0-9]//g;
  $user=~s/[^a-zA-Z0-9\-]//g;
  print "Set-Cookie: novelpass=$pass; path=/; expires=Mon, 01-Jan-2010 00:00:00 GMT\n";
  print "Set-Cookie: noveluser=$user; path=/; expires=Mon, 01-Jan-2010 00:00:00 GMT\n";
 } elsif ($words) {
  my @cookies=split(/; /,$ENV{'HTTP_COOKIE'}||'');
  my $part;
  foreach $part (@cookies) {
   my ($a,$b)=split(/=/,$part);
   if ($a eq 'novelpass') {$pass=$b;}
   if ($a eq 'noveluser') {$user=$b;}
  }
 } 

 while (length($user)<10) {$user.=' ';}
 while (length($pass)<10) {$pass.=' ';}
 while (length($words)<10) {$words.=' ';}
 if (length($words)>10) {print "Bloody liar."; exit;}
 open (HANDLE,'+<wordcounts.txt');
 seek (HANDLE,0,0);
 while (read(HANDLE,$buf,20)) {
  if ($buf eq ($user.$pass)) {
   seek(HANDLE,tell(HANDLE),0);
   print HANDLE $words;
  }
  <HANDLE>;
 }
 close(HANDLE);
# @people=('Pacer',$user);
 $q='';
 print "\n";
} else {
 print "Content-type: text/html\n\n";
}

my $tablewidth="90%";

if (!($q=~s/^\&//)) {
 $head='<HTML><HEAD>
<STYLE TYPE="text/css">
<!--
td {font-size:10pt; font-family:Verdana,sans-serif;}
-->
</STYLE>
</HEAD><BODY BGCOLOR="#444444">
';
 $foot='
</BODY></HTML>';
 $tablewidth="100%";
}

my $divisions=1;
while ($q=~s/^\///) {
 $divisions++;
}

if ($#people==-1) {@people=split(/=/,$q);}
open(HANDLE,'wordcounts.txt');
my @data=<HANDLE>;
close(HANDLE);

if ($#people==-1) {
 push(@people,'Pacer');
 grep {if ($_=~/^([A-Za-z]{1,10})/) {push(@people,$1);}} @data;
}

print $head.'<DIV ALIGN="CENTER" STYLE="margin:0px; padding:0px;">
<TABLE WIDTH="'.$tablewidth.'" BORDER=0 CELLPADDING=2 CELLSPACING=0><TR><TD ALIGN="CENTER" BGCOLOR="#444444" COLSPAN='.$divisions.' STYLE="color:#ffffff; font-size:10px;">Projects Status</TD></TR>';

my $subdiv=$divisions-1;
my $subdivwidth=int(100/$divisions);
foreach $person (@people) {
 if ($subdiv==$divisions-1) {print "<TR>";}
 print '
<TD WIDTH="'.$subdivwidth.'%" BGCOLOR="#444444">
';
 if ($person=~/Pacer/i) {
  print &ProgressBar($pacercount,$finish,500,'#ffffff','#000000','Pacer: '.$pacercount).'
</TD>';
 } else {
  my $wordcount=0; my $colour='#ffffff'; my $myfinish='0=words&50000=done';
  grep {if ($_=~/^$person/) {$wordcount=substr($_,20,9); $colour=substr($_,30,7); $myfinish=substr($_,38,1024); chomp $myfinish;}} @data;
  print &ProgressBar(int($wordcount),$myfinish,500,$colour,'#000000',$person.': '.int($wordcount)).'</TD>';
 }
 if (!$subdiv) {
  print "</TR>";
  $subdiv=$divisions;
 }
 $subdiv--;
}

print '</TABLE></DIV>'.$foot;


sub ProgressBar {
 my ($pos,$myfinish,$size,$color1,$color2,$text)=@_;
 my $max=0;
 my $currentstage='';
 my $nextstage='';
 $size=100;
 {
  my @parts=split(/\&/,$myfinish);
  my $part;
  my $lowestundone=999999;
  my $highestdone=0;
  foreach $part (@parts) {
   my ($a,$b)=split(/=/,$part);
   if ($a>$max) {$max=$a;}
   if ($a>=$highestdone and $pos>=$a) {$currentstage=$b; $highestdone=$a;}
   if ($a<$lowestundone and $pos<$a) {$nextstage=$b.', at '.$a; $lowestundone=$a;}
  }
 }
 my $ret='<TABLE WIDTH="'.$size.'%" BORDER=0 CELLSPACING=0 CELLPADDING=1><TR>';
 my $xpos=int($pos*$size/($max||999999));
 my $units='';
 if ($max==100) {$units.='%';}
 elsif ($max>=50000) {$units.=' words';}
 $text.=$units;
 $text='<A TITLE="Currently doing: '.$currentstage.' - next stage: '.$nextstage.$units.'">'.$text.'</A>';
 if ($pos>$max/2) {
  $ret.='
<TD ALIGN="RIGHT" BGCOLOR="'.$color1.'" STYLE="color:'.$color2.'; font-size: 10px;">'.$text.'</TD>';
  if ($size-$xpos>6) {
   $ret.='
<TD WIDTH="'.int(($size-$xpos)*100/$size).'%" BGCOLOR="'.$color2.'" STYLE="font-size: 10px;">&nbsp;</TD>';
  }
 } else {
  if ($xpos>0) {
   $ret.='
<TD WIDTH="'.int($xpos*100/$size).'%" BGCOLOR="'.$color1.'" STYLE="font-size: 10px;">&nbsp;</TD>';
  }
  $ret.='
<TD BGCOLOR="'.$color2.'" STYLE="color:'.$color1.'; font-size: 10px;">'.$text.'</TD>';
 }
 $ret.='</TR></TABLE>';
 return $ret;
}

