"""
    MoinMoin - Macro to compare pages, e.g. to find out, if a page with a
    german translation is still "in sync" with the english original page.

    If you generate some sort of "derived work" of a page, go to page info
    of the original page and copy & paste the PageCompare macro shown there
    to your derived page.
    
    Usage:
        [[PageCompare(masterpagename,masterdate,mastersha)]]

        masterpagename	name of the master (original) page
	masterdate	original date of master page (only for information)
	mastersha	original SHA digest of master page

    Examples:
	[[PageCompare(HelpIndex,2002-02-02 13:45:00,5AE4EF821719D7AD65E3D258178E851CA9B39320)]]
        This will compare the given mastersha with the current sha of the
	masterpage. If they are the same, everything is still in sync and you
	get an appropriate message. If not, you will be informed, that the
	masterpage has changed and therefore the current page is not in sync any
	more (e.g. because it was a translation of the older masterpage).
	
    Copyright (c) 2002 by Thomas Waldmann <tw@waldmann-edv.de>
    All rights reserved, see COPYING for details.

"""

import string, re
# import sys, cStringIO
# from MoinMoin import user
from MoinMoin.Page import Page
from MoinMoin.i18n import _

_args_re_pattern = r'(?P<masterpage>[^,]*),(?P<masterdate>[^,]*),(?P<mastersha>.*)'

def execute(macro, text, args_re=re.compile(_args_re_pattern)):

    # parse and check arguments
    args = args_re.match(text)
    if not args:
        return ('<p><strong class="error">%s</strong></p>' % _('Invalid MasterPage arguments "%s"!')) % (text,)

    masterpagename = args.group('masterpage')
    origmasterdate = args.group('masterdate')
    origmasterdigest = args.group('mastersha')

#    this_page = macro.formatter.page

    masterpage = Page(masterpagename)
    import sha
    masterdigest=string.upper(sha.new(masterpage.get_raw_body()).hexdigest())
    if masterdigest != origmasterdigest:
        ret = '<p><b>This page is not in sync with %s, last update was at %s.</b></p>' \
	      % (macro.formatter.pagelink(masterpagename),origmasterdate)
    else:
        ret = '<p>This page is in sync with its original at %s.</p>' % macro.formatter.pagelink(masterpagename)
    return ret
    
