#!/perl/bin/perl -w

# 
# gen-pages.pl Version 1.01
# 
# Rick Umali 7/8/2001
# 
# RENAMED from gen-mia-pages to gen-pages on 8/19/2001
#
# See POD documentation for usage and more information. To do this,
# type "perldoc gen-pages.pl".
#
# This Perl program reads a file called contents.txt, and generates a
# single HTML page for each valid line of that file. A valid line
# consists of four fields, each separated by a ":". The first field is
# the name of the photo. The second field is the 'caption' of the photo.
# The third field is the date in which the photo was added to the set
# of pages (used in the Original Post string). An optional fourth
# field is used to indicate "grouping".
#
# Any lines beginning with a "#" is considered a comment, and is ignored
# during page generation.
#
# The contents.txt drives the entire 'hierarchy' of the pages. The first
# page is the first one displayed. The last is the last one displayed.
# There are corresponding links that are created on each page to navigate
# through all the pictures. It was the creation of these navigational
# links that caused me to write this program.

# TODO: Add META Refresh in headers of HTML pages
# TODO: Use TEMPLATES for God's sake!
# TODO: Incorporate category as a specific brand, traverse through
#       categories
# TODO: Add interactive mode (to suppress unlink error)
# TODO: Make index.html page a parameter
# TODO: POD
# TODO: Make a page containing links to their groups.
# TODO: Make a thumbnailer?
# TODO: Should make deletion of files a parameter (or at least prompt
#       user that this would happen).
# TODO: For template, use "require" Perl tag.
# TODO: Add option for cap_list (if nogenerate, then cap_list='').
# TODO: On caption page, highlight the latest 'batch' of photos.
# TODO: Check that image file "exists"
# TODO: Add "latest.html" for the latest picture.

# ADDED: New index.html sorted by date (12/31/2001) Version 1.01

use Getopt::Long;
use Date::Manip;

#
# Date::Manip is a module for handling dates. I use these Date::Manip
# functions: Date_Init, ParseDate, UnixDate
#

BEGIN {
        # Initialize the TIMEZONE for Date::Manip
        $ENV{TZ}=EDT;
}

Date_Init;

#
# Used by sort function for date comparison 
#
sub date_compare {
        $date_a = ParseDate($a);
        $date_b = ParseDate($b);
        $date_b cmp $date_a;
}

$page_gen_date = localtime(time);

my $opt_debug = 0;   # option variable with default value (false)
my $opt_version = 0;
my $opt_file = "contents.txt";
my $opt_pagename = "mia";
GetOptions ('debug' => \$opt_debug,
            'version|v' => \$opt_version,
            'file=s' => \$opt_file,
            'pagename=s' => \$opt_pagename);

if ($opt_debug) {
        print "I'm in debug mode.\n\nAll options:\n";
        print "\$opt_debug = $opt_debug\n";
        print "\$opt_version = $opt_version\n";
        print "\$opt_file = $opt_file\n";
        print "\$opt_pagename = $opt_pagename\n";
}

if ($opt_version) {
        print "gen-pages.pl - Version 1.01 - 12/31/2001 Orig: 8/19/2001";
        exit(0);
}

open (CONTENTS, $opt_file) or die "Can't open $opt_file\n";

