#!/usr/bin/perl
# Filename:	make_project
# Author:	David Ljung Madison <DaveSource.com>
# See License:	http://MarginalHacks.com/License
# Description:	Makes all the HTML for a project page
use strict;

##################################################
# Setup the variables
##################################################
my $PROGNAME = $0;
$PROGNAME =~ s|.*/||;

chdir('/WWW/web/MarginalHacks.com/Hacks/album/');

my $CHAPTERS = "Chapters";

sub printname {
  my ($chap) = @_;
  $chap =~ s/^\d+\.//;
  $chap =~ s/_/ /g;
  $chap = "Themes/Examples" if $chap eq "Themes";
  $chap;
}

sub filename {
  my ($chap) = @_;

  return "/Contact/" if $chap =~ /^\d+\.Contact$/;
  return "Docs/" if $chap =~ /^\d+\.Documentation$/;
  return "index.html" if ($chap =~ /home$/i);
  $chap =~ s/\?//g;
  return "$'.html" if ($chap =~ /^\d+\./);
  return "$chap.html";
}

##################################################
# Main code
##################################################
opendir(DIR,$CHAPTERS) || die("Couldn't opendir $CHAPTERS\n");
my @chap = sort grep(! -d "$CHAPTERS/$_", readdir(DIR));
@chap = grep(!/\.eperl$/, @chap);
@chap = grep(!/\.old$/, @chap);
closedir(DIR);
print "@chap\n";

open(HEADER,"Header") || die("Couldn't read Header\n");
my @header = <HEADER>;
close(HEADER);

sub flags {
	my ($dir) = @_;
	opendir(D,$dir) || return;
	my @d = grep(-f "$dir/$_/flag.png", readdir D);
	closedir D;
	join(" ",map("<a href='$dir$_'><img src='$dir$_/flag.png' alt='$_' border=0></a>", @d));
}

foreach my $chap ( @chap ) {
  my $f = "$CHAPTERS/$chap";
  print "eperl $f.eperl -o $f\n" if -f "$f.eperl";
	system("eperl $f.eperl -o $f") if -f "$f.eperl";
  next unless -s "$f";
  open(CHAP,"<$f") || die("Couldn't read $f\n");
  my @text = <CHAP>;
  close CHAP;

  my $file = filename($chap);
  chmod 0644, $file;	# Make temporarily writable
  open(CHAP,">$file") || die("Couldn't write $file\n");

  print CHAP @header;
  print CHAP "<table cellpadding=8 width='100%'> <tr>\n";
  print CHAP "  <td valign=top>\n";

  print CHAP "    <table width='100%'>\n";
  foreach my $c ( @chap ) {
    my $f = filename($c);
    my $h = ($f eq "index.html") ? "." : $f;
    my $p = printname($c);
    print CHAP "      <tr bgcolor='#BBBBBB'><td>\n";
    print CHAP "        <h3>";
    print CHAP "<a href='$h'>" unless ($f eq $file);
    print CHAP "$p";
    print CHAP "</a>" unless ($f eq $file);
		print CHAP "<br />".flags($f) if $f eq "Docs/";
    print CHAP "        </h3>\n";
    print CHAP "        <p>\n";
    print CHAP "      </td></tr>\n";
  }
  print CHAP "    </table>\n";

	if ($f =~ /Home/) {
		# Clustrmaps
		print CHAP "<br /><a rel=nofollow href=\"http://www3.clustrmaps.com/user/5363ccf6\"><img src=\"http://www3.clustrmaps.com/stats/maps-no_clusters/MarginalHacks.com-Hacks-album--thumb.jpg\" /></a>\n";

		# Download mirror
		print CHAP <<MIRROR if 0;
			<p />
			<font size='-1'>
			<img src="Pics/Download.png" width=36 height=40 alt="" align=left border=0>
			<a href="/redir.cgi?MarginalHacks.pcwin.com/bin/album.versions/">Download mirror</a>
			<br />
			courtesy <a href="http://pcwin.com">PCWin.com</a>
			</font>
MIRROR
	}

  print CHAP "  </td>\n";
  print CHAP "  <td valign=top>\n";

  print CHAP @text;

  print CHAP "  </td>\n";
  print CHAP "</tr> </table>\n";

  print CHAP "<font size='-1'>\n";
  print CHAP "These pages were, of course, generated by a";
  print CHAP " <a href='$0'>perl script</a> with this";
  print CHAP " <a href='$CHAPTERS'>source</a>\n";
  print CHAP "</font>\n";

#  # Blah
#  if ($chap =~ /^01/ && open(SP,"<Blah")) {
#    print CHAP "<p><hr><p>\n";
#    while (<SP>) { print CHAP; }
#  }

  print CHAP "</body>\n";
  print CHAP "</html>\n";

  close CHAP;
  chmod 0444, $file;	# Non-writable
}
