#!/usr/bin/perl

my @topics = qw(Calendar Clothing Discuss Contact_Us);

sub write_file {
  my ($which) = @_;
  my $file = "$which/index.html";
  close(STDOUT);
  open(STDOUT,">$file") || die("Couldn't write file [$file]\n");
}

sub contents {
  my ($which) = @_;

  my $file = "$which/txt";
  open(CON,"<$file") || die("Couldn't read file [$file]\n");
  while (<CON>) { print; }
  close(CON);
}

sub header {
  my ($which) = @_;

  # Create list o' topic links
  my $topics;
  foreach my $topic ( "SFLX", @topics ) {
    my $name = $topic;
$name = "Calendar / Tickets" if $name eq "Calendar";
    my $url = "/SFSO/$topic";
		$url = "/" if $topic eq "SFLX";
#    $url = "http://register.sflindyexchange.com" if ($topic eq "Registration");
    $name =~ s/_/ /g;
    $topics .= ( ($topic eq $which) ?
      "$name<p>\n" :
      "<a href='$url'>$name</a><p>\n" );
  }

  open(HEADER,"<Header") || die("Couldn't read Header");
  my @hdr;
  while (<HEADER>) {
    s/\@TOPICS_GO_HERE/$topics/g;
    push(@hdr,$_);
  }
  close(HEADER);

  print @hdr;
}

sub footer {
  open(FOOTER,"<Footer") || die("Couldn't read Footer");
  print <FOOTER>;
  close FOOTER;
}

foreach my $topic ( @topics, @build_topics ) {
  print STDERR "$topic\n";
  write_file($topic);
  header($topic);
  contents($topic);
  footer($topic);
}

