#!/usr/bin/perl -w

use Tie::File;
use CGI;
use Fcntl 'O_RDONLY';
require "info.pl";

$query = new CGI;
$word = $query->param("term");
$boolword = $query->param("boolterm");
$book = $query->param("verse");
$verse = $book;
$Andor = $query->param("Andor");
$NextWord = $query->param("secondterm");
$SearchBook = $query->param("limit");

print "status:200 OK\ncontent-type: text/html\n\n";
$current = "Bible.txt";

tie @array, 'Tie::File', $current, mode => 'O_RDONLY' or die "Unable to tie file\n";

%numbers = ("1" => "1st ", "2" => "2nd ", "3" => "3rd ");

%andorValues = ("1" => "and", "2" => "or", "3" => "but not");

while(($key, $value) = each %books){#create a reverse hash of the hash %books
	$bookValues{$value} = $key;
}
foreach $val (keys %filePos){
	($a, $b) = split ':', $filePos{$val};
	$endPos{$val} = $b; #make a hash of the file positions of the end of each book
	$startPos{$val} = $a; #make a hash of the file positions of the start of each book
}
while(($key, $value) = each %startPos){ #reverse the keys and values of the startPos hash
	$RevStartPos{$value} = $key;
}
$num_lines = 0;
$hits = 0;
$bookhits = 0;
$next = 0;
$counter = 0;
$currBook = "Ge";
$found == 0;

if($SearchBook != 9){
	($a,$b) = split ':', $filePos{$SearchBook};
for $i($a..$b){
	$line = $array[$i];
	$line = $line."\n";#this is done to match the line again
	$copyLine = $line;
		if($endPos{$currBook} < $num_lines || ($num_lines + $a) == $b){ #if not in the current book
			if($bookhits > 0){
				if($SearchBook != 9){
					$currBook = $SearchBook;
				}
				print "<br><a 
href=\"verse.cgi?book=$currBook&word=$word&next=$NextWord&Andor=$Andor&boolword=$boolword\">$books{$currBook} had $bookhits hits</a></br>\n";
			}
			$currBook = $RevStartPos{$num_lines};
			$bookhits = 0;	
		}
if($NextWord){
	if($Andor == 1){#in the case of 'and'
		if($line =~ m/^$boolword\W|\W$boolword\W/ig){
			if($copyLine =~ m/^$NextWord\W|\W$NextWord\W/i){
				$hits++;
				$bookhits++;
			}
		}		
	}
	if($Andor == 2){#in the case of 'or'
		if($line =~ m/^$boolword\W|\W$boolword\W/ig){
			$hits++;
			$bookhits++;
		}
		else{
			if($copyLine =~ m/^$NextWord\W|\W$NextWord\W/i){
				$hits++;
				$bookhits++;
			}
		}
	}
	if($Andor == 3){#in the case of 'but not'
		if($line =~ m/^$boolword\W|\W$boolword\W/ig){
			if($copyLine =~ m/^$NextWord\W|\W$NextWord\W/i){
			}
			else{
				$hits++;
				$bookhits++;
			}
		}
	}#end Andor
}
else{
	while($line =~ m/^$word\W|\W$word\W/ig){
		$hits++;
		$bookhits++;
	}
}
$num_lines++;
}#end for
}#end if SearchBook
else{
open(FILE, $current) or die "Could not open text";
while($line = <FILE>){
	$copyLine = $line;
		if($endPos{$currBook} < $num_lines){ #if not in the current book
			if($bookhits > 0){
				print "<br><a 
href=\"verse.cgi?book=$currBook&word=$word&next=$NextWord&Andor=$Andor&boolword=$boolword\">$books{$currBook} had $bookhits hits</a></br>\n";
			}
			$currBook = $RevStartPos{$num_lines};
			$bookhits = 0;	
		}
if($NextWord){
	if($Andor == 1){#in the case of 'and'
		if($line =~ m/^$boolword\W|\W$boolword\W/ig){
			if($copyLine =~ m/^$NextWord\W|\W$NextWord\W/i){
				$hits++;
				$bookhits++;
			}
		}		
	}
	if($Andor == 2){#in the case of 'or'
		if($line =~ m/^$boolword\W|\W$boolword\W/ig){
			$hits++;
			$bookhits++;
		}
		else{
			if($copyLine =~ m/^$NextWord\W|\W$NextWord\W/i){
				$hits++;
				$bookhits++;
			}
		}
	}
	if($Andor == 3){#in the case of 'but not'
		if($line =~ m/^$boolword\W|\W$boolword\W/ig){
			if($copyLine =~ m/^$NextWord\W|\W$NextWord\W/i){
			}
			else{
				$hits++;
				$bookhits++;
			}
		}
	}#end Andor
}
else{
	while($line =~ m/^$word\W|\W$word\W/ig){
		$hits++;
		$bookhits++;
	}
}
$num_lines++;
}#end while
}#end else

if($boolword){
	print "<br> You had <b>$hits</b> hits on your search for <b>$boolword</b> $andorValues{$Andor} <b>$NextWord</b></br>";
}
else{	
	print "<br>You had $hits hits on your search for $word over $num_lines lines</br>";
}



