#!/bin/perl
# overview.pl

use strict;

use CGI qw(:standard);
use myHTML;

#print header();
my ($t1,$mi,$hh,$dd,$mo,$yy,$t2)=localtime;
      $mo++;
      $mo = '0' . $mo if ($mo <10);
      $dd = '0' . $dd if ($dd <10);
      $yy+=1900;

my $show = param('show');

p2	"<html>",
	" <head>",
	"  <title>Übersicht</title>",
	" </head>",
	" <body>",

	"  <table border='1'>",
	"   <tr>",
	"    <td>count</td>",
	"    <td>flag</td>",
	"    <td>pic of day</td>",
	"   </tr>",
	"   <tr>",
	"    <td>";

	print_count();

p2     "    </td>",
	"    <td>";

	print_flag();

p2     "    </td>",
	"    <td>";

	print_pic_of_day();

p2     "    </td>",
	"   </tr>",
	"  </table>",
	"  <br>";

p2	"  <table border='1'>",
	"   <tr>";

my @act = ("votings","count_vote_hist","zip_comment","bild_log","download_log","google_log","hilfe_log","info_log","tellafriend_log","voting_log");

foreach my $act (@act) {
	print "<td>";
	if ($show ne "$act") {
		print	"<a href='overview.pl?show=$act'>$act</a> ";
	}
	else {
		print	"$act ";
	}
	print " (" . log_count_today("$act") . ")" if ($act ne "votings" and $act ne "zip_comment" and $act ne "count_vote_hist");
	print "</td>\n";
}

p2	"   </tr>",
	"  </table>",
	"  <br>";

print_voting()          if ($show eq "votings");
print_count_vote_hist() if ($show eq "count_vote_hist");
print_zip_comment()     if ($show eq "zip_comment");
print_bild_log()        if ($show eq "bild_log");
print_download_log()    if ($show eq "download_log");
print_google_log()      if ($show eq "google_log");
print_hilfe_log()       if ($show eq "hilfe_log");
print_info_log()        if ($show eq "info_log");
print_tellafriend_log() if ($show eq "tellafriend_log");
print_voting_log()      if ($show eq "voting_log");

p2	" </body>",
	"</html>";

sub print_count {
   my $dp   = my_dat_path();
   my $file = $dp . "count";
   open FILE, "<$file";
   my $content=<FILE>;
   close FILE;
   p2	"     $content";
}

sub print_flag {
   my $dp   = my_dat_path();
   my $file = $dp . "flag";
   open FILE, "<$file";
   my $content =<FILE>;
   close FILE;
   my ($d, $m, $y) = split /-/,$content;
   $m++;
   $y+=1900;
   p2	"$d.$m.$y";
}

sub print_pic_of_day {
   my $dp   = my_dat_path();
   my $file = $dp . "pic_of_day";
   open FILE, "<$file";
   my $content =<FILE>;
   close FILE;
   my ($link) = $content =~ /..\/(.*)/;
   p2 "<a href='http://www.bildergallerie.de/$link' target='bild'>$content</a>";
}

sub print_voting {
   my $dp1     = my_dat_path();
   my $file1   = $dp1 . "voting";

   open FILE, "<$file1";
   my @content =<FILE>;
   close FILE;


##### Welche Girls wurden heute gevoted?
   my $dp2     = my_log_path();
   my $file2   = $dp2 . "VOTING_actions.log";

   open FILE, "<$file2";
   my @vot_log =<FILE>;
   close FILE;

   my @voted;
   foreach (@vot_log) {
      @voted = (@voted, $_) if ($_ =~ /$dd\.$mo\.$yy/);
   }
   # 23.04.2006 17:19     VOTING Gabrielle Reece 2 gabrielle-reece-00001.jpg
################


   my @girls;
   p2 "<table border='1'>",
	"<tr><td>pos</td><td>name</td><td>avg</td><td>clicks</td><td>summe</td></tr>";
   my $all_count = 0;
   my $all_votings = 0;
   foreach (@content) {
      chomp;
      my ($n,$c,$v) = split /,/,$_;
      my $r = $v/$c;
      $all_count = $all_count + $c;
      $all_votings = $all_votings + $v;
      my $todayvoted = 0;
      foreach (@voted) {
         $todayvoted = 1 if ($_ =~ /$n/);
      }

      push @girls, $r . ',' . $n . ',' . $c . ',' . $v . ',' . $todayvoted;
   }
   my $all_ranks = $all_votings / $all_count;
   printf "<tr><td></td><td>&sum;</td><td>";
   printf "%3.2f", $all_ranks;
   printf "</td><td>$all_count</td><td>$all_votings</td></tr>\n";

   my @sorted = sort {$b <=> $a} @girls;
   my $pos=1;
   my $printed_avg_line=0;
   foreach (@sorted) {
      my ($r,$n,$c,$v,$t) = split /,/,$_;
      if ($r < $all_ranks and $printed_avg_line == 0) {
          print  "<tr><td>-</td><td>&sum;</td>";
          printf "<td>%3.2f</td>",$all_ranks;
          print  "<td>$all_count</td><td>$all_votings</td></tr>";
          $printed_avg_line=1;
      }
      printf "<tr><td>";
      printf "$pos";
      $pos++;
      printf "</td><td>";
      printf "<b>" if ($t == 1);
      printf "%s", $n;
      printf "</b>" if ($t == 1);
      printf "</td><td>";
      printf "<b>" if ($t == 1);
      printf "%3.2f", $r;
      printf "</b>" if ($t == 1);
      printf "</td><td>";
      printf "<b>" if ($t == 1);
      printf "%d", $c;
      printf "</b>" if ($t == 1);
      printf "</td><td>";
      printf "<b>" if ($t == 1);
      printf "%d", $v;
      printf "</b>" if ($t == 1);
      printf "</td></tr>\n";
   }
   p2 "</table>";
}

