#
# Associative Arrays
#
$|=1;
%TypeToCountVariableMap = ('TopDocs', 'path',
			   'TopSites', 'ipdom',
			   'TopDoms', 'dom',
			   'TopFlds', 'fld',
			   'Breakdown', 'bdown',
			   'TopHosts', 'host');

#
# PreProc Calls
#
%PPCode= ('TopDocs', '',
	  'TopHosts', '',
	  'TopSites', '$ipdom = &GetIpDom($host);',
	  'TopFlds', '$fld = &GetFLD($host);',
	  'Breakdown', '$bdown = &GetBdown($path);',
	  'TopDoms', '$dom = &GetDom($host);',

	  );

#
#  Report Type Subroutines
#
sub GetBdown {
    local($path) = @_;
    local($return);

    $return = "Other";
    $return = "Top level" if $path=~/gnn/i;
    $return = "Metacenters" if $path =~ /meta/;
    $return = "Whole Internet Catalog" if $path =~ /wic/;
    $return = "News" if $path =~/news/;
    $return = "Magazine" if $path =~/\/mag\//;
    $return = "Marketplace" if $path =~ /mkt/;
    
    $return;
}

sub TopFlds {
    local($name, $howmany, @format) = @_;
    local($fmt, $format, $fmtheader);
    local($code) = '';

    $format=join(" ", @format);
    ($fmt=$format) =~ s/fld(\d+)/&pad(&whois_lookup(\$tops[\$n]), \1), /g;
    $fmt =~ s/hits(\d+)/&pad(\$$name{\$tops[\$n]}), /g;
    $fmt =~ s/, $//;

    ($fmtheader=$format) =~ s/fld(\d+)/&pad(" Name", \1), /g;
    ($fmtheader) =~ s/hits(\d+)/&pad("Hits", \1), /g;
    $fmtheader =~ s/, $//;

    $code .="\@tops = sort {\$$name{\$b} <=> \$$name{\$a}} keys \%$name;"."\n";
    $code .="print($fmtheader.\"\\n\");"."\n";
    $code .="for \$n (0..$howmany){"."\n";
    $code .="   print($fmt.\"\\n\");"."\n";
    $code .="}"."\n";
    $code .="print(\"Total:  \$total_$name\\n\");"."\n";


    $code;
}

sub Breakdown{
    local($name, $howmany, @format) = @_;

    &Tops($name, $howmany); # , "bdown", "Document type", @format);
}

sub TopDoms {
    local($name, $howmany, @format) = @_;
    local($code);

    $code = &Tops($name, $howmany); #, "dom", "Domain", @format);
    $code .= "print(\"Total International Hits: \", \$total_$name - \$$name{'edu'} - \$$name{'com'} - \$$name{'gov'} - \$$name{'org'} - \$$name{'us'} - \$$name{'net'} - \$$name{'mil'} - \$$name{'numeric-ip-address'});"."\n";
}

sub TopDocs{
    local($name, $howmany, @format) = @_;
    &Tops($name, $howmany); #, "doc", "Document", @format);
}

sub TopHosts {
    local($name, $howmany, @format) = @_;

    &Tops($name, $howmany, "host", "Hostname", @format);
}

sub TopSites {
    local($name, $howmany) = @_;
    local(@report) = ();
    local($code) ='';

    print("Adding Tops code for $name ($howmany)\n");

    # @report is as follows:
    # (Total, name, key, value, key, value,...)

    $code .= "\n# Beginning of report construction\n";
    $code .="\@report=();","\n";
    $code.="\@tops = sort {\$$name{\$b} <=> \$$name{\$a}} keys \%$name;"."\n";
    $code .="for \$n (0..$howmany-1){"."\n";
    $code .="push(\@report, &whois_lookup(\$tops[\$n]), \$$name{\$tops[\$n]});"."\n";
    $code .="}"."\n";
    $code .="\@report = (\$total_$name, \"$name\", \@report);"."\n";
    $code .= "# End of report construction\n";
    
    $code;
}


sub Tops {
    local($name, $howmany) = @_;
    local(@report) = ();
    local($code) ='';

    print("Adding Tops code for $name ($howmany)\n");

    # @report is as follows:
    # (Total, name, key, value, key, value,...)

    $code .= "# Beginning of report construction\n";
    $code .="\@report=();","\n";
    $code.="\@tops = sort {\$$name{\$b} <=> \$$name{\$a}} keys \%$name;"."\n";
    $code .="for \$n (0..$howmany-1){"."\n";
    $code .="push(\@report, \$tops[\$n], \$$name{\$tops[\$n]});"."\n";
    $code .="}"."\n";
    $code .="\@report = (\$total_$name, \"$name\", \@report);"."\n";
    $code .= "# End of report construction\n";
    
    $code;
}

sub GetDom {
    local($hostname) =@_;
    $testcount++;
    if($hostname =~ /[a-zA-Z]/){
	(@hparts) = split(/\./, $hostname);
	$hparts[$#hparts];
    }
    else{
	"numeric-ip-address";
    }    
}

sub GetFLD {
    local($hostname) =@_;
    local($fld);

    if($hostname=~/[A-Za-z]/){
	split(/\./, $hostname);
	if(length($_[$#_]) == 3){
	    $fld = $_[$#_-1].'.'.$_[$#_];
	}
	else{
	    $fld = $_[$#_-2].'.'.$_[$#_-1].'.'.$_[$#_];
	}
    }
    else{
	$fld = "Numeric-ip-address";
    }
    $fld;
}

sub GetIpDom {
    local($name) = @_;
    local(@ipaddr,$aliases,$type,$len,$thisaddr, $fld);
    $resolves++;
    return $Resolve{$name} if $Resolve{$name};

    if($name=~/[a-zA-Z]/){
	($name,$aliases,$type,$len,$thisaddr) =gethostbyname($name);
	@ipaddr = unpack('C4', $thisaddr);
    }
    else{
	@ipaddr = split(/\./, $name);
    }

    if($ipaddr[0] < 127){
	$fld = "net $ipaddr[0]";
    }
    elsif($ipaddr[0] < 191){
	$fld = "net $ipaddr[0]".".".$ipaddr[1];
    }
    else{
	$fld = "international-site";
#	$fld = "net ".$ipaddr[0].".".$ipaddr[1].".".$ipaddr[2];
    }
    if($resolves < 100){
	print ".";
    }
    else{
	print "*";
	$resolves=0;
    }
    $Resolve{$name} = $fld;
    $fld;
}

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

    return $Whois{$domain} if $Whois{$domain};
    return "--------------------" if $domain eq "";

    open(WH, "whois -h internic.net \"$domain\"|");
    while(<WH>){
	if(/MILES, JAMES/){
	    return "--------------------";
	}
        if(/([^\(]+) \([A-Z\-1-9]+\)/){
	    $Whois{$domain} = $1." [$domain]";
            return $1." [$domain]";
        }
        if(/No match for/){
	    $Whois{$domain} = "Unknown [$domain]";
            return "Unknown [$domain]";
        }
        if(/system load/){
            return "NIC is overloaded [$domain]";
        }
    }
    close(WH);
    "Hey, what gives? [$domain]";
}


1;
