#!/usr/bin/perl -w

##########################################################
#  bdays.pl
#
# reads the birthday entries in the Evolution contact
# database and generates calendar entries to remind you
# in time
#
# (c) 2004 by daduke <daduke@daduke.org>
##########################################################


use strict;
use DB_File;
sub parse_cal();
sub get_event();

my $categorie = "Geburtstag";				#calendar category, probably "Birthday" for english Evolution
my $OUTFILE = "/tmp/bdays.vcf";			#file to import in Evolution

my $home = $ENV{'HOME'};
my $ADDRFILE = "$home/evolution/local/Contacts/addressbook.db";		#contacts are here
my $CALFILE = "$home/evolution/local/Calendar/calendar.ics";			#calendar (sort out existing birthdays)
my @cal_entries;
my %bdays;
my $entry = 0;
open OUT, ">$OUTFILE" or die "could not create VCF file!\n";

my ($CDAY, $CMONTH, $CYEAR) = (localtime)[3,4,5];		#get today's date
$CYEAR += 1900;
$CMONTH++;
$CMONTH = sprintf("%02d", $CMONTH);
$CDAY = sprintf("%02d", $CDAY);

print "today is: $CYEAR-$CMONTH-$CDAY\n\n";
parse_cal();			#get existing birthday entries

print OUT "BEGIN:VCALENDAR
VERSION:2.0\n";		#header for VCF file

my %DBIN;
return 0 unless dbmopen(%DBIN, $ADDRFILE, 0666);	#open contact database
foreach my $key ( keys(%DBIN) ) {						#step thru all entries and check if
		  my $line = $DBIN{$key};
		  next if $line !~ m/.*:.*/;
		  my @list = split /\r\n/, $line;
		  my %phash;
		  foreach my $item (@list) {
					 next if $item !~ /.+:.+/;
					 my ($tag, $content) = split(/:/, $item);
					 $phash{$tag} = $content;
		  }
		  if ($phash{'BDAY'}) {								#they have birthdays attached
					 my ($BYEAR, $BMONTH, $BDAY) = split (/-/, $phash{'BDAY'});	#if so, it may be
					 my $dtstart = "$CYEAR$BMONTH$BDAY";
					 if ($bdays{$phash{'FN'}} && $bdays{$phash{'FN'}} == $dtstart) {	#already existing,
							print "birthday already exists for $phash{'FN'}\n";
					 } elsif ($BMONTH < $CMONTH) {												#already over or
							print "birthday already over for $phash{'FN'}\n";
					 } else {																			#new ->
							print "adding birthday entry for $phash{'FN'} on $dtstart\n";
																											#write calendar entry
							print OUT <<EOF;
BEGIN:VEVENT
DTSTART:${dtstart}T080000
DTEND:$CYEAR$BMONTH${BDAY}T080100
SUMMARY:$phash{'FN'}
CATEGORIES:$categorie
CLASS:PUBLIC
LAST-MODIFIED:$CYEAR$CMONTH${CDAY}T080000
BEGIN:VALARM
TRIGGER;VALUE=DURATION;RELATED=START:-PT0S
ACTION:DISPLAY
DESCRIPTION:$phash{'FN'}
END:VALARM
END:VEVENT
EOF
					 }
		  }
}
print OUT "END:VCALENDAR";
print "\n\nnow import $OUTFILE into Evolution!\n\n";

dbmclose %DBIN;
close OUT;

#-------------------

sub parse_cal() {
		  my $lastline;

		  open CAL, $CALFILE;
		  while (<CAL>) {
					 last if (/END:VTIMEZONE/);
		  }
		  <CAL>;
		  do {
					 $lastline = get_event();
					 $entry++;
		  } until ($lastline =~ /END:VCALENDAR/);

		  close CAL;
		  my $date;
		  for (my $i=0; $i < scalar (@cal_entries); $i++) {
					 if ($cal_entries[$i]{'CATEGORIES'} && $cal_entries[$i]{'CATEGORIES'} =~ /$categorie/) {
								($date) = $cal_entries[$i]{'DTSTART'} =~ /(\d{8})/ if ($cal_entries[$i]{'DTSTART'});
								my $name = $cal_entries[$i]{'SUMMARY'};
								chomp $name;
								$bdays{$name} = $date;
					 }
		  }
}

sub get_event() {
		  while (<CAL>) {
					 if (/:\n/) { chomp; $_ .= <CAL>; }
			 		 my ($tag, $item) = split /:/;
					 $cal_entries[$entry]{"$tag"} = $item;
					 last if /END:VEVENT/;
		  }
		  return <CAL>;
}
