#!/bin/perl

use lib '..';
use CGI;
$query = new CGI;
require "ADMIN/config";

$get_args = $query->param('keywords');

($thread,$message,$linenumber) = split(/,/,$get_args,3);

print "Content-type: text/html\n\n";

open(THREADFILE,"DATA/$thread.th");
$thread_head = <THREADFILE>;
chop;
($title,$author,$email,$date_sec,$mod_val,$post) = split(/\t/,$thread_head,6);

$date = &date_format($date_sec);

if ($email) {
    $author = "<A HREF=\"mailto:$email\">$author</A>";
}

if ($message == 0) {
    $title1 = $title;
    $author1 = $author;
    $date1 = $date;
    $post1 = $post;
	 
    $new = &is_new($date_sec);
    if ($new) {
	$author = "$author <EM><FONT COLOR=\"#800000\">new!</FONT></EM>";
    }
    $output = "<UL><LI>$date  <STRONG>$title</STRONG> - $author\n<UL>";
}
else {
    $new = &is_new($date_sec);
    if ($new) {
	$author = "$author <EM><FONT COLOR=\"#800000\">new!</FONT></EM>";
    }
    if ($linenumber) {
	$output = "<UL><LI>$date  <STRONG><A HREF=\"thread.cgi?$thread,0,$linenumber\">$title</A></STRONG> - $author\n<UL>";
    }
    else {
	$output = "<UL><LI>$date  <STRONG><A HREF=\"thread.cgi?$thread,0\">$title</A></STRONG> - $author\n<UL>";
    }
}

$x = 0;
while (<THREADFILE>) {
    ($title,$author,$email,$date_sec,$mod_val,$post) = split(/\t/,$_,6);
    
# if moderator-approved
    if ($mod_val == 0) { 
	$date = &date_format($date_sec);
	
	if ($email) {
	    $author = "<A HREF=\"mailto:$email\">$author</A>";
	}
	$x++;
	if ($message == $x) {
	    $title1 = $title;
	    $author1 = $author;
	    $date1 = $date;
	    $post1 = $post;
	    
	    $new = &is_new($date_sec);
	    if ($new) {
		$author = "$author <EM><FONT COLOR=\"#800000\">new!</FONT></EM>";
	    }
	    $output = join("\n",$output,"<LI>$date  <STRONG>$title</STRONG> - $author");
	}
	else {
	    $new = &is_new($date_sec);
	    if ($new) {
		$author = "$author <EM><FONT COLOR=\"#800000\">new!</FONT></EM>";
	    }
	    if ($linenumber) {
		$output = join("\n",$output,"<LI>$date  <STRONG><A HREF=\"thread.cgi?$thread,$x,$linenumber\">$title</A></STRONG> - $author");
	    }
	    else {
		$output = join("\n",$output,"<LI>$date  <STRONG><A HREF=\"thread.cgi?$thread,$x\">$title</A></STRONG> - $author");
	    }
	}
    }
}

# post
$output = join("\n",$output,"</UL>\n</UL>\n<HR>\n$date1<BR>\n<STRONG>$title1</STRONG><BR>\n$author1<HR>\n$post1\n<HR>\n");

# footer	
$output = join("\n",$output,"<CENTER>\n[ <A HREF=\"reply.cgi?$thread,$message\">Reply to this thread</A> ]\n[ <A HREF=\"add.cgi\">Start a new thread</A> ]<BR>");

if ($linenumber) {
    $output = join("\n",$output,"[ <A HREF=\"list.cgi?$linenumber\">Back to thread list</A> ]<BR></CENTER>");
}
else {
    $output = join("\n",$output,"[ <A HREF=\"list.cgi\">Back to thread list</A> ]<BR></CENTER>");
}

# print into template
open(TEMPLATE,"TEMPLATES/view.html");
while(<TEMPLATE>) {
    s/\$post/$output/;
    print;
}

close DATAFILE;



