// Generated by IcedCoffeeScript 1.3.3f
(function() {
  var colorize, diff, fs, iced, tty, __iced_k, __iced_k_noop,
    __slice = [].slice;

  iced = {
    Deferrals: (function() {

      function _Class(_arg) {
        this.continuation = _arg;
        this.count = 1;
        this.ret = null;
      }

      _Class.prototype._fulfill = function() {
        if (!--this.count) return this.continuation(this.ret);
      };

      _Class.prototype.defer = function(defer_params) {
        var _this = this;
        ++this.count;
        return function() {
          var inner_params, _ref;
          inner_params = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
          if (defer_params != null) {
            if ((_ref = defer_params.assign_fn) != null) {
              _ref.apply(null, inner_params);
            }
          }
          return _this._fulfill();
        };
      };

      return _Class;

    })(),
    findDeferral: function() {
      return null;
    }
  };
  __iced_k = __iced_k_noop = function() {};

  fs = require('fs');

  tty = require('tty');

  diff = require('./index').diff;

  colorize = require('./colorize').colorize;

  module.exports = function(argv) {
    var data1, data2, err1, err2, json1, json2, options, result, ___iced_passed_deferral, __iced_deferrals, __iced_k,
      _this = this;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    options = require('dreamopt')(["Usage: json-diff [-vjC] first.json second.json", "Arguments:", "  first.json              Old file #var(file1) #required", "  second.json             New file #var(file2) #required", "General options:", "  -v, --verbose           Output progress info", "  -C, --[no-]color        Colored output", "  -j, --raw-json          Display raw JSON encoding of the diff #var(raw)"], argv);
    if (options.verbose) {
      process.stderr.write("" + (JSON.stringify(options, null, 2)) + "\n");
    }
    if (options.verbose) process.stderr.write("Loading files...\n");
    (function(__iced_k) {
      __iced_deferrals = new iced.Deferrals(__iced_k, {
        parent: ___iced_passed_deferral,
        filename: "lib/cli.iced",
        funcname: "exports"
      });
      fs.readFile(options.file1, 'utf8', __iced_deferrals.defer({
        assign_fn: (function() {
          return function() {
            err1 = arguments[0];
            return data1 = arguments[1];
          };
        })(),
        lineno: 24
      }));
      fs.readFile(options.file2, 'utf8', __iced_deferrals.defer({
        assign_fn: (function() {
          return function() {
            err2 = arguments[0];
            return data2 = arguments[1];
          };
        })(),
        lineno: 25
      }));
      __iced_deferrals._fulfill();
    })(function() {
      var _ref;
      if (err1) throw err1;
      if (err2) throw err2;
      if (options.verbose) process.stderr.write("Parsing old file...\n");
      json1 = JSON.parse(data1);
      if (options.verbose) process.stderr.write("Parsing new file...\n");
      json2 = JSON.parse(data2);
      if (options.verbose) process.stderr.write("Running diff...\n");
      result = diff(json1, json2);
      if ((_ref = options.color) == null) {
        options.color = tty.isatty(process.stdout.fd);
      }
      if (options.raw) {
        if (options.verbose) process.stderr.write("Serializing JSON output...\n");
        return process.stdout.write(JSON.stringify(result, null, 2));
      } else {
        if (options.verbose) process.stderr.write("Producing colored output...\n");
        return process.stdout.write(colorize(result, {
          color: options.color
        }));
      }
    });
  };

}).call(this);
