#!/usr/perl5/bin/perl -- -*-perl-*-

#------------------------------------------------------------------------
#  Filename	:	search_engine.pl
#  Author	:	Oyewole, Olanrewaju J.
#  Version	:	1.0
#  Date		:	01/05/2002
#  Description	:
#------------------------------------------------------------------------
# This module implements an interface for serving online search requests.
# The module uses the services of the directory_list_lib.pl and the
# search_lib.pl libraries to process search requests, and to display the
# results.
# System Configuration information is retrieved from the
# SystemConfig.pm package.
#
#------------------------------------------------------------------------

use lib qw(./nettech/online_search_engine ../nettech/online_search_engine);
use SystemConfig;
use CGI;

require "directory_list_lib.pl";
require "search_lib.pl";


	my $root_directory	= SystemConfig::root_directory;
	my $data_file		= SystemConfig::data_file_name;
	my $index_file		= SystemConfig::index_file_name;
	my $keyword_file	= SystemConfig::keyword_file_name;
	my $files_path		= SystemConfig::base_file_path;
	my $search_template	= SystemConfig::search_template;
	my $remove_file_path	= SystemConfig::system_file_path;
	my $replace_url		= SystemConfig::http_address;
	my $search_admin_header	= SystemConfig::search_admin_headers;
	my $search_header	= SystemConfig::search_headers;
	my $search_footer	= SystemConfig::search_footers;
	my $search_section	= SystemConfig::search_section;
	my $search_max_records	= SystemConfig::search_max_records;
	my $search_url_base	= SystemConfig::search_url_base;
	my @search_file_types	= SystemConfig::search_file_types;
	my $cgi = new CGI;
	my $admin_password;
	my %cgi_hash;
	my $index_time = gmtime(time);
	my @directoryList;
	my $files_list = '';

	# Print out a content-type for HTTP/1.0 compatibility
	print "Content-type: text/html\n\n";

	# Collect CGI parameters into a local hash array for easy manipulation and reuse.
	foreach $cgi_key ($cgi->param()) {
		$cgi_hash{uc($cgi_key)} = $cgi->param($cgi_key);
	}

	if ($cgi_hash{ADMIN_PASSWORD}) {
		print "\n$search_admin_header";
		if ($cgi_hash{ADMIN_PASSWORD} eq 'olodumare') {
			print "\n<H1>Begin Indexing @ ... $index_time</H1>";
			print "\n<P><HR><P><H3>Building Index and Data files</H3>\n<H4>";

			print "\n<UL><LI>Traversing Directory tree ($root_directory)";
			@directoryList = directory_list($root_directory, 0, @search_file_types);

			print "\n<LI>Building Data file ... ";
			&build_data_file(\@directoryList, $data_file, $keyword_file, $files_path, $remove_file_path, $replace_url);

			print "\n<LI>Reformating Directory entries for web ... ";
			@directoryList = map({ $_ .= "\n"; } @directoryList);
			for (@directoryList) { $_ =~ s/$remove_file_path/$replace_url/; }

			print "\n<LI>Writing Index file ... </UL>";
			&write_index_file(\@directoryList, $index_file, $files_path);

			$index_time = gmtime(time);
			print "\n</H4><HR><H1>Done!<BR>Finished Indexing\@ $index_time</H1><P><HR>";
		} else { print "\n<P><BR><HR><H1>Invalid Password .. please see Administrator</H1><HR>"; }
	} else {
		my @keywords = @{&resolve_keywords($cgi_hash{CGI_SEARCH_STRING})};
		my $document_hash;
		my $document_array = &find_documents($files_path, $keyword_file, $data_file, $cgi_hash{CGI_SEARCH_OPTION}, \@keywords);
		&spool_results($document_array, \@keywords, $search_header, $search_section, $search_url_base, $cgi_hash{CGI_SEARCH_START}, $cgi_hash{CGI_SEARCH_INCREMENT}, $cgi_hash{CGI_SEARCH_TARGET}, $cgi_hash{CGI_SEARCH_OPTION}, $search_max_records);
	}
	print "\n$search_footer";

exit;




