// Generated by CoffeeScript 1.3.3

/*
# Omelette Simple Auto Completion for Node
*/


(function() {
  var EventEmitter, Omelette,
    __hasProp = {}.hasOwnProperty,
    __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
    __slice = [].slice;

  EventEmitter = require("events").EventEmitter;

  Omelette = (function(_super) {
    var log;

    __extends(Omelette, _super);

    log = console.log;

    function Omelette() {
      var isZsh;
      this.compgen = process.argv.indexOf("--compgen");
      this.install = process.argv.indexOf("--completion") > -1;
      isZsh = process.argv.indexOf("--compzsh") > -1;
      this.fragment = parseInt(process.argv[this.compgen + 1]) - (isZsh ? 1 : 0);
      this.word = process.argv[this.compgen + 2];
      this.line = process.argv[this.compgen + 3];
    }

    Omelette.prototype.setProgram = function(program) {
      this.program = program;
    };

    Omelette.prototype.setFragments = function() {
      var fragments;
      fragments = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
      this.fragments = fragments;
    };

    Omelette.prototype.generate = function() {
      this.emit("complete", this.fragments[this.fragment - 1], this.word, this.line);
      this.emit(this.fragments[this.fragment - 1], this.word, this.line);
      this.emit("$" + this.fragment, this.word, this.line);
      return process.exit();
    };

    Omelette.prototype.reply = function(words) {
      console.log(words.join("\n"));
      return process.exit();
    };

    Omelette.prototype.checkInstall = function() {
      var completion;
      if (this.install) {
        completion = "_" + this.program + "_complette";
        log("### " + this.program + " completion - begin. generated by omelette ###\nif type compdef &>/dev/null; then\n  " + completion + "() {\n    compadd -- `" + this.program + " --compzsh --compgen \"${CURRENT}\" \"${words[CURRENT-1]}\" \"${BUFFER}\"`\n  }\n  compdef " + completion + " " + this.program + "\nelif type complete &>/dev/null; then\n  " + completion + "() {\n    COMPREPLY=( $(compgen -W '$(" + this.program + " --compbash --compgen \"${COMP_CWORD}\" \"${COMP_WORDS[COMP_CWORD-1]}\" \"${COMP_LINE}\")' -- \"${COMP_WORDS[COMP_CWORD]}\") )\n  }\n  complete -F " + completion + " " + this.program + "\nfi\n### " + this.program + " completion - end ###");
        return process.exit();
      }
    };

    Omelette.prototype.init = function() {
      if (this.compgen > -1) {
        return this.generate();
      }
    };

    return Omelette;

  })(EventEmitter);

  module.exports = function(template) {
    var fragments, program, _omelette, _ref;
    _ref = template.split(/\s+/), program = _ref[0], fragments = 2 <= _ref.length ? __slice.call(_ref, 1) : [];
    fragments = fragments.map(function(fragment) {
      return fragment.replace(/^\<+|\>+$/g, '');
    });
    _omelette = new Omelette;
    _omelette.setProgram(program);
    _omelette.setFragments.apply(_omelette, fragments);
    _omelette.checkInstall();
    return _omelette;
  };

}).call(this);
