#!/local/bin/perl
###############################################################################
# submit_guest.pl: A cgi-bin perl script to add a guest book entry to a guest
#                  book web page.
#
###############################################################################
# copyright (c) 1995 Thomas Engh Sevaldrud (sevaldt@iu.hioslo.no)
###############################################################################

$guestbook_page = "/iu/nexus/u1/mark/www/tmp/cfguest.html";
$tmp_page = "/iu/nexus/u1/mark/www/tmp/guests.tmp";

$remote_host = $ENV{"REMOTE_HOST"};

###############################################################################
# LEVEL 0:
###############################################################################

print "Content-type: text/html\n\n";
print "<br><hr><br>\n";
print "Thank you for submitting your comment!<br><br>\n";
print "best wishes,<br><br>";
print "-Mark<br><br><br>";
print "Now return to the <a href=http://www.iu.hioslo.no/~mark/cfengine.html>menu</a>\n";

# print "REMOTE_HOST = $remote_host\n";

# PROCESS ARGUMENT STRING:

$args = <STDIN>;
$args =~ s/Link=//;
$args =~ s/\+/ /g;		                    # Get rid of + delimiters
$args =~ s/%(..)/pack("c",hex($1))/ge;              # Decode control characters


($link, $message) = split(/&Comments=/, $args, 2); 

($nothing,$name) = split(/name=/,$link,2);

if(! open (PAGE, $guestbook_page)) 
{
    print "Content-type: text/html\n\n";
    print "couldn't open guestbook page file!";
}

if(! open (TMP, "+>$tmp_page"))
{
    print "Content-type: text/html\n\n";
    print "couldn't open temporary output file!";
}

while ($line = <PAGE>)
{
    if($line =~ /<h3>Number of entries: (..)/)
    {
	$entry_no = $1;
	$entry_no++;
	$line = "<h3>Number of entries: $entry_no </h3>\n";
    }

    if($line =~ /<!-- LAST ENTRY -->/)
    {
        $date = `date +"%A, %b %d %Y"`;	
        print TMP "<b>Entry $date from host: $remote_host</b>\n<p>\n";
	print TMP "From: $name\n<p>\n";
	print TMP $message;
	print TMP "\n<hr>\n";
    }

    print TMP "$line";

}
    
close PAGE;
close TMP;

if(! rename ($tmp_page, $guestbook_page))
{
    print "Oops! Rename operation failed!\n";
}

chmod (0666, $guestbook_page);









