#
# perl script for netstumbler summary export files to import in Mappoint 2001/2002 German Version
#
# (c) by wardriving.ch
# 
# release:  1.3e
# date   :  13. Nov 2003
# 
#
#
#
# creat different output files
#
# map file with all AP Points
# null file with AP, who have Lat. 0.00000
# wep file with AP WEP = YES
# nowep file with AP WEP = NO
# total file with WEP/NOWEP feld
#
# 
#
#################
#
# file selection
#
#################

print "Enter filename: ";
my $filename =<>;
chomp $filename;
my $outputfile = ">map_".$filename;
my $outputfile1 = ">null_".$filename;
my $outputfile2 = ">wep_".$filename;
my $outputfile3 = ">nowep_".$filename;
my $outputfile4 = ">total_".$filename;

open IN, $filename or die "Cannot open ".$filename." for reading";
open MAPOUT, 	$outputfile  or die "Cannot open ".$outputfile." for writing";
open NIXKOR, 	$outputfile1 or die "Cannot open ".$outputfile1." for writing";
open WEPOUT, 	$outputfile2 or die "Cannot open ".$outputfile2." for writing";
open NOWEPOUT, 	$outputfile3 or die "Cannot open ".$outputfile3." for writing";
open TOTAL, 	$outputfile4 or die "Cannot open ".$outputfile4." for writing";

#################
#
# file selection
#
# Writing Header Map-Point German needs: Breitengrad Längengrad Name Name2
#
#################

print "Processing............\n";
print MAPOUT "Breitengrad\tLängengrad\tName\tName 2\n";
print NIXKOR "Breitengrad\tLängengrad\tName\tName 2\n";
print WEPOUT "Breitengrad\tLängengrad\tName\tName 2\n";
print NOWEPOUT "Breitengrad\tLängengrad\tName\tName 2\n";
print TOTAL "Breitengrad\tLängengrad\tAdresse 1\tName\tName 2\n";

#################
#
# read / processing / write Data
#
#################

while (<IN>)
{
chomp;
   if (m/^#/) 
      {
      print "Found a header line - skipping.\n";
      }
   else
      {
      ($Lat, $Long, $SSID, $MAC, $FLAG) = (split /\t/)[0,1,2,4,8]; 
      chomp $Lat, $Long, $SSID, $MAC, $FLAG;
      $Lat=~s/N /+/;   # replace N<SPACE> to +
      $Lat=~s/S /-/;   # replace S<SPACE> to -
      $Long=~s/W /-/;  # replace W<SPACE> to -
      $Long=~s/E /+/;  # replace E<SPACE> to +
      $SSID=~s/\(//;
      $SSID=~s/\)//;
      $MAC=~s/\(//;
      $MAC=~s/\)//;
      $MAC=~s/ //g;
      chomp $Lat, $Long, $SSID, $MAC, $FLAG;
      
      $FLAG=~s/0011/WEP/;
      $FLAG=~s/0012/WEP/;
      $FLAG=~s/0.../NOWEP/;
      
      print 		$Lat,"\t", $Long, "\t", $SSID,"\t", $MAC,"\t", $FLAG,"\n";
      print TOTAL 	$Lat,"\t", $Long, "\t", $SSID,"\t", $MAC,"\t", $FLAG,"\n";

#################
#
# write AP with NO Lat / Log to a sep. file 
#
#################
      
      if (($Lat =~ "0.0000000") or ($Long =~ "-0.0000000"))
         {
         print "NULL Koordinaten LAT\n";
         print NIXKOR 	$Lat,"\t", $Long, "\t", $SSID,"\t", $MAC,"\t", $FLAG,"\n";
         if ( $FLAG =~ "\^WEP\$")
         	{
         	print WEPOUT 	$Lat,"\t", $Long, "\t", $SSID,"\t", $MAC,"\t", $FLAG,"\n";
        	}
         elsif ( $FLAG =~ "NOWEP" )
         	{
         	print NOWEPOUT 	$Lat,"\t", $Long, "\t", $SSID,"\t", $MAC,"\t", $FLAG,"\n";
        	}
         else 
         	{
         	print NOWEPOUT 	$Lat,"\t", $Long, "\t", $SSID,"\t", $MAC,"\t", $FLAG,"\n";
        	}	
         }

#################
#
# write AP to the mapfile / wepfile / nowepfile
#
#################
      else
      	{
         print 	MAPOUT 	$Lat,"\t", $Long, "\t", $SSID,"\t", $MAC,"\t", $FLAG,"\n";
         
         if ( $FLAG =~ "\^WEP\$")
         	{
         	print WEPOUT 	$Lat,"\t", $Long, "\t", $SSID,"\t", $MAC,"\t", $FLAG,"\n";
        	}
         elsif ( $FLAG =~ "NOWEP" )
         	{
         	print NOWEPOUT 	$Lat,"\t", $Long, "\t", $SSID,"\t", $MAC,"\t", $FLAG,"\n";
        	}
         else 
         	{
         	print NOWEPOUT 	$Lat,"\t", $Long, "\t", $SSID,"\t", $MAC,"\t", $FLAG,"\n";
        	}	
         }
      }
} 
#################
#
# cleanup
#
#################
print "Finished!\n";
close IN;
close MAPOUT;
close NIXKOR;
close NOWEPOUT;
close WEPOUT;
close TOTAL;
