#!/usr/bin/perl
use Digest::SHA1 qw(sha1 sha1_hex);
my @fileName;
my @fileTwoName;
my $inputNameOne;
my $inputNameTwo;
my $seqCount = 1;
my %seqHash;
my $counter = 0;
if(@ARGV != 3){
        die "Parameter Error!\n";
}
while(1){
	@fileName = glob $ARGV[0]."/*.$counter";
	@fileTwoName = glob $ARGV[1]."/*.$counter";
	if(@fileName == 0 || @fileTwoName == 0){
		last;
	}
	foreach $inputNameOne (@fileName){
		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;
			open FILEONE, "<$inputNameOne";
			open FILETWO, "<$inputNameTwo";
			$array[0][0] = 0;
			while(<FILEONE>){
				if($_ =~ /\d\d:\d\d:\d\d\.\d\d\d\d\d\d ([\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 = 1, $maxString = 0; $count <= @fileOneSeq; $count++){
				for($countTwo = 1; $countTwo <= @fileTwoSeq; $countTwo++){
					if($fileOneSeq[$count - 1] =~ $fileTwoSeq[$countTwo - 1]){
						$array[$count][$countTwo] = $array[$count - 1][$countTwo - 1] + 1;
						if($array[$count][$countTwo] > $maxString){
							$maxString = $array[$count][$countTwo];
							$maxX = $count;
							$maxY = $countTwo;
						}
					}
					else{
						$array[$count][$countTwo] = 0;
					}
				}
			}
			for($count = 0; $count < $maxString; $count++){
				push @resultOutput, $fileOneSeq[$maxX - $count - 1]."\n";
			}
			$tmpHashValue = sha1_hex(reverse @resultOutput);
			if($maxString > 1 && $seqHash{$tmpHashValue} != 1){
				$seqHash{$tmpHashValue} = 1;
				open FILEOUTPUT, ">>$ARGV[2]/$tmpHashValue.sq.$counter";
				$seqCount++;
				print FILEOUTPUT (reverse @resultOutput);
				close FILEOUTPUT;
			}
		}
	}
	$counter++;
}
