#! /usr/bin/perl -w

use strict;

sub mkdatafile($$) {
	my ($globfmt, $output) = @_;
	my %res;
	my @files = glob($globfmt);
	for(@files) {
		m#(api|vg)/(\d{8})# or die;
		my $cust = $1;
		my $date = $2;
		open(FD, "$_") or die;
		while(<FD>) {
			if(/^\s*(2001:0::|2002::).*(?:6to4 \(RFC 3068\)|Teredo)\s+(\d+)/) {
				$res{$date}->{"Nd6-6to4"} += $2 if($1 eq "2002::");
				$res{$date}->{"Nd6-Teredo"} += $2 if($1 eq "2001:0::");
				next;
			}
			/^\[(N|Ns|Nd|Nd6)\].+?:\s+(\d+)/ or next;
			$res{$date}->{$1} += $2;
			
		}
		close(FD) or die;
	}

	open(OUTPUT, ">$output") or die "$!";
	print OUTPUT "# Date\t\tN\tNs\tNd\tNd6\tNd6-6to4\tNd6-Teredo\n";
	for(sort(keys(%res))) {
		print OUTPUT $_, "\t";
		print OUTPUT $res{$_}->{N} || 0, "\t";
		print OUTPUT $res{$_}->{Ns} || 0, "\t";
		print OUTPUT $res{$_}->{Nd} || 0, "\t";
		print OUTPUT $res{$_}->{Nd6} || 0, "\t";
		print OUTPUT $res{$_}->{"Nd6-6to4"} || 0, "\t\t";
		print OUTPUT $res{$_}->{"Nd6-Teredo"} || 0, "\n";

	}
	close(OUTPUT) or die "$!";
}

sub mkdatafile_osxversions($$) {
	my ($globfmt, $output) = @_;
	my %res;
	my @files = glob($globfmt);
	for(@files) {
		m#(api|vg)/(\d{8})# or die;
		my $cust = $1;
		my $date = $2;
		open(FD, "$_") or die;
		while(<FD>) {
			/^(.+?)\t\S+\t(\d+)$/ and $res{$date}->{$1} += $2;
		}
		close(FD) or die;
	}
	open(OUTPUT, ">$output") or die "$!";
	print OUTPUT "# Date\t\ttotal\t10.4\t10.5\t10.6.?\t10.6.0\t10.6.5\t10.6.8\t10.7\n";
	for(sort(keys(%res))) {
		# skip old file while regenerating
		next if(!$res{$_}->{"total"});
		print OUTPUT $_, "\t";
		print OUTPUT $res{$_}->{"total"} || 0, "\t";
		print OUTPUT $res{$_}->{"10.4"} || 0, "\t";
		print OUTPUT $res{$_}->{"10.5"} || 0, "\t";
		print OUTPUT $res{$_}->{"10.6.?"} || 0, "\t";
		print OUTPUT $res{$_}->{"10.6.0"} || 0, "\t";
		print OUTPUT $res{$_}->{"10.6.5"} || 0, "\t";
		print OUTPUT $res{$_}->{"10.6.8"} || 0, "\t";
		print OUTPUT $res{$_}->{"10.7"} || 0, "\n";
	}
	close(OUTPUT) or die "$!";
}

sub mkdatafile_operaversions($$) {
	my ($globfmt, $output) = @_;
	my %res;
	my @files = glob($globfmt);
	for(@files) {
		m#(api|vg)/(\d{8})# or die;
		my $cust = $1;
		my $date = $2;
		open(FD, "$_") or die;
		while(<FD>) {
			/^(FIXED|TOTAL)\s+\S+\s+(\d+)$/ and $res{$date}->{$1} += $2;
		}
		close(FD) or die;
	}
	open(OUTPUT, ">$output") or die "$!";
	print OUTPUT "# Date\t\ttotal\t>=10.50\n";
	for(sort(keys(%res))) {
		print OUTPUT $_, "\t";
		print OUTPUT $res{$_}->{TOTAL} || 0, "\t";
		print OUTPUT $res{$_}->{FIXED} || 0, "\n";
	}
	close(OUTPUT) or die "$!";
}

