#!/usr/bin/perl -w
#  
#  Copyright (c) 2002 Steve Slaven, All Rights Reserved.
#  
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License as
#  published by the Free Software Foundation; either version 2 of
#  the License, or (at your option) any later version.
#  
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#  GNU General Public License for more details.
#  
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
#  MA 02111-1307 USA
#  

use strict;
use vars qw( %ENV );
use Getopt::Std;
use POSIX;

my %o;
getopts( 'hw:t:m:M:', \%o );

die( qq|
today.pl v0.90
Steve Slaven - http://hoopajoo.net

Usage: $0 [-h] [-w weekday] [-m monthday] [-M month] [-t title] [config file]

	config	If not passed tried ~/.todayrc then today.dat
	-w	Day of week, 0 is sunday
	-m	Day of month 
	-M	Month
	-t	Title (passed through strftime)

Config file is line delimited.  The format is:

when,what

Where when is a criteria to match to show an item, and
what is what will be displayed if it matches.  The format
of when is:

#	Day of week (0 is sunday)
[#]	Day of month
[#:#]	Month:Day of month (1 is january)
+	Indent this line (can be used multiple times)

If when doesn't have any criteria and just +'s it will use the last
criteria selected, to allow blocking

e.g.:

# This is a comment
12345,Monday thru friday
134[28],Monday,wednesday,thursday, and the 28th of the month
1,Only monday
+,There
+,Is a
++,Multi level set of
+,Stuff
+,To do

| ) if $o{ h };

my $file = shift( @ARGV ) || ( -f "$ENV{HOME}/.todayrc" ? 
	"$ENV{HOME}/.todayrc" : "today.dat" );

my @now = localtime();
my $wday = defined( $o{ w } ) ? sprintf( '%01d', $o{ w } ) : $now[ 6 ];
my $mday = defined( $o{ m } ) ? sprintf( '%01d', $o{ m } ) : $now[ 3 ];
my $month = defined( $o{ M } ) ? sprintf( '%01d', $o{ M } ) : ( $now[ 4 ] + 1 );

my $title = $o{ t } || 'today.pl on %A, %B %d, %Y';
print strftime( $title, localtime() ) . "\n";
print "-" x 75 . "\n";

my( $line, $plus, $when, $what, @specials );
my $last_when = '';
open( IN, $file ) || die( "Could not open '$file': $!" );
while( $line = <IN> ) {
	chomp( $line );
	$line =~ s/#.*//; # clear comment

	( $plus, $when, $what ) = $line =~
		/^(\+*)(.*?),(.*)/;

	if( $what ) {
		# handle carry over
		$when = $last_when unless $when ne '';

		# handle saving last value, will carry if the last line
		# carried so we don't have to handle undef
		$last_when = $when;

		# Parse specials
		@specials = $when =~ /(\[.*?\])/g;
		$when =~ s/\[.*?\]//g;

		# Day of week
		if( ( $when =~ /\Q$wday\E/ ) ||
				# Day of month + month
				( grep { $_ eq "[$month:$mday]" } @specials ) ||
				# Day of month no month
				( grep { $_ eq "[$mday]" } @specials ) ) {
			print "    " x length( $plus );
			print "[  ] ";
			print $what;
			print "\n";
		}
	}
}
close( IN );
