#!/usr/bin/perl
# Time-stamp: "2006-08-10 17:03:50 ADT"   sburke@cpan.org
# desc{   generates a simple blank day-planner in RTF   }

use strict;
use warnings;
use RTF::Writer;
use Time::Local;
use Lingua::EN::Numbers::Ordinate;

my $year;
my $this_year = ( (localtime)[5] + 1900 );
if( ($ARGV[0] || $this_year) =~ m<^([12]\d\d\d)$> ) {  # No arg = this year
                                                       # "2009" = 2009
  $year = $1;
} elsif ($ARGV[0] =~ m<^([-+]+\d\d?)$> ) {             # "+1" = next year
  $year = $this_year + $1;
} else {
  die "I don't understand the parameter \"$ARGV[0]\"\nAborting";
}

my $day_size = 6060; # twips

my $r = RTF::Writer->new_to_file("plan$year.rtf");
$r->prolog( 'fonts' => ['Georgia', 'Courier New'] );
 # or use Times or Tahoma instead of Georgia
$r->paragraph(
  \"\\sa900\\f0\\fs60\\lang1033\\qc",
   "Agenda\n\n$year"
);


my $now = timegm(0,0,12, 16 , 11 , $year - 1900 - 1);
my $end = timegm(0,0,12, 11 ,  0 , $year - 1900 + 1);

my @months = qw(
 January February March      April   May      June
 July    August   September  October November December
);
my @dows  = qw(
 Sunday Monday Tuesday Wednesday Thursday Friday Saturday
);
{
  my $cal = join '', grep !m/^\s+\d\d\d\d\s+$/s, `cal $year`;
  $cal =~ s/\t/        /g;
  $r->paragraph( \'\fs21\f1', $cal );
}

$r->Page;

while(1) {
  my($y,$m,$d, $wday) = (gmtime($now))[5,4,3,6];
  $y += 1900;
  my $date = sprintf
    "\x85   %04d-%02d-%02d   \x97   %s, %s %s    \x85",
    $y, $m+1, $d,
    $dows[$wday], $months[$m], ordinate($d),
  ;
  #print $date, "\n";
  my $space = int $day_size;
  $space = int($space / 2) if $wday == 0 or $wday == 6;
  $r->paragraph( \"\\sa$space\\f0\\fs30\\lang1033\\qc",
    $date
  );
  
  $now += 24 * 60 * 60;
  last if $now > $end;
}

$r->close;
undef $r;
sleep 0;  # HONK SIU
exit;

