var http = require("http");
var path = require("path");
var paperboy = require("paperboy");
var B = require("buster-core");
var testServer = require("./middleware");
var cli = require("buster-cli");
var DOCUMENT_ROOT = path.join(__dirname, "../public");

function servePublicFiles(documentRoot, req, res) {
buster.log(documentRoot);
    paperboy
        .deliver(documentRoot, req, res)
        .addHeader("Expires", 300)
        .error(function(statCode, msg) {
            res.writeHead(statCode, { "Content-Type": "text/plain" });
            res.end("Error " + statCode);
        })
        .otherwise(function(err) {
            res.writeHead(404, { "Content-Type": "text/plain" });
            res.end("Error 404: File not found");
        });
}

module.exports = {
    create: function (stdout, stderr, options) {
        options = options || {};
        var c = cli.create(options);
        var logger = c.createLogger(stdout, stderr);
        c.addHelpOption(options.missionStatement, options.description);
        return B.extend(B.create(this), {
            cli: c,
            logger: logger,
            documentRoot: options.documentRoot || DOCUMENT_ROOT,
            templateRoot: options.templateRoot
        });
    },

    run: function (args, callback) {
        this.loadOptions();
        this.cli.parseArgs(args, function (err) {
            if (this.cli.loggedHelp) { return callback(); }
            try {
                var server = this.createServer(this.port(), this._binding.value);
                process.nextTick(function () {
                    callback(null, server);
                    this.logger.log("buster-server running on " +
                                    "http://localhost:" + this.port());
                }.bind(this));
            } catch (e) {
                this.handleError(e);
                callback(e);
            }
        }.bind(this));
    },

    createServer: function (port, binding) {
        var middleware, documentRoot = this.documentRoot;
        var httpServer = http.createServer(function (req, res) {
            if (middleware.respond(req, res)) { return; }
            servePublicFiles(documentRoot, req, res);
        });
        httpServer.on("error", B.bind(this, "handleError"));
        httpServer.listen(port, binding);
        middleware = testServer.create(httpServer, {
            logger: this.logger,
            templateRoot: this.templateRoot
        });
        return httpServer;
    },

    loadOptions: function () {
        this._port = this.cli.opt("-p", "--port", "The port to run the server on", {
            defaultValue: 1111,
            validators: { "integer": "--port ($1) must be a number" }
        });

        this._binding = this.cli.opt("-b", "--binding", "The address to bind the server to", {
            hasValue: true
        });
    },

    port: function () {
        return parseInt(this._port.value, 10);
    },

    handleError: function (err) {
        if (/EADDRINUSE/.test(err.message)) {
            this.cli.err("Address already in use. Pick another " +
                         "port with -p/--port to start buster-server");
        } else {
            this.cli.err("Something went horribly wrong. This is most " +
                         "likely a bug, please report at\n" +
                         "http://github.com/busterjs/buster/issues\n" +
                         err.stack);
        }
    }
};

@@ -1264,24 +1264,71 @@
 callback) %7B%0A
+        callback = callback %7C%7C function () %7B%7D;%0A
         this

Make server CLI runnable without a callback