+ print PLAN "END:VCALENDAR\n";
+ close PLAN;
+}
+
+#
+# Should probably leave this task to Net::ICal. But it is not
+# available in debian/sarge, so I make a quick-n-dirty solution
+# instead.
+#
+sub read_icalendar {
+ my $filename = shift;
+ open (ICALENDAR, "<$filename") or die "Unable to read from $filename";
+ my $oldval = $/;
+ $/ = "\r\n";
+ while (<ICALENDAR>) {
+ chomp;
+ if (m/^BEGIN:VEVENT/) {
+ my %event;
+ while (<ICALENDAR>) {
+ chomp;
+ last if (m/END:VEVENT/);
+ $event{description} = $1 if (m/^DESCRIPTION\s*:\s*(.+)$/);
+ $event{created} = $1 if (m/^CREATED\s*:\s*(.+)$/);
+ $event{dtend} = $1 if (m/^DTEND\s*:\s*(.+)$/);
+ $event{dtstamp} = $1 if (m/^DTSTAMP\s*:\s*(.+)$/);
+ $event{dtstart} = $1 if (m/^DTSTART\s*:\s*(.+)$/);
+ $event{last-modified} = $1 if (m/^LAST-MODIFIED\s*:\s*(.+)$/);
+ $event{sequence} = $1 if (m/^SEQUENCE\s*:\s*(.+)$/);
+ $event{summary} = $1 if (m/^SUMMARY\s*:\s*(.+)$/);
+ $event{uid} = $1 if (m/^UID\s*:\s*(.+)$/);
+ }
+ push @events, \%event;
+ }
+ }
+ close (ICALENDAR);
+ $/ = $oldval;
+}
+
+# Simple version covering the chars I need.
+sub utf8tolocalmap {
+ my $string = shift;
+ $string =~ s/ø/ø/g;
+ $string =~ s/æ/æ/g;
+ $string =~ s/Ã¥/å/g;
+ return $string;
+}
+
+sub write_planfile {
+ my $filename = shift;
+ open(PLAN, ">$filename") or die "Unable to write to $filename";
+ for my $event (@events) {
+ # offset = 0 -- assume all timestamps in local time zone
+ my $ical = Date::ICal->new( ical => $event->{dtstart}, offset => 0 );
+ my $date = join("/", $ical->month, $ical->day, $ical->year);
+ my $start = join(":", $ical->hour, $ical->min, $ical->sec);
+
+ my $icalstop = Date::ICal->new( ical => $event->{dtend}, offset => 0 );
+ my $icalduration = $icalstop - $ical;
+ my $duration = join(":",
+ $icalduration->hours || "0",
+ $icalduration->minutes || "0",
+ $icalduration->seconds || "0");
+
+ my $code = "N";
+ my $summary = utf8tolocalmap($event->{summary});
+ print PLAN "$date $start $duration 0:0:0 0:0:0 ---------- 0 0\n";
+ print PLAN "$code\t$summary\n";
+ }