## moinmoinloader19.py
##
## entry point between the ISAPI extension
## and the MoinMoin WSGI server
## using ISAPI WSGI
##
## MoinMoin:DimitriJanczak / 18.11.2009 / 1.0 - Initial release
## MoinMoin:DimitriJanczak / 15.12.2009 / 1.1 - adapted for MoinMoin 1.9
## MoinMoin:DimitriJanczak / 05.04.2010 / 1.2 - automated virtual dirs parameters by importing farmconfig 
## portions from the examples installed with ISAPI WSGI

## Import MoinMoin WSGI Server

## MoinMoin 1.6 to 1.8
## from MoinMoin.server.server_wsgi import moinmoinApp, WsgiConfig
## MoinMoin 1.9
from MoinMoin.web.serving import make_application

## no longer needed in 1.9
##class Config(WsgiConfig):
##    pass
## config = Config() 

## import the ISAPI WSGI glue
import isapi_wsgi

# The entry points for the ISAPI extension.
def __ExtensionFactory__():
    ## new way to instantiate in 1.9
    moinmoinApp = make_application(shared=True) 
    return isapi_wsgi.ISAPIThreadPoolHandler(moinmoinApp)

	
## Installation code
if __name__=='__main__':
    from isapi.install import *

    # If run from the command-line, install ourselves.
    params = ISAPIParameters()

    sm = [
        ScriptMapParams(Extension="*", Flags=0)
    ]

    # get the wikis list
    try:
        from farmconfig import wikis
    except ImportError:
        print "Update the PYTHONPATH variable with the directory where your farmconfig.py resides"
        print "or add the following code to this file if you do not want system-wide inclusion:"
        print "import sys"
        print "sys.path.append(r'C:\Path\ToFarmConfig')"
        raise
        
    # Create a Virtual Directory per wiki
    params.VirtualDirs = [	VirtualDirParameters(Name=wikiName,
                            Description = "ISAPI-WSGI gateway for %(wikiName)s " % { 'wikiName':wikiName } ,
                            ScriptMaps = sm,
                            ScriptMapUpdate = "replace" )
                            for (wikiName,_) in wikis ]					  

    HandleCommandLine(params)
