#!/bin/perl

require "config";

use lib '../..';
use CGI;
$query = new CGI;

if (!($qstring = $query->param('keywords'))) {
    &show_threads;
}
else {   
    ($threadnum, $postnum, $confirm) = split(/,/,$qstring,3);
    
    if ($postnum || ($postnum eq '0')) {
	if ($confirm eq 'y') {
	    &remove_message;
	}
	
	elsif ($confirm eq 'n') {
	    print "Location: $base_url/ADMIN/removepost.cgi\n\n";
	}
	
	else {
	    &ask_for_confirmation;
	}
    }
    else {
	&show_messages;
    }
}

###
sub show_threads {
    open(LOCK,"../list.lock"); flock(LOCK,2);

    open(DATABASE,"$main_thread_file") || die;

    print "Cache-Control: no-cache\nPragma: no-cache\nContent-type: text/html\n\n<HTML><HEAD><TITLE>Remove a Message</TITLE></HEAD><BODY BGCOLOR=\"#FEFEFE\"><A HREF=\"admin.cgi\">Back to Administration</A><H1>Remove a Message</H1>Click on the thread that contains the message you want to remove:<HR>";

# get, print $listlength threads
    while(<DATABASE>) {
	chop;
	($num,$title,$author,$email,$date_mod,$replies,$last_mod,
	 $mod_val,$mod_num) = split(/\t/);
	
	if (!$mod_val) {
	    $date = &date_format($date_mod);
	    $last = &date_format($last_mod);
	    
	    print "<STRONG>$num</STRONG> $date <A HREF=\"removepost.cgi?$num\">$title</A> - $author [ $replies - <EM>$last</EM> ]<BR>\n";
	}
    }
    
    print "</BODY></HTML>";
    
    flock(LOCK,8);
    close(DATABASE);
}

sub remove_message {
    open(LOCK,"../list.lock"); flock(LOCK,2);

    open(BAK,">$main_thread_file.bak") || die;
    open(THREADFILE,"../DATA/$threadnum.th") || die;

    $x = 0;
    while(<THREADFILE>) {
	if ($postnum != $x) {
				print BAK;
			    }
	$x++;
    }
	 
    open(BAK,"../DATA/main.th.bak") || die;
    open(THREADFILE,">../DATA/$threadnum.th") || die;
	 
    select(THREADFILE); $| = 1; select(STDOUT);
    
    while(<BAK>) {
	print THREADFILE;
    }
    
    open(BAK,">../DATA/main.th.bak") || die;
    open(MAINFILE,"$main_thread_file") || die;
    
    while(<MAINFILE>) {
	if (/^$threadnum\t/) {
	    ($thread_num_new,$title,$author,$email,$date_1,$replies,$date_2,
	     $mod_val,$mod_num) = split(/\t/,$_,9);
	    if ($postnum != 0) {
		$replies--;
		print BAK "$thread_num_new\t$title\t$author\t$email\t$date_1\t$replies\t$date_2\t$mod_val\t$mod_num";
	    }
	    elsif ($replies != 0) {
		print BAK "$thread_num_new\t$title\t$author\t$email\t$date_1\t$replies\t$date_2\t$mod_val\t$mod_num";
	    }
	}
	else {
	    print BAK;
	}
    }
    
    open(BAK,"../DATA/main.th.bak") || die;
    open(MAINFILE,">$main_thread_file") || die;

    select(MAINFILE); $| = 1; select(STDOUT);
    
    while(<BAK>) {
	print MAINFILE;
    }
    flock(LOCK,8);
    print "Location: $base_url/ADMIN/removepost.cgi\n\n";
	
}

sub ask_for_confirmation {
    open(THREADFILE,"../DATA/$threadnum.th") || die;
    $x = 0;
    while(<THREADFILE>) {
	($title,$author,$email,$date_sec,$mod_val,$post) 
	    = split(/\t/,$_,6);
	if (!$mod_val) {
	    if ($postnum == $x) {
		$date = &date_format($date_sec);
		last;
	    }
	    $x++;
	}
    }
	 
    print "Content-type: text/html\n\n";	
    print <<EOF
<HTML><HEAD><TITLE>Remove a Message</TITLE></HEAD>
<BODY BGCOLOR="#FEFEFE">
<H3>Are you sure you want to remove this message?</H3>
<TABLE>
<TR><TD><STRONG>Title:</STRONG> </TD><TD>$title<BR></TD></TR>
<TR><TD><STRONG>Name:</STRONG> </TD><TD>$author $email<BR></TD></TR>
<TR><TD><STRONG>Date:</STRONG> </TD><TD>$date<BR></TD></TR>
</TABLE>
<STRONG>Message:</STRONG> 
<HR>
$post
<HR>
<A HREF="removepost.cgi?$threadnum,$postnum,y">Yes</A><P>
<A HREF="removepost.cgi?$threadnum,$postnum,n">No</A>
</BODY></HTML>
EOF
}

sub show_messages {
    # show messages in thread

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

    print "<HTML><HEAD><TITLE>Remove a Message</TITLE><HEAD><BODY BGCOLOR=\"#FEFEFE\"><H1>Remove a Message</H1>Click on the message you want to remove:<HR>";

    
    open(THREADFILE,"../DATA/$threadnum.th");
    
    $x = 0;
    while (<THREADFILE>) {
	($title,$author,$email,$date_sec,$mod_val,$post) 
	    = split(/\t/,$_,6);
	if (!$mod_val) {
	    $date = &date_format($date_sec);
	    
	    if ($email) {
		$author = "<A HREF=\"mailto:$email\">$author</A>";
	    }
		 if ($x) {
			  print "$date  <STRONG><A HREF=\"removepost.cgi?$threadnum,$x\">$title</A></STRONG> - $author<BR>";
		 }
		 else {
			  print "$date  <STRONG>$title</STRONG> - $author<BR>";
		 }
	    $x++;
	}
    }
}

