/*
 * grunt-dojo
 * https://github.com/phated/grunt-dojo
 *
 * Copyright (c) 2013 Blaine Bublitz
 * Licensed under the MIT license.
 */

module.exports = function(grunt) {

  'use strict';

  grunt.registerMultiTask('dojo', 'build dojo by spawning a child process', function(){

    var done = this.async();

    var options = this.options({
      dojo: null,
      load: 'build',
      profile: null,
      package: null,
      packages: null,
      require: null,
      requires: null,
      releaseDir: null,
      cwd: null,
      dojoConfig: null,
      basePath: null
    });

    grunt.log.subhead('Building Dojo...');

    var args = [];
    /*
     * Add parameter(s) to the argument list and log them, if in verbose mode.
     */
    var addParam = function() {
        grunt.verbose.write("\t");
        grunt.util._.forEach(arguments, function(param) {
            args.push(param);
            grunt.verbose.write(param +" ");
        });
        grunt.verbose.write("\n");
    };

    if(options.dojo){
      grunt.verbose.writeln("Dojo build parameters:");
      addParam(options.dojo);
      addParam('load=' + options.load);

      if(options.basePath){
        addParam('--basePath', options.basePath);
      }
      if(options.profile){
        addParam('--profile', options.profile);
      }

      /*
       * Support both the singular and plural form of the 'package' and 'require' parameters
       */
      ['package', 'require'].forEach(function(dojoParam){
          if(!Array.isArray(options[dojoParam+'s'])) {
            options[dojoParam+'s'] = [];
          }
          if(options[dojoParam]){
            options[dojoParam+'s'].push(options[dojoParam]);
          }
          options[dojoParam+'s'].forEach(function(paramValue){
           addParam('--'+dojoParam, paramValue);
          });
      });


      if(options.dojoConfig){
       addParam('--dojoConfig', options.dojoConfig);
      }

      if(options.releaseDir){
       addParam('--releaseDir', options.releaseDir);
      }
    } else {
      grunt.log.error('No dojo specified');
      done(false);
    }

    var opts = {};
    if(options.cwd){
      opts.cwd = options.cwd;
    }

    var child = grunt.util.spawn({
      cmd: 'node',
      args: args,
      opts: opts
    }, function(err, result){
      if(err){
        grunt.log.error(err);
        return done(false);
      }

      grunt.log.success('Dojo Successfully Built...');

      done();
    });

    child.stdout.on('data', function (data) {
      grunt.verbose.write(data);
    });
    child.stderr.on('data', function (data) {
      grunt.verbose.error(data);
    });
  });

};

@@ -310,247 +310,1826 @@
     
-var options = this.options(%7B%0A      dojo: null,%0A      load: 'build',%0A      profile: null,%0A      package: null,%0A      packages: null,%0A      require: null,%0A      requires: null,%0A      releaseDir: null,%0A      cwd: null,%0A      dojoConfig: null,
+/**%0A     * @namespace defaults%0A     */%0A    var options = this.options(%7B%0A      /**%0A       * Path to dojo.js file in Dojo source%0A       * @type %7BString%7D%0A       * @memberOf defaults%0A       * @default%0A       */%0A      dojo: null,%0A      /**%0A       * Utility to bootstrap%0A       * @type %7BString=%7D%0A       * @memberOf defaults%0A       * @default%0A       */%0A      load: 'build',%0A      /**%0A       * Profile for the build%0A       * @type %7BString=%7D%0A       * @memberOf defaults%0A       * @default%0A       */%0A      profile: null,%0A      /**%0A       * Location to search for package.json%0A       * @type %7BString=%7D%0A       * @memberOf defaults%0A       * @default%0A       */%0A      package: null,%0A      /**%0A       * Array of locations to search for package.json files%0A       * @type %7BArray=%7D%0A       * @memberOf defaults%0A       * @default%0A       */%0A      packages: null,%0A      /**%0A       * Module to require for the build%0A       * @type %7BString=%7D%0A       * @memberOf defaults%0A       * @default%0A       */%0A      require: null,%0A      /**%0A       * Array of modules to require for the build%0A       * @type %7BArray=%7D%0A       * @memberOf defaults%0A       * @default%0A       */%0A      requires: null,%0A      /**%0A       * Release directory for the build%0A       * @type %7BString=%7D%0A       * @memberOf defaults%0A       * @default%0A       */%0A      releaseDir: null,%0A      /**%0A       * Directory to execute build within%0A       * @type %7BString=%7D%0A       * @memberOf defaults%0A       * @default%0A       */%0A      cwd: null,%0A      /**%0A       * Location of dojoConfig to be used in build%0A       * @type %7BString=%7D%0A       * @memberOf defaults%0A       * @default%0A       */%0A      dojoConfig: null,%0A      /**%0A       * Base Path to pass at the command line%0A       * Takes precedence over all other basePaths%0A       * @type %7BString=%7D%0A       * @memberOf defaults%0A       * @default%0A       */
 %0A   

add jsdoc to options