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

import time

import sys
import chatevents
import meeting


nicks = {
    'michael': 'Michael Schneider',   # for a meeting with only this Michael
    'mschnei': 'Michael Schneider',
    'm_schnei': 'Michael Schneider',
    'm_schneider': 'Michael Schneider',
    'msch': 'Michael Schneider',
    'thomassch': 'Thomas Schneider',
    'clu': 'Carsten Lutz',
    'jim': 'James Hendler',
    'pfps': 'Peter Patel-Schneider',
    'peter': 'Peter Patel-Schneider',
    'scribe': 'Scribe Error',
    'ekw': 'Evan Wallace',
    'ew': 'Evan Wallbace',
    'mikesmith': 'Michael Smith',
    'mike': 'Michael Smith',
    'jjc': 'Jeremy Carroll',
    'jjc2': 'Jeremy Carroll',
    'dlm': 'Deborah McGuinness',
    'bcuencag': 'Bernardo Cuenca Grau',
    'bcuencagrau': 'Bernardo Cuenca Grau',
    'bercuencagrau': 'Bernardo Cuenca Grau',
    'alanrec': 'Alan Rector',
    'alanrut': 'Alan Ruttenberg',
    'g-stoilos': 'Giorgos Stoilos',
    'evrin': 'Evren Sirin',
    'jar': 'Jonathan Rees',
    'baojie': 'Jie Bao',
    #a'Alan': 
    }

users = [
    # Invited
    #'Alan Rector', # guest
    'Christine Golbreich',
    'Jie Bao',
    'Matthew Horridge', # guest
    'Carsten Lutz',
    'Jeremy Carroll',
    'Martin Dzbor',
    'Thomas Schneider',
    'Sean Bechhofer',
    'Sebastian Brandt',
    'Dimitri Tsarkov',
    # manual from tracker, later
    'Michael Schneider',
    'Jonathan Rees',
    'Vojtech Svatek',
    #'Peter Haase',
    'Fabien Gandon',
    'Ratnesh Sahay',
    'Vit Novacek',
    'Zhe Wu',
    # cut and paste from Tracker
    'Diego Calvanese',
    'Conrad Bock',
    'Anne Cregan',
    'Bernardo Cuenca Grau',
    'Achille Fokoue',
    'Enrico Franconi',
    'Sandro Hawke',
    'James Hendler',
    'Ivan Herman',
    'Pascal Hitzler',
    'Rinke Hoekstra',
    'Ian Horrocks',
    'Vipul Kashyap',
    'Elisa Kendall',
    'Markus Krötzsch',
    'Doug Lenat',
    'Joanne Luciano',
    'Deborah McGuinness',
    'Tommie Meyer',
    'Boris Motik',
    'Fabian Neuhaus',
    'Jeff Pan',
    'Bijan Parsia',
    'Peter Patel-Schneider',
    'Alan Ruttenberg',
    'Uli Sattler',
#    'Michael Sintek',
    'Evren Sirin',
    'Michael Smith',
    'Giorgos Stamou',
    'Giorgos Stoilos',
    'Suzette Stoutenburg',
    'Evan Wallace',
    ]

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]
        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

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-#owl.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))

for c in chatevents.fromWikiRRSAgent(infile):
#for c in chatevents.fromRRSAgent(infile):
#for c in chatevents.fromXChatLog(infile):
    if True: #c.when > start:
        count += 1
        m.addChatEvent(c, peopleDirectory)
    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

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?
