
@filenames = <dec*.log> ;
$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" ;
}


system "cls" ;

# Declare Subroutines
sub ACTIONORTEST ;
sub DATACLEANUP ;
sub PRINTIT ;

# Initilize Variables
$firstcheckdatecheck = 0 ;
$filecounter = 0 ;
$mymailserver = "\[192.168.254.1\]" ; # word[11]
$linenumber = 0 ;
$whitelister = 0 ;
$actionarraycounter = 0 ;
$testarraycounter = 0 ;
$firstline = 0 ;
$dotcounter = 0 ;

# Open File for reading and pull in data


while ( $filecounter <= $countfilenames ) {
open ( INFILE, $filenames[$filecounter] )   or die "Can't open current.log: $!";
print "\n Opening File : $filenames[$filecounter] " ;
while (<INFILE>) {     				# reads in file line by line
	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] ;
		}
	}	
	if ( $checkdate ne $word[0] ) {
		print "\n corruption in logfile on line : $linenumber \n" ;
		$corruptlines = $corruptlines + 1 ;
		$linenumber = $linenumber + 1 ;
	}	
	else {
		$messID1 = $word[2] ;
			if ( $messID0 !~ $messID1 ) {
				$messagenumber = $messagenumber + 1 ;
				$messagenumber2 = $messagenumber2 + 1 ;
				$multitester = $word[5] ;
					if ( $word[3] =~ /ERROR|WARNING/ ){
						$errwarn = $errwarn + 1 ;
					}
					elsif ( $word[4] eq "whitelisted" ) {
						$otherwhitelist = $otherwhitelist + 1 ;
						if ( $word[11] eq $mymailserver ) {
							$whitelister = $whitelister + 1 ;
						}
					}
					else {
						ACTIONORTEST ; # if first line, still needs to check if action or test
					}
			}
			else {
				if ( $multitester eq $word[5] ) {
					$messagenumber = $messagenumber + 1 ;
				}	
				if ( $word[3] =~ /ERROR|WARNING/ ){
					$errwarn = $errwarn + 1 ;
				}
				elsif ( $word[4] eq "whitelisted" ) {
						$otherwhitelist = $otherwhitelist + 1 ;
						if ( $word[11] eq $mymailserver ) {
							$whitelister = $whitelister + 1 ;
						}
				}

				else {
				ACTIONORTEST ; # check if action or test
				}
			}
		}
		$messID0 = $messID1 ;
		$dotcounter = $dotcounter + 1 ;
		$linenumber = $linenumber + 1 ;
	}
close(INFILE) ;
$filecounter = $filecounter + 1 ;
$firstline = 0 ;
}

$linenumber = $linenumber - 1 ;
$actionarraycounter = $actionarraycounter - 1 ;
$testarraycounter = $testarraycounter - 1 ;

# Start subroutines




DATACLEANUP ;
PRINTIT ;

# ********************  SUBROUTINES BELOW THIS LINE  ************************************

sub PRINTIT {
	system "cls" ;
	$printcounter = 0 ;
	if ( $countfilenames >= 1 ) {
	print "\n Log file dates from : $firstcheckdate1 to $checkdate \n" ;
	} 	
	else {
	print "\n Log file date     : $checkdate \n" ;
	}



	print " Lines Processed   : $linenumber \n" ;
	print " My Mail Server IP : $mymailserver \n" ;
	print " Whitelisted from Internal Server: $whitelister \n" ;
	if ( $errwarn >= 1 ) {
		print "\n \n CAUTION : You have $errwarn WARNINGS/ERRORS in your log file\n" ;
	}
	if ( $corruptlines >= 1 ) {
		print " CAUTION : You have $corruptlines corrupt lines in your log file \n" ;
	}
	print "\n\n Total Messages Logged   : $messagenumber  \n" ;
	print " Unique SMTP ID's Logged : $messagenumber2  \n \n" ;
	print "* ACTIONS LOGGED  * COUNT **** PERCENTAGE ****************************** \n \n" ;
	$totalpercentage = 100 * (  $otherwhitelist / $messagenumber ) ;
	printf( "  White Listed    : %5d  \t %-3.1f  \n"  , $otherwhitelist, $totalpercentage ) ;
	while ( $printcounter <= $actionarraynumber ) {
		$totalpercentage = 100 * ( $finalactionarraynumber[$printcounter] / $messagenumber ) ;
		printf( "  %-15s : %5d  \t %-3.1f  \n"  , $finalactionarray[$printcounter], $finalactionarraynumber[$printcounter], $totalpercentage ) ;
		$printcounter = $printcounter + 1 ;
	}
	$printcounter = 0 ;
	print "\n* TESTS LOGGED  *** COUNT **** PERCENTAGE ****************************** \n \n" ;
	while ( $printcounter <= $testarraynumber ) {
		$totalpercentage = 100 * ( $finaltestarraynumber[$printcounter] / $messagenumber ) ;
		printf( "  %-15s : %5d \t  %-3.1f \n", $finaltestarray[$printcounter], $finaltestarraynumber[$printcounter], $totalpercentage ) ;
		$printcounter = $printcounter + 1 ;
	}
	print "\n************************************************************************** \n" ;
}

