#!/usr/bin/perl -w

# Perl script to get system fares from the web, so I don't have to
# type them in myself.  Uses lynx to download from the web.  This
# script assumes below that the excursion fare is $3.80.

# 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 $data_dir to the directory that this script is in
my $data_dir = ($0 =~ m@^(?:(.*)/)?[^/]+$@)[0] || ".";
my $fares_file = "$data_dir/fares";
my $base_url = "http://www.transitinfo.org/cgi-bin/all_times?";
my @fields = ("C", "D", "ALL", "TIME", "ATIME", "E");
my @values = ("BA", "SA", "N", "1:35am", "2:00am", "");



my @stations = split '\n',
"12ST
16ST
19ST
24ST
ASHBY
BALPK
BFAIR
BRK
C-VLY
CIVIC
COLIS
COLMA
CONCD
DALY
DUBPL
DEL-N
PLAZA
EMBAR
FREMT
FRTVL
GLNPK
HAY
LAFAY
LAKEM
MACAR
MONTG
N-BRK
N-CNC
ORNDA
PITTS
PHILL
POWEL
RICH
ROCKR
SLEAN
SHAY
UCITY
W-CRK
W-OAK
";

sub sanitize {
   local($_) = shift;
   s/[^\w]|_/sprintf "%%%X", unpack "c", $&/ge;
   $_;
}

sub form_get_string {
   my ($fields_ref,$values_ref) = @_;
   my (@fields) = @$fields_ref;
   my (@values) = @$values_ref;
   my (@pairs);
   my $i;

   for ($i = 0; $i < @fields; $i++) {
      my ($field,$value) = ($fields[$i], $values[$i]);
      push @pairs, join("=",&sanitize($field),&sanitize($value));
   }

   join "&", @pairs;
}

sub get_fare {
   my $url = shift;
   my $fare;
   local($_);

   # print "\nlynx -source $url\n";
   open(URL,"lynx -source \"$url\" |") || die "can't run lynx on url: $url\n";
   while (defined($_ = <URL>)) {
      if (/Fare:/) {
	 ($fare) = /\$(\d+\.\d\d)/;
	 last;
      }
   }
   close(URL);

   $fare;
}

sub my_print {
   print @_;
   print FARES @_;
}

sub my_printf {
   printf @_;
   printf FARES @_;
}

my ($i,$j);

$| = 1;
open(FARES,"> $fares_file") || die "can't open fares file: $fares_file\n";
for ($i = 0; $i < @stations; $i++) {
   my_print "$stations[$i]";
   for ($j = $i; $j < @stations; $j++) {
      my $fare;
      
      if ($i == $j) {
	 $fare = 3.80;
      }
      else {
	 my (@flds) = (@fields, "FromStation", "ToStation");
	 my (@vals) = (@values, $stations[$i], $stations[$j]);
	 my $url = $base_url.&form_get_string(\@flds,\@vals);
	 $fare = &get_fare($url);
      }
      my_printf " %4.2f", $fare;
   }
   my_print "\n";
}
close(FARES);
