#!/usr/bin/perl -w

# Copyright (c) 2006 Antoine Imbert <antoine.imbert@gmail.com> All rights reserved.
# This program is free software; you can redistribute it and/or modify it under
# the same terms as Perl itself.

use strict;
use lib 'lib';
use MT;
my $mt = MT->new or die MT->errstr;
use Geo::IP::PurePerl;
use HTML::GoogleMaps;
use MT::Comment;

my $gi = Geo::IP::PurePerl->new(GEOIP_STANDARD);
my $map_key = 'XXXXXXX';
my $map = HTML::GoogleMaps->new(key => $map_key, height => 500, width => 700);
$map->v2_zoom(2);
$map->controls('small_map_control');

my $markers = {};
my $iter = MT::Comment->load_iter({junk_status => -1});
while (my $comment = $iter->()) {
    my $h = $gi->get_city_record_as_hash($comment->ip) or next;
    next unless $h->{longitude} && $h->{latitude} && $h->{city};
    my $key = $h->{country_code}.$h->{city};
    $markers->{$key} = $h unless $markers->{$key};
    $markers->{$key}->{count}++;
}

for my $key (keys %{$markers}) {
    my $m = $markers->{$key};
    $map->add_marker(   
        point => [$m->{longitude}, $m->{latitude}], 
        html => $m->{city} .': <b>'. $m->{count}. ' comment spam</b>' );
}

my ($head, $map_div, $map_script) = $map->render;
print "<html><head>$head</head>
<body><div>$map_div</div>$map_script</body></html>";
