#!/usr/bin/env python

import sys
sys.path.append('/home/acb/null_include')

from nullcgi import *
from upcoming import *
import string
import time
import os
import pg

#DATADIR = "/home/acb/www/upcoming/data"
DATADIR = "/home/acb/null_htdoc/upcoming/data"

db = pg.DB('null', user='acb')


TOP = """<HTML><HEAD><TITLE>the null device - Upcoming events in Melbourne</TITLE>
<LINK REL="StyleSheet" HREF="upcoming.css" TYPE="text/css" MEDIA="screen">
</HEAD><BODY>"""

TITLE="""
<H1>Upcoming Events</H1>
"""

SIDEBAR="""<P>Upcoming Events brought to you by <A HREF="http://dev.null.org/">The Null Device</A>.
</P><P>
This page lists upcoming events (mostly live music and performance) 
happening in Melbourne and the vicinity, which are of interest to the maintainer. 
It is not intended to be a comprehensive "what's-on-in-Melbourne" page,
and will probably never be such.  (Nobody pays me to cater to anybody's
tastes but my own.)  If you find this useful, good for you. If not, too bad.
</P><P>
<H1>Other links</H1>
<UL>
<LI><A HREF="http://grudnuk.com/records/">Records Ad Nauseam</A>
<LI><A HREF="http://www.beat.com.au/beatgigs.shtml">Beat gig guide</A>
</UL>
</P>
"""

BOTTOM = """
</BODY></HTML>"""

# automatic URL substitution for bands and venues

artist_urls = {
 'Prop': 'http://www.prop.com.au',
 'FourPlay': 'http://www.fourplay.com.au',
 'Sir': 'http://sir.shorts.cx',
}
venue_urls = {
 'Corner Hotel':'http://www.cornerhotel.com',
 'Empress Hotel':'http://www.aian.com.au/empress/',
 'Empress':'http://www.aian.com.au/empress/',
 'Punters Club':'http://www.aian.com.au/punters/',
 'Esplanade Hotel':'http://www.theesplanadehotel.com.au/',
 'Espy':'http://www.theesplanadehotel.com.au/',
}

# def emitItem(d):
#   "emits the contents of a dictionary as taken from the database"
# 
#   (title,venue,artist,cat,artisturl,venueurl) = (None,None,None,None,None,None)
#   if d.has_key('uc_title'): title = d['uc_title']
#   if d.has_key('uc_category'): cat = d['uc_category']
#   date = d['uc_date']
#   (y,m,dd)=map(int,string.split(date,'-'))
#   print '<div class="item">'
#   print '<span class="itemdate">%d/%d/%d</span>'%(dd,m,y)
#   if cat:
#     print '<span class="itemcat">[category: %s]</span>'%cat
#   print '<div class="itemhead">%s</div>'%title,
#   if d.has_key('uc_venue'):
#     if venue_urls.has_key(d['uc_venue']):
#       print '<div class="itemurl">[<A HREF="%s" TITLE="Go to the %s web site">%s</a>]</div>'%(venue_urls[d['uc_venue']],d['uc_venue'],d['uc_venue'])
#   print ''
#   if(d.has_key('uc_body')):
#     print '<div class="itembody">%s</div>'%d['uc_body']
#   print '</div>'
# 
(year,mon,day) = time.localtime(time.time())[:3]

files = filter(lambda x,y=int("%04d%02d%02d"%(year,mon,day)):int(x[:8])>=y,os.listdir(DATADIR))
files.sort()

print """Content-Type: text/html
"""

print TOP

print '<DIV ID="title">%s</DIV>'%TITLE

q = db.query("select * from upcoming where uc_end_date >= '%04d-%02d-%02d' order by uc_date"%(year,mon,day))

print '<DIV ID="main">'
(ly,lm)=(None,None)
for c in q.dictresult():
  date = c['uc_date']
  (y,m,d) = map(int,string.split(date, '-'))
  if(y,m)!=(ly,lm):
    print '<P CLASS="date">%s</P>'%(time.strftime('%B %Y',(y,m,d,0,0,0,0,0,0)))
    ly,lm = y,m
  emitItem(c)

print '</DIV>'
print '<DIV ID="sidebar" CLASS="sidebar">%s</DIV>'%SIDEBAR

print BOTTOM
