/*******************************************************************************
 * @license
 * Copyright (c) 2012 VMware, Inc. All Rights Reserved.
 * THIS FILE IS PROVIDED UNDER THE TERMS OF THE ECLIPSE PUBLIC LICENSE
 * ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS FILE
 * CONSTITUTES RECIPIENTS ACCEPTANCE OF THE AGREEMENT.
 * You can obtain a current copy of the Eclipse Public License from
 * http://www.opensource.org/licenses/eclipse-1.0.php
 *
 * Contributors:
 *     Kris De Volder - initial API and implementation
 ******************************************************************************/
 
/*global console require*/

var servlets = require('../servlets');
var makeRequestHandler = require('./servlet-utils').makeRequestHandler;

var cpExec = require('child_process').exec;

function exec(cmd, callback, errback) {

	//cmd looks like this:
	// {cmd: "ls -la", ...other options to pass to the nodejs exec function...}

	var options = cmd;
	cmd = cmd.cmd;

	/*var process = */cpExec(cmd, options, callback);
}
exec.remoteType = ['JSON', 'callback'];
servlets.register('/exec', makeRequestHandler(exec));
/*******************************************************************************
 * @license
 * Copyright (c) 2012 VMware, Inc. All Rights Reserved.
 * THIS FILE IS PROVIDED UNDER THE TERMS OF THE ECLIPSE PUBLIC LICENSE
 * ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS FILE
 * CONSTITUTES RECIPIENTS ACCEPTANCE OF THE AGREEMENT.
 * You can obtain a current copy of the Eclipse Public License from
 * http://www.opensource.org/licenses/eclipse-1.0.php
 *
 * Contributors:
 *     Kris De Volder - initial API and implementation
 ******************************************************************************/

/*global console require*/

var servlets = require('../servlets');
var makeRequestHandler = require('./servlet-utils').makeRequestHandler;

var cpExec = require('child_process').exec;

function exec(cmd, callback, errback) {

	//cmd looks like this:
	// {cmd: "ls -la", ...other options to pass to the nodejs exec function...}

	var options = cmd;
	cmd = cmd.cmd;

	// TODO use mixin, if scipted had one...
	// options.env = mixin({}, options.env, process.env);
	options.env = options.env || {};
	for (var env in process.env) {
		if (!(env in options.env)) {
			options.env[env] = process.env[env];
		}
	}

	/*var process = */cpExec(cmd, options, callback);
}
exec.remoteType = ['JSON', 'callback'];
servlets.register('/exec', makeRequestHandler(exec));
Allow exec 'env' to inherit from 'process.env'
Allow exec 'env' to inherit from 'process.env'

When executing a command Node uses `process.env` by default. If the 'env'
option is specified, Node assumes that the complete environment is
provided.

As Scripted exec configurations are declarative, and it's practically
impossible to correctly configure an environment declaratively, Scripted
should mix the declared env vars into the 'process.env'.

For example, this is my .scripted config:
"exec": {
  "onKeys": {
    "cmd+b": {
      "cmd": "npm test",
      "cwd": "${projectDir}",
      "name": "Test Project",
      "env": {
        "BUSTER_TEST_OPT": "--color none --reporter specification"
      }
    }
  }
}

Before this patch, Node is unable to spawn the child process because the
environment is incomplete.  After this patch, it's able to run the command
with 'process.env' as the environment, augmented with 'BUSTER_TEST_OPT'.
