#!/usr/cs/bin/python2.5

import cgi
import headers

def readFile (fname):
   records = []

   f = open (fname, 'r')
   for line in f:
      # print "Reading: " + line
      entry = line.split(',')
      if not len(entry) == 4:
          print "Bad entry: " + str(entry)
      entry = map(lambda e: e.lstrip(' \t\n'), entry)
      entry = map(lambda e: e.rstrip(' \t\n'), entry)
      records.append(entry)
   return records

def cmpPicno(a, b):
  if a == '' and b == '': return 0
  if a == '': return 1
  if b == '': return -1
  return cmp(int(a), int(b))

def sortRecords(records, col):
    # columns: 0 =picture number, 1=last name, 2=first name, 3=institution
    if col == 0:
        records.sort (lambda a, b: cmpPicno(a[0], b[0]))
    elif col <= 3:
        records.sort (lambda a, b: cmp(a[col], b[col]))
    else:
        print "Error: bad sort index"

def printTableKey(col):
   print '<tr>'
   if col==0:
       print """<td class="hed">&darr;&nbsp;Key</a></td>"""
   else:
       print """<td class="hed"><a href="show-people.py?col=0">Key</a></td>"""
   if col==1:
       print """<td class="hed">&darr;&nbsp;Last Name</a></td>"""
   else:
       print """<td class="hed"><a href="show-people.py?col=1">Last Name</a></td>"""
   if col == 2:
       print """<td class="hed">&darr;&nbsp;First Name</a></td>"""
   else:
       print """<td class="hed"><a href="show-people.py?col=2">First Name</a></td>"""
   if col == 3:
       print """<td class="hed">&darr;&nbsp;Institution</a></td>"""
   else:
       print """<td class="hed"><a href="show-people.py?col=3">Institution</a></td>"""
   print '</tr>'
   
def printSortedRecords(col):     
   print """
   <table class="people" cellspacing='0'>
   """
   printTableKey(col)

   records = readFile("data.csv")
   sortRecords (records, col)
   oddrow = True
   for entry in records:
       if oddrow: td = '<td class="oddrowr">'
       else: td = '<td class="evenrowr">'
       # right align first entry
       for el in entry:
          print td + str(el) + "</td>"
          if oddrow: td = '<td class="oddrow" text-align="left">'
          else: td = '<td class="evenrow" text-align="left">'
       print "</tr>"
       oddrow = not oddrow
   printTableKey(col)
   print "</table>"


headers.printHeader ("1999 IEEE Symposium on Security and Privacy")

print "<center><h1>20<sup>th</sup> IEEE Symposium on Security and Privacy</h1></center>\n"
print "<p><br>\n"
print '<center><a href="SP99_key.jpg"><img src="SP99_key-small.jpg"></a><br><b>Photo from the 20<sup>th</sup> Anniversary Symposium, May 1999</b><br><a href="SP99_key.jpg"><b>High Resolution Image</b></a> [<a href="SP99.jpg"><b>High Resolution without Numbers</a>]</b></center>\n'
print "<p><br>\n"

form = cgi.FieldStorage()
if not form.has_key('col'):
    colid = 1
else:
    colid = int(form['col'].value)

print "<center>"
printSortedRecords(colid)
print "</center>"
print """
<p>
<center>
Name and affiliation information is from the 1999 Symposium attendee list.
</center>
<p><br><p>

If you can help identify any of the untagged people in the picture, please send the picture number and name to David Shambroom (<a href="mailto:wds@intersystems.com"><em>wds@intersystems.com</em></a>) and David Evans (<a href="mailto:evans@cs.virginia.edu"><em>evans@cs.virginia.edu</em></a>).

</center>
"""

headers.printFooter ()



