############################### Initilize User Variables #############################################################

$TestThreshHold = 10 ; 		# No Total Actions under this number will not be counted in the final report.. keeps bogus answers out
$HTMLOutput = 1 ; 		# 1 for TEXT Output, 0 for No TEXT output
$HTMLFileName = "Output.txt" ;	# Name of the output file...

#   File Locations.... Change to your site
#@filenames = <//ns1/d\$/Progra\~1/SmarterTools/SmarterMail/Spool/logs/dec*.log> ; # Windows/Samba Share  
#@filenames = <D:/Scripts/Perl/DECLOG/dec*.log> ; 	# Remote drive
@filenames = <dec*.log> ; 				# For Current Directory


######################################################################################################################
# UNLESS YOU ARE A PERL PROGRAMMER, MOLEST BELOW THIS LINE AT YOUR OWN PERIL
#####################################################################################################################
system "cls" ; 


# Declare Subroutines
sub DATEFIX ;
sub DAYBACK ;
sub ACTIONORTEST ;
sub DATACLEANUPTESTS ;
sub DATACLEANUPACTIONS ;
sub PRINTIT ;


# Declare Variables
$firstcheckdatecheck = 0 ;
$filecounter = 0 ;
$linenumber = 0 ;
$whitelister = 0 ;
$actionarraycounter = 0 ;
$testarraycounter = 0 ;
$firstline = 0 ;
$dotcounter = 0 ;



# Get all files that match the name wildcard in the working directory
$countfilenames = @filenames ;
$countfilenames = $countfilenames - 1 ;
if ( $countfilenames < 0 ) { 
	print die "\n  Declog requires at least one file in the directory of type : decMMDD.log \n" ;
}


if ( $ARGV[0] =~ /week|day/ ) {
	DATEFIX ;
}
elsif ( $ARGV[0] =~ /\?|help/ ){
	print " Usage : declog.pl week, report of previous week \n" ;
	print " Usage : declog.pl day,  report of previous day \n" ;
	print " Usage : declog.pl ? or Help, for this output \n" ;
	print " Usage : declog.pl , report of all files on the directory MEMORY INTENSIVE ! \n" ;
	die ; 
}
else {
	print " Processing all files.. MEMORY INTENSIVE ! \n" ;
}


# Open File(s) for reading and pull in data

#die ;


while ( $filecounter <= $countfilenames ) {
	open ( INFILE, $filenames[$filecounter] )   or die "Can't open any log files: $!";
	print "\n Opening File : $filenames[$filecounter] \n" ;
	while (<INFILE>) {     				# reads in file line by line
		$linenumber = $linenumber + 1 ;
		if ( $dotcounter >= 999 ) {
			print "." ;
			$dotcounter = 0 ;	
		}	
	
		@word = split /\s+/, $_ ; 		# breaks lines into space delimited fields
		if ( $firstline =~ 0 ) {
			$messID1 = $messID0 ;
			$firstline = 1 ;
			$checkdate = $word[0] ;
			if ( $firstcheckdatecheck =~ 0 ) {
				$firstcheckdatecheck = 1 ;
				$firstcheckdate1 = $word[0] ;
			}
		}	
	
		
		# Check if the first section is the date, if it is NOT, it's corrupt
		if ( $checkdate ne $word[0] ) {
			#print " corruption on line : $linenumber\n" ;
			$corruptlines = $corruptlines + 1 ;
			#$linenumber = $linenumber + 1 ;
		}	

		# if it is a date, send it down to be processed
		else {
			ACTIONORTEST ;
			$dotcounter = $dotcounter + 1 ;
		}

	}

close(INFILE) ; # Close the file, and open the next, reset counters
$filecounter = $filecounter + 1 ;
$firstline = 0 ;
}

$linenumber = $linenumber - 1 ;
$actionarraycounter = $actionarraycounter - 1 ;
$testarraycounter = $testarraycounter - 1 ;

DATACLEANUPTESTS ;
DATACLEANUPACTIONS ;
PRINTIT ;


# ********************  SUBROUTINES BELOW THIS LINE  ************************************

