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

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

&read_countries();

for $f (<nsf-*.country>) {
    $f =~ /nsf-(\d{2})(\d{2})\.country/;

    $month = $1."/".$2;

    open(F, $f) || die "open $f: $!\n";

    while(<F>) {
	last if /Bytes/;
    }

    %traffic = ();

    while(<F>) {
	if (/\d+\s+(\D+[a-zA-Z])\s+([\d,]+)\s+([\d,]+)/) {
	    ($country = $1) =~ tr/A-Z/a-z/;
	    ($from = $2) =~ s/,//g;
	    ($to = $3) =~ s/,//g;

	    next if ($nameregion{$country} eq "north america");

	    if (!defined($nameregion{$country})) {
		warn "unknown country $country\n";
	    } else {
		$rimport{$month}{$nameregion{$country}} += $from;
		$rimport{$month}{"total"} += $from;
	    
		$rexport{$month}{$nameregion{$country}} += $to;
		$rexport{$month}{"total"} += $to;
	    }
	}
    }
}

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