#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
    @copyright: 2011 Mika Seppänen
    @license: MIT <http://www.opensource.org/licenses/mit-license.php>
"""

import sys
import socket
import optparse
from zipfile import ZipFile
from opencollab.util.config import parseOptions
from opencollab.wiki import CLIWiki, WikiFailure


def handlePackage(collab, package):
    zip = ZipFile(package, "r")
    files = zip.namelist()
    if not "MOIN_PACKAGE" in files:
        sys.exit("Not a valid MoinMoin package (MOIN_PACKAGE file missing from zip)")

    for line in zip.open("MOIN_PACKAGE").readlines()[1:]:
        line = line.strip().split("|")
        action, args = line[0], line[1:]
        if action == "AddRevision":
            filename, pagename = args[0:2]
            print "Creating page %s" % pagename
            try:
                collab.putPage(pagename, zip.open(filename).read())
            except WikiFailure, e:
                if not "You did not change the page content" in str(e):
                    raise WikiFailure(e)
        elif action == "AddAttachment":
            filename, attachmentname, pagename = args[0:3]
            print "Adding attachment %s to page %s" % (attachmentname, pagename)
            collab.putAttachment(pagename, attachmentname, zip.open(filename).read(), True)


def main():
    parser = optparse.OptionParser()
    parser.set_usage("%prog [options] MOINPACKAGES")
    sect = "import-package"
    ops = parseOptions(parser, sect)
    x509 = ops[sect]["x509"]
    x509_ca_file = ops[sect]["x509_ca_file"]

    args = ops[sect]["args"]
    if len(args) < 1:
        parser.error("At least one moin package needs to be specified.")

    while True:
        try:
            collab = CLIWiki(ssl_verify_cert=x509, ssl_ca_certs=x509_ca_file, **ops['creds'])
        except WikiFailure:
            print "ERROR: Authentication failed."
        except (UnicodeError, socket.gaierror):
            sys.exit("ERROR: Not a valid URL.")
        else:
            break

    for package in args:
        print "Importing MoinMoin package %s." % package
        handlePackage(collab, package)

if __name__ == "__main__":
    try:
        main()
    except KeyboardInterrupt:
        print "Script interrupted via CTRL+C."
