#!/bin/perl

require "ADMIN/config";

use lib '..';
use CGI;

$query = new CGI;

open(LOCK,"list.lock"); flock(LOCK,2);

open(DATABASE,"$main_thread_file");

print "Cache-Control: no-cache\nPragma: no-cache\nContent-type: text/html\n\n";

if (!(@names = $query->param)) {

# get, print $listlength threads
    $list = "";
    $c = 0;
    while(<DATABASE>) {
	chop;
	($num,$title,$author,$email,$date_mod,$replies,$last_mod,
	 $mod_val,$mod_num) = split(/\t/);
	
# if moderator-approved
	if ($mod_val == 0) {
	    $date = &date_format($date_mod);
	    
	    $new = &is_new($last_mod);
	    if ($new) {
		$last = "<FONT COLOR=\"#800000\">new!</FONT>";
	    }
	    else {
		$last = '';
	    }
	    
	    $nextline = "$date <A HREF=\"thread.cgi?$num,0\">$title</A> - $author [ $replies <EM>$last</EM> ]<BR>\n";
	    $list = join("\n",$list,$nextline);
	    if (++$c > ($listlength - 1)) { last; }
	}
    }
    $list = join("\n",$list,"<HR><CENTER>");
    if (!eof()) {
	$getarg = $listlength + 1;
	$list = join("\n",$list,"[ <A HREF=\"list.cgi?$getarg\">View Previous $listlength</A> ]");
    }
}

else {
    
# get starting line number to view from $datafile
    $linenumber = $query->param('keywords');
    $linenumarg = $linenumber;

#skip line until $linenumber is reached
    if ($linenumber > 1) {
	while(<DATABASE>) {
	    ($num,$title,$author,$email,$date_mod,$replies,$last_mod,
	     $mod_val,$mod_num) = split(/\t/);
	    if (($mod_val == 0) && (--$linenumber <= 1)) { last; }
	}
    }
    
# read next $listlength threads
    $list = "";
    $c = 0;
    while(<DATABASE>) {
	chop;
	($num,$title,$author,$email,$date_mod,$replies,$last_mod,
	 $mod_val,$mod_num) = split(/\t/);
	
#if moderator-approved
	if ($mod_val == 0) {
	    $date = &date_format($date_mod);
	    
	    $new = &is_new($last_mod);
	    if ($new) {
		$last = "<FONT COLOR=\"#800000\">new!</FONT>";
	    }
	    else {
		$last = '';
	    }
	    
	    $list = join("\n",$list,"$date <A HREF=\"thread.cgi?$num,0,$linenumarg\">$title</A> - $author [ $replies <EM>$last</EM> ]<BR>");
	    if (++$c > ($listlength - 1)) { last; }
	}
    }
# print link to see the top $listlength threads
    $list = join("\n",$list,"<HR><CENTER>\n[ <A HREF=\"list.cgi\">View Top $listlength</A> ]");

# print link to list the next $listlength threads
    $currentlinenum = $query->param('keywords');
    $nextlinenum = $currentlinenum - $listlength;
    if ($nextlinenum > 1) {
	$list = join("\n",$list,"[ <A HREF=\"list.cgi?$nextlinenum\">View Next $listlength</A> ]");
    }

# print link to list the previous $listlength threads
    if (!eof()) {
	$prevlinenum = $currentlinenum + $listlength;
	$list = join("\n",$list,"[ <A HREF=\"list.cgi?$prevlinenum\">View Previous $listlength</A> ]");
    }
}

$list = join("\n",$list,"[ <A HREF=\"add.cgi\">Start a New Thread</A> ]\n</CENTER><BR><FONT SIZE=\"-1\">meep!Board 1.0 is a product of <A HREF=\"http://www.meep.com/\">meep! media inc.</A></FONT>");

close(DATABASE);
flock(LOCK,8);

# print out using template
open(TEMPLATE,"TEMPLATES/list.html");
while (<TEMPLATE>) {
    s/\$list/$list/;
    print;
}
close(TEMPLATE);

