#Discus board search script
#-------------------------------------------------------------------------------
#This script is copyright (c) 1997-98 by Kevin W. Paulisse and William F. Polik,
#all rights reserved.  You may not modify or delete this copyright header.
#The use of this product is subject to a license agreement.  Contact Kevin
#Paulisse (paulisse@mulliken.chem.hope.edu) for further information.
#-------------------------------------------------------------------------------

open (FILE, "$discus_conf");
@file = <FILE>;
close (FILE);
$evals = "";
foreach $line (@file) {
	if ($line =~ /^(\w+)=(.*)/) {
		$varname = $1;
		$value = $2;
		$value =~ s/'/\\'/g;
		$evals .= "\$$varname='$value'; ";
	}
}
eval($evals);
require "$admin_dir/source/src-board-subs-common";
&parse_form;

if ($FORM{'query'} eq "") {
	($bg, $tx, $li, $vl, $al, $face, $size, $image) = &ex('extract_colorsonly', 1);
	$str = "$L{BSCHTITLE}";
	&header;
	&ex('printuntil', 1, 1, 0, "$L{BSCHTITLE}");
	print <<EOFILE;
<FONT SIZE=+1><CENTER><B>$L{BSCHTITLE}</B></CENTER></FONT>
<HR>
<H4>$L{BSCHSIMPLE}</H4>
<FORM ACTION="$script_url/board-search.$cgi_extension" METHOD=POST>
<TABLE><TR><TD><FONT FACE="$face" SIZE="$size">$L{BSCHSEARCHFOR}</FONT></TD>
<TD><INPUT TYPE=text NAME=query SIZE=40></TD></TR>
<TR><TD><FONT FACE="$face" SIZE="$size">$L{BSCHSEARCHIN}</FONT></TD>
<TD><SELECT NAME=topic SIZE=1>
<OPTION VALUE=all>$L{BSCHALLTOPICS}
EOFILE
	open (TOPIC, "$message_dir/board-topics.html");
	@topic = <TOPIC>;
	close (TOPIC);
	foreach $line (@topic) {
		if ($line =~ /<!-Top: (\d+)-!>/) {
			$num = $1;
			&extract ("//$num/$num.$ext");
			print "<OPTION VALUE=$num>$topic_name\n";
		}
	}
	print "</SELECT>\n";
	print "<INPUT TYPE=SUBMIT VALUE=\"$L{BSCHBUTTONTEXT}\"></TD></TR></TABLE>";
	print "<HR><H4>$L{BSCHADVANCEDOPTIONS}</H4>\n";
	print "<TABLE><TR><TD>";
	print "<INPUT TYPE=CHECKBOX NAME=number VALUE=on></TD><TD><FONT FACE=\"$face\" SIZE=\"$size\">$L{BSCHRETURNBEST}</FONT></TD></TR>\n";
	print "<TR><TD><INPUT TYPE=CHECKBOX NAME=time VALUE=on></TD><TD><FONT FACE=\"$face\" SIZE=\"$size\">$L{BSCHWITHIN}";
	print "</FONT></TD></TR>\n";
	print "</TABLE>\n";
	print "</FORM><HR><BR>\n";
	for ($i = 3; $i <= 17; $i += 2) {
		&ex('printuntil', $i, $i, 0, "");
	}
	exit(0);
}

&header;
&ex('printuntil', 1, 1, 0, "$L{BSCHRESULTS}");
print "<FONT SIZE=+1><CENTER><B>$L{BSCHRESULTS}</B></CENTER></FONT><HR>\n";
$_ = $FORM{'query'};
undef @optional; undef @badwords; undef @required;
while (/([\+\-]?)"([^"]*)"/) {
	($prefix, $phrase) = ($1, $2);
	$_ = join(" ", $`, $');
	$phrase =~ s/\\//g;
	$phrase =~ s/([^\w\s])/\\$1/g;
	push (@optional, $phrase) if $prefix eq "";
	push (@badwords, $phrase) if $prefix eq "-";
	push (@required, $phrase) if $prefix eq "+";
}
while (/([\+\-]?)(\w+)/) {
	($prefix, $phrase) = ($1, $2);
	$_ = join(" ", $`, $');
	$phrase =~ s/\\//g;
	$phrase =~ s/([^\w\s])/\\$1/g;
	push (@optional, $phrase) if $prefix eq "";
	push (@badwords, $phrase) if $prefix eq "-";
	push (@required, $phrase) if $prefix eq "+";
}

