#!/usr/bin/perl
use Digest::SHA1 qw(sha1 sha1_hex);
my @fileName;
my @fileTwoName;
my $inputNameOne;
my $inputNameTwo;
my $maxMatch = 0;
my $round = 0;
my $oneCountMatch;
my $showRound = 0;
my %seqHash;
my $thisDocName;
my $benignProbFile = "probability_of_benign.log";
my $maliciousProbFile = "probability_of_malicious.log";
my $benignNum = 300;
my $maliciousNum = 29;
my $seqThreshold = 0;
my %bEntry;
my %mEntry;
my %probHash;

my $seqThreshold = 100;

if(@ARGV != 2){
        die "Parameter Error!\n";
}
@fileName = glob $ARGV[0]."/*";

open FILEIN, "<$benignProbFile";
while(<FILEIN>){
	$_ =~ /\/([a-z0-9\.]*):(\d*)$/;
	$bEntry{$1} = $2;
}
close FILEIN;
open FILEIN, "<$maliciousProbFile";
while(<FILEIN>){
	$_ =~ /\/([a-z0-9\.]*):(\d*)$/;
	$mEntry{$1} = $2;
}

my @ducumentTwoName = glob $ARGV[1]."/*";
foreach $thisDocName (@ducumentTwoName){
	my $mProbability = 1;
	my $bProbability = 1;
	$seqHash{$thisDocName} = 0;
	$seqHash{$thisDocName} = 0;
	@fileTwoName = glob $thisDocName."/*";
	print $showRound++, ":", $thisDocName, "\n";
	
foreach $inputNameOne (@fileName){
	$oneCountMatch = 0;
	foreach $inputNameTwo (@fileTwoName){
		my %hash;
		my $count;
		my $countTwo;
		my @fileOneSeq;
		my @fileTwoSeq;
		my $maxString;
		my @array;
		my $maxX;
		my $maxY;
		my @resultOutput;
		my $tmpHashValue;
		my $checkPoint;
		my $testProb;
		open FILEONE, "<$inputNameOne";
		open FILETWO, "<$inputNameTwo";
		$array[0][0] = 0;
		while(<FILEONE>){
			if($_ =~ /^([\w\d]+)$/){
				push @fileOneSeq, "$1";
				$array[@fileOneSeq][0] = 0;
			}
		}

		while(<FILETWO>){
			if($_ =~ /\d\d:\d\d:\d\d\.\d\d\d\d\d\d ([\w\d]+)\(/){
				push @fileTwoSeq, "$1";
				$array[0][@fileTwoSeq] = 0;
			}
		}

		close FILEONE;
		close FILETWO;
		for($count = 0, $maxString = 0, $countTwo = 1; $countTwo <= @fileTwoSeq; $countTwo++){
			if($fileOneSeq[$count] =~ $fileTwoSeq[$countTwo - 1]){
				my $counterXX;
				my $counterYY;
				my $seqLength = 0;
				for($counterXX = $count, $counterYY = $countTwo - 1; $counterXX < @fileOneSeq && $counterYY < @fileTwoSeq; $counterXX++, $counterYY++){
					if($fileOneSeq[$counterXX] =~ $fileTwoSeq[$counterYY]){
						$seqLength++;
					}
					else{
						last;
					}
				}
				if($maxString < $seqLength){
					$maxString = $seqLength;
				}
			}
		}
		$inputNameOne =~ /\/([a-z0-9\.]*)$/;
		$testProb = (($mEntry{$1} / $maliciousNum) * ($maliciousNum / ($maliciousNum + $benignNum))) / ((($mEntry{$1} / $maliciousNum) * ($maliciousNum / ($maliciousNum + $benignNum))) + (($bEntry{$1} / $benignNum) * ($benignNum / ($maliciousNum + $benignNum)))) * 100;
		if($maxString == @fileOneSeq && $testProb >= $seqThreshold){
			$seqHash{$thisDocName}++;
			$inputNameOne =~ /\/([a-z0-9\.]*)$/;
			$mProbability = $mProbability * ($mEntry{$1} / $maliciousNum);
			$bProbability = $bProbability * ($bEntry{$1} / $benignNum);
			last;
		}
	}
}

if($seqHash{$thisDocName} > 0){
	$probHash{$thisDocName} = ($mProbability * $maliciousNum / ($maliciousNum + $benignNum)) / (($mProbability * $maliciousNum / ($maliciousNum + $benignNum)) + ($bProbability * $benignNum / ($maliciousNum + $benignNum)));
}
else{
	$probHash{$thisDocName} = 0;
}
print "Result: ", $probHash{$thisDocName}, "\n";

}
open LOGFILE, ">log_of_detection.log";
foreach (sort keys %seqHash){
	print LOGFILE $_, ":", $seqHash{$_}, "\t\t\t(", $probHash{$_}, ")\n";
}
