var initialized = false;
var data = {
	// if this value is true no actual release will be performed,
	// to enable debug mode execute `grunt release -d=1`
	"debug": false,
	// execute `grunt release --env=production` for actual release
	"env": "dev",
	// flag is set to true when we execute release task
	"release": false
};
var child_process = require("child_process");

exports.init = function(grunt) {
	"use strict";

	var exports = {};
	var _ = grunt.utils._;

	exports.config = function(key, value) {
		if (typeof arguments[1] !== "undefined") {
			return grunt.utils.namespace.set(data, key, value);
		} else {
			return grunt.utils.namespace.get(data, key);
		}
	};

	// readOptionalJSON by Ben Alman (https://gist.github.com/2876125)
	exports.readOptionalJSON = function(filepath) {
		var data = {};
		try {
			data = grunt.file.readJSON(filepath);
			grunt.log.write("Reading data from " + filepath + "...").ok();
		} catch(e) {}
		return data;
	};

	exports.exec = function(command, callback) {
		grunt.log.subhead(command.yellow);
		child_process.exec(command, function(err, stdout, stderr) {
			if (err) {
				grunt.fail.fatal(err);
			}
			if (stderr) {
				grunt.log.writeln(stderr);
			}
			callback(stdout, stderr);
		});
	};

	exports.checkVersions = function(versions, done) {
		var self = this;
		var failed = false;
		this.exec("grunt --version | awk '{ print $2; }'", function(version) {
			version = _.trim(version);
			if (version < versions.grunt) {
				failed = true;
				grunt.log.writeln("grunt version is " + version.red + " but must be at least " + versions.grunt.green + ". Update it by running command `" + "sudo npm update -g grunt".yellow + "`.");
			} else {
				grunt.log.ok();
			}
			self.exec("command -v jsduck", function(stdout) {
				// check if jsduck is installed
				if (!_.trim(stdout)) {
					grunt.log.writeln("jsduck is not installed so you won't be able to generate docs. See README for instructions.".yellow);
					done();
					return;
				}
				self.exec("jsduck --version | awk '{ print $2; }'", function(version) {
					version = _.trim(version);
					if (version < versions.jsduck) {
						failed = true;
						grunt.log.writeln("jsduck version is " + version.red + " but must be at least " + versions.jsduck.green + ". Update it by running command `" + "sudo gem update jsduck".yellow + "`.").cyan;
					} else {
						grunt.log.ok();
					}
					done(!failed);
				});
			});
		});
	};

	if (!initialized) {
		initialized = true;

		grunt.cli.optlist.env = {
			"info": "Specify working environment. Some actions might be skipped or added during build process. Possible values are: \"production\", \"staging\", \"test\", \"dev\".",
			"type": String
		};


		exports.config("debug", !!grunt.option("debug"));
		var env = grunt.option("env");
		if (!_.contains(["production", "staging", "test", "dev"], env)) {
			env = "dev";
		}
		exports.config("env", env);
		grunt.log.writeln("");
		grunt.log.writeln("DEBUG mode is " + (exports.config("debug") ? "ON".green : "OFF".red));
		grunt.log.writeln("Working in the " + (env === "production" ? "PRODUCTION".red : env.toUpperCase().green) + " environment");
		grunt.log.writeln("");
	}

	return exports;
};

@@ -1715,283 +1715,8 @@
 %09%09%7D%0A
-%09%09%09self.exec(%22command -v jsduck%22, function(stdout) %7B%0A%09%09%09%09// check if jsduck is installed%0A%09%09%09%09if (!_.trim(stdout)) %7B%0A%09%09%09%09%09grunt.log.writeln(%22jsduck is not installed so you won't be able to generate docs. See README for instructions.%22.yellow);%0A%09%09%09%09%09done();%0A%09%09%09%09%09return;%0A%09%09%09%09%7D%0A%09
 %09%09%09s
@@ -1782,25 +1782,24 @@
 (version) %7B%0A
-%09
 %09%09%09%09version 
@@ -1821,17 +1821,16 @@
 n);%0A%09%09%09%09
-%09
 if (vers
@@ -1859,17 +1859,16 @@
  %7B%0A%09%09%09%09%09
-%09
 failed =
@@ -1874,17 +1874,16 @@
 = true;%0A
-%09
 %09%09%09%09%09gru
@@ -2072,17 +2072,16 @@
 an;%0A%09%09%09%09
-%09
 %7D else %7B
@@ -2081,17 +2081,16 @@
  else %7B%0A
-%09
 %09%09%09%09%09gru
@@ -2102,17 +2102,16 @@
 g.ok();%0A
-%09
 %09%09%09%09%7D%0A%09%09
@@ -2112,17 +2112,16 @@
 %09%09%7D%0A%09%09%09%09
-%09
 done(!fa
@@ -2131,16 +2131,8 @@
 d);%0A
-%09%09%09%09%7D);%0A
 %09%09%09%7D

Revert "Check if jsduck is installed. Not everyone needs it to build the project"