#!/usr/bin/perl
use CGI;

my $cgi = new CGI;
my $dir = $cgi->param('dir');
my $file = $cgi->param('file');

my $markers;
my $test;

if( $file ) {
	while( <$file> ) {
		$test = $test . $_;
		if( /^#.*/ ) {
			# If the line starts with a # then ignore it
		} else {
			($lat, $long, $ssid, $type, $bssid, $encrypt, $lastseen, $key, $crc) = split /\t/;
			# Clean up the lat and long
			$lat =~ /([^NS]*)(.)/;
			if( $2 eq "N" ) {
				$lat = $1;
			} else {
				$lat = "-" . $1;
			}
			$long =~ /([^WE]*)(.)/;
			if( $2 eq "E" ) {
				$long = $1;
			} else {
				$long = "-" . $1;
			}
			# Clean up the SSID
			$ssid =~ s/\+/ /g;
			$ssid =~ s/%([0-9A-Fa-f]{2})/"&#" . hex($1) . ";"/eg;
			
			$markers = $markers . "\t\t\t\t<marker lat=\"$lat\" lng=\"$long\" ssid=\"$ssid\" encrypt=\"$encrypt\"/>\n";
		}
	}
}

# Print out our xhtml result file

print "Content-type: text/html\n\n";

print <<"EndOfXHTMLText";
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<script src="http://maps.google.com/maps?file=api&v=1&key=ABQIAAAAEQpbeQYW-BE0GP_n1p-mARQ4G1-joowy6xx282dlTZgylNEQWxSjUQKF53X1Ocy3LAXg8LmvdRaPiQ" type="text/javascript"></script>
		<style type="text/css">
		<!--
		div.scroll {
			height: 434px;
			width: 100%;
			overflow: auto;
			border: 1px solid #666;
			background-color: #ccc;
			padding: 8px;
		}
		-->
		</style>
	</head>
	<body>
		<p>This app works with a Kismac ".kismac Webservice file" which can be 
		made by exporting from the Kismac application. It will plot all of the
		access points in the file with google maps.</p>
		<FORM ENCTYPE="multipart/form-data" ACTION="/jon/wifiGoogle.pl" METHOD="POST">
			Please select a file to upload: <BR> 
			<INPUT TYPE="FILE" NAME="file">
			<p>
			<INPUT TYPE="submit">
		</FORM>
		
		<table>
		  <tr>
			<td>
			   <div id="map" style="width: 550px; height: 450px"></div>
			</td>
			<td width = 200 valign="top" style="text-decoration: underline; color: #4444ff;">
			   <div id="sidebar" class="scroll"></div>
			</td>
		  </tr>
	
		</table>
		<p/>
		
		<xml id="xmlData" style="visibility:hidden;">
			<markers>
