module.exports = function(grunt) {

	var shared = require("../lib.js").init(grunt);
	var _ = grunt.utils._;

	grunt.registerInitTask("docs", "Generate and push docs to Github Pages", function(target) {
		if (!target) {
			var tasks = [
				"docs:prepare",
				"copy:docs",
				"docs:generate",
				"clean:build"
			];
			if (shared.config("release")) {
				tasks.push("docs:release");
			}
			grunt.task.run(tasks);
			return;
		}
		switch (target) {
			case "prepare":
				var done = this.async();
				checkJSDuckVersion(function(success) {
					if (success === false) {
						done(false);
						return;
					}
					grunt.config("copy.docs", {
						"files": {
							"<%= dirs.build %>": [
								"<%= dirs.src %>/!(backplane).js",
								"<%= dirs.src %>/!(tests|third-party)/**/*.js",
								"<%= dirs.src %>/tests/!(qunit|sinon)/**/*.js",
								"<%= dirs.src %>/third-party/bootstrap/plugins/echo-*.js"
							]
						},
						"options": {
							"basePath": "<config:dirs.src>",
							"processContent": shared.replacePlaceholdersOnCopy
						}
					});
					done();
				});
				break;
			case "generate":
				generate(this.async());
				break;
			case "release":
				release(this.async());
				break;
		}
	});

	function checkJSDuckVersion(done) {
		shared.exec("jsduck --version | awk '{ print $2; }'", function(version) {
			var desiredVersion = "5.0.0";
			var failed = false;
			version = _.trim(version);
			if (!version) {
				failed = true;
				grunt.log.writeln("jsduck is not installed. Install it by running command `" + ("gem install jsduck -v " + desiredVersion).yellow + "`.").cyan;
			} else if (version !== desiredVersion) {
				failed = true;
				grunt.log.writeln("jsduck version is " + version.red + " but must be " + desiredVersion.green + ". Update it by running command `" + ("gem install jsduck -v " + desiredVersion).yellow + "`.").cyan;
			} else {
				grunt.log.ok();
			}
			done(!failed);
		});
	}

	function generate(done) {
		var cmd = "jsduck --config=config/jsduck/config.json";
		if (shared.config("debug")) {
			console.log(cmd);
			done();
			return;
		}
		var path = grunt.config("dirs.dist") + "/docs";
		shared.exec("rm -rf " + path + " && mkdir -p " + path, function() {
			shared.exec(cmd, function() {
				// copy Echo specific images and CSS to documentation directory
				shared.exec("cp -r docs/patch/* " + path, done);
			});
		});
	}

	function release(done) {
		var cmd = [
			"git checkout gh-pages",
			"git pull",
			"cp -r " + grunt.config("dirs.dist") + "/docs/* docs",
			"cp -r " + grunt.config("dirs.dist") + "/tests/* tests",
			"cp -r " + grunt.config("dirs.dist") + "/demo/* demo",
			"git add docs/ tests/ demo/",
			"git commit -m \"up to v" + grunt.config("pkg.version") + "\"",
			"git push origin gh-pages",
			"git checkout master"
		].join(" && ");
		if (shared.config("debug") || shared.config("env") !== "production") {
			console.log(cmd);
			done();
			return;
		}
		shared.exec(cmd, function() {
			grunt.log.ok();
			done();
		});
	}
};

@@ -100,16 +100,53 @@
 utils._;
+%0A%09var desiredJSDuckVersion = %225.0.0%22;
 %0A%0A%09grunt
@@ -1370,41 +1370,8 @@
 ) %7B%0A
-%09%09%09var desiredVersion = %225.0.0%22;%0A
 %09%09%09v
@@ -1567,32 +1567,38 @@
 k -v %22 + desired
+JSDuck
 Version).yellow 
@@ -1636,32 +1636,38 @@
 sion !== desired
+JSDuck
 Version) %7B%0A%09%09%09%09f
@@ -1760,24 +1760,30 @@
  %22 + desired
+JSDuck
 Version.gree
@@ -1858,16 +1858,22 @@
  desired
+JSDuck
 Version)

Move version var to the top to be able to find it easier