#!/usr/local/bin/perl

use strict;

open (INDEX,"htdocs/index.html") or die "can't read htdocs/index.html";
my $index = join ('',<INDEX>);
close INDEX;

my ($old_changes) = ( $index =~ /<\!-- changes -->(.*?)<\!-- changes -->/s );

open (CHANGES, "Changes") or die "can't read Changes";
<CHANGES>;<CHANGES>;<CHANGES>;
my $changes = join ('',<CHANGES>);
close CHANGES;

$changes =~ s/\s+$/\n/;
$changes =~ s/</&lt;/g;
$changes =~ s/>/&gt;/g;

my ($old_todo) = ( $index =~ /<\!-- TODO -->(.*?)<\!-- TODO -->/s );

open (TODO, "TODO") or die "can't read TODO";
<TODO>;
my $todo = join ('',<TODO>);
close TODO;

$todo =~ s/\s+$/\n/;
$todo =~ s/</&lt;/g;
$todo =~ s/>/&gt;/g;

if ( $changes ne $old_changes or $old_todo ne $todo ) {
	$index =~ s/<\!-- changes -->.*?<\!-- changes -->/<!-- changes -->$changes<!-- changes -->/s;
	$index =~ s/<\!-- TODO -->.*?<\!-- TODO -->/<!-- TODO -->$todo<!-- TODO -->/s;
	open (INDEX, ">htdocs/index.html") or die "can't write htdocs/index.html";
	print INDEX $index;
	close INDEX;
}
