#!/usr/bin/perl
use strict;
use HTML::GoogleMaps;
use Data::Dumper;

# google maps api key, good for godfather.f0rked.com:91/temp only
#my $key="ABQIAAAABiA91VzAaGVRn6h4Scg-HBSu9CUU_o9JQs-bEhQlHJlNsuvVLBRQafOhxnwKJSCOKk0IQbE1ZtUcEg";

# http://archive.f0rked.com/maps
my $key="ABQIAAAABiA91VzAaGVRn6h4Scg-HBR3jm9Nz9Lxsa_3nQgy-Ruh0ltMbxQd1igjQOz1cJT_Z5vD8TyK1ldtjA";

chomp(my $line=<STDIN>);
my @fields=split /;/, $line;

my @networks;
while(<STDIN>) {
    chomp;
    my @values=split /;/;
    my $network; #hashref
    for(my $i=0;$i<=$#values;$i++) {
        $network->{$fields[$i]}=$values[$i];
    }
    push @networks,$network;
}

#print Dumper @networks;

#$VAR2 = {
#          'LastTime' => 'Wed Jul 21 20:23:12 2004',
#          'BestSignal' => '70',
#          'Carrier' => 'IEEE 802.11b',
#          'NetType' => 'infrastructure',
#          'MaxSeenRate' => '0',
#          'MaxRate' => '54.0',
#          'BSSID' => '00:0F:66:3E:98:5D',
#          'LLC' => '176',
#          'GPSBestAlt' => '655.049988',
#          'Info' => '',
#          'BestNoise' => '50',
#          'Beacon' => '100',
#          'BestQuality' => '0',
#          'GPSMinLat' => '39.671917',
#          'Network' => '^M2',
#          'Decrypted' => 'No',
#          'GPSMinLon' => '-89.691360',
#          'GPSMaxLon' => '-89.691048',
#          'IP' => '0.0.0.0',
#          'WEP' => 'Yes',
#          'GPSMinAlt' => '613.140015',
#          'GPSMaxAlt' => '673.859985',
#          'Encoding' => '',
#          'FirstTime' => 'Wed Jul 21 19:33:35 2004',
#          'GPSMaxLat' => '39.673630',
#          'GPSMinSpd' => '0.000000',
#          'GPSBestLat' => '39.671951',
#          'Total' => '180',
#          'Cloaked' => 'No',
#          'IPType' => 'None',
#          'GPSBestLon' => '-89.691063',
#          'Channel' => '6',
#          'ESSID' => '<no ssid>',
#          'Weak' => '0',
#          'DataSize' => '280',
#          'Crypt' => '4',
#          'Data' => '4',
#          'GPSMaxSpd' => '31.301226'
#        };

my $map=HTML::GoogleMaps->new(key => $key, width => 800, height => 600);

$map->zoom(3);	 	
$map->controls('large_map_control','map_type_control');

my($lat_total,$lon_total);
my($points,$html);
for my $n (@networks) {
    my($essid,$lat,$lon)=($n->{ESSID});
    if ($n->{GPSBestLat} ne "0.000000") {
        $lat=$n->{GPSBestLat};
    } else {
        $lat=$n->{GPSMaxLat};
    }
    if ($n->{GPSBestLon} ne "0.000000") {
        $lon=$n->{GPSBestLon};
    } else {
        $lon=$n->{GPSMaxLon};
    }
    if ($lon && $lat) {
        $lat_total+=$lat;
        $lon_total+=$lon;
        $points++;
        my $html_essid=$essid;
        $html_essid =~ s/</&lt;/;
        $html_essid =~ s/>/&gt;/;
        my($ainfo,$binfo);
        $ainfo.="$points. ESSID: $html_essid";
        $binfo.="Time: $n->{LastTime}<br />";
        $binfo.="Best Signal: $n->{BestSignal}<br />";
        $binfo.="Best Noise: $n->{BestNoise}<br />";
        $binfo.="WEP: $n->{WEP}<br />";
        $binfo.="Total: $n->{Total}";
        #print "Point: ($lon,$lat)\n";
        $html.="$points. $html_essid<br />\n";
        $html.="<div class=\"network\">$binfo</div>\n";
        $map->add_marker(point => [$lon, $lat], html => "$ainfo<br />$binfo");
    }
    #last;
}

# Get an average of the points, set the center there.
# TODO: Make this a median.
exit if !$points;
my($avg_lat,$avg_lon)=($lat_total/$points,$lon_total/$points);
$map->center([$avg_lon,$avg_lat]);

my($head,$body)=$map->render;

#print "Content-type: text/html\n\n";
print qq(<html>
<head>
<title>Kismet GPS Map</title>
<style type="text/css">
<!--
body,td {
    font-family: verdana, sans-serif;
    font-size: 11px;
}
div.network {
    padding-left: 22px;
    font-size: 10px;
    color: #AAAAAA;
}
-->
</style>
$head
</head>
<body>
<table>
 <tr valign="top">
  <td>
   $body
  </td>
  <td>
   <b>Networks</b>:<br />
   $html
  </td>
 </tr>
</table>
</body>
</html>);
    
