#################################################
# create_diary_content.pl
# by Daniel Wedul (daniel@wedul.com)
# Created on: January 25, 2007
# Last Modified: January 25, 2007
#*********************
# This program requires the following perl programs to be in the same directory
#   create_page.pl
#*********************
#  This script will create all the diary html pages as well as the content file that will be used
#  To create the main diary browser page.
#  Do not include a SAVEAS tag in any of the diary pages.  By not having a SAVEAS tag, the html files will
#	be named with the same basename as the diary page content files.  This is a necessity.
#*********************
#  Usage: create_diary_content.pl <diary_directory> <diary_page_template> <extension>
#     <diary_directory> is the directory containing all the dwdp files.
#     <diary_page_template> is the template for each diary entry.
#				This file is assumed to be in the <diary_directory>
#     <extension> is the the extension used by all diary entries.  All files in <diary_directory>
#		   with the extension <extension> are used as content files.
##################################################
use strict;
#<put the use lines here>

#################################################
############ Command Line Arguments #############
#################################################

#check argument number
die "ERROR: Argument count mismatch.\n".
    "Usage: create_diary_content.pl <directory> <template> <extension>.\n" if ($#ARGV != 2);

#get the arguments
my $Directory = shift (@ARGV);
my $Template = shift (@ARGV);
my $Ext = shift (@ARGV);

#make sure the directory and template exist
die "ERROR: directory $Directory does not exist.\n" if (!(-e $Directory));
die "ERROR: $Directory exists but is not a directory.\n" if (!(-d $Directory));
die "ERROR: template file $Directory\\$Template does not exist.\n" if (!(-e $Directory."\\".$Template));

#################################################
##################### Setup #####################
#################################################

#set the ouput filename
my $OutFile = $Directory."\\content.txt";

#get the contents of the directory
my @files = ReadDirectory($Directory);

#find the files with the proper extension
my @ContentFiles = ();
foreach my $file (@files) {
	my @parts = split(/\./, $file);
	if (pop(@parts) eq $Ext) { push (@ContentFiles, join(".", @parts) ); }
}
#sort them in reverse alphabetical order
#   (because of my naming structure "yyyy_mm_dd" they will be sorted with the newest first).
@ContentFiles = sort { $b cmp $a } @ContentFiles;   # ASCII-betical sort

#create a list to hold the final template.
my @MainTemplate = ();

#################################################
#################  main program  ################
#################################################

#add some static stuff to the top
push (@MainTemplate, '<!--###SAVEAS=diary.html###-->');
push (@MainTemplate, '<!--###TITLE="www.wedul.com -> diary"###-->');

push (@MainTemplate, "<p><center><h1>My [Public] diary</h1></center></p>");
push (@MainTemplate, "<table> <tr>");
push (@MainTemplate, "	<td> <table> <tr> <td> <h4> Disclaimer: </h4> </td> </tr>");
push (@MainTemplate, "<tr> <td> &nbsp; </td> </tr>");
push (@MainTemplate, "<tr> <td> &nbsp; </td> </tr>");
push (@MainTemplate, "<tr> <td> &nbsp; </td> </tr>");
push (@MainTemplate, "<tr> <td> &nbsp; </td> </tr>");
push (@MainTemplate, "</table> </td>");
push (@MainTemplate, "<td width=5> </td>");
push (@MainTemplate, "<td> <p> These are just some random ramblings of mine.  If they offend you feel free to let me");
push (@MainTemplate, "know.  However, be foreworned that any complements or complaints that I receive about anything");
push (@MainTemplate, "posted on this page is fair game for me to post.  I'm not promising that I will post it, but keep in");
push (@MainTemplate, "mind that I might.  Again, please don't be offended at anything on this page.  I never intend to hurt");
push (@MainTemplate, "anyone. </p>");
push (@MainTemplate, "</tr></table>");
push (@MainTemplate, "<p> click on an entry to view it. </p>");
push (@MainTemplate, "<hr>");
push (@MainTemplate, '<table width="425"');

push (@MainTemplate, "");

#create the html files from the content files.  Also put together the main template
foreach my $file (@ContentFiles) {
	`create_page.pl $Directory\\$Template $file.$Ext`;
	
	#Get the stamp and the short title from the diary entry content file.
	my $stamp = "";
	my $short = "";
	open (INFILE, "$Directory\\$file.$Ext") or die "Could not open $Directory\\$file.$Ext for reading.\nERROR:$!\n";
	my $line = <INFILE>;
	while ($line =~ /\<\!\-\-\#\#\#.*\#\#\#\-\-\>/) {
		$line =~ s/\<\!\-\-\#\#\#//;
		$line =~ s/$\#\#\#\-\-\>//;
		if ($line =~ /$ /) { $line =~ s/$ //; }
		if ($line =~ /^STAMP=/) { 
			$line =~ s/STAMP=//;
			$stamp = $line;
		}
		if ($line =~ /^SHORTTITLE=/) {
			$line =~ s/SHORTTITLE=//;
			$short = $line;
		}
		$line = <INFILE>;
	}
	close (INFILE);		

	#add the table entry to the main template
	push (@MainTemplate, '<tr>');
	push (@MainTemplate, '<td width="10"><hr></td>');
	push (@MainTemplate, '<td width="100" align="center"><a href="diary/'.$file.'.html" target="_blank"> '.$stamp.' </a></td>');
	push (@MainTemplate, '<td width="10"><hr></td>');
	push (@MainTemplate, '<td width="100">'.$short.'</td>');
	push (@MainTemplate, '<td><hr></td>');
	push (@MainTemplate, '</tr>');
}

push (@MainTemplate, "");

push (@MainTemplate, "</table>");
push (@MainTemplate, "<hr>");
push (@MainTemplate, "</body>");
push (@MainTemplate, "</html>");

#save the template! :D
open (OUTFILE, ">".$OutFile) or die "Could not open $OutFile for writing.\nERROR:$!\n";
foreach (@MainTemplate) { print OUTFILE $_."\n"; }
close (OUTFILE);


########################################################################
#
#  Read Directory - read the contents of a directory
#	USAGE: my @files = ReadDirectory($directory);
########################################################################
sub ReadDirectory {
	#get the input
	my $dir = shift;

	#open the file
	opendir (INDIR, $dir) or die "Could not open directory $dir for reading.\nERROR:$!\n";

	#get the contents
	my @contents = readdir(INDIR);

	#close the file
	closedir (INDIR);

	#get rid of the newline characters
	chomp @contents;

	#return
	return @contents;
}