/**
 * Created by AlexanderC on 10/6/15.
 */

var exports = module.exports = function(callback) {
  var DEEP = 'deep-framework';
  var BROWSER_BUILD = 'browser/framework.js';
  var FW_DEP_PATH = 'Frontend/js/lib/deep-framework.js';

  var exec = require("child_process").exec;
  var path = require('path');
  var fs = require('fs');

  console.log('Checking for ' + DEEP + ' globally');

  var installation = exec(
    'npm list -g --depth 1 ' + DEEP + ' > /dev/null 2>&1 || npm install -g ' + DEEP + ' --production --loglevel warn',
    function(error, stdout, stderr) {
      if (error) {
        console.error('Error while installing ' + DEEP, error);
        callback();
        return;
      }

      exec('npm root -g', function(error, stdout, stderr) {
        if (error) {
          console.error('Error getting NPM root', error);
          callback();
          return;
        }

        var npmRoot = stdout.replace(/\s+/, '');
        var fw = path.join(npmRoot, DEEP);
        var browserFw = path.join(fw, BROWSER_BUILD);

        if (!fs.existsSync(browserFw)) {
          console.error('Missing browser version of ' + DEEP + ' in ' + browserFw);
          callback();
          return;
        }

        var fwDep = path.join(__dirname, FW_DEP_PATH);

        exec('rm -f ' + fwDep + '; cp ' + browserFw + ' ' + fwDep, function(error, stdout, stderr) {
          if (error) {
            console.error('Error while copying browser version of ' + DEEP, error);
            callback();
            return;
          }

          console.log('Browser version of ' + DEEP + ' was successfully copied into ' + fwDep);

          callback();
        });
      });
    }
  );

  installation.stdout.pipe(process.stdout);
  installation.stderr.pipe(process.stderr);
};

@@ -524,16 +524,28 @@
 vel warn
+ &%3E/dev/null
 ',%0A    f
@@ -557,32 +557,16 @@
 on(error
-, stdout, stderr
 ) %7B%0A    
@@ -733,32 +733,24 @@
 rror, stdout
-, stderr
 ) %7B%0A        
@@ -1262,35 +1262,53 @@
     
-exec('rm -f ' + fwDep + '; 
+if (!fs.existsSync(fwDep)) %7B%0A          exec('
 cp '
@@ -1353,28 +1353,14 @@
 rror
-, stdout, stderr
 ) %7B%0A
+  
     
@@ -1370,32 +1370,34 @@
    if (error) %7B%0A
+  
             cons
@@ -1456,32 +1456,34 @@
 + DEEP, error);%0A
+  
             call
@@ -1494,32 +1494,34 @@
 ();%0A            
+  
 return;%0A        
@@ -1514,35 +1514,41 @@
 turn;%0A          
-%7D%0A%0A
+  %7D%0A  %0A  
           consol
@@ -1627,17 +1627,18 @@
 fwDep);%0A
-%0A
+  
         
@@ -1655,28 +1655,40 @@
 ();%0A        
+  
 %7D);%0A
+        %7D%0A
       %7D);%0A  

Update hook.init.js