#!/usr/bin/perl  

print qq(Content-type: text/html\n\n);

use CGI;
$query    = new CGI;

$dirs     = "dirs.txt";
$type     = $query->param('type');
$search   = $query->param('search');
$topic    = $query->param('topic');
$show     = $query->param('view');
$skip     = $query->param('skip');
$u        = $query->param('u');
$p        = $query->param('p');

$search_type  = "\&" . $type;

# convert multiple spaces to single spaces
$search   =~ tr/ / /s;

# chop the last space
chop($search) if ($search =~ / $/);

# convert spaces to verticle bars
$search   =~ tr/ /\|/s;

$head     = "parts/header.html";
$foot     = "parts/footer.html";
$admmail	  = "www\@mordor.nazgul.com";

$doc_root = "./";

open(HEAD, "$head") || print "$head $!";
open(FOOT, "$foot") || print "$foot $!";

print <HEAD>;
&results;
print qq(<P> to remove a message e-mail: <A HREF="mailto:$admmail">$admmail</A>);
print <FOOT>;



# subs
#-----------------------------------------------------------------------------

sub results {

	&navagation;

	open(DIRS, "./admin/$dirs") || print "Can't open $dirs, $!";
	@dirs = <DIRS>;
	close(DIRS);
	
	foreach $directory (@dirs) {
		chomp($directory);
		$index = $doc_root . $directory . "/archive.index";
		open(INDEX, "$index");
		@index = <INDEX>;
		foreach $entry (@index) {
			chomp($entry);
			($o_thread, 
			 $action, 
			 $id, 
			 $title, 
			 $name, 
			 $email, 
			 $url, 
			 $reply) = split(/\t/, $entry);

			$match  = $doc_root . $action . "\/archive/" . $id . "\.archive";
			$rep_f  = " (a reply to message $reply)" if ($reply);
			
			&find_messages;
		}
		close(INDEX);
	  	print qq(\n<P>\n);
	}
	
	&disp;
	&navagation;
	print qq(\n<P>\n);
}

