Quick hack to improve vCal output
[tpope-extra.git] / perl / schedproc
index 8c45f83bbd334fde796f909982f554c92be2b348..dc5df8cb38fca955a6094bfc125dec95efc66d6e 100755 (executable)
@@ -40,7 +40,7 @@ if($arg eq "-x") {
 
 Getopt::Long::Configure ("bundling", "auto_help");
 die "Invalid arguments\n" unless
-GetOptions(\%opts, 'schedule|S=s', 'grades|G=s', 'facmail=s', 'facurl=s', 'name|n=s', 'format|f=s', 'config|F=s', 'out|o=s');
+GetOptions(\%opts, 'schedule|S=s', 'grades|G=s', 'faculty=s', 'name|n=s', 'format|f=s', 'config|F=s', 'out|o=s');
 
 if (-r $opts{'config'}) {
     open CONFIG, $opts{'config'} or die $!;
@@ -48,7 +48,7 @@ if (-r $opts{'config'}) {
        s/\#.*//;
        next unless m/^([^=]*)=(.*)/;
        my ($l, $r) = ($1, $2);
-       if ($l =~ /^(schedule|grades|name|facmail|facurl)$/) {
+       if ($l =~ /^(schedule|grades|name|faculty)$/) {
            $opts{$l}||=$r;
        } else {
            warn "Unknown config file option $l.\n";
@@ -59,13 +59,11 @@ if (-r $opts{'config'}) {
 
 $opts{'schedule'} ||= "~/schedule.xml";
 $opts{'grades'} ||= "~/grades.xml";
-$opts{'facmail'} ||= "";
-$opts{'facurl'} ||= "";
+$opts{'faculty'} ||= "";
 
 $opts{'schedule'} =~ s/(^|,)~\//$1$ENV{HOME}\//;
 $opts{'grades'} =~ s/(^|,)~\//$1$ENV{HOME}\//;
-$opts{'facmail'} =~ s/(^|,)~\//$1$ENV{HOME}\//;
-$opts{'facurl'} =~ s/(^|,)~\//$1$ENV{HOME}\//;
+$opts{'faculty'} =~ s/(^|,)~\//$1$ENV{HOME}\//;
 
 if(!defined($opts{'out'}) && defined($ARGV[0])) {
     $opts{'out'} = shift;
@@ -75,7 +73,7 @@ if(!defined($opts{'format'}) && defined($opts{'out'})) {
     $opts{'format'} = $opts{'out'};
     $opts{'format'} =~ s/.*\.//;
     $opts{'format'} =~ s/^(.*\/|)\.?schedule$/mhc/;
-    undef $opts{'out'} if($opts{'out'} =~ /^(html|mhc|csv|cvs|xml|grades)$/);
+    undef $opts{'out'} if($opts{'out'} =~ /^(html|mhc|csv|vcs|xml|grades)$/);
 }
 
 $opts{'format'} ||= "";
@@ -89,6 +87,27 @@ sub generate_id {
     return $id;
 }
 
+sub first_class {
+    my %class = @_;
+    $class{'duration'} =~ /(\d\d\d\d)(\d\d)(\d\d)-(\d\d\d\d)(\d\d)(\d\d)/;
+    my $days = Delta_Days($1,$2,$3,$4,$5,$6);
+    my $firstday = Date::Calc->new($1,$2,$3);
+    my $lastday = Date::Calc->new($4,$5,$6);
+    my $today;
+    my @days=();
+    foreach(split(" ",$class{'days'})) {
+       push @days, Decode_Day_of_Week($_);
+    }
+    my @off = ();
+    @off = @{$class{'off'}} if ($class{'off'});
+    for($today = $firstday; $today < $lastday; $today++) {
+       next unless(grep($_ == Day_of_Week($today->date), @days));
+       next if(grep($_ == "$today", @off));
+       return $today;
+    }
+    return undef;
+}
+
 sub next_class {
     my %class = @_;
     $class{'duration'} =~ /(\d\d\d\d)(\d\d)(\d\d)-(\d\d\d\d)(\d\d)(\d\d)/;
@@ -167,6 +186,7 @@ sub read_fileurl {
 sub get_schedule {
     my $content = read_fileurl($opts{'schedule'});
     my $ref = XMLin($content, ForceArray => [ 'class', 'off' ], KeyAttr => "");
+    die "Could not load schedule.\n" unless $ref->{'class'};
     return @{$ref->{'class'}};
 }
 
@@ -176,20 +196,26 @@ sub get_grades {
     return $ref;
 }
 
-sub get_faculty_email {
-    my ($name, $school, $email);
-    if((-f ($opts{'facmail'}||"")) && ! %faculty) {
-       open INS,$opts{'facmail'} or die $!;
-       while($_ = <INS>) {
-           chomp;
+sub load_faculty {
+    my ($name, $email, $url, $content);
+    if(($opts{'faculty'}) && ! %faculty) {
+       $content = read_fileurl($opts{'faculty'});
+       foreach $_ (split("\n", $content)) {
            m/"([^"]*)",([^,]*),([^,]*)/; # "
-           ($name, $email, $school) = ($1, $2, $3);
+           ($name, $email, $url) = ($1, $2, $3);
+           next unless ($name);
            $name =~ s/ [A-Z]\.//g;
            $name = lc $name;
            $name =~ s/\W//g;
-           $faculty{$name} = $email;
+           $faculty{$name} = $email if($email);
+           $facurl{$name} = $url if ($url);
        }
     }
+}
+
+sub get_faculty_email {
+    load_faculty();
+    my ($name);
     $name = shift;
     $name =~ s/ [A-Z]r?\.//g;
     $name = lc $name;
@@ -198,19 +224,8 @@ sub get_faculty_email {
 }
 
 sub get_faculty_url {
-    my ($name, $url);
-    if((-f ($opts{'facurl'}||"")) && ! %facurl) {
-       open INS, $opts{'facurl'} or die $!;
-       while($_ = <INS>) {
-           chomp;
-           m/([^,]*),([^,]*)/; # "
-           ($name, $url) = ($1, $2);
-           $name =~ s/ [A-Z]\.//g;
-           $name = lc $name;
-           $name =~ s/\W//g;
-           $facurl{$name} = $url;
-       }
-    }
+    load_faculty();
+    my ($name);
     $name = shift;
     $name =~ s/ [A-Z]r?\.//g;
     $name = lc $name;
@@ -368,8 +383,8 @@ sub do_vcalendar_schedule {
        print "DESCRIPTION:", $row->{'id'}, "\r\n";
        print "LOCATION:", $row->{'location'}, "\r\n";
        print "CATEGORIES:Education\r\n";
-       print "DTSTART:", $startdate."T".$starttime, "00\r\n";
-       print "DTEND:", $startdate."T".$stoptime, "00\r\n";
+       print "DTSTART:", first_class(%$row)."T".$starttime, "00\r\n";
+       print "DTEND:", first_class(%$row)."T".$stoptime, "00\r\n";
        print "RRULE:W1 ", $row->{'days'} . " $stopdate", "T000000\r\n";
        print("EXDATE:$day\r\n") if($day);
        print "ATTENDEE;ROLE=OWNER;STATUS=CONFIRMED:", $opts{'name'}, "\r\n" if(defined($opts{'name'}));
@@ -421,10 +436,10 @@ sub do_html_schedule {
            #$instructor =~ s/ [A-Z. ]* / /;
            my $url = get_faculty_url($row->{'instructor'});
            if($url) {
-               print '<td><a href="' . $url . '">'
+               print '<td class="instructor"><a href="' . $url . '">'
                . $row->{'instructor'} . "</a></td>";
            } else {
-               print "<td>" . $instructor . "</td>";
+               print '<td class="instructor">' . $instructor . "</td>";
            }
            print "<td>" . $row->{'days'} . "</td>";
            my ($a,$b) = ($row->{'begin'}, $row->{'end'});