#!/usr/bin/perl
# This script will convert Opte Project database of 
# internet paths and backbones to Google Earth KML format.
#
# There are several things required for this tool to run:
# * Geo::IP perl module
# * The latest maxmind GeoLiteCity.dat (available on maxmind.com)
# * Opte *.2D.coords, *.edgecolors, and *.lgl files (http://www.opte.org/maps/)
#
# Author: iphelix
use Socket;
use Geo::IP;

unless (exists $ARGV[0]) {
print "opte2kml - Converts Opte database to Google Earth's KML format\n";
print "For questions and bug reports contact: iphelix \@t midnightresearch.com\n\n";
print "Typical Usage: ./opte2kml.pl filename.kml\n"; 
exit 1;
}

open(OUT, ">$ARGV[0]") || die "[!!!] Couldn't open output file\n";
print OUT "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n
<kml xmlns=\"http://earth.google.com/kml/2.1\">\n
<Document>\n
  <name>Internets</name>\n
  <Style id=\"black\">\n
    <LineStyle>\n
      <color>7f000000</color>\n
      <width>2</width>\n
    </LineStyle>\n
  </Style>\n
  <Style id=\"blue\">\n
    <LineStyle>\n
      <color>blue</color>\n
      <width>2</width>\n
    </LineStyle>\n
  </Style>\n
  <Style id=\"green\">\n
    <LineStyle>\n
      <color>7f00ff00</color>\n
      <width>2</width>\n
    </LineStyle>\n
  </Style>\n
  <Style id=\"cyank\">\n
    <LineStyle>\n
      <color>7f00ffff</color>\n
      <width>2</width>\n
    </LineStyle>\n
  </Style>\n
  <Style id=\"red\">\n
    <LineStyle>\n
      <color>7fff0000</color>\n
      <width>2</width>\n
    </LineStyle>\n
  </Style>\n
  <Style id=\"purple\">\n
    <LineStyle>\n
      <color>7fff00ff</color>\n
      <width>2</width>\n
    </LineStyle>\n
  </Style>\n
  <Style id=\"yellow\">\n
    <LineStyle>\n
      <color>7f00ffff</color>\n
      <width>2</width>\n
    </LineStyle>\n
  </Style>\n
  <Style id=\"white\">\n
    <LineStyle>\n
      <color>7fffffff</color>\n
      <width>2</width>\n
    </LineStyle>\n
  </Style>\n
";

my $gi = Geo::IP->open("./GeoLiteCity.dat", GEOIP_STANDARD);

open(EDG, "1105841711.edgecolors") || die "[!!!] Couldn't open edge file\n";
while(<EDG>) {
	chomp;
	my($startip,$endip, @color) = split(/\s/);
	if($startip =~ "RFC" || $endip =~ "RFC") { next; }
	my $startip_coord = $gi->record_by_name($startip);
	my $endip_coord = $gi->record_by_name($endip);

	my $color = "@color";
	my $color_name = "white";
	if($color eq "0.0 0.0 0.0") { $color_name = "black" }
	elsif($color eq "0.0 0.0 1.0") { $color_name = "blue" }
	elsif($color eq "0.0 1.0 0.0") { $color_name = "green" }
	elsif($color eq "1.0 0.0 0.0") { $color_name = "cyan" }
	elsif($color eq "1.0 0.0 1.0") { $color_name = "red" }
	elsif($color eq "1.0 1.0 0.0") { $color_name = "purple" }
	elsif($color eq "1.0 1.0 1.0") { $color_name = "white" }
	
	print OUT "	<Placemark>\n";
	print OUT "		<name>$startip - $endip</name>\n";
	print OUT "		<styleUrl>#".$color_name."</styleUrl>\n";
	print OUT "		<LineString>\n";
	print OUT "		        <extrude>1</extrude><tessellate>1</tessellate>\n";
	print OUT "			<coordinates>\n";
	print OUT "			".$startip_coord->longitude.", ".$startip_coord->latitude.",100 ";
	print OUT $endip_coord->longitude.", ".$endip_coord->latitude.",100\n";
	print OUT "			</coordinates>\n";
	print OUT "		</LineString>\n";
	print OUT "	</Placemark>\n";
}

print OUT "</Document>\n</kml>\n";
