var path = require('path');
var spawn = require('child_process').spawn;

commands['start'].prototype.initialize = function(plugin, callback) {
    Bones.plugin.command = this;
    Bones.plugin.children = {};
    process.title = 'tilemill';
    process.once('SIGUSR2', function() {
        _(Bones.plugin.children).chain()
            .pluck('pid')
            .each(function(pid) { process.kill(pid, 'SIGUSR2') });
        process.kill(process.pid, 'SIGUSR2');
    });
    this.child('core');
    this.child('tile');
    callback && callback();
};

commands['start'].prototype.child = function(name) {
    var args = [
        path.resolve(path.join(__dirname + '/../index.js')),
        name
    ];
    // Pass any args set on main process into children as well.
    _(require('optimist').argv).forEach(function(val, key) {
        if (key !== '$0' && key !== '_') {
            args.push('--' + key);
            args.push(val);
        }
    });
    Bones.plugin.children[name] = spawn(process.execPath, args);
    Bones.plugin.children[name].stdout.pipe(process.stdout);
    Bones.plugin.children[name].stderr.pipe(process.stderr);
    Bones.plugin.children[name].once('exit', function(code, signal) {
        if (code === 0) this.child(name);
    }.bind(this));
};


@@ -66,16 +66,324 @@
 spawn;%0A%0A
+// Retrieve args to pass to child process here%0A// prior to Bones filtering out options.%0Avar args = _(require('optimist').argv).chain()%0A    .map(function(val, key) %7B%0A        if (key === '$0') return;%0A        if (key === '_') return;%0A        return '--' + key + '=' + val;%0A    %7D)%0A    .compact()%0A    .value();%0A%0A
 commands
@@ -907,26 +907,69 @@
 ) %7B%0A    
-var args =
+Bones.plugin.children%5Bname%5D = spawn(process.execPath,
  %5B%0A     
@@ -1046,322 +1046,22 @@
    %5D
-;%0A    // Pass any args set on main process into children as well.%0A    _(require('optimist').argv).forEach(function(val, key) %7B%0A        if (key !== '$0' && key !== '_') %7B%0A            args.push('--' + key);%0A            args.push(val);%0A        %7D%0A    %7D);%0A    Bones.plugin.children%5Bname%5D = spawn(process.execPath, 
+.concat(
 args)
+)
 ;%0A  

Fix arg passing to child processes. Refs #1108.