#!/usr/local/bin/perl

use CGI;
use IO::All;
use Time::HiRes qw(gettimeofday);
use Data::Dumper;

my $start_ts;
{	my ($seconds, $micros) = gettimeofday;
	$start_ts = $seconds + ($micros/1000000);
}
my $q = new CGI;
print $q->header;

my $name = clean_input($q->param('xyzzy'));
if($q->param('idkfa')) {
	my $templ = read_templ('bot.templ');
	print $templ;
	exit;
}
my $comment = clean_input($q->param('mxyptlk'));
my $id = clean_input($q->param('id'));
my $page = clean_input($q->param('page')) || 1;
if($name and $comment) {
	my $to_file = 0;
	my @files;
	foreach my $file (io('/home/dana/realms/blog/blog/')->all) {
		$file =~ s/.*\///;
		next unless $file =~ /^$id\./;
		$to_file++;
	}
	my $time = scalar localtime;
	my $commentfile = "/home/dana/realms/blog/blog/$id.$to_file";
	open FH, ">$commentfile";
	print FH "<p>$time</p>\n";
	print FH "<p>Comment from $name</p>\n";
	print FH "<p>$comment</p>\n";
	close FH;
}
if($id) {
	my @comment_files = get_comment_files($id);
	my $templ = read_templ('post.templ');
	my $contents < io "/home/dana/realms/blog/blog/$id";
	$contents .= "<hr/><p>Comments:</p>\n";
	foreach my $file (@comment_files) {
		my $comment < io "/home/dana/realms/blog/blog/$file";
		$contents .= "$comment<hr/>\n";
	}
	$templ =~ s/TEMPL/$contents/;
	$templ =~ s/TID/$id/;
	print $templ;
} elsif($page) {
	show_page($page);
} else {
	show_all();
}
sub
clean_input {
	my $input = shift;
	return $input;
	if($input =~ /^(\w|\s|\(|\)|\.|\,|\[|\]|\@|\!|\$|\/|\<|\>)*$/) {
		return $input
	} else {
		return 'bad input';
	}
}
sub
show_page {
	my $page_id = shift;
	generate_cache() if not -e 'cache/blog.cache';
	generate_cache() if [stat('cache/blog.cache')]->[9] < [stat("blog")]->[9];
	my $index = do 'cache/page.index';
	print STDERR Dumper $index;

	my $start_pos = $index->{$page_id};
	#my $read_length = ($index->{$page_id+1} - 1) - $start_pos;
	my $read_length = ($index->{$page_id+1} - 0) - $start_pos;

	open FH, 'cache/blog.cache';
	my $blogs;
	seek FH, $start_pos, 0;
	read FH, $blogs, $read_length;
	close FH;
	my $templ = read_templ('index.templ');

	$templ =~ s/TEMPL/$blogs/;
	my $total_time;
	{	my ($seconds, $micros) = gettimeofday;
		my $end_ts = $seconds + ($micros/1000000);
		$total_time = $end_ts - $start_ts;
		$total_time = int($total_time * 10000)/10000;
	}
	$templ =~ s/RENDER_TIME/$total_time/;
	print $templ;
	return;
}
sub
show_all {
	generate_cache() if not -e 'cache/blog.cache';
	generate_cache() if [stat('cache/blog.cache')]->[9] < [stat("blog")]->[9];
	open FH, 'cache/blog.cache';
	my $out;
	while(<FH>) {
		$out .= $_;
	}
	close FH;
	my $total_time;
	{	my ($seconds, $micros) = gettimeofday;
		my $end_ts = $seconds + ($micros/1000000);
		$total_time = $end_ts - $start_ts;
		$total_time = int($total_time * 10000)/10000;
	}
	$out =~ s/RENDER_TIME/$total_time/;
	print $out;
	return;
}

sub
generate_cache {
	my $templ = read_templ('index.templ');
	my $page_index = {};
	my $page_index_count = 1;
	my $blog_post_count = 0;
	my $page_index_position = 0;
	{	my $tmp_templ = $templ;
		$tmp_templ =~ s/TEMPL.*//s;
		$page_index_position = length $tmp_templ;
	}

	$page_index->{$page_index_count} = $page_index_position;
	$page_index_count++;
	my @files;
	foreach my $file (io('/home/dana/realms/blog/blog/')->all) {
		$file =~ s/.*\///;
		next if $file =~ /\./;
		push @files, $file;
	}
	@files = sort { $b <=> $a } @files;
	
	my $blogs = '';
	foreach my $file (@files) {
		my $one_blog = '';
		$blog_post_count++;
		if($blog_post_count == 40) {
			$blog_post_count = 0;
			$page_index->{$page_index_count} = $page_index_position;
			$page_index_count++;
		}
		my $contents;
		$contents < io "/home/dana/realms/blog/blog/$file";
		$contents =~ s/\<p\>(.*?)\<\/p\>//;
		my $date = $1;
		$date = "<a href=\"http://www.realms.org/blog/index.cgi?id=$file\">$date</a>";
		$one_blog .= "<p>$date</p>\n$contents\n\n";
		my @comment_files = get_comment_files($file);
		my $comment_file_ct = scalar @comment_files;
		my $comment_text = 'comments';
		$comment_text = 'comment' if $comment_file_ct == 1;
		my $popup_value = 'Comments from:&lt;br/>' . join '', get_commenters($file);
		$popup_value =~ s/\</&lt;/g;
		if($comment_file_ct > 0) {
			$one_blog .= qq<p><a href="javascript:void(0);"
onmouseover="return overlib('$popup_value', AUTOSTATUS, WRAP);"
onmouseout="nd();">($comment_file_ct $comment_text to this post)<br/></a></p><hr/>
;
		} else {
			$one_blog .= "<p>($comment_file_ct $comment_text to this post)<br/></p><hr/>";
		}
		$blogs .= $one_blog;
		$page_index_position += length($one_blog);
	}
	$page_index->{$page_index_count} = $page_index_position;
	$templ =~ s/TEMPL/$blogs/;

	open FH, '>cache/page.index';
	print FH Dumper $page_index;
	close FH;

	open FH, '>cache/blog.cache';
	print FH $templ;
	close FH;
}

sub
get_comment_files {
	my $id = shift;
	my @files = ();
	foreach my $file (io('/home/dana/realms/blog/blog')->all) {
		$file =~ s/.*\///;
		next unless $file =~ /^$id\.(\d+)$/;
		push @files, $file;
	}
	return @files;
}

sub
get_commenters {
	my $id = shift;
	my %commenters;
	foreach my $comment_file (get_comment_files($id)) {
		open FH, "/home/dana/realms/blog/blog/$comment_file";
		<FH>;
		my $commenter = <FH>;
		chomp $commenter;
		close FH;
		$commenter =~ s/Comment from /&nbsp\;&nbsp\;&nbsp\;&nbsp\;/;
		$commenters{$commenter} = 1;
	}
	return keys %commenters;
}

sub
read_templ {
	my $filename = shift;
	open FH, $filename;
	my $file;
	while(<FH>) {
		$file .= $_;
	}
	close FH;
	return $file
}
