#!/usr/bin/perl

use Storable;
use Data::Dumper;

my $indexfile = "./aquinasindex.storable";
my @sources;

use lib "./";
# use Aquinas qw(indexfile books);
require "./Aquinas.pl";

my %index;
my $count = 0;
my $allBooks;
my @allBooksList;

foreach my $b (keys(%books)) {
#	print "B: $b, @{$b}\n";
	push (@allBooksList, @{$books{$b}});
	}

foreach my $b (keys(%books)) {
	print "B: $b, @{$books{$b}}\n";
	foreach $bk(@{$books{$b}}) {
		print "$bk -> $b\n";
		$reverseBooks{$bk} = $b;
		}
	}


$allBooks = join('|', @allBooksList);

# These can also be chapters
# @alsoChapters = ("ultimo");
%alsoChapters = ("ultimo" => 1, "Ultimo" => 1);

$allChapters = "[MDCLXVI]+|ultimo";

# Read the file with URLs
open(FH, '<', "./aquinas_urls.txt") or die $!;
while (<FH>) {
	$url = $_;
	$url =~ m/.*\/(.*)$/;
	if ($1) {
		my $file = $1;
		$file =~ s/\~//g;
		push @sources, $file;
		}
	}

my $tick = 0;

print "ALL BOOKS: $allBooks\n";
print "ALL CHAPTERS $allChapters\n"; 
# exit;

# With the array, read the local files (see fetch.pl)
while (my $file = pop @sources) {
	open FILE, "<", "./source/$file";
	
	my @splits = split(/\./, $file);
	my $section = $splits[0];
	
	#print "OPENING ./source/$file\n";
	my $wholefile = do { local $/; <FILE> };

	$wholefile =~ m/<div id="vl_ul" .*?>(.*?)<\/div>/sm;
	$content = $1;
	#print "WHOLEFILE: $wholefile\n";
	#print "CONTENT: $content\n";
#	last if ++$tick > 100;
	
	close FILE;

	if ($content eq $remembered) { 
		# print "Duplicate contetn\n"; 
		next; 
		}

	$remembered = $content;

		
	while ($content =~ m/<vl-r id="(.*?)">(.*?)<\/vl-r>/gsm) {
		my $node  = $1;
		my $block = $2;
		my $book = undef;
		# print "-----\n";
		# print "BLOCK: $block\n";
		$block =~ m/<vl-c.*?>(.*?)<\/vl-c>.*?<vl-c.*?>(.*?)<\/vl-c>/sm;
		#my $english = $2;
		#my $latin   = $1;
		my $english = $2;
		my $latin   = $1;
		
		# Simple strip HTML
		$english =~ s|<.+?>||g;
		$latin   =~ s|<.+?>||g;

		$orig = $latin;
		# Do we have a "book" in this paragraph
		# if ($latin =~ s/.*?\b($allBooks)\b//sm) {
		
#		my @words = split(/\s+/, $latin);
		my @words = split(/[\s[:punct:]]+/, $latin);
		my $distance;
		
		while ($word = shift(@words)) {

	#		print "$word - ";

			# See if we have a book
	#		if (indexOf($word, @allBooksList)) {
			if ($reverseBooks{$word}) {
				# $book = $1;
				$book = $word;
	 			#print "https://aquinas.cc/la/en/~$file\n";
	 			#print "Found BOOK: $book\n";
	 			#print "IN BLOCK: $orig\n";
				$distance = 5;
				}

			# See if we have a chapter
			if ($word =~ m/\b[MDCLXVI]+\b/ || $alsoChapters{$word}) {
				$chapter = $word;
		 		# print "Found CHAPTER: $word\n";
			
				if ($book && $chapter) {

		 			$count ++;
					#print "IN BLOCK: $orig\n";
					$distance = 5;
					
					my %instanse;
					$instanse{latin} = $latin;
					$instanse{english} = $english;
					$instanse{verse} = "";
					$instanse{link} = "https://aquinas.cc/la/en/~$file";
										
					my $fullname = $reverseBooks{$book};
					
					push(@{$index{$fullname}{$chapter}}, \%instanse);

					print "BOOK AND CHAPTER: $fullname/$chapter\n";
					print "$file\n";
		 			print "-------\n";
					
					}
				}
		
			if ($distance > 0) { $distance --; } else { $book = undef; }
		
#		if ($latin =~ s/.*?\b($allBooks)\b//smi) {
#			$book = $1;
# 			print "https://aquinas.cc/la/en/~$file\n";
# 			print "1st BOOK: $book\n";
# 			
# 			}
# 		
# 		while ($book && $latin =~ m/\b($allBooks|$allChapters)\b/gsm) {
# 			$match = $1;
# 			print "MATCH: $match\n";
# 			
# 			# Is it a book or a chapter
# 			if ($allBooks =~ $match) {
# 				$book = $match;
# 				print "Match is new Book: $book\n"
# 				}
# 			# elsif ($allChapters =~ $match) {
# 			elsif ($match =~ m/\b$allChapters\b/) {
# 				print "BOOK AND CHAPTER: $book/$match\n";
# 				print "IN BLOCK: $orig\n";
# 				}
# 			else {
# 				print "DONT'T KNOW WHAT TO DO WITH --$match--: $orig\n";
# 				}
# 			

			}
		# print "EN: $english\nLA: $latin\n-----\n\n";
		}
	}
 
store \%index, $indexfile;
 
sub indexOf { 
	($word, @array) = @_; 
	foreach (0..$#array) { 
		if ($array[$_] eq $word) { 
			return $_; }
		} 
	return 0; 
	}

print "ALL BOOKS: $allBooks\n";
print "Hits: $count\n"
