#!/usr/bin/perl

use lib '/home/slowtwitch/slowtwitch.com/cgi-bin';

# The CGI.pm module is by far the best CGI module for Perl
use CGI qw(:standard);

# This library keeps me honest and enforces rules I need to
# to follow, ESPECIALLY for mod_perl
use strict;

# Disables strict referencing... Gotta do this to allow 
# package-based config file
no strict 'refs';

# Ah yes... The wonderful Spindex configuration and 
# search routine modules themselves. :)
use Spindex::config;
use Spindex::search;

# Yes it is lame- Deal with it. :)
my $VERSION="4.1.5";

#Globally define the "return sequence" 
my $RET="\n";

#Let's fix the time so mod_perl works ok... :o)
$^T=time;

# Invoked by 'Print_Page' when it happens upon an HTML 
# comment in a template.
# Syntax: Handle_Comment($cgi-process,$line-with-a-comment);
sub Handle_Comment {
	my($cur,$cline)=@_;

	# Make sure it is a real comment, all on one line
	# and strip out the innards (shoving the real comment into $1)
	if($cline =~ /<!--(.+)-->/) {
		my $comment=$1;
		$comment =~ s/\s//g; # strip all whitespace	
	
		# COMMENT DISPATCH TABLE

		if($comment =~ /(.+)RESULTS(.+)/) {
			# Ok, put the results here, formatted as
			# requested.
			my $sf=$1; # opening formatting
			my $ef=$2; # ending formatting
			for(Handle_Submission($cur)) {
				print "$sf$_$ef$RET"
			}
		} #endif
	} #endif
} #end Handle_Comment

# Print a Spindex template page
# Syntax: Print_Page($cgi-process)
sub Print_Page {
	my $cur=shift;
	# Open the normal template
	unless(-e "$templateFile") { die "File does not exist: $templateFile\n"; }
	open(TEMPL,"<$templateFile") or die "Cannot open $templateFile.\n";

	# Spit the expiration jazz to the browser to control 
	# content caching. NOTE: Usually ignored by IE. :(
	print header(-expires=>"$expire");
	while(<TEMPL>) {
		my $tline="$_";
		# Print the line to the browser, or else handle
		# it as a comment
		unless($tline =~ /<!--/) { print "$tline"; }
		else { Handle_Comment($cur,"$tline"); }
	}
	close TEMPL;
}

# My default logging mechanism. $logfile must be writable by the
# Apache user (usually nobody)
# Syntax: Write_Log(@array-of-stuff-to-write);
sub Write_Log {
	my @msg=@_;
	unless(defined($logFile)) { return; }
	open(SPL,">>$logFile") or warn "Couldnot open $logFile\n";
	# Automattically add the local time
	print SPL scalar(localtime(time))," - ",@msg,"\n";
	close SPL;
}

# Called from Handle_Comment when a RESULTS comment is found.
# Syntax: @results=Handle_Submission($cgi-process);
sub Handle_Submission {
	my $cur=shift;
	unless($cur->param('searchoption')) {
		# Not a POST
		if($allDefault) {
			# If they want a full index, give it to 'em.
			$BYDATE=0;
			# Search for '.' (everything)
			return(Do_Search("."));
		} else {
			# Otherwise, we are done.
			return;
		}
	} #end unless

	# If they are searching BYDATE, set it, else clear it.
	if($cur->param('searchoption') eq "By Date") { $BYDATE=1; }
	else { $BYDATE=0; }

	my $toSearch;
	my @SearchInDirs=();

	if(defined(%OptionSearch)) {
		@SearchInDirs=$cur->param('searchIn');
	}

	# If we're searching BYDATE, set $toSearch to the number of days,
	# otherwise, set it to the search term.
	if($BYDATE) {
		$toSearch=$cur->param('updateDays');
	} else {
		$toSearch=$cur->param('searchFor');
		# Log it if $logSearches is set
		if($logSearches) { Write_Log($ENV{REMOTE_ADDR}," - $toSearch"); }
	}
	return(Do_Search("$toSearch",@SearchInDirs));
}

{##############MAIN

	my $client=new CGI();

	# Make it so
	Print_Page($client);

}