$markers
			</markers>
		</xml>
	
		<script type="text/javascript">
			var redImage = "http://www.steinmetz.org/jon/mm_18_red.png";
			var greenImage = "http://www.steinmetz.org/jon/mm_18_green.png";
			var shadowImage = "http://www.steinmetz.org/jon/mm_18_shadow.png";
			
			// Make small red marker
			var redIcon = new GIcon();
			redIcon.image = redImage;
			redIcon.shadow = shadowImage;
			redIcon.iconSize = new GSize(12, 18);
			redIcon.shadowSize = new GSize(22, 18);
			redIcon.iconAnchor = new GPoint(6, 18);
			redIcon.infoWindowAnchor = new GPoint(5, 1);
			
			// Make small green marker
			var greenIcon = new GIcon();
			greenIcon.image = greenImage;
			greenIcon.shadow = shadowImage;
			greenIcon.iconSize = new GSize(12, 18);
			greenIcon.shadowSize = new GSize(22, 18);
			greenIcon.iconAnchor = new GPoint(6, 18);
			greenIcon.infoWindowAnchor = new GPoint(5, 1);
	
			var map = new GMap(document.getElementById("map"));
			map.addControl(new GSmallMapControl());
			map.addControl(new GMapTypeControl());

			var xmlData = document.getElementById( "xmlData" );
			var markers = xmlData.getElementsByTagName( "marker" );
			var visibleMarkers = new Object();

			function buildSidebar() {
				var sidebar_html = "";
				
				for (var i = 0; i < markers.length; i++) {
					var element = markers[i];
					var flagImage = redImage;
					if( element.getAttribute( "encrypt" ) == "NO" ) {
						flagImage = greenImage;
					}
					sidebar_html += '<img src="' + flagImage + '">'
							+ '<a href="javascript:sidebarClick(' + i + ')" >' 
							+ element.getAttribute("ssid") + '</a><br>';
				}
				
				document.getElementById( "sidebar" ).innerHTML = sidebar_html;
			}
			
			function createMarker( data ) {
				
				var lngString = data.getAttribute( "lng" );
				var latString = data.getAttribute( "lat" );
				
				// Create the marker
				var point = new GPoint( parseFloat( lngString ), parseFloat( latString ) );
				var icon = redIcon;
				if( data.getAttribute( "encrypt" ) == "NO" ) {
					icon = greenIcon;
				}
				var marker = new GMarker( point, icon );
				
				// Add listener to open info window
				var html = "SSID <b>" + data.getAttribute("ssid") + "</b>";
				GEvent.addListener(marker, "click", function() {
					marker.openInfoWindowHtml(html);
				});
				
				return marker;
			}
			
			function sidebarClick( index ) {
				var element = markers[ index ];
				var lngString = element.getAttribute( "lng" );
				var latString = element.getAttribute( "lat" );
				var keyString = lngString + "," + latString;
				
				var marker = visibleMarkers[ keyString ];
				if( !marker ) {
					marker = createMarker( element );
					map.addOverlay( marker );

					// Add the marker to our dictionary
					visibleMarkers[ keyString ] = marker;
				}
				GEvent.trigger( marker, "click" );
			}
			
			function addVisibleMarkers() {
				var bounds = map.getBoundsLatLng();
				for (var i = 0; i < markers.length; i++) {
					var element = markers[i];
					var lngString = element.getAttribute( "lng" );
					var latString = element.getAttribute( "lat" );
				
					var point = new GPoint( parseFloat( lngString ), 
							parseFloat( latString ) );
					if( point.x >= bounds.minX
							&& point.x <= bounds.maxX
							&& point.y >= bounds.minY 
							&& point.y <= bounds.maxY ) {
						var keyString = lngString + "," + latString;
						if( !visibleMarkers[ keyString ] ) {
							var marker = createMarker( element );
							map.addOverlay( marker );
	
							// Add the marker to our dictionary
							visibleMarkers[ keyString ] = marker;
						}
					}
				}
			}
			
			function removeHiddenMarkers() {
				var bounds = map.getBoundsLatLng();

				for( var markerKey in visibleMarkers ) {
					var marker = visibleMarkers[ markerKey ];
					var point = marker.point;
					if( point.x < bounds.minX
							|| point.x > bounds.maxX
							|| point.y < bounds.minY 
							|| point.y > bounds.maxY ) {
						map.removeOverlay( marker );
						delete visibleMarkers[ markerKey ];
					}
				}
			}
			
			function updateMarkers() {
				removeHiddenMarkers();
				addVisibleMarkers();
			}
			
			buildSidebar();
			
			GEvent.addListener( map, "moveend", updateMarkers );
			GEvent.addListener( map, "zoom", updateMarkers );
			
			if( markers.length > 0 ) {
				var element = markers[0];
				var point = new GPoint(
						parseFloat(element.getAttribute("lng")),
						parseFloat(element.getAttribute("lat")));
				map.centerAndZoom( point, 4 );
			}
		</script>
	</body>
</html>
EndOfXHTMLText

exit(0);