'use strict';

var spawn = require('child_process').spawn,
    exec = require('child_process').exec,
    fs = require('fs'),
    os = require('os');

function isWindows () {
    return os.platform().indexOf('win') === 0;
}

module.exports = function (grunt) {

    function start (mbPath, args, done) {
        var command = mbPath,
            mb;

        if (!fs.existsSync(mbPath)) {
            grunt.fail.warn('No such file: ' + mbPath);
        }

        if (isWindows()) {
            args.unshift(mbPath);

            if (mbPath.indexOf('.cmd') >= 0) {
                // Accommodate the self-contained Windows zip files that ship with mountebank
                args.unshift('/c');
                command = 'cmd';
            }
            else {
                command = 'node';
            }
        }

        grunt.log.writeln(command + ' ' + args.join(' '));
        mb = spawn(command, args);

        mb.on('error', function (error) {
            throw error;
        });
        mb.stderr.on('data', function (data) {
            grunt.log.error(data.toString('utf8'));
        });
        mb.stdout.on('data', function (data) {
            // Looking for "mountebank va.b.c (node vx.y.z) now taking orders..."
            if (data.toString('utf8').indexOf('now taking orders') > 0) {
                done();
            }
        });
    }

    function stop (mbPath, args, done) {
        var command = mbPath + ' ' + args.join(' ');

        if (!fs.existsSync(mbPath)) {
            grunt.fail.warn('No such file: ' + mbPath);
        }

        if (isWindows() && mbPath.indexOf('.cmd') < 0) {
            command = 'node ' + command;
        }
        grunt.log.writeln(command);
        exec(command, done);
    }

    grunt.registerMultiTask('mb', 'start or stop mountebank', function () {
        var done = this.async(),
            command = this.target || 'start',
            args = [command].concat(this.data),
            options = this.options({
                path: 'mb',
                pathEnvironmentVariable: ''
            }),
            mbPath = process.env[options.pathEnvironmentVariable] || options.path;

        if (command === 'start' || command === 'restart') {
            start(mbPath, args, done);
        }
        else if (command === 'stop') {
            stop(mbPath, args, done);
        }
        else {
            grunt.fail.warn('Unrecognized mb target.  Valid targets are start, stop, and restart');
        }
    });
};

@@ -98,32 +98,8 @@
 ec,%0A
-    fs = require('fs'),%0A
     
@@ -118,16 +118,16 @@
 ('os');%0A
+
 %0Afunctio
@@ -323,113 +323,8 @@
 b;%0A%0A
-        if (!fs.existsSync(mbPath)) %7B%0A            grunt.fail.warn('No such file: ' + mbPath);%0A        %7D%0A%0A
     
@@ -836,19 +836,30 @@
     
-throw 
+grunt.fail.warn(
 error
+)
 ;%0A  
@@ -1338,113 +1338,8 @@
 );%0A%0A
-        if (!fs.existsSync(mbPath)) %7B%0A            grunt.fail.warn('No such file: ' + mbPath);%0A        %7D%0A%0A
     
@@ -1500,20 +1500,239 @@
 ommand, 
-done
+function (error, stdout, stderr) %7B%0A            if (error) %7B grunt.fail.warn(error); %7D%0A            if (stdout) %7B grunt.log.writeln(stdout); %7D%0A            if (stderr) %7B grunt.log.error(stderr); %7D%0A            done();%0A        %7D
 );%0A    %7D

Fix running mb if it's on the PATH