#!/afs/athena/contrib/perl/p

while($ARGV[0] =~ /^\-/){
    $arg = shift(@ARGV);
    if($arg eq "-whois"){
	$whoisfeature = 1;
    }
    elsif($arg eq "-wic"){
	$wicfeature = 1;
	print("WIC feature on\n");
    }
    elsif($arg eq "-rr"){
	$regreports=1;
    }
    elsif($arg eq "-wicr"){
	$wicreport = 1;
	print("WIC reporting on\n");
    }
}

$| = 1;
#$SIG{'INT'} = 'report';
while(<>){
    next if /graphics/;
#    print("***$_\n");
    ($host, $wday, $mon, $day, $time, $year, $method, $path)= split;
    (@hparts) = split(/\./, $host);
    (@pparts) =  split(/\//, $path);
    $fld = $hparts[$#hparts-1].".".$hparts[$#hparts];
    $fld = "Unresolved" if $host!~/[A-Za-z]/;

######### Whois block    
    if($whoisfeature){
	if($host =~ /[a-zA-Z]/){
	    $whois{$fld} = &whois_lookup($fld) unless $whois{$fld};
	}
	else{
	    if($hparts[0] < 127){
		$fld = "net $hparts[0]";
	    }			# 
	    elsif($hparts[0] < 191){
		$fld = "net $hparts[0]".".".$hparts[1];
	    }
	    else{
		$fld = "net ".$hparts[0].".".$hparts[1].".".$hparts[2];
	    }
	    $whois{$fld} = &whois_lookup($fld) unless $whois{$fld};
	}
    }
######### End Whois Block
    
    $hostc{$fld}++;
    $count++;

######### WIC block
    if($wicfeature){
#	print("Is it wic?: \"$pparts[1]\"\n");
	if($pparts[1] eq "wic"){
	    $wicdocs{$path}++;
	    ########## WIC report block
	    if($wicreport){
#		print("$path: $wicdocs{$path}\n");
		if(($wicdocs{$path}/100) == int($wicdocs{$path}/100) &&
		   $wicdocs{$path} > $highest){
		    &wicreport;
		}
	    }
	    ######### End WIC report block
	}
    }
######### End WIC block

######### Regreports block
    if($regreports == 1){
	if($count==25){
	    &report2;
	    $count = 0;
	}
    }
######### End Regreports block


}

&report2;
&wicreport if $wicfeature;

sub wicreport {
    print("WICReporting...................................................\n");
    @topdocs = sort {$wicdocs{$b} <=> $wicdocs{$a}} keys %wicdocs;
    $highest = $wicdocs{$topdocs[0]};
    for $doc (@topdocs){
	print(&pad($doc, 40), ": ", $wicdocs{$doc}, "\n") 
	    if $wicdocs{$doc} > ($highest/25);
    }
    print("...............................................................\n");

}

sub report2 {
    print("Reporting......................................................\n");
    @topdoms = sort {$hostc{$b} <=> $hostc{$a}} keys %hostc;
    for $dom (@topdoms){
	print(&pad($dom, 20), ": ", &pad($hostc{$dom}, 5), 
	      "  [", $whois{$dom}, "]\n");
    }
    print("...............................................................\n");
}

sub pad {
    local($string, $len) = @_;
    local($padding);

    $padding = " " x ($len-length($string));
    $string.$padding;
}

sub whois_lookup {
    local($domain) = @_;

    print("Looking up $domain\n");
    open(WH, "whois -h internic.net \"$domain\"|");
    while(<WH>){
	if(/([^\(]+) \([A-Z\-]+\)/){
	    print("New connection from \"$1\"\n");
	    return $1;
	}
	if(/No match for/){
	    print("Unknown FLD $domain\n");
	    return "Unknown";
	}
	if(/system load/){
	    return "";
	}
    }
    close(WH);
}
