#!/usr/bin/env python
"""
This is based on on the 12_to_13mig1.py script.

Here we take our htdocs/files/<wikiname> directory and tweak all of the
directorynames in it.

The old directory will be put into <wikiname>-old.
"""

from_encoding = 'iso8859-1'
#from_encoding = 'utf-8'

to_encoding = 'utf-8'

import os.path, sys, shutil, urllib

sys.path.insert(0, '../../..')
from MoinMoin import wikiutil

from migutil import opj, listdir, copy_file, copy_dir

# this is a copy of the wikiutil.unquoteFilename of moin 1.2.1

def unquoteFilename12(filename, encoding):
    """
    Return decoded original filename when given an encoded filename.
    
    @param filename: encoded filename
    @rtype: string
    @return: decoded, original filename
    """
    str = urllib.unquote(filename.replace('_', '%'))
    try:
        newstr = str.decode(encoding)
    except UnicodeDecodeError: # try again with iso
        newstr = str.decode('iso-8859-1')
    return newstr

def convert_string(str, enc_from, enc_to):
    try:
        newstr = str.decode(enc_from)
    except UnicodeDecodeError: # try again with iso
        newstr = str.decode('iso-8859-1')
    return newstr.encode(enc_to)
    
def qf_convert_string(str, enc_from, enc_to):
    str = unquoteFilename12(str, enc_from)
    str = wikiutil.quoteWikinameFS(str, enc_to)
    return str

def convert_pagedir(dir_from, dir_to, enc_from, enc_to):
    os.mkdir(dir_to)
    for dname_from in listdir(dir_from):
        dname_to = qf_convert_string(dname_from, enc_from, enc_to)
        print "%s -> %s" % (dname_from, dname_to)
        shutil.copytree(opj(dir_from, dname_from), opj(dir_to, dname_to), 1)


if __name__ == '__main__':

    if len( sys.argv ) != 2:
	print 'must provide the name of a subdirectory to munge names of'
	sys.exit( 1 )

    origdir = sys.argv[1] + '-old'
    targdir = sys.argv[1]
    try:
	os.rename( targdir, origdir )
    except OSError:
        print "You need to be in the directory where the original data lies"
        sys.exit(1)

    convert_pagedir( origdir, targdir, from_encoding, to_encoding)
