#!/afs/athena/contrib/perl/perl5

## The data set is too large to actually untar, so this script does
## rudimentary tar-format parsing as well as interpreting the actual
## data.

## At this time, the most useful form to have the data is number of
## bytes exported from and imported to each country, and each region
## (north america, europe, south pacific). This really only works with
## this data set because most continental traffic crosses the US.  In
## particular, it is most useful to view Europe as a single entity,
## since FIX-west sampling does not see any of the substantial
## international traffic there.

require "ctime.pl";

require "/mit/marc/Classes/urop/Iap96/helpers.pl";

&read_countries();

while(<>) {
    if (m,CC/(Old/)?(\d+),) {
	print STDERR &ctime(gmtime($2)),"\n";

	$month = sprintf("%02d/%02dwk%d",(gmtime($2))[5], (gmtime($2))[4]+1,
			 int(((gmtime($2))[3]-1)/7.6));
	print STDERR "$month\n";
	$count{$month}++;
    } else {
	split;

	next if ($coderegion{$_[0]} eq $coderegion{$_[1]});

	if (!defined($coderegion{$_[0]})) {
	    warn "unknown country code $_[0]\n"
		if ($_[0] !~ /\<unk\>|NIDB/);
	} elsif ($coderegion{$_[0]} ne "north america") {
	    $rexport{$month}{$coderegion{$_[0]}} += $_[5];
	    $rexport{$month}{"total"} += $_[5];
	}

	if (!defined($coderegion{$_[1]})) {
	    warn "unknown country code $_[1]\n"
		if ($_[1] !~ /\<unk\>|NIDB/);
	} elsif ($coderegion{$_[0]} ne "north america") {
	    $rimport{$month}{$coderegion{$_[1]}} += $_[5];
	    $rimport{$month}{"total"} += $_[5];
	}
    }
}

&table("rexport");
&table("rimport");

for (keys %count) {
    print STDERR $_,"\t",$count{$_},"\n";
}
