+sub do_icalendar_schedule {
+ $| = 1;
+ my $r="\r";
+ # Ugh, I can't find a better solution than hardwiring it to CST
+ my $tzn = "America/Chicago";
+ my $timezone = <<EOF
+BEGIN:VTIMEZONE$r
+TZID:$tzn$r
+BEGIN:STANDARD$r
+DTSTART:20041031T020000$r
+RRULE:FREQ=YEARLY;INTERVAL=1;BYDAY=-1SU;BYMONTH=10$r
+TZOFFSETFROM:-0500$r
+TZOFFSETTO:-0600$r
+TZNAME:Standard Time$r
+END:STANDARD$r
+BEGIN:DAYLIGHT$r
+DTSTART:20050403T020000$r
+RRULE:FREQ=YEARLY;INTERVAL=1;BYDAY=1SU;BYMONTH=4$r
+TZOFFSETFROM:-0600$r
+TZOFFSETTO:-0500$r
+TZNAME:Daylight Savings Time$r
+END:DAYLIGHT$r
+END:VTIMEZONE$r
+EOF
+ ;
+ my %days = (M => "MO", T => "TU", W => "WE", R => "TH", F => "FR", S => "SA", U => "SU");
+ my $file = $opts{'out'};
+ my @schedule = get_schedule(@_);
+ open(STDOUT, ">" . $file) || die $! if(defined($file) && (! -d $file));
+ open(STDOUT, ">/dev/null") || die $! if(defined($file) && (-d $file));
+ print "BEGIN:VCALENDAR$r\nVERSION:2.0$r\n$timezone";
+ foreach my $row (@schedule) {
+ map { s/\n/-/g; $_} %$row;
+ my @day = ();
+ @day = @{$row->{'off'}} if ($row->{'off'});
+ my ($starttime, $stoptime)=($row->{'begin'}, $row->{'end'});
+ my ($startdate, $stopdate)=split(/-/, $row->{'duration'});
+ $starttime =~ s/://;
+ $stoptime =~ s/://;
+ my $first = first_class(%$row);
+ if(defined($file) && (-d $file)) {
+ open FH, ">$file/" . $row->{'id'} . ".vcs" or die "$!";
+ select FH;
+ print "BEGIN:VCALENDAR$r\nVERSION:2.0$r\n$timezone";
+ }
+ print "BEGIN:VEVENT$r\n";
+ print "SUMMARY:", $row->{'title'}, "$r\n";
+ print "DESCRIPTION:", $row->{'id'}, "$r\n";
+ print "LOCATION:", $row->{'location'}, "$r\n";
+ print "CATEGORIES:Education$r\n";
+ print "TRANSP:OPAQUE$r\n";
+ #print "DTSTART;$tzn$first,$starttime), "$r\n";
+ #print "DTEND;$tzn$first,$stoptime), "$r\n";
+ print "DTSTART;TZID=\"$tzn\":", $first."T".$starttime, "00$r\n";
+ print "DTEND;TZID=\"$tzn\":", $first."T".$stoptime, "00$r\n";
+ $row->{'days'} =~ s/([MTWRFS])/,$days{$1}/g;
+ $row->{'days'} =~ s/^,//;
+ print "RRULE:FREQ=WEEKLY;UNTIL=$stopdate;BYDAY=", $row->{'days'}, "$r\n";
+ foreach my $day (@day) {
+ print("EXDATE;TZID=\"$tzn\":$day$r\n");
+ }
+ print "ATTENDEE;CN=".$row->{'instructor'}.";RSVP=FALSE;PARTSTAT=ACCEPTED;ROLE=CHAIR:mailto:" . (get_faculty_email($row->{'instructor'}) || "fake\@ddress"), "$r\n";
+ print "ATTENDEE;CN=".$opts{'name'}.";RSVP=FALSE;PARTSTAT=ACCEPTED;ROLE=REQ-PARTICIPANT:mailto:" . ($opts{'email'} || "fake\@ddress"), "$r\n" if $opts{'name'};
+ print "END:VEVENT$r\n";
+ if(defined($file) && (-d $file)) {
+ print "END:VCALENDAR$r\n";
+ close FH;
+ select STDOUT;
+ }
+ }
+ print "END:VCALENDAR$r\n";
+}
+