use Getopt::Long;
use LWP::UserAgent;
use XML::Simple;
-use vars qw(%opts %faculty);
+use vars qw(%opts %faculty %facurl);
$opts{'config'} = $ENV{HOME} . "/.schedprocrc";
-if(($ARGV[0] || "") eq '-F') {
- shift;
- $opts{'config'} = shift;
-}
-
my $arg = $ARGV[0] || "";
if($arg eq "-x") {
shift;
Getopt::Long::Configure ("bundling", "auto_help");
die "Invalid arguments\n" unless
-GetOptions(\%opts, 'schedule=s', 'grades=s', 'name=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'};
+ open CONFIG, $opts{'config'} or die $!;
while(<CONFIG>) {
s/\#.*//;
next unless m/^([^=]*)=(.*)/;
my ($l, $r) = ($1, $2);
- if ($l =~ /^(schedule|grades|name)$/) {
+ if ($l =~ /^(schedule|grades|name|faculty)$/) {
$opts{$l}||=$r;
} else {
warn "Unknown config file option $l.\n";
$opts{'schedule'} ||= "~/schedule.xml";
$opts{'grades'} ||= "~/grades.xml";
+$opts{'faculty'} ||= "";
-$opts{'schedule'} =~ s/^~\//$ENV{HOME}\//;
-$opts{'grades'} =~ s/^~\//$ENV{HOME}\//;
+$opts{'schedule'} =~ s/(^|,)~\//$1$ENV{HOME}\//;
+$opts{'grades'} =~ s/(^|,)~\//$1$ENV{HOME}\//;
+$opts{'faculty'} =~ s/(^|,)~\//$1$ENV{HOME}\//;
if(!defined($opts{'out'}) && defined($ARGV[0])) {
$opts{'out'} = shift;
$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'} ||= "";
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'}};
}
return $ref;
}
-sub get_faculty_email {
- my ($name, $school, $email);
- if((-f $ENV{'HOME'} . "/public_html/faculty.csv") && ! %faculty) {
- open INS, $ENV{'HOME'} . "/public_html/faculty.csv";
- 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;
return $faculty{$name};
}
+sub get_faculty_url {
+ load_faculty();
+ my ($name);
+ $name = shift;
+ $name =~ s/ [A-Z]r?\.//g;
+ $name = lc $name;
+ $name =~ s/\W//g;
+ return $facurl{$name};
+}
+
sub get_mhc_header {
return (
"X-SC-Subject: New Years Day\nX-SC-Category: Holiday\nX-SC-Cond: 1 Jan\nX-SC-Duration: 00010101-\nX-SC-Record-Id: <New_Years_Day\@from.sctweb>\n",
my @lines;
foreach my $name (<$file/[1-9]*>) {
next unless $name =~ /^$file\/[1-9][0-9]*$/;
- open FH, $name;
+ open (FH, $name) || die $!;
@lines = <FH>;
close FH;
foreach (@lines) {
my $i=0;
foreach (@mhc) {
while(-f ++$i) {}
- open FH, ">$file/$i" or die $!;
+ open (FH, ">$file/$i") || die $!;
print FH $_;
close FH;
}
} else {
- open(STDOUT, ">" . $file) if(defined($file));
+ open(STDOUT, ">" . $file) || die $! if(defined($file));
print "# MHC school schedule\n# Autogenerated by sctweb ".localtime()."\n\n";
print join("\n", @mhc);
}
my @mon = qw(Jan. Feb. Mar. Apr. May June July Aug. Sept. Oct. Nov. Dec.);
my ($current, @mhc, @schedule);
@schedule = get_schedule(@_);
- open(STDOUT, ">" . $opts{'out'}) if(defined($opts{'out'}));
+ open(STDOUT, ">" . $opts{'out'}) || die $! if(defined($opts{'out'}));
foreach my $row (@schedule) {
map {s/\n/-/g if defined; $_} %$row;
my $id=generate_id($row->{'id'});
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) if(defined($file) && (! -d $file));
- open(STDOUT, ">/dev/null") if(defined($file) && (-d $file));
+ open(STDOUT, ">>" . $file) || die $! if(defined($file) && (! -d $file));
+ open(STDOUT, ">/dev/null") || die $! if(defined($file) && (-d $file));
print "BEGIN:VCALENDAR\r\nVERSION:1.0\r\n";
foreach my $row (@schedule) {
map { s/\n/-/g; $_} %$row;
my $schedule = { class => [ get_schedule(@_) ] };
my $xml = XMLout($schedule, NoAttr => 1, RootName => 'schedule');
if($file) {
- open FH, ">$file";
+ open FH, ">$file" || die $!;
print FH $xml;
close FH;
} else {
my $shade = "dark";
my @schedule = get_schedule(@_);
if($opts{'out'}) {
- open FH, ">" . $opts{'out'};
+ open (FH, ">".$opts{'out'}) || die $!;
select FH;
}
print '<table id="schedule" cellpadding="3" cellspacing="0">'."\n<tr><th>";
print '<span class="coursetitle">' .$row->{'title'}. '</span></td>';
my $instructor = $row->{'instructor'};
#$instructor =~ s/ [A-Z. ]* / /;
- my $email = get_faculty_email($row->{'instructor'});
- #if($email) {
- #print '<td><a href="mailto:' . $email . '">'
- #. $row->{'instructor'} . "</a></td>";
- # } else {
- print "<td>" . $instructor . "</td>";
- #}
+ my $url = get_faculty_url($row->{'instructor'});
+ if($url) {
+ print '<td class="instructor"><a href="' . $url . '">'
+ . $row->{'instructor'} . "</a></td>";
+ } else {
+ print '<td class="instructor">' . $instructor . "</td>";
+ }
print "<td>" . $row->{'days'} . "</td>";
my ($a,$b) = ($row->{'begin'}, $row->{'end'});
$b .= "AM";
my ($row);
my $shade = "dark";
if($opts{'out'}) {
- open FH, ">" . $opts{'out'};
+ open (FH, ">".$opts{'out'}) || die $!;
select FH;
}
print '<table id="grades" cellpadding="3" cellspacing="0">'."\n<tr>";
} else {
die "Unknown format. Try specifying --format.\n"
}
-