#!/usr/athena/bin/perl

%mit_types = ('/', 0, 'weather', 0, 'callsign', 0, 'geo', 0, 'finger', 0,
	      'zlocate', 0, 'cgi', 0, '.html', 0, '.htm', 0, '.txt', 0,
	      'machine', 0, 'madlib', 0, 'home', 0,
	      '.gif', 0, '.jpeg', 0, '.xbm', 0, '.bmp', 0, 'empty', 0,
	      '400', 0, '404', 0, '403', 0, '304', 0, '302', 0, '500', 0,
	      '501', 0, 'POST', 0, 'HEAD', 0, 'dir', 0, 'other', 0);
%mit_sizes = %mit_types;
%out_types = %mit_types;
%out_sizes = %mit_types;
$skipped = 0;

while (<>)
{
    chop;
    if (!/^(\d+\.\d+\.\d+\.\d+) [^\"]+"([^ ]+) (.+) HTTP\/1\.0" ([^ ]+) ([^ ]+)$/)
    {
	if (!/^(\d+\.\d+\.\d+\.\d+) [^\"]+"([^ ]+) (.+)" ([^ ]+) ([^ ]+)$/)
	{
	    if (!/^(\d+\.\d+\.\d+\.\d+) [^\"]+"-" 200 -$/)
	    {
		$skipped = $skipped + 1;
	    }
	    elsif ($1 =~ /^18\./)
	    {
		$mit_types{"empty"} = $mit_types{"empty"} + 1;
	    }
	    else
	    {
		$out_types{"empty"} = $out_types{"empty"} + 1;
	    }
	    next;
	}
	else
	{
	    $addr = $1;
	    $method = $2;
	    $url = $3;
	    $status = $4;
	    $size = $5;
	}
    }
    else
    {
	$addr = $1;
	$method = $2;
	$url = $3;
	$status = $4;
	$size = $5;
    }

    if ($status != 200)
    {
	print "weird status: $status\n" if ! defined $mit_types{"$status"};
	if ($addr =~ /^18\./)
	{
	    $mit_types{"$status"} = $mit_types{"$status"} + 1;
	}
	else
	{
	    $out_types{"$status"} = $out_types{"$status"} + 1;
	}
	next;
    }

    if ($method ne 'GET')
    {
	print "weird method: $method\n" if ! defined $mit_types{"$method"};
	if ($addr =~ /^18\./)
	{
	    $mit_types{"$method"} = $mit_types{"$method"} + 1;
	}
	else
	{
	    $out_types{"$method"} = $out_types{"$method"} + 1;
	}
	next;
    }

    if ($url eq '/')		{ $type = '/'; }
    elsif ($url =~ m#^/weather#)	{ $type = 'weather'; }
    elsif ($url =~ m#^/callsign#)	{ $type = 'callsign'; }
    elsif ($url =~ m#^/geo#)	{ $type = 'geo'; }
    elsif ($url =~ m#^/finger#)	{ $type = 'finger'; }
    elsif ($url =~ m#^/zlocate#)	{ $type = 'zlocate'; }
    elsif ($url =~ m#^/cgi/#)	{ $type = 'cgi'; }
    elsif ($url =~ m#^/machine#)	{ $type = 'machine'; }
    elsif ($url =~ m#^/madlib#)	{ $type = 'madlib'; }
    elsif ($url =~ m#^/home#)	{ $type = 'home'; }
    elsif ($url =~ /\.html$/i)	{ $type = '.html'; }
    elsif ($url =~ /\.htm$/i)	{ $type = '.htm'; }
    elsif ($url =~ /\.txt$/i)	{ $type = '.txt'; }
    elsif ($url =~ /\.gif$/i)	{ $type = '.gif'; }
    elsif ($url =~ /\.jpg$/i)	{ $type = '.jpeg'; }
    elsif ($url =~ /\.jpeg$/i)	{ $type = '.jpeg'; }
    elsif ($url =~ /\.xbm$/i)	{ $type = '.xbm'; }
    elsif ($url =~ /\.bmp$/i)	{ $type = '.bmp'; }
    elsif ($url =~ m#/$#)	{ $type = 'dir'; }
    else			{ $type = 'other'; }

    if ($addr =~ /^18\./)
    {
	$mit_types{"$type"} = $mit_types{"$type"} + 1;
	$mit_sizes{"$type"} = $mit_sizes{"$type"} + $size;
    }
    else
    {
	$out_types{"$type"} = $out_types{"$type"} + 1;
	$out_sizes{"$type"} = $out_sizes{"$type"} + $size;
    }
}

print "$skipped skipped\n\n";
print "type    mit hits    bytes    other hits    bytes    total hits    bytes\n\n";
$tmh = 0;
$tmb = 0;
$toh = 0;
$tob = 0;
$tth = 0;
$ttb = 0;
for $key (sort keys %mit_types)
{
    $mh = $mit_types{$key};
    $mb = $mit_sizes{$key};
    $oh = $out_types{$key};
    $ob = $out_sizes{$key};
    $th = $mh + $oh;
    $tb = $mb + $ob;
    write;
    $tmh = $tmh + $mh;
    $tmb = $tmb + $mb;
    $toh = $toh + $oh;
    $tob = $tob + $ob;
    $tth = $tth + $th;
    $ttb = $ttb + $tb;
}
$mh = $tmh;
$mb = $tmb;
$oh = $toh;
$ob = $tob;
$th = $tth;
$tb = $ttb;
print "\n";
write;
exit;

format STDOUT =
@<<<<<<<< @##### @#######        @##### @#######        @##### @#######
$key,     $mh,   $mb,            $oh,   $ob,            $th,   $tb
.
