#!/usr/bin/perl -w

use strict;

my $output = "course.html";
die "File '$output' exists!" if -e $output;
open OUT, "> $output" or die "Cannot open '$output': $!\n";

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

defined(my $dir = (getpwnam("donald"))[7]) or die "No such user!";

$dir .= "/.www/comp3710/diary/2003";
die "No such directory '$dir'" if ! -d $dir;

chdir $dir or die "Cannot change to '$dir': $!\n";

undef $/;

for (glob "*/*/index.html") {
	if (! -r) {
		print "File $_ is not readable.";
		last;
	} elsif (! -s) {
		print "File $_ is empty.";
		next;
	}
	open HTML, $_ or die "Cannot open file '$_': $!\n";
	my ($html) = (<HTML> =~ m{<body>(.*)</body>}s);
	close HTML;
	print OUT $html;
}
print OUT "</body>\n</html>";
close OUT;
