#! /usr/bin/perl
use RRDs;
use Date::Parse;

my $mbox = shift;
my $dbpath = "/var/lib/listgraph/";

my $basename=$mbox;
$basename=~s/^.*\///g; # filename
$basename=~s/\.stats$//g; # extension
$basename=~s/\.mbox$//g; # extension

my %cnt;
open(INPUT, "< $mbox") or die 'can not open mbox file';
foreach (grep(/^From /, <INPUT>)) {
	~m/^From ([^ ]*) (.*)$/;
	#print "N:".$1." -- D:$2 \n";
	my $t=str2time($2);
	my $d = int($t/86400);
	$cnt{$d}= 1 + $cnt{$d};
}

@dates= sort keys %cnt;
$start = @dates[0];
$end = @dates[-1];

my $t;

$rrd=$dbpath.$basename.".rrd";
RRDs::create ($rrd, "--start",((int($start)-1)*86400), "--step",86400,
	      "DS:messages:GAUGE:100000:U:U",
	      "RRA:AVERAGE:0.5:1:3650",
	      "RRA:AVERAGE:0.5:7:500",
	      "RRA:MIN:0.5:1:3650",
	      "RRA:MAX:0.5:1:3650",
	      "RRA:MIN:0.5:7:500",
	      "RRA:MAX:0.5:7:500",
);

my $ERROR = RRDs::error;
die "$0: unable to create `$rrd': $ERROR\n" if $ERROR;


for ($t=$start; $t <=$end ;$t++) { 
	RRDs::update $rrd, int($t*86400).":".int($cnt{$t});
	if ($ERROR = RRDs::error) {
		die "$0: unable to update `$rrd': $ERROR\n";
	}
}
