#!/usr/bin/env python2.5
#      -*-mode: python -*-    -*- coding: utf-8 -*-

"""

    Copy an RRSAgent log at 'src' over to a wiki page at 'dest'.

    What makes this interesting is that we can do this repeately, and
    whatever gets added to 'src' (since the last time we did it) get
    added to 'dest'.


"""
__version__="$Id: chatsync.py,v 1.4 2009-03-05 16:22:46 sandro Exp $"

import urllib2
from optparse import OptionParser
import re

import debugtools 
from debugtools import debug


import simple_wiki

marker = u"# SPECIAL MARKER FOR CHATSYNC.  DO NOT EDIT THIS LINE OR BELOW.  SRCLINESUSED="
digits = 8

def chat_sync(src, old_wiki_text):
    """
    Called as part of edit_page --- that is, we're called with the
    page_text of the wiki page, and we much return new text for that
    page.
    """
    
    if old_wiki_text is None:
        debug('chatsync', 'wiki page does not exist')
        pre = u"""{{chatlog|rrsagent=%s|chatlog={{fullurl:{{PAGENAME}}}}}}\n<pre><nowiki>\n""" % src
        old_line_count = 0
        post = u"\n</nowiki></pre>"
    else:
        debug('chatsync', 'wiki page exists')

        try:
            pos = old_wiki_text.index(marker)
        except ValueError:
            print "End marker not found."
            print "Text ends with:"+`old_wiki_text[-200:]`
            raise RuntimeError, 'Marker disturbed.   Manual fix required.'

        pre = old_wiki_text[0:pos]
        end = pos+len(marker)
        old_line_count = int(old_wiki_text[end:end+digits])
        debug('chatsync', 'old line count:', old_line_count)
        post = old_wiki_text[end+digits:]

    src_stream = urllib2.urlopen(src)
    src_text = src_stream.read()
    src_text = src_text.decode('utf-8')
    f = open("x", "w")
    f.write(src_text.encode('utf-8'))
    f.close()
    src_stream.close()
    src_text = src_text.rstrip()
    src_lines = src_text.split('\n')
    debug('chatsync', 'rrsagent log has', len(src_lines), 'lines')

    additional_lines = src_lines[old_line_count:]
    additional_line_count = len(additional_lines)
    debug('chatsync', 'so we need to copy', additional_line_count, 'lines')
    total_line_count = old_line_count + len(additional_lines)
    debug('chatsync', 'giving a new total lines:', total_line_count)
    if additional_lines:
        additional_text = u"\n".join(additional_lines) + u"\n"
    else:
        additional_text = u""   # should cause no change at all.
    #debug('chatsync', 'additional_text =', additional_text)

    new_wiki_text = (pre + 
                     additional_text +
                     marker + u"%0*d" % (digits, total_line_count) +
                     post)

    debug('chatsync', 'done, returning')
    # new_wiki_text = new_wiki_text.encode('utf-8')
    return (new_wiki_text, u'%d lines added by chatsync' % additional_line_count)
    
rrsagent_url  = re.compile(r'''^http://www.w3.org/(\d+)/(\d+)/(\d+)-(.*?)-irc.txt$''')
def parse_rrsagent_url(url):
    m = rrsagent_url.match(url)
    if m:
        year = int(m.group(1))
        month = int(m.group(2))
        day = int(m.group(3))
        channel = m.group(4)
        return (year, month, day, channel)
    raise ValueError('Cannot parse RRSAgent URL')

def run(source, family=None, title=None):

    if source.endswith(".html"):
        source = source[0:-5]
    if source.endswith(".rdf"):
        source = source[0:-4]
    if source.endswith(".txt"):
        pass
    else:
        source += ".txt"

    if family is None or title is None:
        (year, month, day, channel) = parse_rrsagent_url(source)
        if family is None:
            family = channel
        if title is None:
            title = "Chatlog_%04d-%02d-%02d" % (year, month, day)

    return simple_wiki.edit_page(family, title, 
                      lambda(page_text): chat_sync(source, page_text))


def run_cmdline():
    parser = OptionParser(usage="%prog [options] rrsagent_location family page_title",
                          version=__version__)
    parser.set_defaults(verbose=True)
    parser.add_option("-q", "--quiet",
                      action="store_false", dest="verbose", 
                      help="don't print status messages to stderr")
    parser.add_option("-D", "--debug",
                      action="append", dest="debugTags", 
                      help="turn on debugging of a particular type (try 'all')")
                      
    (options, args) = parser.parse_args()

    if options.debugTags:
        debugtools.tags.update(options.debugTags)
    verbose = options.verbose

    debug('cmdline', 'args:', args)
    
    if len(args) >= 1 and len(args) <= 3:
        run(*args)
    else:
        parser.print_help()
        sys.exit(1)

if __name__ == "__main__":
    import doctest, sys
    doctest.testmod(sys.modules[__name__])

    run_cmdline()