# *********************************************************************************************************************************************

sub ACTIONORTEST {
	if ( $word[3] =~ /Msg|E-mail/ ) {
		if ( $word[6] =~ /Weight|Total/ ) {
			$actionarray[$actionarraycounter] = $word[5] ;
			$actionarraycounter = $actionarraycounter + 1 ;
		}
		else {
			$testarray[$testarraycounter] = $word[5] ;
			$testarraycounter = $testarraycounter + 1 ;
		}	
	}
}




# ********************************************************************************************************************************************

sub DATACLEANUP {
print "\n Sorting arrays and cleaning up data \n\n" ;
	$testcounter0 = 0 ;
	$testcounter1 = 1 ;
	$actioncounter0 = 0 ;
	$actioncounter1 = 1 ;
	$testwhilecounter = 0 ;
	$actionwhilecounter = 0 ;
	$temparraycounter = 0 ;
	$tempitemcounter = 1 ;
	@sorted_actionarray = sort(@actionarray) ;
	@sorted_testarray = sort(@testarray) ;
	while ( $actionwhilecounter <= $actionarraycounter ) {
		if ( $sorted_actionarray[$actioncounter0] eq $sorted_actionarray[$actioncounter1] ) {
			$tempitemcounter = $tempitemcounter + 1 ;
		}
		else {
			#print "  Action is : $sorted_actionarray[$actioncounter0] Number : $tempitemcounter \n" ;
			$finalactionarray[$temparraycounter] = $sorted_actionarray[$actioncounter0] ;
			$finalactionarraynumber[$temparraycounter] = $tempitemcounter ;
			$sorted_actionarraynumber[$actioncounter0] = $tempitemcounter ;
			$tempitemcounter = 1 ;
			$temparraycounter = $temparraycounter + 1 ;	
		}
		$actioncounter0 = $actioncounter0 + 1 ;
		$actioncounter1 = $actioncounter1 + 1 ;
		$actionwhilecounter = $actionwhilecounter + 1 ;	
	}
	$actionarraynumber = $temparraycounter - 1 ;
	$tempitemcounter = 1 ;
	while ( $testwhilecounter <= $testarraycounter ) {
		if ( $sorted_testarray[$testcounter0] eq $sorted_testarray[$testcounter1] ) {
			$tempitemcounter = $tempitemcounter + 1 ;
		}
		else {
			#print "  Test is : $sorted_testarray[$testcounter0] Number : $tempitemcounter \n" ;
			$finaltestarray[$temptestcounter] = $sorted_testarray[$testcounter0] ;			
			$finaltestarraynumber[$temptestcounter] = $tempitemcounter ;
			$sorted_testarraynumber[$testcounter0] = $tempitemcounter ;	
			$tempitemcounter = 1 ;
			$temptestcounter = $temptestcounter + 1 ;		
		}
		$testcounter0 = $testcounter0 + 1 ;
		$testcounter1 = $testcounter1 + 1 ;
		$testwhilecounter = $testwhilecounter + 1 ;	
	} 
	$testarraynumber = $temptestcounter - 1 ;
}