/**
 * @fileoverview: The webapp daemon control script invoked by
 * the Apache Commons Daemon jsvc tool.
 */

var Parser = require('ringo/args').Parser;
var Server = require('ringo/httpserver').Server;
var fileutils = require('ringo/fileutils');
var system = require('system');
var log = require('ringo/logging').getLogger(module.id);

var options,
    server,
    parser;

module.shared = true;

export('parseOptions', 'init', 'start', 'stop', 'destroy', 'getServer');

function parseOptions(arguments, defaults) {
    // parse command line options
    parser = new Parser();
    parser.addOption("a", "app", "APP", "The exported property name of the JSGI app (default: 'app')");
    parser.addOption("c", "config", "MODULE", "The module containing the JSGI app (default: 'config')");
    parser.addOption("j", "jetty-config", "PATH", "The jetty xml configuration file (default. 'config/jetty.xml')");
    parser.addOption("H", "host", "ADDRESS", "The IP address to bind to (default: 0.0.0.0)");
    parser.addOption("m", "mountpoint", "PATH", "The URI path where to mount the application (default: /)");
    parser.addOption("p", "port", "PORT", "The TCP port to listen on (default: 80)");
    parser.addOption("s", "static-dir", "DIR", "A directory with static resources to serve");
    parser.addOption("S", "static-mountpoint", "PATH", "The URI path where ot mount the static resources");
    // parser.addOption("v", "virtual-host", "VHOST", "The virtual host name (default: undefined)");
    parser.addOption("h", "help", null, "Print help message to stdout");
    options = parser.parse(arguments, defaults);
    return options;
}

function init() {
    log.info("init");
    if (!options) {
        parseOptions(system.args.slice(1), {
            app: "app",
            config: "config",
            port: 80
        });
    }
    if (options.help) {
        print("Available options:");
        print(parser.help());
    }

    var config = require(options.config || "config");

    // check for jar files to add to classpath
    if (Array.isArray(config.jars)) {
        config.jars.forEach(function(p) {
            log.info("Adding to classpath: " + p);
            addToClasspath(p);
        });
    }
    // pick up extra http-config from config module
    if (config.httpConfig) {
        options = Object.merge(options, config.httpConfig);
    }
    server = new Server(options);
}

function start() {
    log.info("start");
    var config = require(options.config || "config");

    // check for additional static mounts
    if (Array.isArray(config.static)) {
        config.static.forEach(function(spec) {
            var dir = fileutils.resolveId(options.config, spec[1]);
            server.addStaticResources(spec[0], null, dir);
        });
    }
    server.start();
}

function stop() {
    log.info("stop");
    server.stop();
}

function destroy() {
    log.info("destroy");
    server.destroy();
    server = null;
}

function getServer() {
    return server;
}
@@ -200,25 +200,27 @@
 er;%0Avar 
-fileutils
+%7BresolveId%7D
  = requi
@@ -2650,18 +2650,8 @@
 r = 
-fileutils.
 reso
@@ -2783,69 +2783,1329 @@
 ();%0A
-%7D%0A%0Afunction stop() %7B%0A    log.info(%22stop%22);%0A    server.stop();
+    // check extensions%0A    if (Array.isArray(config.extensions)) %7B%0A        config.extensions.forEach(function startExtension(ext) %7B%0A            try %7B%0A                log.debug(%22Starting extension%22, ext);%0A                var module = ext;%0A                if (typeof ext == %22string%22) %7B%0A                    module = require(resolveId(options.config, ext));%0A                %7D%0A                if (typeof module.serverStarted == %22function%22) %7B%0A                    module.serverStarted(server);%0A                %7D%0A            %7D catch (e) %7B%0A                log.error(%22Error starting extension %7B%7D:%22, ext, e);%0A            %7D%0A        %7D);%0A    %7D%0A%7D%0A%0Afunction stop() %7B%0A    log.info(%22stop%22);%0A    server.stop();%0A    // stop extensions%0A    if (Array.isArray(config.extensions)) %7B%0A        config.extensions.forEach(function stopExtension(ext) %7B%0A            try %7B%0A                log.debug(%22Stopping extension%22, ext);%0A                var module = ext;%0A                if (typeof ext == %22string%22) %7B%0A                    module = require(resolveId(options.config, ext));%0A                %7D%0A                if (typeof module.serverStopped == %22function%22) %7B%0A                    module.serverStopped(server);%0A                %7D%0A            %7D catch (e) %7B%0A                log.error(%22Error stopping extension %7B%7D:%22, ext, e);%0A            %7D%0A        %7D);%0A    %7D
 %0A%7D%0A%0A

Add experimental support for webapp extensions.