#!/usr/bin/perl

use LWP::Simple;
use CGI;
use strict;

my $cgi = new CGI;
print $cgi->header("text/html");
#  Get the calendar from the Sierra Club website
my $html = get 'http://www.sanfranciscobay.sierraclub.org/chapter/events/calendar.asp';
my $filter = $cgi->param('filter');

#  Split out header and footer from what we want to process
my ($head,$html) = split(/\<\!.*?Get events from calendar.*?--\>/,$html);
my ($html,$tail) = split(/\<\!.*?End of database.*?--\>/,$html);
#  Add info to header so relative links are found
$head =~ s|\<head\>|<head><base href="http://www.sanfranciscobay.sierraclub.org/chapter/events/calendar.asp">|;
#  Strip contents and ongoing events from header
$head =~ s/\<\!-- \!\!\! *Get event categories.*//sm;

my $newhtml = '';
foreach my $item ($html =~ /\<p\>\<p\>.*?\<\/p\>/gms) {
  $newhtml .= $item."\n\n" if $item =~ /$filter/i;
}

print join("\n\n",$head,$newhtml,$tail);
