#!/usr/bin/perl -w
use strict;
use warnings; 
undef $/;

##################################################################################################
#######################<<<<<<<<<<<<MESH-ACTIVITY>>>>>>>>>>>>######################################
##################################################################################################
my $file = "log_MP-25_010112.log";
#Getting Specific logfile from Main Logfile
my $output = "temp.log";

# Using the open() function to open the log file.
unless(open INPUT, $file) {
	# Die with error message 
	# if we can't open it.
	die "\nUnable to open $file\n";
}

# To create the output file
	# (open it for writing)
	unless(open OUTPUT, '>'.$output) {
		die "\nUnable to create '$output'\n";
	}

# Read the file one line at a time.
while(my $file = <INPUT>) {
       #For extracting the print "Originator:$1 last-seen(#/255):
       # $2 Nexthop [outgoingIF]:$3 Potential nexthops:$4\n" 
       print ">>>>>>>>>>>>> DATA LOGFILE EXTRACTION <<<<<<<<<<<<<<<<<\n";
       my $data;
       $data = $1 if $file =~ s/(Originator[\s\S]+?)client/client/ms;
       my @fields = split( /^\s+/, $data);
       
       my @iplists = split(/\n/, $data);
       foreach my $ipl (@iplists) {
       
       my @asdf = split(/:/, $ipl);
       #print "the preamble info:\n";
       #print $asdf[0], "\n";
       my @arr = split(/\+s/,$asdf[0]);
       #print "XXXXXXX @arr\n";
       #print "all the hops:\n";
       while( $asdf[1] =~ s/(\d{0,3}\.\d{0,3}\.\d{0,3}\.\d{0,3}\s*\([\s\d]+\))// ){
             # print $1, " \n";
       }
}
       print "\n";       
       print OUTPUT "Main head\n @fields\n";
       #print "\n";
       #For extracting the Device Mac number and each node Ip address
       print OUTPUT "Device Mac Addresses || Node Ip Address:\n";
        my $cnt = 0;
       while( $file =~ s/(\w{2}:\w{2}:\w{2}:\w{2}:\w{2}:\w{2})\s+(([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3}))//mis ){
       print OUTPUT "$1 $2\n";
 
     }   
       #Extracting the RSSI
       $data = $1 if $file =~ s/(ADDR[\s\S]+?)kernel/kernel/ms;
       my @fieldRssis = split( /\n+/, $data); #Getting the entire RSSI table printing 
       my $i=0;
       while($i <= $#fieldRssis ){

       		my @newfield = split(/\s+/, $fieldRssis[$i]);
       		# split $fieldsRassis[0] on whitespace
      		       # split $fieldsRassis[1] on whitespace
      		       # split $fieldsRassis[2] on whitespace, and so on
       		#print OUTPUT "New Rssis\n@newfield";
       		$i++;

       		my $count = 0;
       		
              while (($count <= $#newfield) && ($count <= 0)){
            		 # print OUTPUT $newfield[$j],"\n";
            		 chomp;
            		print OUTPUT "$newfield[$count]";
            		 $count++;           		
            		}
            	
            	my $j = 0;
      		while ($j <= $#newfield){
            		 # print OUTPUT $newfield[$j],"\n";
            		 chomp;
            		
                       $j++; 
 		
            		if ($j ==4){
                        print OUTPUT "\t\t$newfield[$j]";           		
            		}
            	}
       my $cnt = 0;		
       while ($cnt <= $#newfield){
            		 # print OUTPUT $newfield[$j],"\n";
            		 chomp;
            		
                       $cnt++; 
 		
            		if ($cnt ==3){
                         print OUTPUT "\t\t\t\t$newfield[$cnt]","\n";           		
            		}
            	}	
       }
       
       #For extracting the rate data transmission
       $file =~ s/(rate data[\s\S]+)rate data/rate data/ms;
       print OUTPUT "\n$1";
 
       # find the second data rate table
       $file =~ s/(rate data[\s\S]+lookaround\s+\d+)//ms;
       print OUTPUT "$1";
    
       
       #For extracting the packet and Recieved transmission. 
       #print "Doing packets from ping\n";
       $data = '';
       #$data = $1 if $file =~ m/(\d+(\d+) packets transmitted[^\n]+time\s+\d+)/g;
       my @fieldPacket = $file =~ m/(\d+(\d+) packets transmitted[^\n]+time\s+\d+)[^\n]/g; 
       my $k = 0;
       while ($k <= $#fieldPacket){
              print OUTPUT "\n", $fieldPacket[$k],"\n";
              $k++;
       } 
       
       #for wifi-device "wifi0" interface;
       #print "Doing config\n";
       $data='';
       $data = $1 if $file =~ s/(config[\s\S]+[^\n])/(config[^\n])/s;
       # Write the line to the output file.
       print OUTPUT "wifi0 interface\n$data\n";
       #my @field = split( /\s+\n/, $data);
       #print @field;
       
       #For extracting the Wireless setting
       #print "Wireless setting\n";
       $data='';
       #$data = $1 if $file =~ s/(ath0 [\s\S]+Noise level\s+\d+)//;
       $data = $1 if $file =~ s/(^ath0[\s\S]+)ath0/ath0/sm;
       # Write the line to the output file.
       print OUTPUT "Wireless setting\n$data\n";
      
       #For extracting the BAT0 Wireless setting
       $data='';
       $data = $1 if $file =~ s/(^ath0[\s\S]+)bat0/bat0/sm;
       # Write the line to the output file.
       print OUTPUT "BAT0 Wireless setting\n$data\n";

}
# Close the files.
close INPUT;
close OUTPUT;
print ">>>>>>>>>>>> BEFORE REDUCTION <<<<<<<<<<<<\n";
my $filesize = -s "log_MP-25_010112.log"; 
# the -s operator returns the file size in bytes 
# the result in kilobyte and megabyte
my $carlos_file = ($filesize)/(1024 * 1024);
 
print "The collected Logfile size is $filesize in kilo-bytes from the Mesh Potatoes\n";
print "Logfile size = $carlos_file MB\n"; 
print "\n";
print ">>>>>>>>>>>> AFTER REDUCTION <<<<<<<<<<<<\n";
my $file_size = -s "temp.log";
my $bode_file =  ($file_size)/(1024 * 1024);
print "Processed logfile size is $file_size in kilo-bytes\n";
print "Logfile size = $bode_file  MB\n";
print "\n";
print ">>>>>>>>>>>>> DATA EXTRACTION SUCCESS <<<<<<<<<<<<<<<<<<<<<\n";
exit 0;
