package org.tomdz.maven.sphinx;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import org.python.core.Py;
import org.python.core.PySystemState;

public class SphinxRunner
{
    /**
     * For running sphinx via a forked jvm (or standalone).
     *
     * @param args
     */
    public static void main(String[] args) throws ScriptException
    {
        System.exit(run(args));
    }

    public static int run(String[] args) throws ScriptException
    {
        // this setting supposedly allows GCing of jython-generated classes but I'm
        // not sure if this setting has any effect on newer jython versions anymore
        System.setProperty("python.options.internalTablesImpl", "weak");

        PySystemState engineSys = new PySystemState();
        String sphinxSourceDirectory = null;
        List<String> sphinxArgs = new ArrayList<String>(Arrays.asList(args));

        for (Iterator<String> it = sphinxArgs.iterator(); it.hasNext();) {
            String arg = it.next();
            if ("--sphinxSourceDirectory".equals(arg) && it.hasNext()) {
                // we need to remove it from the argument list as sphinx wouldn't like it
                it.remove();
                sphinxSourceDirectory = it.next();
                it.remove();
                break;
            }
        }
        if (sphinxSourceDirectory == null) {
            throw new IllegalArgumentException("No --sphinxSourceDirectory argument given");
        }

        engineSys.path.append(Py.newString(sphinxSourceDirectory));
        Py.setSystemState(engineSys);

        ScriptEngine engine = new ScriptEngineManager().getEngineByName("python");

        engine.put("args", sphinxArgs.toArray(new String[sphinxArgs.size()]));
        engine.eval("import sphinx");
        return (Integer) engine.eval("sphinx.main(args)");
    }
}

@@ -501,32 +501,164 @@
 Exception%0A    %7B%0A
+        // use headless mode for AWT (prevent %22Launcher%22 app on Mac OS X)%0A        System.setProperty(%22java.awt.headless%22, %22true%22);%0A%0A
         System.e

Enable headless mode for AWT in fork mode