I wrote a perl script to take an exported icalendar/vcalendar file from Kontact/Korganizer and split it into individual event files so they can be viewed on an ipod. Everything works except for one thing I can't figure out. Every calendar entry produces its own individual file successfully, except the last file created is always incomplete, thus rendering that event unviewable by the ipod. (So, with a calendar containing 47 events, file "event0047.ics" is incomplete and unviewable). This happens when run on both Windows and Linux (perl 5.8.6), and consistently happens, no matter how many entries a calendar contains. Any insights would be greatly appreciated. Here is the script (usage: $ cal2ipod.pl [calendar_name.ics]):
Code:
#!/usr/bin/perl
# cal2ipod
# perl script to reformat icalendar output from Kontact for use on Apple ipod
# Kris Shaffer, (C) 2005
# -----Begin Program-----
use strict;
# put icalendar header in separate array
my $file = shift;
open CAL, $file or die "No calendar file expressed!\n$!";
my @headtmp = <CAL>;
my @head;
foreach (0..2) {
$head[$_] = $headtmp[$_];
}
open CAL, $file or die "No calendar file expressed!\n$!";
my @orig = <CAL>;
foreach (1..3) {
shift @orig;
}
# generate temporary file with remaining event data
open TEMPOUT, "> icaltemp" or die "Failed to create temp file icaltemp.ics: $!";
print TEMPOUT @orig;
open TEMPIN, 'icaltemp' or die "Failed to open temp file icaltemp.ics: $!";
# create individual files for each entry
$/ = "";
my $entryfile;
my $event;
my $num = 1;
while (<TEMPIN>) {
my $numf = (sprintf '%04s', $num);
$entryfile = "event" . $numf . '.ics';
open ENTRYFILE, "> $entryfile" or die "Failed to create calendar entry $!";
my @entry = @head;
push @entry, $_;
print ENTRYFILE @entry;
print "$entryfile created.\n";
$num++;
}
unlink "icaltemp";