#!/usr/bin/perl -w

my $inputfile = "/var/www/self/index.skeleton";
my $commentfile = "/var/www/self/comments.dat";

use HTML::Entities;
use CGI;

my $q = CGI->new;
print $q->header;

open F, "<$inputfile";
print while (<F>);
close F;

my @comments;
if (open F, "<$commentfile")
{
	@comments = split(/\n\n\n/m,join('', <F>));
	close F;
}

my $comment = "";
my $commentok = 0;

if (defined $q->param('post'))
{
	$comment = encode_entities($q->param('nick'))."::".encode_entities($q->param('post'));
	$comment =~ s/\n/<BR>/g;
	#$comment =~ s/\n\n\n/\n\n/mg;
	if ($comment =~ /\S::.*\S/ && ($#comments < 0 || $comment ne $comments[-1]))
	{
		open F, ">>$commentfile" || print "ERROR: can't open comment file for writing\n";
		print F "$comment\n\n\n";
		close F;
	}
	else
	{
		print "<h2 align=center><font size=\"+1\" color=#d00>ERROR: empty, duplicated and noname comments are not allowed</font></h2>\n";
		$comment = "";
	}
}

push(@comments, $comment) if ($comment ne "");

foreach(1..$#comments+1)
{
	print "<HR width=25%><P align=center>$1:<P align=center>$2\n" if ($comments[-$_] =~ /(.+)::(.*)/);
}

print "</body></html>\n";
