#
# perl script for netstumbler summary export files to import in Mappoint 2001 German Version
#
# (c) by wardriving.ch
# 
# release 1.2c
#
#
# creat diferent 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
#
# 
#
#################
#
# 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;

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";

#################
#
# file selection
#
# Writing Header Map-Point German needs: Breitengrad Längengrad Name Name2
#
#################

print "Processing............\n";
print MAPOUT "Breitengrad\tLängengrad\tName\tName2\n";
print NIXKOR "Breitengrad\tLängengrad\tName\tName2\n";
print WEPOUT "Breitengrad\tLängengrad\tName\tName2\n";
print NOWEPOUT "Breitengrad\tLängengrad\tName\tName2\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/\)//;
      chomp $MAC, $SSID;

 
      print $Lat,"\t", $Long, "\t", $SSID,"\t", $MAC,"\t", $FLAG,"\n";

#################
#
# write AP with NO Lat / Log to a sep. file 
#
#################
      
      if ($Lat == "+0.0000000")
         {
         print "NULL Koordinaten\n";
         print NIXKOR $Lat,"\t", $Long, "\t", $SSID,"\t", $MAC,"\n";
         }
      elsif($Lat == "-0.0000000")
         {
         print "NULL Koordinaten\n";
         print NIXKOR $Lat,"\t", $Long, "\t", $SSID,"\t", $MAC,"\n";
         }

#################
#
# write AP to the mapfile / wepfile / nowepfile
#
#################

      else
      	 {
      	 print  $Lat,"\t", $Long, "\t", $SSID,"\t", $MAC,"\n";
         print MAPOUT $Lat,"\t", $Long, "\t", $SSID,"\t", $MAC,"\n";
         
         if ( $FLAG == "0001")
         	{
         	print NOWEPOUT $Lat,"\t", $Long, "\t", $SSID,"\t", $MAC,"\n";
        	}
         elsif ( $FLAG == "0002" )
         	{
         	print NOWEPOUT $Lat,"\t", $Long, "\t", $SSID,"\t", $MAC,"\n";
        	}
         elsif ( $FLAG == "0011" )
         	{
         	print WEPOUT $Lat,"\t", $Long, "\t", $SSID,"\t", $MAC,"\n";
        	}
         elsif ( $FLAG == "0012" )
         	{
         	print WEPOUT $Lat,"\t", $Long, "\t", $SSID,"\t", $MAC,"\n";
        	}
         elsif ( $FLAG == "0021" )
         	{
         	print NOWEPOUT $Lat,"\t", $Long, "\t", $SSID,"\t", $MAC,"\n";
        	}
         elsif ( $FLAG == "0031" )
         	{
         	print WEPOUT $Lat,"\t", $Long, "\t", $SSID,"\t", $MAC,"\n";
        	}
         else 
         	{
         	print NOWEPOUT $Lat,"\t", $Long, "\t", $SSID,"\t", $MAC,"\n";
        	}	
         		
         }
      }
} 
#################
#
# cleanup
#
#################
print "Finished!\n";
close IN;
close MAPOUT;
close NIXKOR;
