#!/usr/bin/perl -w
#
use IP::Country::MaxMind;
use Data::Dumper;

#my $host = $ARGV[0] or die "usage: $0 hostname";

my %countries = ();

while (<>) {
    my $host = $_;
    chomp($host);
    my $country = get_country($host);
    print("$host \t $country\n");
    next unless ($country);
    if (exists $countries{"$country"}) {
        print("$country existriert bereits.\n");
	my $current = $countries{"$country"};
	$current++;
        $countries{"$country"} = $current;
    } else {
        print("$country existiert noch nicht.\n");
        $countries{"$country"} = "1"; 
    }
    print("Counter: ". $countries{"$country"} ."\n");
}

my $cnum = "0";
my @clist = keys %countries;
printf("-" x 79 . "\n");
foreach my $c (@clist) {
    $cnum++;
    print("$c: " . $countries{"$c"} . "\n");
}
printf("-" x 79 . "\n");
print("Total Countries: $cnum\n");
printf("-" x 79 . "\n");

#print Dumper(%countries);
my %c = %countries;
    my $chtm = "world";
    my $chco="FFFFFF,80FF80,FF0000";	# no = white, low = green, high = red
    my $chld = "";
    my $chd = "";
    my $chf="bg,s,EAF7FE";
    foreach $cn (keys %c) {
        $chld = join("", $chld, $cn );
        $chd  = join(",", $chd,  $c{"$cn"} );
    }
    $chd=~s/^,//;
    my $url="http://chart.apis.google.com/chart?cht=t&chs=440x220&chtm=$chtm&chd=t:$chd&chco=$chco&chld=$chld&chf=$chf";
    print("CHD: $chd\n");
    print("CHLD: $chld\n");
    print("$url\n");
    #my $url = create_map_url(%countries);
print("MAP: $url\n");

##############################################################################
sub get_country {
    my $host = shift;
    my $gi = IP::Country::MaxMind->open("GeoIP.dat");
    my $result = $gi->inet_atocc("$host");
    return $result;
}

sub create_map_url {
    my %c = shift;
    print Dumper(%c);
    my $chtm = "world";
    my $chco="FFFFFF,00FF00,FFFF00,FFFFFF80,FF0000";
    my $chld = "";
    my $chd = "";
    my $chf="bg,s,EAF7FE";
    foreach $cn (keys %c) {
        $chld = join("", $chld, $cn );
        $chd  = join(",", $chd,  $c{"$cn"} );
    }
    $chd=~s/^,//;
    my $url="http://chart.apis.google.com/chart?cht=t&chs=440x220&chtm=$chtm&chd=t:$chd&chco=$chco&chld=$chld&chf=$chf";
    print("CHD: $chd\n");
    print("CHLD: $chld\n");
    return $url;
}
