#!/bin/perl
# remove_response.pl
# (C) Feb 4, 1999 by Jerome C. Parks
# Version 1.0
# This program updates the correct files to remove any guestbook entries

if ((!@ARGV) || (@ARGV < 1))
{
    &print_error;
    exit;
}

$file = $ARGV[0];
@files = ("guestlist.html","html_middle.txt");
&update_files($file,@files);
print "Removing file $file...\n";
system "rm -f $file";
print "Done.\n\n";

exit;


##########################
# subroutine print_error #
##########################
# This subroutines displays the correct format to use the program
sub print_error {
    print "usage: remove_response.pl <filename>\n";
    print "       where filename is the name of the response file to be ";
    print "removed from the list.\n";
    print "example: remove_response.pl response-00010.html\n";
    print "This will remove this file and update the correct files.\n\n";
} # END subroutine print_error

###########################
# subroutine update_files #
###########################
# This subroutine takes in the files containing the response to be removed
# and the response filename, and removes the references from the files.
sub update_files {
    local(@files) = @_;
    local(@f);
    local($file);
    local($listing);
    local($line);
    local($temp);

    $file = splice(@files,0,1);
    $temp = "this_is_just_a_temp.txt";

    foreach $listing (@files)
    {
        system "cp $listing $temp";
        system "mv -f $temp $listing";
        system "chmod 666 $listing";
        print "Opening file $listing to remove references to $file...\n";
        open(IN,"$listing") || warn "Can't open $listing for read: $!\n\n";
        chop(@f=<IN>);
        close IN;
        if (@f) 
        {
            open(OUT,">$listing") || 
                warn "Can't open $listing for write: $!\n\n";
            foreach $line (@f)
            {
                if ($line!~/$file/) { print OUT "$line\n"; }
            }
            close OUT;
        }
    }
} # END subroutine update_files

            
    
