#!/usr/bin/perl -w

# where are domains
# by AmP
# 06.2009

use constant DEBUG => 1;

use Geo::IP;
use Data::Dumper;
use GD::Graph::pie;
use strict;

my $gi = Geo::IP->new(GEOIP_STANDARD) or die $!;

my $file ="input.txt";
my %result = ();

open(FH, "< $file");

while (<FH>) {
  $/ = "\r\n"; # i hate dos files
    chomp;
  my $line = $_;
  print "[ii]\tsearching for $line\n" if DEBUG;
  my $country = $gi->country_code_by_name( $line );
  $country = "DOWN" if not $country;
  print "[ii]\t$line seems to be in $country\n" if DEBUG;
  if ($result{$country}) {
    $result{$country} += 1;
  } else {
    $result{$country} = 1;
  }
}

print Dumper(%result) if DEBUG;
print "-----------------------------------------------------\n" if DEBUG;
print "|                     Results                       |\n" if DEBUG;
print "-----------------------------------------------------\n" if DEBUG;

my (@countries, @count);
while ( my ($key, $value) = each(%result) ) {
  push(@countries, $key);
  push(@count, $value);
  print "\t$key\t:\t$value\n";
}
print "-----------------------------------------------------\n" if DEBUG;

generate_image();

sub generate_image {
  my $name = "pornsites";
  my $graph;
  my @pdata = ([@countries], [@count]);
  $graph = GD::Graph::pie->new(550, 550);
  $graph->set(
      title       => $name,
      '3d'        => 1,
      ) or warn $graph->error;
  my $image = $graph->plot(\@pdata) or warn "Can't plot $name";
  open (IFH, "> $name.png") or die "Can't open '$name.png': $!";
  binmode(IFH);
  if ($image) {
    print IFH $image->png;
  }
  close(IFH);

}