sub disp {
	
	$count  = @entrys;
	$up     = $u;
	$pop    = ($up - 10);
	$up     = (10) if ($up < 10 || !$up);
	$shift  = ($count - $up);

	for($i = 0; $i < $shift; $i++) {
		shift(@entrys);
	}

	for($ii = 0; $ii < $pop; $ii++) {
		pop(@entrys);
	}
	sort(@entrys);

	if ($up <= ($count - 5)) {
		$up_   = ($up + 5);
	} else {
		$up_   = $count;
	}

	$down_ = ($up - 5);
	$view  = (2);

	if ($count > 0) {
		print qq(<CENTER><TABLE BORDER=0><TR><TD>);
		print qq(<TABLE BORDER=0><TR><TD BGCOLOR="#ffffff">);
		print qq(<A HREF="search.cgi?topic=$topic&view=$view&type=$type&u=$count">);
		print qq(<IMG SRC="top.gif" ALIGN=top BORDER=0></A><BR>);
		print qq(<A HREF="search.cgi?topic=$topic&view=$view&type=$type&u=$up_">);
		print qq(<IMG SRC="back.gif" BORDER=0></A></TD></TR>);
		print qq(<TR><TD BGCOLOR="#ffffff"><IMG SRC="/spacer.gif" HEIGHT=100 WIDTH=5></TD></TR><TR><TD BGCOLOR="#ffffff">);
		print qq(<A HREF="search.cgi?topic=$topic&view=$view&type=$type&u=$down_">);
		print qq(<IMG SRC="parts/backd.gif" BORDER=0></A><BR>);
		print qq(<A HREF="search.cgi?topic=$topic&view=$view&type=$type&u=0">);
		print qq(<IMG SRC="parts/bottom.gif" ALIGN=bottom BORDER=0></A><BR>);
		print qq(</TD></TR></TABLE>);
		print qq(</TD><TD VALIGN=TOP>);
		print qq(<TABLE BORDER=0>);
		print qq(<TR><TD BGCOLOR="#000000"><B>Message:</B></TD>);
		print qq(<TD BGCOLOR="#000000"><B>Posted By:</B></TD>);
		print qq(<TD BGCOLOR="#000000"><B>Posted On:</B></TD>);
		print qq(</TR>);
		foreach $entry (@entrys) {
			my ($o_thread, $reply, $id, $match, $name, $title, $email) = split(/\ -\,-\ /, $entry); 
			if ($reply && $show == 2) {
				print qq(<TR><TD WIDTH=200 BGCOLOR="#c0c0c0">\n);
				print qq(<IMG SRC="/spacer.gif" WIDTH=20 HEIGHT=2>);
				print qq(<A HREF="view.cgi?file=$match&replyto=$id&type=topic_display">);
				print qq($title</A>);
				print qq(</TD><TD BGCOLOR="#c0c0c0">);
				print qq(<FONT SIZE=1>);
				print qq( Posted by: <A HREF="mailto:$email">$name</A><BR>) if ($name);
				print qq(</FONT>);
				print qq(</TD><TD BGCOLOR="#c0c0c0">\n);
				$tm = localtime($id);
				print qq(<FONT SIZE=1>$tm</FONT>);
				print qq(</TD></TR>);
			}
			if (!$reply) {
				print qq(<TR><TD WIDTH=200 BGCOLOR="#c0c0c0">\n);
				print qq(<B>) if ($show == 2);
				print qq(<A HREF="view.cgi?file=$match&replyto=$id&type=topic_display">);
				print qq($title</A>);
				print qq(</B>) if ($show == 2);
				print qq(</TD><TD BGCOLOR="#c0c0c0">);
				print qq(<FONT SIZE=1>);
				print qq( Posted by: <A HREF="mailto:$email">$name</A><BR>) if ($name);
				print qq(</FONT>);
				print qq(</TD><TD BGCOLOR="#c0c0c0">\n);
				$tm = localtime($id);
				print qq(<FONT SIZE=1>$tm</FONT>);
				print qq(</TD></TR>\n);
			}
		}
		print qq(</TABLE></TD></TABLE>);
		&search_form;
	} else {
		print qq(<B>Sorry, no matches.</B>);
		&search_form;
	}
}	
sub find_messages {

	eval($search_type);

	sub search_topic {
		if ($action =~ /$topic/i && 
			$body   =~ /$search/ig || 
			$title  =~ /$search/ig || 
			$name   =~ /$search/ig || 
			$email  =~ /$search/ig) {
			$entry = "$o_thread -,- $reply -,- $id -,- $match -,- $name -,- $title -,- $email";
		    push(@entrys, $entry);
	  		}
	}
	sub search_all {
		if ($body  =~ /$search/ig || 
			$title =~ /$search/ig || 
			$name  =~ /$search/ig || 
			$email =~ /$search/ig) {
			$entry = "$o_thread -,- $reply -,- $id -,- $match -,- $name -,- $title -,- $email";
			push(@entrys, $entry);
		}
	}
sub topic_display {
		if ($action =~ /$topic/i) {
			$entry = "$o_thread -,- $reply -,- $id -,- $match -,- $name -,- $title -,- $email";
			push(@entrys, $entry);
		}
	}
#    sort(@entrys);
}

sub navagation {

	print qq(<CENTER> [);
	print qq(<A HREF="search.cgi?topic=$topic&view=2&type=$type">) if ($show != 2);
	print qq(View replys</A> /);
	print qq( <A HREF="search.cgi?topic=$topic&view=1&type=$type">) if ($show == 2);
	print qq( Close replys</A>]);
	print qq( [<A HREF="edit.cgi?action=$topic">Add a new message</A>]);
	print qq( [<A HREF="./">Back to topics</A>]);
	print qq(</CENTER>);

}

sub search_form {

print qq(
		 <FORM METHOD=POST ACTION="search.cgi">
		 <INPUT TYPE=HIDDEN NAME="view" VALUE="2">
		 <INPUT TYPE=HIDDEN NAME="type" VALUE="search_all">
		 <INPUT TYPE=TEXT SIZE=30 NAME="search">
		 <INPUT TYPE=SUBMIT VALUE="search"> <INPUT TYPE=RESET VALUE="clear">
		 </FORM>
		 );
}




