sub mkreport($$$) {
	my ($datafile, $datafile_t10, $output) = @_;
	open(OUTPUT, ">$output") or die;
	open(FD, "$datafile") or die;
	my @data = <FD>;
	close(FD) or die;
	open(FD, "$datafile_t10") or die;
	my @data_t10 = <FD>;
	close(FD) or die;
	@data = splice(@data, -7);
	@data_t10 =splice(@data_t10, -7);
	my $n;
	my $ns;
	my $nd;
	my $nd6;
	my $nd6_6to4;
	my $nd6_teredo;
	my $startdate;
	my $enddate;
	for(@data) {
		my @fields = split(/\s+/, $_);
		$startdate ||= $fields[0];
		$enddate = $fields[0];
		$n += $fields[1];
		$ns += $fields[2];
		$nd += $fields[3];
		$nd6 += $fields[4];
		$nd6_6to4 += $fields[5];
		$nd6_teredo += $fields[6];
	}
	print OUTPUT "<table border=\"1\" cellpadding=\"2\"><tr><td><pre>";
	print OUTPUT "Overall stats (without timeout), \n";
	print OUTPUT "last week ($startdate - $enddate):\n\n";
	printf OUTPUT "Client loss:                        %.3f%%\n", 100/$n*($ns-$nd);
	printf OUTPUT "Overall IPv6 dualstack preference:  %.3f%%\n", 100/$nd*$nd6;
	if($nd6*$nd6_6to4) {
		printf OUTPUT "- of which are 6to4:                %.3f%%\n", 100/$nd6*$nd6_6to4;
	} else {
		printf OUTPUT "- of which are 6to4:                %.3f%%\n", 0;
	}
	if($nd6*$nd6_teredo) {
		printf OUTPUT "- of which are Teredo:              %.3f%%\n", 100/$nd6*$nd6_teredo;
	} else {
		printf OUTPUT "- of which are Teredo:              %.3f%%\n", 0;
	}
	if($nd6*($nd6-$nd6_6to4-$nd6_teredo)) {
		printf OUTPUT "- of which are other/native:        %.3f%%", 100/$nd6*($nd6-$nd6_6to4-$nd6_teredo);
	} else {
		printf OUTPUT "- of which are other/native:        %.3f%%", 0;
	}
	print OUTPUT "</pre></td><td><pre>";
	undef $n;
	undef $ns;
	undef $nd;
	undef $nd6;
	undef $nd6_6to4;
	undef $nd6_teredo;
	undef $startdate;
	undef $enddate;
	for(@data_t10) {
		my @fields = split(/\s+/, $_);
		$startdate ||= $fields[0];
		$enddate = $fields[0];
		$n += $fields[1];
		$ns += $fields[2];
		$nd += $fields[3];
		$nd6 += $fields[4];
		$nd6_6to4 += $fields[5];
		$nd6_teredo += $fields[6];
	}
	print OUTPUT "Overall stats (with 10s timeout), \n";
	print OUTPUT "last week ($startdate - $enddate):\n\n";
	printf OUTPUT "Client loss:                        %.3f%%\n", 100/$n*($ns-$nd);
	printf OUTPUT "Overall IPv6 dualstack preference:  %.3f%%\n", 100/$nd*$nd6;
	if($nd6*$nd6_6to4) {
		printf OUTPUT "- of which are 6to4:                %.3f%%\n", 100/$nd6*$nd6_6to4;
	} else {
		printf OUTPUT "- of which are 6to4:                %.3f%%\n", 0;
	}
	if($nd6*$nd6_teredo) {
		printf OUTPUT "- of which are Teredo:              %.3f%%\n", 100/$nd6*$nd6_teredo;
	} else {
		printf OUTPUT "- of which are Teredo:              %.3f%%\n", 0;
	}
	if($nd6*($nd6-$nd6_6to4-$nd6_teredo)) {
		printf OUTPUT "- of which are other/native:        %.3f%%", 100/$nd6*($nd6-$nd6_6to4-$nd6_teredo);
	} else {
		printf OUTPUT "- of which are other/native:        %.3f%%", 0;
	}
	print OUTPUT "</pre></td></tr></table>\n";
	close(OUTPUT) or die;
}

mkdatafile("../reports/{api,vg}/20??????-{hits,v6use}.txt", "standard.txt");
system("gnuplot standard-v6use.gnuplot");

mkdatafile("../reports/{api,vg}/20??????-{hits,v6use}-no-mac-os-x.txt", "noosx.txt");
system("gnuplot noosx-v6use.gnuplot");

mkdatafile("../reports/{api,vg}/20??????-{hits,v6use}-only-mac-os-x.txt", "onlyosx.txt");
system("gnuplot onlyosx-v6use.gnuplot");

mkdatafile("../reports/{api,vg}/20??????-{hits,v6use}-no-buggy-opera.txt", "nobuggyopera.txt");
system("gnuplot nobuggyopera-v6use.gnuplot");

mkdatafile("../reports/{api,vg}/20??????-{hits,v6use}-no-known-brokenness.txt", "no-known-brokenness.txt");
system("gnuplot no-known-brokenness-v6use.gnuplot");

mkdatafile_operaversions("../reports/{api,vg}/20??????-operaversions.txt", "operaversions.txt");
system("gnuplot operaversions.gnuplot");

mkdatafile_osxversions("../reports/{api,vg}/20??????-osxversions.txt", "osxversions.txt");
system("gnuplot osxversions.gnuplot");

system("rsync -av --delete . greed.fud.no:/var/www/http_www.fud.no/ipv6/gnuplot/");

