// Generated by IcedCoffeeScript 1.7.1-f
(function() {
  var KeyInstall, KeyUpgrade, chain, fpeq, iced, keyring, log, make_esc, __iced_k, __iced_k_noop;

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

  make_esc = require('iced-error').make_esc;

  keyring = require('gpg-wrapper').keyring;

  fpeq = require('pgp-utils').util.fpeq;

  chain = require('iced-utils').util.chain;

  KeyInstall = require('./key_install').KeyInstall;

  log = require('./log');

  exports.KeyUpgrade = KeyUpgrade = (function() {
    function KeyUpgrade(config) {
      this.config = config;
      this._v = {};
    }

    KeyUpgrade.prototype.fetch = function(cb) {
      var err, res, ___iced_passed_deferral, __iced_deferrals, __iced_k;
      __iced_k = __iced_k_noop;
      ___iced_passed_deferral = iced.findDeferral(arguments);
      (function(_this) {
        return (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/home/max/src/keybase/node-installer/src/key_upgrade.iced",
            funcname: "KeyUpgrade.fetch"
          });
          _this.config.request("/sig/files/" + _this._v.old + "/keyset-" + _this._v["new"] + ".asc", __iced_deferrals.defer({
            assign_fn: (function(__slot_1) {
              return function() {
                err = arguments[0];
                res = arguments[1];
                return __slot_1._sig = arguments[2];
              };
            })(_this),
            lineno: 20
          }));
          __iced_deferrals._fulfill();
        });
      })(this)((function(_this) {
        return function() {
          return cb(err);
        };
      })(this));
    };

    KeyUpgrade.prototype.verify = function(cb) {
      var err, ___iced_passed_deferral, __iced_deferrals, __iced_k;
      __iced_k = __iced_k_noop;
      ___iced_passed_deferral = iced.findDeferral(arguments);
      (function(_this) {
        return (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/home/max/src/keybase/node-installer/src/key_upgrade.iced",
            funcname: "KeyUpgrade.verify"
          });
          _this.config.oneshot_verify({
            which: 'code',
            sig: _this._sig
          }, __iced_deferrals.defer({
            assign_fn: (function(__slot_1) {
              return function() {
                err = arguments[0];
                return __slot_1._keyset = arguments[1];
              };
            })(_this),
            lineno: 26
          }));
          __iced_deferrals._fulfill();
        });
      })(this)((function(_this) {
        return function() {
          return cb(err);
        };
      })(this));
    };

    KeyUpgrade.prototype.install = function(cb) {
      var err, keys, ki, ___iced_passed_deferral, __iced_deferrals, __iced_k;
      __iced_k = __iced_k_noop;
      ___iced_passed_deferral = iced.findDeferral(arguments);
      ki = new KeyInstall(this.config, this._keyset);
      (function(_this) {
        return (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/home/max/src/keybase/node-installer/src/key_upgrade.iced",
            funcname: "KeyUpgrade.install"
          });
          ki.run(__iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                return err = arguments[0];
              };
            })(),
            lineno: 33
          }));
          __iced_deferrals._fulfill();
        });
      })(this)((function(_this) {
        return function() {
          if (typeof err === "undefined" || err === null) {
            keys = ki.keys();
            keys.version = _this._v["new"];
            _this.config.set_keys(keys);
          }
          return cb(err);
        };
      })(this));
    };

    KeyUpgrade.prototype.run = function(cb) {
      var esc, ___iced_passed_deferral, __iced_deferrals, __iced_k;
      __iced_k = __iced_k_noop;
      ___iced_passed_deferral = iced.findDeferral(arguments);
      esc = make_esc(cb, "KeyUpgrade::run");
      (function(_this) {
        return (function(__iced_k) {
          if ((_this._v["new"] = _this.config.index().keys.latest) > (_this._v.old = _this.config.key_version())) {
            log.info("Key upgrade suggested; new version is " + _this._v["new"] + ", but we have " + _this._v.old);
            (function(__iced_k) {
              __iced_deferrals = new iced.Deferrals(__iced_k, {
                parent: ___iced_passed_deferral,
                filename: "/home/max/src/keybase/node-installer/src/key_upgrade.iced",
                funcname: "KeyUpgrade.run"
              });
              _this.fetch(__iced_deferrals.defer({
                lineno: 46
              }));
              __iced_deferrals._fulfill();
            })(function() {
              (function(__iced_k) {
                __iced_deferrals = new iced.Deferrals(__iced_k, {
                  parent: ___iced_passed_deferral,
                  filename: "/home/max/src/keybase/node-installer/src/key_upgrade.iced",
                  funcname: "KeyUpgrade.run"
                });
                _this.verify(esc(__iced_deferrals.defer({
                  lineno: 47
                })));
                __iced_deferrals._fulfill();
              })(function() {
                (function(__iced_k) {
                  __iced_deferrals = new iced.Deferrals(__iced_k, {
                    parent: ___iced_passed_deferral,
                    filename: "/home/max/src/keybase/node-installer/src/key_upgrade.iced",
                    funcname: "KeyUpgrade.run"
                  });
                  _this.install(esc(__iced_deferrals.defer({
                    lineno: 48
                  })));
                  __iced_deferrals._fulfill();
                })(__iced_k);
              });
            });
          } else {
            return __iced_k();
          }
        });
      })(this)((function(_this) {
        return function() {
          return cb(null);
        };
      })(this));
    };

    return KeyUpgrade;

  })();

}).call(this);
