#!/usr/bin/perl -w

# an rdiff-backup statistics generating script
#
# dean gaudet <dean@arctic.org>
#
# this code is public domain.

use strict;

my $dir = shift or die "usage: $0 mirror-directory [date_tag]\n";

$dir="$dir/rdiff-backup-data";
-d "$dir" or die "$0: $dir is not a directory\n";

my $tag = shift;
if (!defined($tag)) {
	my @current = sort <$dir/current_mirror.*.data>;
	$tag = $current[$#current];
	$tag =~ s#.*/current_mirror.(.*).data#$1#;
}
print "Statistics for $dir $tag:\n";

print "\nSession statistics:\n";
my $sfile = "$dir/session_statistics.$tag.data";
open(SFILE, "<$sfile") or die "$0: unable to read $sfile: $!\n";
print <SFILE>;
close(SFILE);


print "\nAverage statistics:\n";
system("rdiff-backup --calculate-average $dir/session_statistics.*");


my $limit = 4;
print "\nDirectory statistics (total of new or inc filesizes over ${limit}MB):\n";
my $fsfile = "$dir/file_statistics.$tag.data.gz";
-r $fsfile or die "$0: unable to read $fsfile: $!\n";
open(FS, "zcat '$fsfile' |") or die "unable to fork zcat $fsfile: $!\n";

my $line = <FS>;
chomp($line);
$line eq '# Format of each line in file statistics file:' or die "$0: unexpected first line\n";
$line = <FS>;
chomp($line);
$line eq '# Filename Changed SourceSize MirrorSize IncrementSize' or die "$0: unexpected second line\n";

my $warnings = 0;
my %newfile;
my %incfile;

while (<FS>) {
	chomp;
	my ($filename, $changed, $ssize, $msize, $isize) = m#^(.*) (\S+) (\S+) (\S+) (\S+)$#;
	if (!defined($isize)) {
		warn "unable to parse: $_\n";
		++$warnings;
		if ($warnings == 100) {
			die "too many warnings\n";
		}
		next;
	}

	my $newsize = ($ssize ne 'NA' and $msize eq 'NA') ? $ssize : 0;
	my $incsize = ($isize ne 'NA') ? $isize : 0;
	my $parent = $filename;
	while ($parent =~ s#(.*)/[^/]+#$1#) {
		if (!defined($newfile{$parent})) {
			$newfile{$parent} = 0;
			$incfile{$parent} = 0;
		}
		$newfile{$parent} += $newsize;
		$incfile{$parent} += $incsize;
	}
}

my $key;
foreach $key (sort { $newfile{$b} <=> $newfile{$a} } keys %newfile) {
	last if ($newfile{$key} < $limit*1000000);
	printf "new %12u %s\n", $newfile{$key}, $key;
}

foreach $key (sort { $incfile{$b} <=> $incfile{$a} } keys %incfile) {
	last if ($incfile{$key} < $limit*1000000);
	printf "inc %12u %s\n", $incfile{$key}, $key;
}
