#!/perl/bin/perl

# delguest.pl
# Takes CGI input from entries.pl consisting
# of marked checkboxes, each with a value equal to
# the index of a guest book entry that has been
# marked for deletion.

# Get header files.

    require ("c:/Program Files/Sambar/cgi-bin/GuestBook.pm") || 
        die ("Can't find GuestBook header file: $!\n");

# Make sure this is a POSTed CGI conversation.

    if ($ENV{'REQUEST_METHOD'} ne "POST")
        {
        &HTML_Header ("Unauthorized entry!");
        print <<THE_END;
<BODY>
<H1 ALIGN="CENTER">Unauthorized entry attempt</H1>
<H1 ALIGN="CENTER">Goodbye!</H1>
</BODY>
THE_END

        &HTML_Footer;
        exit (0);
        }

# Get the query string and strip out the index numbers.

    read (STDIN, $QueryString, $ENV{'CONTENT_LENGTH'});
    @Info = split (/&/, $QueryString);

    for ($n = 0; $Info[$n]; $n++)
        {
        ($dummy, $index) = split (/=/, $Info[$n]);
        $DelGuest[$n] = $index;
        }

# Open the guest book database file.

    open (GUEST_LOG, $GuestBookPath) || 
        die "Can't open guest book: $!";

# Read the whole database into a big array.

    $n = 0;

    while (read (GUEST_LOG, $buffer, $GuestEntrySize))
        {
        $DataBase[$n] = $buffer;
        $n++;
        }

# Close the guest book file, then reopen for writing.

    close (GUEST_LOG);
    open (GUEST_LOG, ">$GuestBookPath") || 
        die "Can't open guest book: $!";

# Write the entries back, except the ones that have
# been flagged for deletion.

    $RecCount = 0;
    $DelCount = 0;

    foreach $record (@DataBase)
        {
        if ($RecCount == $DelGuest[$DelCount])
            {
            $DelCount++;
            }
        else
            {
            print GUEST_LOG $record;
            }
        $RecCount++;
        }                # End foreach $record...

# All done. Close the file and put up an HTML screen.

    close (GUEST_LOG);
    &HTML_Header ("Finished");
    print <<THE_END;
<BODY>
<H2 ALIGN="CENTER">Finished editing guest book.
$DelCount entries deleted</H2>
</BODY>
THE_END

    &HTML_Footer;

#                    End delguest.pl
