#!/usr/bin/env perl

use warnings;

use Getopt::Std;
use DateTime;

getopts('a:e:m:',\%opt);

%months = ('Jan' => 1, 'Feb' => 2, 'Mar' => 3, 'Apr' => 4,
	   'May' => 5, 'Jun' => 6, 'Jul' => 7, 'Aug' => 8,
	   'Sep' => 9, 'Oct' => 10, 'Nov' => 11, 'Dec' => 12);
$i = 0;
if (exists($opt{a})) {
    print STDERR "opening $opt{a}\n";
    open(A,"$opt{a}");
    while (<A>) {
	chomp;
#	print STDERR "$i\n";
	if ($_ =~ /POST \/nebulous HTTP\/1.1/) { # This is a nebulous query
	    ($ip_string,$date_data_string) = split /\[/; # ]/;
	    ($date_string,$data_string) = split /\]/, $date_data_string;
#	    $data_string =~ s/\"//g;
#	    ($request_string,$http_string) = split /\"/, $data_string;
#	    ($http_code,$bytes) = split /\s+/, $http_string;
#	    ($ip_addr,$user,$pass) = split /\s+/, $ip_string;
	    ($date_string2,$tz) = split /\s+/, $date_string;
	    ($date,$hour,$min,$sec) = split /:/, $date_string2;  #/;
	    ($day,$month,$year) = split /\//, $date;
	    $month = $months{$month};
	    count_it("apache",$year,$month,$day,$hour,$min,$sec);
	}
	$i++;
    }
    close(A);
    print STDERR "closing $opt{a}\n";
}
if (exists($opt{e})) {
    print STDERR "opening $opt{e}\n";
    open(E,"$opt{e}");
    while (<E>) {
	chomp;
	if ($_ =~ /\] \[/) {
	    ($date_string,$message_string) = split /\] \[/; # ]/;
	    $date_string =~ s/\[//;
	    ($dow,$month,$day,$time_string,$year) = split /\s+/, $date_string;
	    ($hour,$min,$sec) = split /:/, $time_string; #/;
	    $month = $months{$month};
#	print "$_ -> $year $month $day $hour $min $sec\n";
	    count_it("apache_error",$year,$month,$day,$hour,$min,$sec);
	}
    }
    close(E);
    print STDERR "closing $opt{e}\n";
}
if (exists($opt{m})) {
    print STDERR "opening $opt{m}\n";
    open(M,"$opt{m}");
    ($year,$month,$day,$hour,$min,$sec) = (1970,1,1,0,0,0);
    while (<M>) {
	chomp;
	if ($_ =~ /^# Time:/) {  # This is a time statement
	    ($trash,$trash2,$date_string,$time_string) = split /\s+/;
	    $year = int($date_string / 10000);
	    $month = int( ($date_string - ($year * 10000)) / 100);
	    $day   = int( ($date_string - ($year * 10000) - ($month * 100)));
	    $year += 2000;
	    ($hour,$min,$sec) = split /:/, $time_string; #/;
#	    print "$date_string >> $time_string :: $year $month $day $hour $min $sec\n";
	}
	elsif ($_ =~ /^# Query_time:/) { # This is an increment at that time;
	    count_it("mysql_slow",$year,$month,$day,$hour,$min,$sec);
	}
    }
    close(M);
    print STDERR "closing $opt{m}\n";
}

print "#epoch\t";
foreach $tag (keys %tags) {
    print "$tag\t";
}
print "\n";
foreach $time (sort {$a <=> $b} (keys %data)) {
    print "$time\t";
    foreach $tag (keys %tags) {
	unless (exists($data{$time}{$tag})) {
	    $data{$time}{$tag} = 0;
	}
	print "$data{$time}{$tag}\t";
    }
    print "\n";
}


sub count_it {
    my $tag = shift;
    my $year = shift;
    my $month = shift;
    my $day = shift;
    my $hour = shift;
    my $min = shift;
    my $sec = shift;
    
    # Truncate to hours;
    $sec = 0;
    $min = int($min / 10) * 10;
#    $min = 0;
    my $dt = DateTime->new( year => $year, month => $month, day => $day, hour => $hour, minute => $min, second => $sec);
    my $time = $dt->epoch;

    $data{$time}{$tag} += 1;
    $tags{$tag} = 1;
}
