var fs   = require("fs"),
    path = require('path'),
    _    = require('underscore');

var emitter = new (require('events').EventEmitter)();

var logger = require('./logger'),
    walk = require('./environment').walk;

exports.detect = function (env, callback) {
    logger.info('detecting subapps...');
    var ctx = (env.subapps = []);

    var dirname = [env.path, 'modules'].join('/');
    fs.readdir(dirname, function (err, relnames) {
        if (err) {
            logger.error('Error when reading subapps: ' + err);
            return;
        }

        if (relnames.length === 0) {
            callback();
        } else {
            relnames.forEach(function (relname, index, relnames) {
                var name = path.join(dirname, relname),
                    counter = 0,
                    listener = function () {
                        counter--;
                        //logger.info('counter: ' + counter);
                        if (counter === 0) {
                            callback();
                        }
                    };

                emitter.on('found', listener);

                fs.stat(name, function (err, stat) {
                    if (err) {
                        logger.error('Error when reading subapp directory: ' + err);
                        return;
                    }
                    if (stat.isDirectory()) {
                        logger.info('Found subapp: ' + name);
                        counter++;
                        ctx.push(name);
                        emitter.emit('found', name);
                    }
                });
            });
        }
    });

};

@@ -331,16 +331,116 @@
 ps = %5B%5D)
+,%0A        finished = function () %7B%0A        callback();%0A    %7D;%0A    emitter.once('finished', finished)
 ;%0A%0A    v
@@ -611,32 +611,70 @@
 apps: ' + err);%0A
+            emitter.emit('finished');%0A
             retu
@@ -696,20 +696,27 @@
         
-if (
+var last = 
 relnames
@@ -723,21 +723,44 @@
 .length 
+- 1;%0A        if (last 
 === 
-0
+-1
 ) %7B%0A    
@@ -763,33 +763,47 @@
             
-callback(
+emitter.emit('finished'
 );%0A        %7D
@@ -806,25 +806,17 @@
    %7D
- else %7B%0A 
+%0A
         
    r
@@ -811,19 +811,16 @@
         
-   
 relnames
@@ -874,28 +874,24 @@
             
-    
 var name = p
@@ -920,372 +920,10 @@
 ame)
-,%0A                    counter = 0,%0A                    listener = function () %7B%0A                        counter--;%0A                        //logger.info('counter: ' + counter);%0A                        if (counter === 0) %7B%0A                            callback();%0A                        %7D%0A                    %7D;%0A%0A                emitter.on('found', listener);%0A%0A    
+;%0A
     
@@ -979,28 +979,24 @@
             
-    
 if (err) %7B%0A 
@@ -1006,36 +1006,32 @@
                 
-  
-  
 logger.error('Er
@@ -1091,36 +1091,32 @@
             
-    
 return;%0A
             
@@ -1099,28 +1099,24 @@
     return;%0A
-    
             
@@ -1133,28 +1133,24 @@
             
-    
 if (stat.isD
@@ -1159,28 +1159,24 @@
 ectory()) %7B%0A
-    
             
@@ -1245,21 +1245,22 @@
     
- 
-   counter++
+ctx.push(name)
 ;%0A  
@@ -1277,36 +1277,48 @@
     
-        ctx.push(name);%0A    
+%7D%0A                if (index === last) %7B%0A
     
@@ -1352,26 +1352,19 @@
 t('f
-ound', name);%0A    
+inished');%0A
     
@@ -1381,36 +1381,32 @@
   %7D%0A            
-    
 %7D);%0A            
@@ -1405,25 +1405,12 @@
     
- 
-   
 %7D);%0A
-        %7D
 %0A   
@@ -1414,12 +1414,11 @@
     %7D);%0A
-%0A
 %7D;%0A

refactor suapp dsicovery