#!/usr/bin/perl

@list = qw(scott_wan0_0_octets scott_wan0_rs_errs scott_wan0_crc_errs
	   scott_wan0_tx_power scott_wan0_lq);
@desc = ("wan0-0 octets input/output",
	 "wan0 Reed-Solomon Errors",
	 "wan0 CRC Errors",
	 "wan0 Transmit and Remote Transmit Power",
	 "wan0 Line Quality",
	 );
$dir = "/usr/local/apache/htdocs/mrtg";
$index = "$dir/index.html";

open(INDEX, "> $index") || die "open $index for write: $!";
print INDEX <<EOM;
<title>My Cisco 675</title>
<h1>My Cisco 675</h1>

<p> Stuff goes here.

<h2>Individual Statistics Pages</h2>

<ul>
EOM

@f = @list;
@d = @desc;
while (defined($f = shift @f)) {
      $d = shift @d;
      print INDEX "<li> <a href=\"$f.html\">$d</a>\n";
}

print INDEX <<EOM;
</ul>

<h2>Statistics by Time</h2>

<ul>
<li> <a href="time-5min.html">5 minute averages (daily)</a>
<li> <a href="time-30min.html">30 minute averages (weekly)</a>
<li> <a href="time-2hour.html">2 hour averages (monthly)</a>
<li> <a href="time-1day.html">1 day averages (yearly)</a>
</ul>
EOM

# Brute force.

open(OUT, "> $dir/time-5min.html") || die "open 5min: $!";
print OUT <<EOM;
<title>5 Minute Averages (Daily)</title>
<body bgcolor="#ffffff">
<h1>5 Minute Averages (Daily)</h1>
EOM

foreach $file (@list) {
	open(IN, "$dir/$file.html") || die "open $file.html: $!";
	while (<IN>) {
		last if /^<TITLE>/;
	}
	chop($title = <IN>);
	print OUT "<h2>$title</h2>\n";
	while (<IN>) {
		last if /5 Minute Average/;
	}
	print OUT "<p> ", $_;
	while (<IN>) {
		last if /^$/;
		print OUT;
	}
	print OUT;
	while (<IN>) {
		last if /^$/;
		print OUT;
	}
	close(IN);
}
close(OUT);

open(OUT, "> $dir/time-30min.html") || die "open 30min: $!";
print OUT <<EOM;
<title>30 Minute Averages (Weekly)</title>
<body bgcolor="#ffffff">
<h1>30 Minute Averages (Weekly)</h1>
EOM

foreach $file (@list) {
	open(IN, "$dir/$file.html") || die "open $file.html: $!";
	while (<IN>) {
		last if /^<TITLE>/;
	}
	chop($title = <IN>);
	print OUT "<h2>$title</h2>\n";
	while (<IN>) {
		last if /30 Minute Average/;
	}
	print OUT "<p> ", $_;
	while (<IN>) {
		last if /^$/;
		print OUT;
	}
	print OUT;
	while (<IN>) {
		last if /^$/;
		print OUT;
	}
	close(IN);
}
close(OUT);

open(OUT, "> $dir/time-2hour.html") || die "open 2hour: $!";
print OUT <<EOM;
<title>2 Hour Averages (Monthly)</title>
<body bgcolor="#ffffff">
<h1>2 Hour Averages (Monthly)</h1>
EOM

foreach $file (@list) {
	open(IN, "$dir/$file.html") || die "open $file.html: $!";
	while (<IN>) {
		last if /^<TITLE>/;
	}
	chop($title = <IN>);
	print OUT "<h2>$title</h2>\n";
	while (<IN>) {
		last if /2 Hour Average/;
	}
	print OUT "<p> ", $_;
	while (<IN>) {
		last if /^$/;
		print OUT;
	}
	print OUT;
	while (<IN>) {
		last if /^$/;
		print OUT;
	}
	close(IN);
}
close(OUT);

open(OUT, "> $dir/time-1day.html") || die "open 1day: $!";
print OUT <<EOM;
<title>1 Day Averages (Yearly)</title>
<body bgcolor="#ffffff">
<h1>1 Day Averages (Yearly)</h1>
EOM

foreach $file (@list) {
	open(IN, "$dir/$file.html") || die "open $file.html: $!";
	while (<IN>) {
		last if /^<TITLE>/;
	}
	chop($title = <IN>);
	print OUT "<h2>$title</h2>\n";
	while (<IN>) {
		last if /1 Day Average/;
	}
	print OUT "<p> ", $_;
	while (<IN>) {
		last if /^$/;
		print OUT;
	}
	print OUT;
	while (<IN>) {
		last if /^$/;
		print OUT;
	}
	close(IN);
}
close(OUT);

