#!/usr/bin/perl -w

# Perl script to get system schedules from the web.  Uses lynx to download.

# 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;
use Getopt::Std;

my (%opts);
getopts('d:',\%opts);

# the following line sets $sched_dir to the directory that this script is in
my $sched_dir = ($0 =~ m@^(?:(.*)/)?[^/]+$@)[0] || ".";
my $dump_dir = $opts{'d'} || "$sched_dir/raw";
my $base_url = "http://www.bart.org/riding/planning";
my $list_url = "http://www.bart.org/riding/planning/3.htm";

my @lines;

open(LINES,"lynx -source $list_url |") || die "error getting lines\n";
while (defined($_ = <LINES>)) {
   push @lines, $1
     if /(\w\w\w_\w\w\w_\w\.htm)/;
}
close(LINES) || die "error getting lines\n";

-d $dump_dir || mkdir $dump_dir, 0777;
opendir(DUMP_DIR,"$dump_dir") || die "can't open dir: $dump_dir\n";
my @files = grep !/^(CVS|\.\.?)/, readdir(DUMP_DIR);
for (@files) {
   unlink "$dump_dir/$_";
}
closedir(DUMP_DIR);

for (@lines) {
   print "$_\n";
   system("lynx -source $base_url/$_ > $dump_dir/$_") == 0 || die "error from lynx\n";
}
