/**
 * Initializes directories, installs dependencies then starts all
 * configured servers for the Hoodie application
 */

var domain = require('domain')

var async = require('async')
var Hapi = require('hapi')

var config = require('./config.js')
var database = require('./database')
var hapi = require('./hapi')
var hooks = require('./hooks')
var log = require('./log')
var plugins = require('./plugins')

var exports = module.exports = function (options, callback) {
  var app_domain = domain.create()

  var env_config = config(options)

  // wrap in top-level domain, otherwise we sometimes don't get uncaught
  // exceptions printed in node 0.10.x
  app_domain.run(exports.init.bind(null, env_config, callback))

  // make sure we print a stack trace in node 0.10.x
  app_domain.on('error', function (err) {
    if (err.stack) {
      log.error('app', err.message || err.toString(), err.stack)
    } else if (err.message && !err.stack) {
      log.error('app', err.message)
    } else if (err.message) {
      log.error('app', err.message, err)
    } else {
      log.error('app', 'Error', err)
    }

    if (err.code === 'MODULE_NOT_FOUND') {
      log.error('app', 'A module couldn\'t be found, so try running `npm install` again')
    }
    process.exit(1)
  })
}

var options = {
  connections: {
    routes: {
      cors: {override: false},
      payload: {maxBytes: 1048576 * 10} // 10 MB
    }
  }
}

exports.init = function (env_config, callback) {
  if (log.raw.levels[log.raw.level] < 4000) {
    log.verbose('hapi', 'Showing internal debug output')
    options.debug = {
      log: ['error'],
      request: ['error']
    }
  }

  var server = new Hapi.Server(options)

  async.applyEachSeries([
    database,
    plugins.load,
    async.apply(exports.configureServer, server),
    plugins.startAll
  ], env_config, function (err) {
    if (err) return callback(err)

    callback(null, server, env_config)
  })
}

exports.configureServer = function (server, env_config, callback) {
  var configuredHooks = hooks(env_config)
  configuredHooks.runStatic('server.pack.pre', [server])

  server.connection({
    port: env_config.www_port,
    labels: ['web']
  })

  server.connection({
    port: env_config.admin_port,
    labels: ['admin']
  })

  log.silly('hapi', 'Registering internal plugins')
  server.register(hapi(env_config), function (err) {
    if (err) return callback(err)

    log.verbose('hapi', 'Registerd internal plugins')
    configuredHooks.runStatic('server.pack.post', [server])
    callback(null)
  })
}

@@ -157,253 +157,28 @@
 var 
-async = require('async')%0Avar Hapi = require('hapi')%0A%0Avar config = require('./config.js')%0Avar database = require('./database')%0Avar hapi = require('./hapi')%0Avar hooks = require('./hooks')%0Avar log = require('./log')%0Avar plugins = require('./plugins
+log = require('./log
 ')%0A%0A
@@ -294,22 +294,35 @@
 onfig = 
+require('./
 config
+')
 (options
@@ -1246,16 +1246,77 @@
 back) %7B%0A
+  var async = require('async')%0A  var Hapi = require('hapi')%0A%0A
   if (lo
@@ -1382,29 +1382,29 @@
 ', '
-Showing internal debu
+Will show http and lo
 g ou
@@ -1525,24 +1525,61 @@
 ver(options)
+%0A  var plugins = require('./plugins')
 %0A%0A  async.ap
@@ -1602,16 +1602,27 @@
     
+require('./
 database
 ,%0A  
@@ -1617,16 +1617,18 @@
 database
+')
 ,%0A    pl
@@ -1904,31 +1904,34 @@
 var 
-configuredHooks = 
+hooks = require('./
 hooks
+')
 (env
@@ -1941,27 +1941,17 @@
 nfig)%0A  
-configuredH
+h
 ooks.run
@@ -2219,20 +2219,33 @@
 egister(
+require('./
 hapi
+')
 (env_con
@@ -2364,19 +2364,9 @@
     
-configuredH
+h
 ooks

move as much code into domain as possible