#!/usr/bin/perl # queue2ics - $Id: queue2ics,v 1.6 2007/09/24 04:08:11 perette Exp $ # Copyright 2007 Perette Barella # All Rights Reserved. ###################################################################### use Time::Local; use Getopt::Std; $source = $ENV {HOME} . "/Documents/Lists/queue.txt"; $pubdir = $ENV {HOME} . "/Sites/Perette"; $publish = "$pubdir/calendar.ics"; $type = ""; $debug = 0; $verbose = 0; #---------------------------------------------------------------------- # Read the command line options. #---------------------------------------------------------------------- if (!getopts ("g:o:Zv")) { usage (); exit 1 } if (defined ($opt_Z)) { $debug = 1; } if (defined ($opt_v)) { $verbose = 1; } if (defined ($opt_g)) { $type = $opt_g; $lctype = $type; $lctype =~ tr /A-Z/a-z/; $publish = $pubdir . "/" . $lctype . ".ics"; } if (defined ($opt_o)) { $publish = $opt_o; } $dest="/var/tmp/$arg0.$$.tmp"; open (SOURCE, "< $source") || die "$source: Can't read"; open (DEST, "> $dest") || die "$dest: Can't write"; $Revision = "Version"; $EMPTY = ""; print DEST "BEGIN:VCALENDAR\r\n" . "VERSION:2.0\r\n" . "PRODID:-//barella/perette/queue2ics/NONSGML $Revision: 1.6 $EMPTY //EN\r\n"; while () { if ($type ne "" && ! /$type/) { next; } if ($verbose) { print; } chomp; ($date, $rest) = /^(\d\d\d\d-\d\d-\d\d) (.*)$/ or next; ($year, $month, $day) = ($date =~ /(\d\d\d\d)-(\d\d)-(\d\d)/); $allday = 0; if ($rest =~ /^(\d\d:\d\d) (.*)$/) { ($start, $rest) = ($rest =~ /^(\d\d:\d\d) (.*)$/); ($starthour, $startminute) = ($start =~ /(\d\d):(\d\d)/); if ($rest =~ /^- (\d\d:\d\d) (.*)$/) { ($endhour, $endminute, $rest) = ($rest =~ /^- (\d\d):(\d\d) (.*)$/); } else { $endhour = $starthour + 2; $endminute = $startminute; } } else { $starthour = 0; $startminute = 0; $endhour = 23; $endminute = 59; $allday = 1; } $starttime = timelocal (0, $startminute, $starthour, $day, $month - 1, $year - 1900); if ($allday == 1) { $endtime = $starttime + 86400; } else { $endtime = timelocal (0, $endminute, $endhour, $day, $month - 1, $year - 1900); if ($endtime < $starttime) { $endtime += 86400; } } my ($sec, $min, $hour, $day, $mon, $year, $crap) = gmtime ($starttime); $start = sprintf ("%04d%02d%02dT%02d%02d00Z", $year + 1900, $mon + 1, $day, $hour, $min); my ($sec, $min, $hour, $day, $mon, $year, $crap) = gmtime ($endtime); $end = sprintf ("%04d%02d%02dT%02d%02d00Z", $year + 1900, $mon + 1, $day, $hour, $min); my ($summary, $conjunction, $rest) = split / *([-@.]) */, $rest, 2; my $location = ""; my $description = ""; while (defined ($rest)) { my ($piece, $nextconj); ($piece, $nextconj, $rest) = split / *([-@.]) */, $rest, 2; if ($conjunction eq "@") { $location .= ($location eq "" ? "" : " ") . $piece; } else { $description .= ($description eq "" ? "" : " ").$piece; } $conjunction = $nextconj; } print DEST "BEGIN:VEVENT\r\n"; if ($allday == 1) { print DEST "DTSTART;VALUE=DATE:" . substr ($start, 0, 8) . "\r\nDTEND;VALUE=DATE:" . substr ($end, 0, 8) . "\r\n"; } else { print DEST "DTSTART:$start\r\n" . "DTEND:$end\r\n"; } print DEST "SUMMARY:$summary\r\n" . ($location ne "" ? "LOCATION:$location\r\n" : "") . ($description ne "" ? "DESCRIPTION:$description\r\n" : "") . "END:VEVENT\r\n"; } print DEST "END:VCALENDAR\r\n"; close (DEST); close (SOURCE); if ($debug) { system ("cat '" . $dest . "'"); unlink $dest; exit (0); } if (system ("cmp -s '" . $dest . "' '" . $publish . "'") == 0) { print "No changes to published calendar.\n"; unlink $dest; } else { print "Calendar updated.\n"; if (system ("cp '" . $dest . "' '" . $publish . "'") == 0) { system ("upload page"); } } exit (0);