#!/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";

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

$dir .= "/.www/comp3710/diary/";

undef $/;
open STYLE, "$dir/styles.css" or die "Cannot find style file: $!";
my $style = <STYLE>;
close STYLE;

print OUT <<"END";
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
	"http://www.w3.org/TR/html4/strict.dtd">
<html>
	<head>
	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
	<title>Course Notes &mdash; Computer Science 3710 (Winter 2004)</title>
	<style type="text/css">
$style
	</style>
	</head>
<body>
END

$dir .= "2004/";
die "No such directory '$dir'" if ! -d $dir;

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

for (glob "*/*/index.php") {
	if (! -r) {
		print "File $_ is not readable.";
		last;
	} elsif (! -s) {
		print "File $_ is empty.";
		next;
	}
	my $url = "http://www.cs.mun.ca/~donald/comp3710/diary/2004/$_";
	print "Getting $url...\n";
	my $result = `/usr/bin/wget -O- $url 2>/dev/null`;
	die "Not able to retrieve web page!" unless $result;
	my ($html) = ($result =~ m{<body>(.*)</body>}s);
	print OUT $html;
	print OUT "<hr>\n"
}
print OUT "</body>\n</html>";
close OUT;