$queries = @optional + @badwords + @required;
$search = 'foreach $file (@ARGV) {open (FILE, $file); $_ = join("", <FILE>); close (FILE); $ARGV = $file; ($foo, $_) = split(/<!--\/?Messages-->/, $_, 3);';
$search .= "study;" if $queries >= 3;
$search .= "s/<BR>/ /gi; s/<[^<>]*>//g; ";
foreach $word (@badwords) {
	$search .= "\$score{\$ARGV} = -1 if /$word$smart/i;";
	$search .= "next if \$score{\$ARGV} == -1;\n";
}
foreach $word (@required) {
	$search .= "\$score{\$ARGV} = -1 if !/$word$smart/i;\n";
	$search .= "next if \$score{\$ARGV} == -1;\n";
	push (@optional, $word);
}
foreach $word (@optional) {
	$search .= "\$match =0;\n";
	$search .= "while (/$word$smart/ig)\n {\$before = \$`;\n \$after = \$';\n \$matched = \$&;\n \$match += 1;\n ";
	$search .= "\$context{\$ARGV} .= join(\"\", \"+++\", substr(\$before, -25, 25), \$matched, substr(\$after, 0, 25), \"|||\");\n}\n";
	$search .= "\$score{\$ARGV} += \$match;";
}
$search .= "}";
undef @files;
open (TOPIC, "$message_dir/board-topics.html");
@topic = <TOPIC>;
close (TOPIC);
@included = grep(/<!-Top/, @topic);
if ($FORM{'time'} eq "on") {
	$value = $FORM{'when'};
	$timerestrict = $value * 60 * 60;
	$thetime = (time) - $timerestrict;
}
foreach $one (@included) {
	$one =~ /<!-Top: (\d+)-!>/;
	&recursive("$1", "$message_dir/$1/$1.$ext") if ($FORM{'topic'} eq "all" || $FORM{'topic'} == $1);
}
@ARGV = @files;

undef %score;
undef %count;
undef %context;
$slash_temp = $/;
eval $search;
$/ = $slash_temp;

undef @DATA; undef @KEYS;

foreach $key (keys(%score)) {
	push (@KEYS, $score{$key}) if $score{$key} > 0;
	push (@DATA, $key) if $score{$key} > 0;
}

@sortedscore = @DATA[reverse sort BYKEYEXPR $[..$#DATA];
$number = scalar(@sortedscore);

if ($number == 0) {
	print $L{BSCHZERO};
} elsif ($number == 1) {
	print $L{BSCHONE};
} else {
	$ts = $L{BSCHPLURAL};
	$ts =~ s/\%d/$number/g;
	print $ts;
}
print "<HR>\n";
&ex('printuntil', 3, 3, 0, "");
&ex('printuntil', 5, 5, 0, "");

$lm = ""; $returned = 0; $counter = 0;
foreach $file (@sortedscore) {
	&extract($file);
	$counter += 1;
	($lm) = &ex('extract_lastmodified', "$topic_number:$me_number");
	print "<B>$counter. <A HREF=\"$message_url/$topic_number/$me_number.$ext?$lm\">";
	print "$topic_name";
	foreach $key (sort by_number (keys(%level_number))) {
		print ": $level_name{$key}";
	}
	print "</A></B><BR><BLOCKQUOTE>\n";
	$context = $context{$file};
	undef @othercontext;
	$count = 0;
	while ($context =~ /\+\+\+([^\|]*)\|\|\|/ && $count < 5) {
		$one = $1;
		$context = join("", $`, $');
		$one =~ s/\n/ /g;
		$one =~ s/\s+/ /g;
		$match = substr($one, 10, 25);
#		$match =~ s/[\(\)\-\/]//g;
		if ($one =~ /\s(.*)\s/) {
			$one = $1 if length($`) < 10;
		}
		foreach $word (@optional) {
			$one =~ s/($word)/<B>$1<\/B>/gi;
		}
		foreach $word (@required) {
			$one =~ s/($word)/<B>$1<\/B>/gi;
		}
#		print "$L{BSCHBULLET}$one<BR>\n" if ($one =~ /\S/ && !grep(/$match/, @othercontext));
		print "$L{BSCHBULLET}$one<BR>\n" if ($one =~ /\S/);
		$count += 1;
		push (@othercontext, $match);
	}
	print "</BLOCKQUOTE>\n";
	$returned += 1;
	if ($returned >= $FORM{'return'} && $FORM{'number'} eq "on") {
		for ($i = 7; $i <= 17; $i += 2) {
			&ex('printuntil', $i, $i, 0, "");
		}
		exit(0);
	}
}
for ($i = 7; $i <= 17; $i += 2) {
	&ex('printuntil', $i, $i, 0, "");
}
exit(0);

sub BYKEYEXPR {
	$KEYS[$a] <=> $KEYS[$b];
}

sub by_number {
	$a <=> $b;
}

sub recursive {
	local ($topic, $file) = @_;
	local (@file, @included, $one);
	open (FILE, $file);
	@file = <FILE>;
	close (FILE);
	@included = grep(/<!-Top:/, @file);
	foreach $one (@included) {
		$one =~ /<!-Top: (\d+)-!>/;
		&recursive($topic, "$message_dir/$topic/$1.$ext");
	}
	if ($thetime) {
		$mtime = (stat($file))[9];
		push (@files, $file) if $mtime >= $thetime;
		$time{$file} = ($mtime - $thetime);
	} else {
		push (@files, $file);
	}
}