$count = 0;
while ($line = <CONTENTS>) {
        next if ($line =~ /^#/); # Ignore comments
        next if ($line =~ /^$/); # Ignore blank lines
        chop $line;
        # NOTE: This code used to read 'category', but I've dropped this
        ($picture, $caption,$orig_post_date) = split(":", $line);
        push @picture, $picture;
        push @caption, $caption;
        push @orig_post_date, $orig_post_date;
        $count++;
}
close (CONTENTS);

$num_pages = $count;

if ($opt_debug) {
        print "\$num_pages = $num_pages\n";
        for ($i = 0; $i < $num_pages; $i++) {
                print $i+1 . " -- $picture[$i] -- $caption[$i] -- $orig_post_date[$i]\n";
        }
}

$first_page_name = sprintf("%s-01.html", $opt_pagename);
$last_page_name = sprintf("%s-%02d.html", $opt_pagename, $num_pages);

$gen_start_time = time;

for ($i = 0; $i < $count; $i++) {
        $page_num = $i + 1;
        $page_name = sprintf("%s-%02d.html", $opt_pagename, $page_num);

        # Build the "new" index based on dates...
        $pix_orig_dates{$orig_post_date[$i]} .= "<A HREF=\"$page_name\">$page_num</A> ";

        # Build FIRST - PREV - CUR OF COUNT - NEXT - LAST Links
        $prev_page = $page_num - 1;
        $next_page = $page_num + 1;

        if ($prev_page == 0) {
                $prev_page_string = "Prev";
        } else {
                $prev_page_string = sprintf("<A HREF=\"%s-%02d.html\">Prev</A>", $opt_pagename, $prev_page);
        }
        if ($next_page > $num_pages) {
                $next_page_string = "Next";
        } else {
                $next_page_string = sprintf("<A HREF=\"%s-%02d.html\">Next</A>", $opt_pagename, $next_page);
        }
        unlink $page_name;
        open (PAGE, ">$page_name") or die "Can't open $page_name\n";
        print PAGE <<"EOF";
<TITLE>Pictures of Mia Umali - $page_num - $caption[$i]</TITLE>

<!-- Generated by gen-pages.pl, a Rick Umali Perl program -->
<!-- Send e-mail to rickumali\@gmail.com for details -->

<BODY BGCOLOR="#FFC0FF">
<CENTER>

<H2>Pictures of Mia Umali - $page_num</H2>
<A HREF="index.html">Index</A> | <A HREF="captions.html#$page_num">Captions</A><BR>
<A HREF="$first_page_name">First</A> | 
$prev_page_string |
$page_num of $num_pages |
$next_page_string |
<A HREF="$last_page_name">Last</A><P>

<IMG SRC="$picture[$i]" ALT="$caption[$i]">

</CENTER>

<HR>
Rick (<A HREF="mailto:rickumali\@gmail.com">rickumali\@gmail.com</A>) Umali<BR>
Original Post: $orig_post_date[$i]<BR>
Last <A HREF="generated.html">Generated</A>: $page_gen_date
</BODY>
EOF
        close PAGE;
}

#
# Generate the list of captions, each of which is a link 
# The list of captions are in reverse order
#
$cap_list = "<TABLE BORDER CELLSPACING=2 CELLPADDING=2 WIDTH=85% ALIGN=CENTER>\n";
for ($i = $count-1; $i >= 0; $i--) {
        $page_num = $i + 1;
        $page_name = sprintf("%s-%02d.html", $opt_pagename, $page_num);

        # This is the row of data
        $cap_list .= "<TR>\n";

        # This is the first column - a link of the page name
        $cap_list .= "<TD>";
        $cap_list .= "<A NAME=\"$page_num\" HREF=\"$page_name\">$page_name</A>";
        $cap_list .= "</TD>\n";
        # This is the second column - the caption
        $cap_list .= "<TD>";
        $cap_list .= "$caption[$i]";
        $cap_list .= "</TD>\n";
        # This is the third column - the date posted
        $cap_list .= "<TD ALIGN=RIGHT>";
        $cap_list .= "$orig_post_date[$i]";
        $cap_list .= "</TD>\n";
        $cap_list .= "</TR>\n\n";
}
$cap_list .= "</TABLE>\n";

#
# Dump the captions into a file
#

unlink "captions.html";
open (CAPTION, ">captions.html") or die "Can't open captions.html\n";
print CAPTION <<"EOF";
<TITLE>Pictures of Mia Umali - Caption Index</TITLE>

<!-- Generated by gen-pages.pl, a Rick Umali Perl program -->
<!-- Send e-mail to rickumali\@gmail.com for details -->

<BODY BGCOLOR="#FFC0FF">
<CENTER>

<H2>Pictures of Mia Umali - Caption Index</H2>

<A HREF="index.html">Index</A><P>

Last Generated: $page_gen_date<P>

$cap_list

</CENTER>
<HR>
Rick (<A HREF="mailto:rickumali\@gmail.com">rickumali\@gmail.com</A>) Umali<BR>
Last <A HREF="generated.html">Generated</A>: $page_gen_date
</BODY>
EOF
close (CAPTION);

# 
# Generate the string of dates, sorted by original posting date
#
foreach $orig_date (sort date_compare keys %pix_orig_dates) {
        $index_string .= UnixDate($orig_date, "%D") . "  ";
        $index_string .= $pix_orig_dates{$orig_date} . "\n";
}
substr($index_string, -2) = '  '; # Remove last "\n"

#
# Generate the "new" index.html page, with picture list sorted by date
#
unlink "index.html";
open (INDEX, ">index.html") or die "Can't open index.html\n";
print INDEX <<"EOF";
<TITLE>Pictures of Mia Umali - Index</TITLE>

<!-- Generated by gen-pages.pl, a Rick Umali Perl program -->
<!-- Send e-mail to rickumali\@gmail.com for details -->

<BODY BGCOLOR="#FFC0FF">
<CENTER>

<H2>Pictures of Mia Umali</H2>

Mia Magdalena Umali<BR>
Born: March 28, 2001 (Wednesday) 10:40AM<BR>
6 pounds 13.3 ounces<BR>
20 inches<P>

Grateful parents: 
<A HREF="http://www.rickumali.com/">Rick and Jenn</A><P>

<A HREF="$first_page_name">First Picture (1)</A> - 
<B><A HREF="captions.html">Caption Index</A></B> -
<A HREF="$last_page_name">Last Picture ($num_pages)</A>
<P>
</CENTER>
<HR WIDTH=70%>
<BLOCKQUOTE>
<PRE>
Date      Picture Numbers
$index_string
</PRE>
</BLOCKQUOTE>
<HR>
Rick (<A HREF="mailto:rickumali\@gmail.com">rickumali\@gmail.com</A>) Umali<BR>
Last <A HREF="generated.html">Generated</A>: $page_gen_date
</BODY>
EOF
close (INDEX);
$gen_finish_time = time;
$gen_time = $gen_finish_time - $gen_start_time;
$count += 2; # Add two for index.html and captions.html
print "Generated $count Pages in $gen_time seconds\n";

=head1 NAME

gen-pages.pl - Perl Script to Generate Simple Picture Site

=head1 SYNOPSIS

./gen-pages.pl [-debug] [-version or -v] [-file FILENAME] 
               [-pagename PAGE_NAME]

=head1 DESCRIPTION

This will create a set of HTML pages based on the list found in 
FILENAME (default: ./contents.txt). The style of the HTML pages
is based on an internal style (for now).

FILENAME should contain lines with this four-field format:

photo:caption:original post:group

The first field is the name of the photo to be displayed on the
HTML page.

The second field is the caption (used in ALT TEXT of IMG SRC).

The third field is the date that this photo was added to the list of
pages.

The fourth field ("group") can be blank.

Lines that begin with "#" are considered comments in FILENAME. Blank lines are
ignored.

An HTML file will be created for every valid line in FILENAME.  Each of these
HTML files will contain naviational links to the first photo, the last photo,
the previous photo, and the next photo. If previous and next photos are
meaningless, no link is created. 

A single index.html file will also be created (again using an internal style).
This index.html file is the main entry into the picture site, and it contains a
link to every photo.

My own goal for writing this program was to automate the creation of a picture
site, which included an index page, and navigation links. Moreover, I knew that
I would be updating the site possibly two or three times a week, and I wanted
the program to be quick (just add the photos to the contents file, and type
gen-pages.pl).

=head1 AUTHOR

Rick Umali (rickumali@gmail.com)