  /*eslint no-sync: "off"*/

'use strict';

var path = require('path')
  , basename = path.basename(__filename, '.js')
  , debug = require('debug')('ezmaster:' + basename)
  , util = require('utile')
  , fs = require('fs')
  , glob = require('glob')
  , Docker = require('dockerode')
  , moment = require('moment')
  , docker = new Docker({ socketPath: '/var/run/docker.sock'});




module.exports.getApps = function (cb) {


    util.async.parallel([

      // Retrieves all instances manifests from the files.
      function (handleManifests) {

        // Read the content of manifests folders in order to
        // extract the instances technicalName and some metadata.
        var manifestPath = path.join(__dirname, '../applications/apps.json').toString();

          var manifests = [];


          fs.readFile(manifestPath, 'utf8', function (err, manifestContent) {
            if (err) {
              debug('cannot read the file, something goes wrong with the file', err);
              return handleManifests(err);
            }

            manifests.push(manifestContent);

            console.error(manifestContent);

            return handleManifests(null, manifests);
          });
      },


      function (handleApplications) {


        docker.listImages({ all : true }, function (err, images) {

        if (err) { return new handleApplications(err); }

        var apps = [];


        images.forEach(function (image) {

          var app = {};
          var nameImage = image.RepoTags[0].split('/')[0];

          if (nameImage === 'inistcnrs' &&
           image.RepoTags[0].split('/')[1].split(':')[0] != 'ezmaster' || nameImage === 'matthd') {
            app.imageId = image.Id.split(':')[1];
            app.imageName = image.RepoTags[0];
            app.creationDate = moment.unix(image.Created).format('YYYY/MM/DD HH:mm:ss');
            apps.push(app);
          }

        });
        return handleApplications(null, apps);

        });
    }


    ], function (err, results) {


      if (err) { return cb(err); }

      var ezmasterApplications = {};
      results[1].forEach(function (dockerApplication) {
        results[0].forEach(function (manifest) {
          console.log(manifest);
          console.log(dockerApplication.imageName);
          if (manifest.imageName === dockerApplication.imageName) {
            ezmasterApplications[manifest.imageName] = _.assign(dockerApplication, manifest);
          }
        });
      });

      // Return the just get instances list.
      return cb(null, ezmasterApplications);
    });
}
@@ -220,35 +220,8 @@
 s')%0A
-  , glob = require('glob')%0A
   , 
@@ -281,16 +281,42 @@
 oment')%0A
+  , _ = require('lodash')%0A
   , dock
@@ -418,18 +418,16 @@
 cb) %7B%0A%0A%0A
-  
   util.a
@@ -443,18 +443,16 @@
 llel(%5B%0A%0A
-  
     // R
@@ -500,26 +500,24 @@
  files.%0A    
-  
 function (ha
@@ -530,26 +530,24 @@
 nifests) %7B%0A%0A
-  
       // Rea
@@ -599,18 +599,16 @@
 o%0A      
-  
 // extra
@@ -657,18 +657,16 @@
 tadata.%0A
-  
       va
@@ -747,28 +747,24 @@
 g();%0A%0A      
-   
- 
 var manifest
@@ -769,28 +769,24 @@
 sts = %5B%5D;%0A%0A%0A
-    
       fs.rea
@@ -851,28 +851,24 @@
 ) %7B%0A        
-   
- 
 if (err) %7B%0A 
@@ -862,28 +862,24 @@
  if (err) %7B%0A
-    
           de
@@ -950,36 +950,32 @@
 err);%0A          
-  
-  
 return handleMan
@@ -991,28 +991,24 @@
 r);%0A        
-    
 %7D%0A%0A         
@@ -1006,20 +1006,16 @@
         
-  
-  
 manifest
@@ -1044,28 +1044,24 @@
 );%0A%0A        
-    
 console.erro
@@ -1085,28 +1085,24 @@
 );%0A%0A        
- 
-   
 return handl
@@ -1130,20 +1130,17 @@
 fests);%0A
-    
+%0A
       %7D)
@@ -1149,17 +1149,13 @@
     
-  
 %7D,%0A%0A%0A
-  
     
@@ -1184,26 +1184,24 @@
 ations) %7B%0A%0A%0A
-  
       docker
@@ -1892,34 +1892,32 @@
 , apps);%0A%0A      
-  
 %7D);%0A    %7D%0A%0A%0A    
@@ -1912,18 +1912,16 @@
     %7D%0A%0A%0A
-  
   %5D, fun
@@ -1945,26 +1945,24 @@
 ts) %7B%0A%0A%0A    
-  
 if (err) %7B r
@@ -1975,26 +1975,24 @@
 cb(err); %7D%0A%0A
-  
     var ezma
@@ -2014,26 +2014,24 @@
 s = %7B%7D;%0A    
-  
 results%5B1%5D.f
@@ -2074,18 +2074,16 @@
 %7B%0A      
-  
 results%5B
@@ -2119,95 +2119,8 @@
 ) %7B%0A
-          console.log(manifest);%0A          console.log(dockerApplication.imageName);%0A  
     
@@ -2191,18 +2191,16 @@
         
- 
- 
 ezmaster
@@ -2285,14 +2285,10 @@
     
-  %7D%0A  
+%7D%0A
     
@@ -2289,26 +2289,24 @@
 %7D%0A      %7D);%0A
-  
     %7D);%0A%0A   
@@ -2302,26 +2302,24 @@
    %7D);%0A%0A    
-  
 // Return th
@@ -2345,18 +2345,16 @@
 s list.%0A
-  
     retu
@@ -2392,13 +2392,12 @@
 s);%0A
-  
   %7D);%0A%7D
+;

fix bithound