var async = require('async');

var logger = require(__dirname+'/src/logger/logger'),
    server = require(__dirname+'/src/server/server'),
    cli = require(__dirname+'/src/cli'),
    arborescence = require(__dirname+'/src/arborescence');

module.exports = {
    cli: cli,
    server: server,
    logger: logger,
    db: function(callback) {
        require(__dirname + '/db')(function(err, res) {
            if (err)
                return callback(err);

            return callback(res.db);
        });
    },
    mongo: function(callback) {
        require(__dirname + '/mongo')(callback);
    },
    user: require(__dirname + '/src/server/user/models/user'),
    mailer: require(__dirname + '/src/email/mailer')
};

require(__dirname + '/db')(function(err, res) {
    if (res.type == 'mongoose') {
        arborescence.getRequiredFiles('models', function (files) {
            arborescence.loadFiles(files, null, function () {
            });
        });
    }
});
@@ -21,16 +21,133 @@
 'async')
+,%0A    events = require('events'),%0A    eventEmitter = new events.EventEmitter();%0A    eventEmitter.setMaxListeners(100)
 ;%0A%0Avar l
@@ -350,16 +350,65 @@
 nce');%0A%0A
+var firstCall = true,%0A    modelsLoaded = false;%0A%0A
 module.e
@@ -493,32 +493,92 @@
 ion(callback) %7B%0A
+        if (firstCall) %7B%0A            firstCall = false;%0A    
         require(
@@ -625,24 +625,28 @@
             
+    
 if (err)%0A   
@@ -642,16 +642,20 @@
 f (err)%0A
+    
         
@@ -701,32 +701,120 @@
     
-return callback(res.db);
+    if (res.type == 'mongoose') %7B%0A                    arborescence.getRequiredFiles('models', function (files) %7B
 %0A   
@@ -822,471 +822,1172 @@
     
-%7D);%0A
+        
     
-%7D,%0A
     
-mongo: function(callback) %7B%0A        require(__dirname + '/mongo')(callback);%0A    %7D,%0A    user: require(__dirname + '/src/server/user/models/user'),%0A    mailer: require(__dirname + '/src/email/mailer')%0A%7D;%0A%0Arequire(__dirname + '/db')(function(err, res) %7B%0A    if (res.type == 'mongoose') %7B%0A        arborescence.getRequiredFiles('models', function (files) %7B%0A            arborescence.loadFiles(files, null, function () %7B%0A            %7D
+arborescence.loadFiles(files, null, function () %7B%0A                            eventEmitter.emit('bluestorm:modelsLoaded');%0A                            return callback(res.db);%0A                        %7D);%0A                    %7D);%0A                %7D%0A            %7D);%0A        %7D%0A        else %7B%0A            if (modelsLoaded) %7B%0A                require(__dirname + '/db')(function(err, res) %7B%0A                    if (err)%0A                        return callback(err);%0A%0A                    return callback(res.db);%0A                %7D);%0A            %7D%0A            else %7B%0A                eventEmitter.on('bluestorm:modelsLoaded', function() %7B%0A                    modelsLoaded = true;%0A                    require(__dirname + '/db')(function(err, res) %7B%0A                        if (err)%0A                            return callback(err);%0A%0A                        return callback(res.db);%0A                    %7D);%0A                %7D);%0A            %7D%0A        %7D%0A%0A    %7D,%0A    mongo: function(callback) %7B%0A        require(__dirname + '/mongo')(callback
 );%0A    
+%7D,%0A
     
-%7D);%0A    %7D
+user: require(__dirname + '/src/server/user/models/user'),%0A    mailer: require(__dirname + '/src/email/mailer')
 %0A%7D
-)
 ;

save models for mongoose before sending db instance