sub DATEFIX {
	$now = localtime time ;
	@now = split /\s+/, $now ; 
	$filepath = substr($filenames[0], 0, - 11);
	print " File path : $filepath \n" ;
	$fileprefix = "dec" ;
	$fileextension = ".log" ;

	# Convert Month to number
	if ( $now[1] eq Dec ) {
		$MonthNumber = 12 ;
	}		
	if ( $now[1] eq Nov ) {
		$MonthNumber = 11 ;
	}
	if ( $now[1] eq Oct ) {
		$MonthNumber = 10 ;
	}
	if ( $now[1] eq Sep ) {
		$MonthNumber = 9 ;
	}
	if ( $now[1] eq Aug ) {
		$MonthNumber = 8 ;
	}
	if ( $now[1] eq Jul ) {
		$MonthNumber = 7 ;
	}
	if ( $now[1] eq Jun ) {
		$MonthNumber = 6 ;
	}
	if ( $now[1] eq May ) {
		$MonthNumber = 5 ;
	}
	if ( $now[1] eq Apr ) {
		$MonthNumber = 4 ;
	}
	if ( $now[1] eq Mar ) {
		$MonthNumber = 3 ;
	}
	if ( $now[1] eq Feb ) {
		$MonthNumber = 2 ;
	}
	if ( $now[1] eq Jan ) {
		$MonthNumber = 1 ;
	}
	if ( $ARGV[0] eq day ) {
		print " Processing a single day \n" ;
		DAYBACK ;
		if ( $now[2] <= 9 ) {
			$now[2] = "0$now[2]" ;
		}
		$singlefile = "$filepath" . "$fileprefix" . "$MonthNumber" . "$now[2]" . "$fileextension" ;
		$thisloopcounter = 0 ;
		while ( $thisloopcounter <= $countfilenames ) {
			if ( $singlefile eq $filenames[$thisloopcounter] ) {
				if (-e $singlefile) {
					unshift (@tempfilenames, $filenames[$thisloopcounter] ) ;
				}
				else {
					print" File $singlefile does not exist \n" ;
					die ;
				}
			}
		$thisloopcounter = $thisloopcounter + 1 ;
		}
		@filenames = @tempfilenames ;
	}
	else {
		print " Processing a whole week \n" ;
		# Create last 7 day strings
		$thisloopcounter = 0 ;
		while ( $thisloopcounter <= 6 ) {
			DAYBACK ;
			if ( $now[2] <= 9 ) {
				$now[2] = "0$now[2]" ;
			}
			$singlefile = "$filepath" . "$fileprefix" . "$MonthNumber" . "$now[2]" . "$fileextension" ;
			if (-e $singlefile) {
				unshift (@tempfilenames, $singlefile ) ;
			}
			else{
				print " File does NOT exist : $singlefile  \n" ;	
			}
		$thisloopcounter = $thisloopcounter + 1 ;
		}
		@filenames = @tempfilenames ;
	}
# reset the counters, so the top works...
$countfilenames = @filenames ;
$countfilenames = $countfilenames - 1 ;

}

# *******************
sub DAYBACK {
	if ( $now[2] == 1 ) {
		print " It's the First of the Month ! Changing Month... \n" ;
		if ( $MonthNumber =~ /1|5|7|10|12/ ) {
			$now[2] = 30 ;
			if ( $MonthNumber eq 12 ) {
				$MonthNumber = "11" ;
			}
			if ( $MonthNumber eq 10 ) {
				$MonthNumber = "09" ;
			}
			if ( $MonthNumber eq 7 ) {
				$MonthNumber = "06" ;
			}
			if ( $MonthNumber eq 5 ) {
				$MonthNumber = "04" ;
			}
			if ( $MonthNumber eq 1 ) {
				$MonthNumber = "12" ;
			}

		}
		elsif ( $MonthNumber == 3 ) {
			$now[2] = 28 ;
			$MonthNumber = "02" ;
		}
		else {
			$now[2] = 31 ;
			if ( $MonthNumber eq 11 ) {
				$MonthNumber = "10" ;
			}
			if ( $MonthNumber eq 9 ) {
				$MonthNumber = "08" ;
			}
			if ( $MonthNumber eq 8 ) {
				$MonthNumber = "07" ;
			}
			if ( $MonthNumber eq 6 ) {
				$MonthNumber = "05" ;
			}
			if ( $MonthNumber eq 4 ) {
				$MonthNumber = "03" ;
			}
			if ( $MonthNumber eq 2 ) {
				$MonthNumber = "01" ;
			}
		}		
	
	}
	else {
		$now[2] = $now[2] - 1 ;	
	}


}

