#!/usr/bin/env node

'use strict';

process.title = 'grunt';

var Liftup = require('liftup');
var v8flags = require('v8flags');
var extensions = require('interpret').jsVariants;
var nopt = require('nopt');
var gruntOptions = require('grunt-known-options');
var completion = require('../lib/completion.js');
var info = require('../lib/info.js');
var pkg = require('../package.json');

// Parse `gruntOptions` into a form that nopt can handle.
var aliases = {};
var known = {};

Object.keys(gruntOptions).forEach(function(key) {
  var short = gruntOptions[key].short;
  if (short) {
    aliases[short] = '--' + key;
  }
  known[key] = gruntOptions[key].type;
});

// Parse them and return an options object.
var options = nopt(known, aliases, process.argv, 2);

if ('completion' in options) {
  completion.print(options.completion);
} else if (options.version) {
  console.log('grunt-cli v' + pkg.version);
}

v8flags(function (err, v8flags) {
  var Grunt = new Liftup({
    name: 'grunt',
    configName: 'Gruntfile',
    // Support a number of languages based on file extension
    extensions: extensions,
    // Flags that are v8 flags will be loaded into node instead of Gruntfile
    v8flags: v8flags
  });
  Grunt.prepare({
    cwd: options.base,
    configPath: options.gruntfile,
    require: options.require,
    preload: options.preload,
    verbose: options.verbose
  }, function (env) {
    Grunt.execute(env, function(env) {
      var tasks = options.argv.remain;
      delete options.argv;
      // No grunt install found!
      if (!env.modulePath) {
        if (options.version) {
          process.exit();
        }
        if (options.help) {
          info.help();
        }
        info.fatal('Unable to find local grunt.', 99);
      } else {
        options.gruntfile = env.configPath;
        var grunt = require(env.modulePath);
        grunt.tasks(tasks, options);
      }
    });
  });
});
