#
# perl script for kimset .csv files konvert 2 mappoirn 2004 english
#
# (c) by wardriving.ch
# 
# release:  1.0b
# date   :  24.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 	"Latitude\tLongitude\tName\tAddress 1\tName 2\n";
print NIXKOR 	"Latitude\tLongitude\tName\tAddress 1\tName 2\n";
print WEPOUT 	"Latitude\tLongitude\tName\tAddress 1\tName 2\n";
print NOWEPOUT 	"Latitude\tLongitude\tName\tAddress 1\tName 2\n";
print TOTAL 	"Latitude\tLongitude\tName\tAddress 1\tName 2\n";

#################
#
# read / processing / write Data
#
#################

while (<IN>)
{
chomp;
   if (m/^Network/) 
      {
      print "Found a header line - skipping.\n";
      }
   else
      {
      ($Lat, $Long, $SSID, $MAC, $FLAG) = (split /;/)[18,19,2,3,7];
      chomp $Lat, $Long, $SSID, $MAC, $FLAG;
      $FLAG=~s/Yes/WEP/;
      $FLAG=~s/No/NOWEP/;
      chomp $Lat, $Long, $SSID, $MAC, $FLAG;
 
      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.000000") or ($Long =~ "-0.000000"))
         {
         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;
