#!/httpd/bin/perl
#
# Remove a post.  The user can do this, but so can you, if you can
# log in as the maintaner.

$installdir = "/u/projects/PF";
$hthome = "/www/Util/projects";


open(CONF,"< $installdir/Projects.conf");

while (<CONF>) {
    next if (/^$/||/^\#/);
    m/^(\S+) (.*)$/;
    $variable = $1;
    $value = $2;
    eval "\$$variable = \"$value\"";
}

print "Please Enter the string in the Removal Information section of the\n";
print "offending post";
print "> ";
$infile = <STDIN>;
chop $infile;
$infile =~ s/^[ >]*//;
if ($infile =~ /(\S+) (\S+)/) {
    &removepost;
} else {
    &oldremovepost;
}
exit();


sub removepost {
    ($remvproject, $remvdir) = split(' ',$infile);
    # Now, find the project and test it for validity.
    $infile =~ m/^\s*(\S+)\s+(\S+)/;
    $theproject = $1;
    $themessfolder = $2;
    $isaproject = 0;
    foreach $VALIDPROJ (keys %projects) {
	$isaproject = 1 if ($theproject =~ /\b$VALIDPROJ\b/);
    }    if ($themessfolder) {
	# Remove the appropriate directory
	system "/bin/rm -rf $hthome/$theproject/$themessfolder/";
    }
    
    # Open the index and strip out any references to this article
    open (IND, "< $hthome/$theproject/index.html" ) || die "no index $hthome/$theproject/index.html";
    
    
    open (NEWIN, "> $hthome/tmp.new" ) || die "no output";
    while (<IND>) {
	print NEWIN if (!(m/\<a href=\"$themessfolder\/\"\>/));  #"
    }
    close IND;
    close NEWIN;
    unlink("$hthome/$theproject/index.html");
    link("$hthome/tmp.new","$hthome/$theproject/index.html");
    unlink ("$hthome/tmp.new");
    # Clean up
    system("chmod go+r $hthome/$theproject/index.html");
}

sub oldremovepost {
    print "\nTrying to remove $hthome/$infile\n";

    $sesame = open (KILLME,"$hthome/$infile");
    if (!$sesame) {
	print  "ERROR!!!\n";
	print  "Could not open $hthome/$infile\n";
	print  "Please be sure you specified the correct name for\n";
	print  "Removal.";
	exit;
    } else {
    }
    
    while (<KILLME>) {
	if (m/^\<b\>From\:\<\/b\>/) {
	    $FromLine = $'; #'
	    $FromLine = m/\b(\S+\@\S+)\b/;
	    $killedemail = $1;
	    print "ORIGINATED BY $killedemail\n";
	}
	last if (/\<hr\>/);
    }
    
# Now, find the project and test it for validity.
    $infile =~ m/^\s*(\S+)\/(\S+)/;
    $theproject = $1;
    $thefname = $2;
    $isaproject = 1;
    
    
# Continue going through killme and strip out all the references
# which are local into an array unlinkthese
    
    @unlinkthese = ("$hthome/$infile", "$hthome/$infile.removal.html");
    while (<KILLME>) {
	last if (/\<address\>/);
	if (m/\<a href=\"(\S+)\">/) { #"
	    @unlinkthese = (@unlinkthese, "$hthome/$theproject/$1");
	}
	if (m/\<img src=\"(\S+)\"/) { #"
	    @unlinkthese = (@unlinkthese, "$hthome/$theproject/$1");
    }
    }
    print "\n--- REMOVING POST AND LINKS ---\n";
    foreach $KILLTHIS (@unlinkthese) {
	print "Removing: $KILLTHIS\n";
	unlink ($KILLTHIS) if (-e $KILLTHIS);
    }
    
# Open the index and strip out any references to this article
    open (IND, "< $hthome/$theproject/index.html" ) || die "no index $hthome/$theproject/index.html";
    
    
    open (NEWIN, "> $hthome/tmp.new" ) || die "no output";
    while (<IND>) {
	print NEWIN if (!(m/\<a href=\"$thefname\"\>/));  #"
    }
    close IND;
    close NEWIN;
    unlink("$hthome/$theproject/index.html");
    link("$hthome/tmp.new","$hthome/$theproject/index.html");
    unlink ("$hthome/tmp.new");
# Clean up
    system("chmod go+r $hthome/$theproject/*");
    print "\n$infile\nSuccesfully Removed\n";
    
}
