#!/usr/bin/env python
# coding: utf-8

import time

import sys
import chatevents
import meeting


nicks = {
    'chris': 'Chris Welty',
    'csma': 'Christian de Sainte Marie',
    'csam': 'Christian de Sainte Marie',
    'jjc': 'Jeremy Carroll',
    'josb': 'Jos de Bruijn',
    'garry': 'Gary Hallmark',
    'mk': 'Michael Kifer',
    'pfps': 'Peter Patel-Schneider',
    'scribe': 'Scribe Error',
    }

users = [
    'Adrian Paschke',
    'Igor Mozetic',
    'Andreas Harth',
    'Axel Polleres',
    'Chris Welty',
    'Christian de Sainte Marie',
    'Dave Reynolds',
    'Gary Hallmark',
    'Harold Boley',
    #'Hassan Ait-Kaci',
    'Hassan Aït-Kaci',    # note the interest double-dotted "i"
    'Jeff Pan',
    'John Hall',
    'Jos de Bruijn',
    'Michael Kifer',
    'Mike Dean',
    'Sandro Hawke',
    ]

class WorkingGroup:
    """Should use two wiki pages...

    In the abstract, a Person should have various forms of their
    name, and one or more URIs, ... but for our purposes, we use a
    wiki hack that name == page....   so we don't need that.

    """

    def match(self, typed_orig):
        matches = set()
        typed = typed_orig.lower()

        if typed.endswith("_") or typed[-1:].isdigit():
            typed = typed[0:-1]

        try:
            return '[['+nicks[typed]+']]'
        except KeyError:
            pass
        
        for name in users:
            (first, last) = name.lower().split(' ', 1)
            if ( typed == (first + " " + last) or
                 typed == (first + "_" + last) or
                 typed == (first + last) or
                 typed == (first + last[0]) or
                 typed == (first[0] + last) or
                 typed == first or
                 typed == last or
                 first.startswith(typed) or 
                 last.startswith(typed)
                 ):
                matches.add(name)

        if len(matches) == 1:
            return "[["+matches.pop()+"]]"
        raise meeting.UnidentifiedPerson(typed_orig, matches)

m = meeting.Meeting()
start = time.time() - 3 * 3600
start = time.time() - 6 * 86400

peopleDirectory = WorkingGroup()
#infile = '/home/sandro/.xchat2/xchatlogs/w3c/px-#owl.log'
#infile = "s2"
if len(sys.argv) == 2:
    infile = sys.argv[1]
else:
    infile = '/home/sandro/.xchat2/xchatlogs/w3c/px-#rif.log'
    #print >>sys.stderr, 'Usage: <filename>'
    #sys.exit(1)

count = 0
print >>sys.stderr, "start is ", time.strftime("%d %B %Y %H:%M", time.localtime(start))

subsbuffer = []

for c in chatevents.fromRRSAgent(infile):
#for c in chatevents.fromXChatLog(infile):
    if c.when > start:
        count += 1
        subsbuffer.append(c)
    else:
        pass
        #print time.strftime("%d %B %Y %H:%M", time.localtime(c.when))
        #print time.strftime("%d %B %Y %H:%M", time.localtime(start))
        #print start - c.when
        #print

chatevents.doSubs(subsbuffer)

for c in subsbuffer:
    m.addChatEvent(c, peopleDirectory)
    
print >>sys.stderr, count, 'events'
m.printForWiki()



# set scribe-times after the fact --- since it's a hard thing to edit...
# set topics after the fact?
