#!/usr/bin/perl -w
#News Goat 1.0
#
#This script loads data entries into a web page.  In other words, it's a weblog.

use strict;
use CGI qw(:standard);

my $header = "$ENV{'DOCUMENT_ROOT'}/header.inc";
my $middle = "$ENV{'DOCUMENT_ROOT'}/middle.inc";
my @descfile = ();
my $entriesDir = "$ENV{'DOCUMENT_ROOT'}/entries/";
my $storiesDir = "$ENV{'DOCUMENT_ROOT'}/stories/";
my @directoryList = ();
my $footer = "$ENV{'DOCUMENT_ROOT'}/footer.inc";
my $places = "$ENV{'DOCUMENT_ROOT'}/places.inc";
my $currentYear = 0;
my $currentMonth = 0;
my @currentTime = ();
my $script = "$ENV{'SCRIPT_NAME'}";

@currentTime = localtime(time);
$currentYear = 1900 + $currentTime[5];
$currentMonth = $currentTime[4] + 1;

print header();
print "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n";
print "<html>\n";
print "<head>\n";
print "<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=ISO-8859-1\">\n";
print "<title>News Goat</title>\n";
print "<link rel=\"stylesheet\" type=\"text/css\" href=\"ngclassic.css\">\n";

open (INF, $header) or die "Cannot open file.\n";
@descfile = <INF>;
close(INF);
print @descfile;

#Left sidebar -- links.
open (PLACES, $places) or die "Can't open places file: $!";
print "<p>";
while (<PLACES>)
	{
	 		my $title = "";
			my $URL = "";
			($title, $URL) = split(',');
			chomp($URL);
			print"<a class=\"sidebar\" href=\"".$URL."\">".$title."</a><br>\n";
	}
print "</p>\n";
print "<p>\n";
print "    <a href=\"http://validator.w3.org/check/referer\"><img border=\"0\"\n";
print "        src=\"images/valid-html401.gif\"\n";
print "        alt=\"Valid HTML 4.01!\" height=\"31\" width=\"88\"></a>\n";
print "  </p>\n";
print "<p>\n";
print " <a href=\"http://jigsaw.w3.org/css-validator/\">\n";
print "  <img border=\"0\" height=\"31\" width=\"88\"\n";
print "       src=\"images/vcss.gif\"\n";
print "       alt=\"Valid CSS!\">\n";
print " </a>\n";
print "</p>\n";
close (PLACES);

open (INF, $middle);
@descfile = <INF>;
close(INF);
print @descfile;

#main text
opendir(DIR, $entriesDir);
@directoryList = grep(!/^\.{1,2}/, readdir(DIR));
closedir(DIR);
@directoryList = sort {$b cmp $a} @directoryList;

my $file = "";
my $entry = '^';
my @archive = ();
my $currArch = "";
my $count = 0;
my $tenEntries = 1;
my $story = "";

if (!param('story'))
{
   if (!param('entry'))
   {
 	    if ($currentMonth < 10) { $currentMonth = "0" . $currentMonth; }
    	$entry .= $currentYear . $currentMonth;
      $entry .= '.*$';
   }
   else
   {
      $entry .= param('entry');
   	  $entry .= '.*$';
	    $tenEntries = 0;
   }

   foreach $file (@directoryList)
   {
	    my $moYear = substr($file, 0, 6);
      if ($moYear ne $currArch)
		  {
			   push(@archive, $moYear);
      }
      $currArch = $moYear;

      if (($file =~ /$entry/) || ($tenEntries && ($count < 10)))
      {
	 		   my $year = substr($file, 0, 4);
		 	   my $iMonth = substr($file, 4, 2);
         my $month = getLongMonthName($iMonth);
		     my $day = substr($file, 6, 2);
		     my $meridian = getMeridian(substr($file, 8, 2));
		     my $hour = getHour(substr($file, 8, 2));
		     my $minute = substr($file, 10, 2);
		     my $fullpath = $entriesDir.$file;
	 	     open (ENTRY, $fullpath);
		     @descfile = <ENTRY>;
			   close (ENTRY);
			   my $entry_title = $descfile[0];
			   chomp($entry_title);
			   print "<H1>&nbsp;<a href=\"".$script."?entry=".$file."\"><img src=\"/images/storylink.gif\" alt=\"Permanent link to this entry.\" border=\"0\"></A>&nbsp;".$entry_title."</h1>\n";
			   print "<p class=\"timestamp\">".$month." ".$day.", ".$year." -- ".$hour.":".$minute.$meridian."</p>\n";
			   splice (@descfile, 0, 1);
			   print @descfile;
			   print "<br>\n";
				 $count++;
			}
   }
}
else
{
   $story = param('story');
	 $story = $storiesDir . $story;
	 open (INF, $story);
	 @descfile = <INF>;
	 close(INF);
	 print @descfile;
}
	 
#right sidebar 	 
open (INF, $footer);
@descfile = <INF>;
close(INF);
print @descfile;

print "<p>";
my $arch = "";
foreach $arch (@archive)
{
   my $archMonth = getLongMonthName(substr($arch, 4, 2));
	 my $archYear = substr($arch, 0, 4);
	 print "<a class=\"sidebar\" href=\"".$script."?entry=".$arch."\">".$archMonth." ".$archYear."</a><br>\n";
}

print '<a class="sidebar" href="http://slashdot.org/~bmabray/journal/">The Slashdot Journal</a><br>'."\n";
print '<a name="SkipHistoryMenu">&nbsp;</a></p></div>'."\n";

print end_html;

sub getLongMonthName() {
	my $month = shift(@_);
	#yuck, a big if else
	if ($month == 1) {return "January";}
	if ($month == 2) {return "February";}
	if ($month == 3) {return "March";}
	if ($month == 4) {return "April";}
	if ($month == 5) {return "May";}
	if ($month == 6) {return "June";}
	if ($month == 7) {return "July";}
	if ($month == 8) {return "August";}
	if ($month == 9) {return "September";}
	if ($month == 10) {return "October";}
	if ($month == 11) {return "November";}
	if ($month == 12) {return "December";}
}

sub getHour() 
{
   my $hour = shift(@_);
	 if ($hour > 0 && $hour <= 12)
	 {
	    return $hour;
	 }
	 else
	 {
	    return abs($hour - 12);
	 }
}
 

sub getMeridian()

{

   my $hour = shift(@_);

	 my $meridian = " a.m.";

	 if ($hour > 11)

	 {

	    $meridian = " p.m.";

	 }

	 return $meridian;

}

 

