'use strict';

var fs = require('fs');
var childProcess = require('child_process');
var crypto = require('crypto');
var q = require('q');
var tmp = require('tmp');

module.exports = {

    openJSONInEditor: function(json) {
        function checksum (str) {
            return crypto
                .createHash('md5')
                .update(str, 'utf8')
                .digest('hex');
        }

        var deferred = q.defer();
        var tmpobj = tmp.fileSync();
        var jsonString = JSON.stringify(json, null, 4);
        var md5 = checksum(jsonString);

        // write to temp file
        fs.writeFileSync(tmpobj.name, jsonString);

        // attempt to open editor
        if (!process.env.EDITOR) {
            deferred.reject('EDITOR environment variable must be specified');
        }
        var editorPath = process.env.EDITOR + ' ' + tmpobj.name;
        var editor = childProcess.exec(editorPath, function (err) {
            if (err) {
                deferred.reject('Error executing ' + editorPath);
            }
        });

        // wait for editor to close and resolve if exit code is 0
        editor.on('exit', function (code) {
            if (code === 0) {
                var editedJSON = JSON.parse(
                    fs.readFileSync(tmpobj.name)
                );
                if (checksum(JSON.stringify(editedJSON, null, 4)) !== md5) {
                    deferred.resolve(editedJSON);
                } else {
                    deferred.reject('Data not changed');
                }
            } else {
                deferred.reject('Editor process exited with code ' + code);
            }
        });

        return deferred.promise;
    }

};

@@ -646,42 +646,8 @@
 );%0A%0A
-        // attempt to open editor%0A
     
@@ -673,24 +673,24 @@
 v.EDITOR) %7B%0A
+
             
@@ -756,32 +756,105 @@
 ied');%0A        %7D
+%0A%0A        // extract any command line arguments specified for the command
 %0A        var edi
@@ -856,20 +856,22 @@
 r editor
-Path
+Tokens
  = proce
@@ -887,17 +887,174 @@
 ITOR
- + ' ' + 
+.split(/%5Cs/);%0A        var editorCommand = editorTokens%5B0%5D;%0A        var editorArgs = (editorTokens.length === 1) ? %5B%5D : editorTokens.slice(1);%0A        editorArgs.push(
 tmpo
@@ -1064,59 +1064,233 @@
 name
+)
 ;%0A
-        var editor = childProcess.exec(editorPath
+%0A        // attempt to open editor%0A        var editor = childProcess.spawn(editorCommand, editorArgs, %7B%0A            stdio: 'inherit'%0A        %7D);%0A%0A        // reject promise if editor fails to start%0A        editor.on('error'
 , fu
@@ -1295,17 +1295,16 @@
 function
- 
 (err) %7B%0A
@@ -1319,23 +1319,28 @@
     
-if (err) %7B%0A    
+console.error(err);%0A
     
@@ -1374,13 +1374,12 @@
 ror 
-execu
+star
 ting
@@ -1393,28 +1393,17 @@
 itor
-Path);%0A            %7D
+Command);
 %0A   
@@ -1478,16 +1478,16 @@
 de is 0%0A
+
         
@@ -1512,17 +1512,16 @@
 function
- 
 (code) %7B

edit commands should redirect stdout/stdin for teminal EDITOR processes