#!/usr/bin/perl
use Date::Parse;
use Date::Manip;
use HTML::Entities;
use DBI;
use CGI;

Date_Init("TZ=PDT");
$cgi = new CGI;
print $cgi->header("text/html");
print <<HTML;
<html><head><title>Square Dance Hoedown and Festival Calendar</title>
<link rel="stylesheet" type="text/css" href="tt.css" />
<link rel="icon" href="favicon.ico" type="image/x-icon" />
<script>
if (document.URL.search(/http.*html/) >= 0) {
  window.location.href = document.URL.replace(/html/,"pl");
}
</script>
<script language="JavaScript" type="text/javascript" src="tt.js"></script>
</head>
<body>
<h1>Square Dance Hoedown and Festival Calendar</h1>

<div class="vnav">
<p onmouseover="this.style.backgroundColor='#00c000'"
   onmouseout="this.style.backgroundColor='#008000'; this.style.borderStyle='outset'"
   onmousedown="this.style.borderStyle='inset'"
   onclick="document.location='index.html'">
<b>HOME</b><br>of the Tam Twirlers</p>

<p onmouseover="this.style.backgroundColor='#00c000'"
   onmouseout="this.style.backgroundColor='#008000'; this.style.borderStyle='outset'"
   onmousedown="this.style.borderStyle='inset'"
   onclick="document.location='about.html'">
<b>ABOUT</b><br>the Tam Twirlers</p>

<p onmouseover="this.style.backgroundColor='#00c000'"
   onmouseout="this.style.backgroundColor='#008000'; this.style.borderStyle='outset'"
   onmousedown="this.style.borderStyle='inset'"
   onclick="document.location='map.html'">
<b>WHERE</b><br>we dance</p>

<p onmouseover="this.style.backgroundColor='#00c000'"
   onmouseout="this.style.backgroundColor='#008000'; this.style.borderStyle='outset'"
   onmousedown="this.style.borderStyle='inset'"
   onclick="document.location='intro.html'">
<b>INTRODUCTION</b><br>to Square Dancing</p>

<p style="background-color: #00c000; cursor: auto">
<b>CALENDAR</b><br>of Square Dance Events</p>

<p onmouseover="this.style.backgroundColor='#00c000'"
   onmouseout="this.style.backgroundColor='#008000'; this.style.borderStyle='outset'"
   onmousedown="this.style.borderStyle='inset'"
   onclick="document.location='taminaion/index.html'">
<b>CALLS</b><br>with TAMinations</p>

<p onmouseover="this.style.backgroundColor='#00c000'"
   onmouseout="this.style.backgroundColor='#008000'; this.style.borderStyle='outset'"
   onmousedown="this.style.borderStyle='inset'"
   onclick="document.location='links.html'">
<b>LINKS</b><br>to other Clubs and Resources</p>

</div>

This calendar lists hoedowns (1-day events) in the Bay Area, and
festivals (multiple-day events) in California, and a few national conventions.
Fliers are displayed to fit your screen, but will print at
a higher resolution (300 dpi).
Send any updates or corrections to
<script>document.write(unescape('E3%aF2%C3%eitsirhC darBE3%22%moc.eitsirhcdarb04%darbA3%otliam22%D3%ferh aC3%'.split('').reverse().join('')))</script>
<table border=1>
<tr><th>Date</th><th>Time</th><th>Title</th>
<th>Location</th><th>Caller</th><th>Level</th>
<th>Details</th><th>Flier</th><th>Link</th></tr>
HTML

$| = 1;
#  Get every square dance hoedown in the future
$dbh = DBI->connect('DBI:mysql:database=db98671327;host=db32.perfora.net',
                    'dbo98671327','YZbNSEHW');
$dbh->{LongReadLen} = 16777214;
$sth = $dbh->prepare("SELECT id,start,end,duration,title,details ".
                     "FROM calendar ".
                     "WHERE category = 'squaredance' AND ".
                     "start >= '".UnixDate(scalar localtime,"%Y-%m-%d")."' ".
                     "ORDER BY start");
$sth->execute;
while (my ($id,$start,$end,$duration,$title,$body) = $sth->fetchrow_array) {
  my $caller = ($body =~ (/caller\s*:\s*(.*)/i))[0];
  next unless $caller =~ /\S/;
  ($start,$end) = (str2time($start),str2time($end));
  my $days = int(($end-$start)/(60*60*24)+0.5);
  my $date = UnixDate(scalar localtime($start),'%A, %B %e, %Y');
  my $link = ($body =~ (/link\s*:\s*(.*)/i))[0];
  $link =~ s|(\S+)|<a href="$1">Link</a>|;
  $fp = 'p1';
  my $flier = ($body =~ (/flier\s*:\s*(\S+)/i))[0];
  if ($flier) {
    if (-e "hoedowns/$flier.html") {
      $flier = "<a href=\"hoedowns/$flier.html\">Flier</a>";
    } elsif (-e "hoedowns/$flier.pdf") {
      $flier = "<a href=\"hoedowns/$flier.pdf\">Flier</a>";
    } else {
      $flier = '<a href="hoedowns/flier.pl?' .
        join '&', map $fp++."=$_", split /,/,$flier . '">Flier</a>';
    }
  }
  my $details = ($body =~ (/details\s*:\s*(.*)/i))[0];
  $details =~ s/\n/<br>\n/gs;
  $time = '';
  if ($duration > 0) {
    $time = UnixDate(scalar localtime($start),'%i:%M').'-'.
            UnixDate(scalar localtime($start+60*$duration),'%i:%M');

  } elsif ($days > 1) {
    $time = $days.' days';
  }
  my $location = ($body =~ (/location\s*:\s*(.*)/i))[0];
  my $level = ($body =~ (/level\s*:\s*(.*)/i))[0];

  print "<tr><td class=green>$date</td>",
        "<td class=green>$time</td>",
        "<td class=green>$title</td>",
        "<td class=green>$location</td>",
        "<td class=green>$caller</td>",
        "<td class=green>$level</td>",
        "<td class=green>$details</td>",
        "<td class=green>$flier</td>",
        "<td class=green>$link</td></tr>\n";

}
print "</table></body></html>\n";
