#!/usr/bin/perl -w

# converts HTML schedules in the raw subdirectory to format that
# make-pdb-file wants

# Copyright (c) 1999, 2000 Michael Wittman
# 
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.

use strict;

# the following line sets $sched_dir to the directory that this script is in
my $sched_dir = ($0 =~ m@^(?:(.*)/)?[^/]+$@)[0] || ".";
my $raw_dir = "$sched_dir/raw";


my (@file_list);
my ($file);
my ($line,$day);
my ($in_sched,$got_header);
my (@stations);
my $am;

# translate to the old abbrevs for these stations (we could as easily use the
# new abbrevs and change the abbrevs in the stations file, but this will allow
# us to still use the pre-2/8/99 schedules for testing).
my (%station_translation) = (
   "BAY-PT" => "PITTS",
   "DUBLIN" => "DUBPL"
);


opendir(DIR,$raw_dir) || die "can't open dir: $sched_dir/raw\n";
@file_list = grep !/^(CVS|\.\.?)$/, readdir(DIR);
closedir(DIR);

for $file (@file_list) {
   $in_sched = 0;
   $got_header = 0;

   open(FILE,"$raw_dir/$file");
   while (defined ($_ = <FILE>)) {
      if ($in_sched) {
	 my (@times,$i);
	 my $ampm_change = 0;
	 my ($first_hour,$last_hour);
	 my ($r_start,$r_end) = ("", "");
	 
	 s/&nbsp;//g;
	 # s@<font color="#999999">-</font>@-@gi;
	 s@</?font.*?>@@gi;
	 s@-@--@g;
	 last if m@</PRE>@;
	 if (/^\s*(AM|PM)\s*$/i) {
	    $am = ($1 =~ /AM/i);
	    next;
	 }
	 next if /^\s*$/i || !/[*:]/;
	 
	 @times = split ' ', $_;
	 for ($i = 0; $i < @times; $i++) {
	    if (!$r_start && $times[$i] =~ /^\d\d?\*\d\d$/) {
	       $r_start = $stations[$i];
	    }
	    elsif ($r_start && !$r_end && $times[$i] =~ /^\d\d?:\d\d$/) {
	       $r_end = $stations[$i-1];
	    }
	    $times[$i] =~ s/^(\d\d?)\*(\d\d)$/$1:$2/;
	    # change 13:00 -> 1:00 in saturday/dalycity-fremont schedule
	    $times[$i] =~ s/^(\d\d)(:\d\d)$/(($1+11)%12+1).$2/e;
	    $times[$i] =~ s/^-$/--/;
	 }
	 $r_end = $stations[$i-1] if $r_start && !$r_end;

	 for ($i = 0; $times[$i] =~ /^--$/; $i++) {}
	 ($first_hour) = ($times[$i] =~ /^(\d\d?):/);
	 for ($i = $#times; $times[$i] =~ /^--$/; $i--) {}
	 ($last_hour) = ($times[$i] =~ /^(\d\d?):/);
	 $first_hour %= 12; $last_hour %= 12;
	 $ampm_change = 1 if int($first_hour/6) == 1 && int($last_hour/6) == 0;

	 if (!$ampm_change) {
	    for (@times) {
	       next if /^--$/;
	       $_ .= ($am ? "a" : "p");
	    }
	 }
	 else {
	    for (@times) {
	       next if /^--$/;
	       
	       my ($hour) = /^(\d\d?):/;
	       $hour %= 12;
	       if (int($hour/6) == 1) {
		  $_ .= ($am ? "a" : "p");
	       }
	       else {
		  $_ .= (!$am ? "a" : "p");
	       }
	    }
	 }
	 
	 # print join(" ",map(sprintf("%6s",$_),@times),$r_start,$r_end), "\n";
	 print OUT join(" ",map(sprintf("%6s",$_),@times),$r_start,$r_end), "\n";
      }
      elsif (/<!--\s*Schedule Title\s*-->/) {
	 <FILE>;
	 $_ = <FILE>;
	 s/^\s*(.*?)\s*$/$1/g;
	 ($line, $day) = split /\s*-\s*/, $_;
	 my ($s1,$s2) = split /\s*to\s*/, $line;
	 if ($s1 =~ /richmond/i && $s2 =~ /daly\s*city/i) {
	    $s2 = "colma";
	 }
	 for ($s1,$s2) {
	    s@/.+@@;
	    s@ +@@;
	 }
	 $line = "$s1-$s2";
	 $line =~ tr[A-Z][a-z];
	 $day =~ tr[A-Z][a-z];
	 $day =~ s@^sundays/holidays$@sundays@;
	 $day =~ s/s$//;
	 # print "$day/$line\n";
      }
      elsif (/<PRE>/) {
	 my $station;
	 
	 $in_sched = 1;
	 system("mkdir $sched_dir/$day") unless -d "$sched_dir/$day";
	 open(OUT,"> $sched_dir/$day/$line");
	 $_ = <FILE>;
	 s/&nbsp;//g;
	 s/^\s*(.*?)\s*$/$1/g;
	 @stations = split /  +/, $_;
	 for $station (@stations) {
	    $station =~ s/ /-/g;
	    $station = $station_translation{$station}
	    if exists $station_translation{$station};
	 }
	 # print join(" ",map(sprintf("%6s",$_),@stations)), "\n";
	 print OUT join(" ",map(sprintf("%6s",$_),@stations)), "\n";
      }
   }
   close(OUT);
   close(FILE);
}