sub print_count_vote_hist {
   my $dp   = my_dat_path();
   my $file = $dp . "count_vote_hist";

   open FILE, "<$file";
   my @content=<FILE>;
   close FILE;

   p2 "<table border='1'><tr><td>Datum</td><td>Besucher</td><td>Votings</td><td>d Besucher</td><td>d Votings</td></tr>";

   my ($oldcount, $oldvote);
   my @table;
   foreach (@content) {
      chomp;
      my ($date, $count, $vote) = split /,/,$_;
      my $diffcount = $count - $oldcount;
      my $diffvote  = $vote  - $oldvote;
      @table = (@table, "<tr><td>$date</td><td>$count</td><td>$vote</td><td>$diffcount</td><td>$diffvote</td></tr>");
      $oldcount = $count;
      $oldvote  = $vote;
   }
   foreach (reverse @table) {
      p "$_";
   }
   p2 "</table>";
}

sub print_zip_comment {
   my $dp   = my_dat_path();
   my $file = $dp . "zip_comment";
   open FILE, "<$file";
   my @content=<FILE>;
   close FILE;
   foreach (@content) {
	print "$_<br>";
   }
}

sub print_bild_log {
   my $dp   = my_log_path();
   my $file = $dp . "BILD_actions.log";
   my @content;
   @content = `tail -n 30 $file`;
   foreach (reverse @content) {
      chomp;
      p2 "<b>" if ($_ =~ /$dd\.$mo\.$yy/);
      print "$_<br>\n";
      p2 "</b>" if ($_ =~ /$dd\.$mo\.$yy/);
   }
}

sub print_download_log {
   my $dp   = my_log_path();
   my $file = $dp . "DOWNLOAD_actions.log";
   open FILE, "<$file";
   my @content=<FILE>;
   close FILE;
   my $i=0;
   foreach (reverse @content) {
      p2 "<b>" if ($_ =~ /$dd\.$mo\.$yy/);
      print "$_<br>";
      p2 "</b>" if ($_ =~ /$dd\.$mo\.$yy/);
      $i++;
      last if ($i == 30);
   }
}

sub print_google_log {
   my $dp   = my_log_path();
   my $file = $dp . "GOOGLE_actions.log";
   open FILE, "<$file";
   my @content=<FILE>;
   close FILE;
   my $i=0;
   foreach (reverse @content) {
      p2 "<b>" if ($_ =~ /$dd\.$mo\.$yy/);
      print "$_<br>";
      p2 "</b>" if ($_ =~ /$dd\.$mo\.$yy/);
      $i++;
      last if ($i == 30);
   }
}

sub print_hilfe_log {
   my $dp   = my_log_path();
   my $file = $dp . "HILFE_actions.log";
   open FILE, "<$file";
   my @content=<FILE>;
   close FILE;
   my $i=0;
   foreach (reverse @content) {
      p2 "<b>" if ($_ =~ /$dd\.$mo\.$yy/);
      print "$_<br>";
      p2 "</b>" if ($_ =~ /$dd\.$mo\.$yy/);
      $i++;
      last if ($i == 30);
   }
}

sub print_info_log {
   my $dp   = my_log_path();
   my $file = $dp . "INFO_actions.log";
   open FILE, "<$file";
   my @content=<FILE>;
   close FILE;
   my $i=0;
   foreach (reverse @content) {
	p2 "<b>" if ($_ =~ /$dd\.$mo\.$yy/);
       print "$_<br>";
	p2 "</b>" if ($_ =~ /$dd\.$mo\.$yy/);
       $i++;
	last if ($i == 30);
   }
}

sub print_tellafriend_log {
   my $dp   = my_log_path();
   my $file = $dp . "TELLAFRIEND_actions.log";
   open FILE, "<$file";
   my @content=<FILE>;
   close FILE;
   my $i=0;
   foreach (reverse @content) {
	p2 "<b>" if ($_ =~ /$dd\.$mo\.$yy/);
       print "$_<br>";
	p2 "</b>" if ($_ =~ /$dd\.$mo\.$yy/);
       $i++;
	last if ($i == 30);
   }
}

sub print_voting_log {
   my $dp   = my_log_path();
   my $file = $dp . "VOTING_actions.log";
   open FILE, "<$file";
   my @content=<FILE>;
   close FILE;
   my $i=0;
   foreach (reverse @content) {
	p2 "<b>" if ($_ =~ /$dd\.$mo\.$yy/);
       print "$_<br>";
	p2 "</b>" if ($_ =~ /$dd\.$mo\.$yy/);
       $i++;
	last if ($i == 30);
   }
}

sub log_count_today {
   my ($f) = @_;
   my $dp = my_log_path();
   $f=~ s/_log//;
   $f=~ tr/[a-z]/[A-Z]/;
   $f=$f."_actions.log";
   my $file = $dp . $f;
  
   open FILE, "<$file";
   my @content = <FILE>;
   close FILE;

   my $count=0;
   foreach (@content) {
      $count++ if $_ =~ /$dd\.$mo\.$yy/;
   }

   return $count;
}
