#!/usr/bin/perl -w
#
#	web.calendar2
#
#	Generate a calendar.
#
#	Generate a simple calendar.  Get the information
#	from a calendar file.
#
#	This generates a single web page with the calendar
#	between the given dates specified.
#
#	Usage:
#	    web.calendar2 [flags] >calendar.html
#	Flags:
#	    --data datafile
#		This is the name of the calendar data file.  It contains
#		which are of the form:
#		    #		Comment line
#		    Year=1999	Introduces a new year
#		    Month=10	Introduces a new month
#		    1=event	Some event on the specified day.
#		    TBA=event	Some event at an unknown date within the month.
#		The event calendar must be in order.  If not specified, the
#		event calendar's filename is assumed to be "calendar.data".
#		The events may contain HTML, but keep it simple.
#	    --start 1998/10
#		This specifies the starting month.  This must be in the
#		format shown.  If unspecified, it starts as early as possible.
#	    --end 1999/05
#		This specifies the ending month.  If unspecified, the calendar
#		goes as long as possible.
#	    --bg color
#		This specifies the background color.  This can be anything
#		that HTML allows.  This defaults to no specific color.
#	    --title "some title"
#		This specifies the title of the calendar.
#	    --banner "some message"
#		This is the header text of the calendar.
#
#	This program is in the public domain.  Do what you want with it.
#	Don't ask too many questions.
#
#	The official source is in on my home computer.  A copy which
#	should be up to date is at:
#		http://cornvalley.peak.org/web.calendar/
#
#			Dave Regan
#			regan@ao.com
#			7 Feb 1999
#

use strict;
use Getopt::Long;

use vars qw(%Activities $EndMonth $EndYear @MonTabShort
	%Params $StartMonth $StartYear $Version);

$StartYear	= 1000;
$StartMonth	= 1;
$EndYear	= 9999;
$EndMonth	= 12;
$Version	= 'web.calendar2 v0.11 regan@ao.com';

%Params =
    (
    	"data"		=> "calendar.data",
	"start"		=> "0000/01",
	"end"		=> "9999/12",
	"bg"		=> "",
	"title"		=> "Calendar",
	"banner"	=> "Calendar",
    );

@MonTabShort	= ( "", "Jan", "Feb", "Mar", "Apr", "May", "Jun",
			"Jul", "Aug", "Sep", "Oct", "Nov", "Dec");

###
###	Main program
###

    my($cal, $day, $disp, $key, $lastmon, $mon, $thismon, $year);

    # Crack the parameters
    GetOptions(\%Params,
    	"data=s", "start=s", "end=s", "bg=s", "title=s", "banner=s");

    # Split the years and month
    ($StartYear, $StartMonth) = split(/[\D]/, $Params{'start'});
    ($EndYear, $EndMonth) = split(/[\D]/, $Params{'end'});

#   chdir($ENV{'HOME'});
    ReadCalendar($Params{'data'});

    $Params{'bg'} = qq|bgcolor="$Params{'bg'}"| if ($Params{'bg'} ne "");
    print << "	EOF";
	<html>
	<head>
	<title>$Params{'title'}</title>
	</head>

	<body $Params{'bg'}>
	<h3><center>$Params{'banner'}</center></h3><p>
	<table>
	EOF

    $lastmon = 0;
    $thismon = 0;
    for $key (sort(keys %Activities))
    	{
    	($year, $mon, $day) = split(/$;/, $key);
    	$disp = $day;
    	$disp = "TBA" if ($day == 0);
	$thismon++;
	if ($mon != $lastmon && $lastmon != 0)
	    {
	    while ($thismon++ < 5)
	    	{
		print "<tr><td><br></td><td><br></td>\n";
		}
	    print "<tr><td colspan=3><hr></td>\n";
	    $thismon = 0;
	    }
    	print "<tr><td>$MonTabShort[$mon] $disp</td>
    		   <td>$Activities{$year, $mon, $day}</td>\n";
    	if ($mon != $lastmon)
    	    {
	    $cal = `cal $mon $year`;
	    $lastmon = $mon;		# Put out a new table for the month
	    print "<td rowspan=5><small><pre>$cal</pre></small></td>";
	    }
    	}
    print "</table>\n";
    exit 0;


###
###	myway
###
###	A sorting routine.
###
sub myway
    {
    my($ax, $bx);

    ($ax = $a) =~ s/TBA/00/g;
    ($bx = $b) =~ s/TBA/00/g;
    return $ax <=> $bx;
    }


###
###	ReadCalendar
###
###	Read a calendar file.
###
sub ReadCalendar
    {
    my($fname) = @_;
    my($month, $name, $value, $year);

    if (!open(CAL, "<$fname"))
    	{
    	print "Cannot open $fname\n";
    	exit 1;
    	}

    while (<CAL>)
    	{
    	chomp;
    	next if (/^\s*#/ || /^\s*$/);
    	if (/^([^=]*)=\s*(.*)/)
    	    {
    	    $value = $2;
    	    ($name = $1) =~ s/\s*//;
    	    if ($name eq "Year")
    	    	{
    	    	$year = $value;
    	    	}
	    elsif ($name eq "Month")
	    	{
	    	$month = sprintf("%2d", $value);
	    	}
	    elsif ($name =~ /^(\d+|TBA)$/)
	    	{
	    	$name = sprintf("%2d", $name) if ($name ne "TBA");
	    	$name = " 0" if ($name eq "TBA");
		if (($year > $StartYear ||
		    ($year == $StartYear && $month >= $StartMonth)) &&
		    ($year < $EndYear ||
		     ($year == $EndYear && $month <= $EndMonth)))
		    {
		    if (!defined($Activities{$year, $month, $name}))
		    	{
			$Activities{$year, $month, $name} = "$value\n";
			}
		    else
		    	{
			$Activities{$year, $month, $name} .= "/ $value\n";
		    	}
		    }
	    	}
    	    }
	}
    close CAL;
    }
