#/usr/bin/perl
use strict;
use warnings;

use LWP::Simple;

my $list_base = 'http://list.georgialibraries.org/pipermail/';
my $dev_list = 'open-ils-dev/';
my $gen_list = 'open-ils-general/';

my $irc_base = 'http://evergreen-ils.org/irc_logs';
my @years = ('2009', '2010', '2011');
my @months = ('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');

sub get_irc_stats {
    foreach my $year (@years) {
        my $year_total = 0;
        my $month_cnt = 0;
        foreach my $month (1..12) {
            my $month_total = 0;
            foreach my $channel ('evergreen', 'openils-evergreen') {
                my $url = sprintf("$irc_base/$channel/%s-%02d", $year, $month);
                my $content = get($url) or print "$url failed: $year $month: $!\n";
                my @logs = $content =~ m!<a href="(%23evergreen.*?)"!gs;
                foreach my $logfile (@logs) {
                    # print "$logfile\n";
                    my $day = get("$url/$logfile") or print "$url failed: $!\n";
                    my $cnt = grep(/^.*?:\d\d  <.*?>.*$/, split(/\n/, $day));
                    $month_total += $cnt;
                }
            }
            print "$year\t$month\t$month_total\n";
            $year_total += $month_total;
            $month_cnt++;
        }
        print "$year total = $year_total\n";
        printf("$year average = %d\n", $year_total / $month_cnt);
    }
}

sub get_message_stats {

    my $list_name = shift;
    foreach my $year (@years) {
        my $total = 0;
        my $cnt = 0;
        foreach my $month (@months) {
            my $url = $list_base . $list_name . "$year-$month/thread.html";
            # print "$url\n";
            my $content = get($url) or print "$url failed; $year $month: $!\n";
            if ($content) {
                $cnt++;
                $content =~ s{^.*?<b>Messages:</b>\s*(\d+).*?$}{$1}s;
                $total += int($content);
                print "$year\t$month\t$content\n";
            }
        }
        printf("$year total: %d\n", $total);
        printf("$year monthly average: %d\n", $total / $cnt);
    }
}

#print "IRC stats\n";
#get_irc_stats();
print "Dev list\n";
get_message_stats($dev_list);
print "General list\n";
get_message_stats($gen_list);