# ********************
sub PRINTIT {
	system "cls" ;
	$finalactionslength = @finalactions ;
	$finaltestslength = @finaltests ;
	if ( $HTMLOutput = 1 ) {
		open(HTML, "> $HTMLFileName ") ;
		#print "in open segment \n" ; 
	}

	# Print the header
	if ( $countfilenames >= 1 ) {
		print "\n Log file dates from : $firstcheckdate1 to $checkdate \n" ;
		
	} 	
	else {
		print "\n Log file date     : $checkdate \n" ;
	}

	print " Unique SMTP ID's logged $uniquesmtpid \n\n" ;
	print " Unique Tests            Counted   Percentage \n" ;
	print "______________________________________________\n" ;


	# If outputting to file, print to file...
	if ( $HTMLOutput = 1 ) {
		open(HTML, "> $HTMLFileName ") ;
		if ( $countfilenames >= 1 ) {
			print HTML"\n Log file dates from : $firstcheckdate1 to $checkdate \n" ;
		} 	
		else {
			print HTML"\n Log file date     : $checkdate \n" ;
		}
		print HTML" Unique SMTP ID's logged $uniquesmtpid \n\n" ;
		print HTML" Unique Tests            Counted   Percentage \n" ;
		print HTML"______________________________________________\n" ;
	}



	# Print out the Tests....
	$placecounter = 0 ;
	while ( $placecounter < $finaltestslength ) {
		#print " $finaltests[$placecounter] ,  \n" ;

		@printarray = split (/ /, $finaltests[$placecounter]);
		$percentage = 100 * ( $printarray[1] / $uniquesmtpid ) ;
		if ( $percentage < 10 ) {
			printf( "  %-20s : %7d  \t  %3.1f  \n"  , $printarray[0], $printarray[1], $percentage ) ;
			if ( $HTMLOutput = 1 ) {
				printf HTML "  %-20s : %7d  \t  %3.1f  \n"  , $printarray[0], $printarray[1], $percentage ;
			}
		}
		else {
			printf( "  %-20s : %7d  \t %3.1f  \n"  , $printarray[0], $printarray[1], $percentage ) ;
			if ( $HTMLOutput = 1 ) {
				printf HTML "  %-20s : %7d  \t %3.1f  \n"  , $printarray[0], $printarray[1], $percentage  ;
			}
		}

		$placecounter = $placecounter + 1 ;
	} 


	# Print out the Actions.....
	print "\n\n Unique Actions          Counted   Percentage \n" ;
	print "______________________________________________\n" ;

	if ( $HTMLOutput = 1 ) {
		print HTML "\n\n Unique Actions          Counted   Percentage \n" ;
		print HTML "______________________________________________\n" ;
	}


	$placecounter = 0 ;
	while ( $placecounter < $finalactionslength ) {
		#print " $finalactions[$placecounter] \n" ;

		@printarray = split (/ /, $finalactions[$placecounter]);
		$percentage = 100 * ( $printarray[1] / $uniquesmtpid ) ;
		#printf( "  %-20s : %7d  \t %3.1f  \n"  , $printarray[0], $printarray[1], $percentage ) ;
		if ( $percentage < 10 ) {
			printf( "  %-20s : %7d  \t  %3.1f  \n"  , $printarray[0], $printarray[1], $percentage ) ;
			if ( $HTMLOutput = 1 ) {
				printf HTML"  %-20s : %7d  \t  %3.1f  \n"  , $printarray[0], $printarray[1], $percentage  ;

			}
		}
		else {
			printf( "  %-20s : %7d  \t %3.1f  \n"  , $printarray[0], $printarray[1], $percentage ) ;

			if ( $HTMLOutput = 1 ) {
				printf HTML"  %-20s : %7d  \t %3.1f  \n"  , $printarray[0], $printarray[1], $percentage  ;
			}
		}

		$placecounter = $placecounter + 1 ;
	} 

	if ( $HTMLOutput = 1 ) {
		close(HTML) ;
	}


}

# *********************
sub ACTIONORTEST {
		$Wordlength = 0 ;
		$Wordlength = @word ;
		if ( $word[3] eq Cumulative ) {
			$ksub = $word[$Wordlength - 1] ;
			$ksub =~ s/ACTION=// ;
			$ksub =~ s/]// ;
			$ksubtest = "$word[2] $ksub" ;
			#print "Actions = $ksubtest \n" ;
			$actionarray[$actionarraycounter] = $ksubtest ;
			$actionarraycounter = $actionarraycounter + 1 ;
		}
		if ( $word[3] eq Tests ) {
			$placecounter = 6 ;
			$kwordtemp = $word[2] ;
			while ( $placecounter < $Wordlength ) {
				$spotholder = index($word[$placecounter], "=" );				
				$spotword = substr($word[$placecounter], 0, $spotholder);
				# print "    New Element : $spotword \n" ;
				$kwordtemp = "$kwordtemp $spotword" ;
				$placecounter = $placecounter + 1 ;
			}
			#print "Element : $kwordtemp \n" ;
			#push(@testarray, $kwordtemp) ;
			$testarray[$testarraycounter] = $kwordtemp ;
			$testarraycounter = $testarraycounter + 1 ;
		}	
}

