// Generated by IcedCoffeeScript 1.7.1-c
(function() {
  var Child, exec, iced, spawn, __iced_k, __iced_k_noop, _ref;

  iced = require('iced-runtime').iced;
  __iced_k = __iced_k_noop = function() {};

  _ref = require('child_process'), spawn = _ref.spawn, exec = _ref.exec;

  exports.Child = Child = (function() {
    function Child(args, opts) {
      this.args = args;
      this.opts = opts;
      this._startup = null;
      this._exit_cb = null;
      this._exit_code = null;
      this._filter = null;
      this._n_exits = 0;
    }

    Child.prototype._got_data = function(buffer, which) {
      var cb, s, _ref1, _ref2;
      s = buffer.toString();
      if (!((_ref1 = this.opts) != null ? _ref1.quiet : void 0)) {
        process[which].write(s);
      }
      if (typeof this._filter === "function") {
        this._filter(s, which);
      }
      if ((_ref2 = this._startup) != null ? _ref2.check_fn(s, which) : void 0) {
        cb = this._startup.cb;
        cb(true);
        return this._startup = null;
      }
    };

    Child.prototype.filter = function(f) {
      this._filter = f;
      return this;
    };

    Child.prototype.run = function() {
      var interp;
      interp = this.opts.interp || 'iced';
      this.proc = spawn(interp, this.args);
      this._n_exits = 3;
      this.proc.stderr.on('data', (function(_this) {
        return function(buffer) {
          return _this._got_data(buffer, 'stderr');
        };
      })(this));
      this.proc.stdout.on('data', (function(_this) {
        return function(buffer) {
          return _this._got_data(buffer, 'stdout');
        };
      })(this));
      this.proc.stderr.on('end', (function(_this) {
        return function() {
          return _this._got_exit();
        };
      })(this));
      this.proc.stdout.on('end', (function(_this) {
        return function() {
          return _this._got_exit();
        };
      })(this));
      this.proc.on('exit', (function(_this) {
        return function(status) {
          return _this._got_exit(status);
        };
      })(this));
      this.pid = this.proc.pid;
      return this;
    };

    Child.prototype.kill = function(sig) {
      return this.proc.kill(sig);
    };

    Child.prototype.startup_check = function(check_fn, cb) {
      this._startup = {
        check_fn: check_fn,
        cb: cb
      };
      return this;
    };

    Child.prototype.toString = function() {
      return ("'" + (JSON.stringify(this.args)) + "'") + (this.pid != null ? " (pid=" + this.pid + ")" : "");
    };

    Child.prototype._do_restart = function(status) {
      var d, ___iced_passed_deferral, __iced_deferrals, __iced_k, _ref1;
      __iced_k = __iced_k_noop;
      ___iced_passed_deferral = iced.findDeferral(arguments);
      d = (typeof opts !== "undefined" && opts !== null ? (_ref1 = opts.restart) != null ? _ref1.delay : void 0 : void 0) || 5;
      if (typeof opts !== "undefined" && opts !== null) {
        if (typeof opts.logfn === "function") {
          opts.logfn("process " + (this.toString()) + " died w/ status=" + status + "; restart in " + d + "s");
        }
      }
      (function(_this) {
        return (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/max/src/iced/iced-utils/src/spawn.iced",
            funcname: "Child._do_restart"
          });
          setTimeout(__iced_deferrals.defer({
            lineno: 70
          }), d * 1000);
          __iced_deferrals._fulfill();
        });
      })(this)((function(_this) {
        return function() {
          return _this.run();
        };
      })(this));
    };

    Child.prototype._got_exit = function(status) {
      var cb, ecb;
      if (status != null) {
        this._exit_code = status;
        this.proc = null;
        if (this._startup != null) {
          cb = this._startup.cb;
          cb(false);
          this._startup = null;
        }
        this.pid = -1;
      }
      if (--this._n_exits === 0) {
        if ((ecb = this._exit_cb)) {
          this._exit_cb = null;
          ecb(this._exit_code);
        }
        if ((typeof opts !== "undefined" && opts !== null ? opts.restart : void 0) != null) {
          return this._do_restart(status);
        }
      }
    };

    Child.prototype.wait = function(cb) {
      if (this._exit_code && this._n_exits === 0) {
        return cb(this._exit_code);
      } else {
        return this._exit_cb = cb;
      }
    };

    return Child;

  })();

  exports.spawn = function(args, cb, opts) {
    var status, ___iced_passed_deferral, __iced_deferrals, __iced_k;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    if (opts == null) {
      opts = {};
    }
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/max/src/iced/iced-utils/src/spawn.iced",
          funcname: "spawn"
        });
        (new Child(args, opts)).run().wait(__iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              return status = arguments[0];
            };
          })(),
          lineno: 102
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        return cb(status);
      };
    })(this));
  };

}).call(this);
