#!/usr/bin/python2
#
# Program to upgrade a moin moin wiki instance on Linux systems.
# (Can probably be adapted for Windows systems).
#
# After installing a new version (1.1) of MoinMoin over an
# existing one, any existing wiki instances will not have
# any of the newer system and help pages.
#
# Instead of blindly copying all the new help and system
# pages from the new base instance in the new version over
# the ones in your instance(s), use this script to compare
# the files to make sure that you do not overwrite any
# files that have been changed since the initial installation.
#
# This program uses the change history information to determine
# that a file has changed. It look into the data/backup directory
# and any files that it find there indicate that that file has
# changed.
#
# How To Use:
# ----------
# 1) Edit this script by changing the MROOT and DEST_WIKI_NAME
#    variables:
#     Set MROOT to where your MoinMoin instances are installed
#     Set DEST_WIKI_NAME to where your particular instance of wiki
#        is installed.
# 2) Run the script, and capture it's output.
# 3) Inspect the output to make sure it is reasonable
# 4) Run the captured output in a shell to copy the files.
#
#
# Author:
#   P.K. Shiu
#   http://www.pkshiu.com/
#   Download from: http://www.pkshiu.com/tech/python/moin_compare.py
#
# Version:
#   1.0. Initial release.
#

import string,sys,os

# SET THESE TWO VARIABLES, e.g. :
# MROOT = '/usr/local/moin/share/moin/'
# DEST_WIKI_NAME = 'main_wiki'
MROOT = None
DEST_WIKI_NAME = None

def prune_history(ls):
    """
    prune history file list, remove .nnnnn suffix and remove duplicate
    """
    ls.sort()
    newlist = []
    prev = ''
    for f in ls:
        fn = f[:string.find(f,'.')]
        if fn != prev:
            newlist.append(fn)
            prev = fn
            
    return newlist

def go():

    srctext = os.path.join(MROOT,'data/text')
    dest = os.path.join(MROOT,DEST_WIKI_NAME)
    dest_filter =  os.path.join(dest,'data/backup')

    print '# Base MoinMoin instance is in ', MROOT
    print '# Upgrading MoinMoin instance in ', dest

    srclist = os.listdir(srctext)
    srclist.sort()

    filterlist = os.listdir(dest_filter)
    filters = prune_history(filterlist)
    
    
    # compare two and remove the filtered files
    fi = 0
    fmax = len(filters)
    si = 0
    smax = len(srclist)
    results = []
    ex = 0
    
    while (si < smax) and (fi < fmax):

        s = srclist[si]
        f = filters[fi]
        if s < f:
            results.append(s)
            # print "keeping ",s
            si = si + 1
        elif s == f:
            si = si + 1
            fi = fi + 1
            print "# ...excluding ",s
            ex = ex + 1
        elif s > f:
            fi = fi + 1

    # copy leftovers
    results = results + srclist[si:]

    print '# %3d files in source' % (smax)
    print '# %3d files changed in destination' % (fmax)
    print '# %3d files changed in destination that is in source' % (ex)
    print '# %3d files to copy' % (len(results))

    # Capture these lines and run them in shell to copy
    # files over from base instance to your instance.
    for r in results:
        print 'cp %s %s' % ( os.path.join(srctext,r),
                             os.path.join(MROOT,DEST_WIKI_NAME,'data/text'))

if __name__ == "__main__":

    if (MROOT == None) or (DEST_WIKI_NAME == None):
        print 'Please set MROOT and DEST_WIKI_NAME variables.'
        sys.exit(0)
        
    go()