# ********************
sub DATACLEANUPTESTS {
print "\n\n Sorting arrays and cleaning up data \n" ;
	local (@sorted_testarray = sort(@testarray)) ;
	undef(@testarray) ;
	$placecounter = 0 ;
	$placecounter2 = 0 ;
	$DupeHolder = abc123 ;
	# Take the sorted Test array, chop it up
	while ( $placecounter <= $testarraycounter ) {
		@testsplit = split (/ /, $sorted_testarray[$placecounter]);			
		$testsplitlength = @testsplit ;
		if ( $DupeHolder eq $testsplit[0] ) {
			#print " :$DupeHolder: :$testsplit[0]: \n" ; 
			}		
		else {
			$placecounter2 = 1 ;
			while ( $placecounter2 < $testsplitlength ) {
				push ( @cleanedtests, $testsplit[$placecounter2] );
				$placecounter2 = $placecounter2 + 1 ;
			}			
		}
		$DupeHolder = $testsplit[0] ;
		$placecounter = $placecounter + 1 ;
	}
	

	# Sort the Array
	local (@sortedcleanedtests = sort(@cleanedtests));
	undef(@cleanedtests) ; 
	# Now take the ordered, cleaned array and count the actions...
	$testcounter = 1 ;
	$placecounter = 0 ;	
	$sortedcleanedlength = @sortedcleanedtests ;
	$DupeHolder = $sortedcleanedtests[1] ;
	while (  $placecounter <= ( $sortedcleanedlength - 1 )  ) {
		if ( $DupeHolder =~ $sortedcleanedtests[$placecounter] ) {
			$testcounter = $testcounter + 1 ;		

		}
		else {
			$AddArray = "$DupeHolder $testcounter" ;
			push ( @finaltests, $AddArray ) ;
			$testcounter = 1 ;
		}


		$DupeHolder = $sortedcleanedtests[$placecounter] ;
		$placecounter = $placecounter + 1 ;
	}

	$AddArray = "$DupeHolder $testcounter" ;
	push ( @finaltests, $AddArray ) ;
	#undef(@sortedcleanedtests) ;

}

# ********************
sub DATACLEANUPACTIONS {
	# Start processing the Action array
	local( @sorted_actionarray = sort(@actionarray)) ;
	$placecounter = 0 ;
	$placecounter2 = 0 ;
	$actioncounter = 1 ;
	$testcounter = 0 ;
	$sorted_actionarraylength = @sorted_actionarray ;	
	$DupeHolder = abc123 ;
	while ( $placecounter <= $sorted_actionarraylength ) {
		@actionsplit = split (/ /, $sorted_actionarray[$placecounter]);
		if ( $actionsplit[0] = $DupeHolder ) {
			if ( $actionsplit[1] =~ TAKEN ) {
				$actionsplit[1] = WHITELISTED ;
			}
			push ( @cleanedactions, $actionsplit[1] );
			$uniquesmtpid = $uniquesmtpid + 1 ;
		}
		else {
			print " Dupe ! \n " ;
		}
		$DupeHolder = $actionsplit[0] ;
		$placecounter = $placecounter + 1 ;
	}

	$uniquesmtpid = $uniquesmtpid - 1 ;
	local (@cleanedandsortedactions = sort(@cleanedactions)) ;	
	undef(@cleanedactions) ;
	$sorted_actionarraylength = @cleanedandsortedactions ; 		
	$placecounter = 1 ;
	$testcounter = 1 ;
	$junkholder = shift ( @cleanedandsortedactions ) ;
	$Dupeholder = $cleanedandsortedactions[1] ;
	while ( $placecounter <= $sorted_actionarraylength ) {
		if ( $Dupeholder ne $cleanedandsortedactions[$placecounter] ) {		
			$AddArray = "$Dupeholder $testcounter" ;
			if ( $testcounter >= $TestThreshHold ) {
				push ( @finalactions, $AddArray) ; 
				$testcounter = 0 ;
			}
			else {
				$uniquesmtpid = $uniquesmtpid - $testcounter ;
			}
			
		}
		$testcounter = $testcounter + 1 ;		
		$Dupeholder = $cleanedandsortedactions[$placecounter] ;		
		$placecounter = $placecounter + 1 ;
	} 
	undef(@cleanedandsortedactions) ;
}