// Generated by IcedCoffeeScript 1.7.1-c
(function() {
  var PasswordManager, crypto, iced, log, read, __iced_k, __iced_k_noop;

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

  read = require('read');

  log = require('./log');

  crypto = require('crypto');

  exports.PasswordManager = PasswordManager = (function() {
    function PasswordManager() {}

    PasswordManager.prototype.init = function(opts) {
      this.opts = opts;
      return true;
    };

    PasswordManager.prototype.get_opts = function() {
      return this.opts;
    };

    PasswordManager.prototype._prompt_1 = function(prompt, 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: "/Users/max/src/keybase/node-client/src/pw.iced",
            funcname: "PasswordManager._prompt_1"
          });
          read({
            prompt: "" + prompt + "> ",
            silent: true
          }, __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return res = arguments[1];
              };
            })(),
            lineno: 24
          }));
          __iced_deferrals._fulfill();
        });
      })(this)((function(_this) {
        return function() {
          if (err) {
            log.error("In prompt: " + err);
            res = null;
          } else if (res != null) {
            res = (res.split(/\s+/)).join('');
            if (res.length === 0) {
              res = null;
            }
          }
          return cb(res);
        };
      })(this));
    };

    PasswordManager.prototype.prompt_for_old_pw = function(cb) {
      var pw, ___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: "/Users/max/src/keybase/node-client/src/pw.iced",
            funcname: "PasswordManager.prompt_for_old_pw"
          });
          _this._prompt_1('password', __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                return pw = arguments[0];
              };
            })(),
            lineno: 36
          }));
          __iced_deferrals._fulfill();
        });
      })(this)((function(_this) {
        return function() {
          return cb(pw);
        };
      })(this));
    };

    PasswordManager.prototype.prompt_for_pw = function(is_new, cb) {
      if (is_new) {
        return this.prompt_for_new_pw(cb);
      } else {
        return this.prompt_for_old_pw(cb);
      }
    };

    PasswordManager.prototype.prompt_for_new_pw = function(cb) {
      var go, pw1, pw2, res, ___iced_passed_deferral, __iced_deferrals, __iced_k;
      __iced_k = __iced_k_noop;
      ___iced_passed_deferral = iced.findDeferral(arguments);
      go = true;
      res = null;
      (function(_this) {
        return (function(__iced_k) {
          var _results, _while;
          _results = [];
          _while = function(__iced_k) {
            var _break, _continue, _next;
            _break = function() {
              return __iced_k(_results);
            };
            _continue = function() {
              return iced.trampoline(function() {
                return _while(__iced_k);
              });
            };
            _next = function(__iced_next_arg) {
              _results.push(__iced_next_arg);
              return _continue();
            };
            if (!(go && !res)) {
              return _break();
            } else {
              (function(__iced_k) {
                __iced_deferrals = new iced.Deferrals(__iced_k, {
                  parent: ___iced_passed_deferral,
                  filename: "/Users/max/src/keybase/node-client/src/pw.iced",
                  funcname: "PasswordManager.prompt_for_new_pw"
                });
                _this._prompt_1('passwrd', __iced_deferrals.defer({
                  assign_fn: (function() {
                    return function() {
                      return pw1 = arguments[0];
                    };
                  })(),
                  lineno: 50
                }));
                __iced_deferrals._fulfill();
              })(function() {
                (function(__iced_k) {
                  if (typeof pw1 !== "undefined" && pw1 !== null) {
                    (function(__iced_k) {
                      __iced_deferrals = new iced.Deferrals(__iced_k, {
                        parent: ___iced_passed_deferral,
                        filename: "/Users/max/src/keybase/node-client/src/pw.iced",
                        funcname: "PasswordManager.prompt_for_new_pw"
                      });
                      _this._prompt_1('confirm', __iced_deferrals.defer({
                        assign_fn: (function() {
                          return function() {
                            return pw2 = arguments[0];
                          };
                        })(),
                        lineno: 52
                      }));
                      __iced_deferrals._fulfill();
                    })(function() {
                      return __iced_k(pw1 === pw2 ? res = pw1 : log.warn("Password didn't match"));
                    });
                  } else {
                    return __iced_k(go = false);
                  }
                })(_next);
              });
            }
          };
          _while(__iced_k);
        });
      })(this)((function(_this) {
        return function() {
          return cb(res);
        };
      })(this));
    };

    PasswordManager.prototype.get_password = function(is_new, cb) {
      var pw, ___iced_passed_deferral, __iced_deferrals, __iced_k;
      __iced_k = __iced_k_noop;
      ___iced_passed_deferral = iced.findDeferral(arguments);
      (function(_this) {
        return (function(__iced_k) {
          if (_this._pw == null) {
            (function(__iced_k) {
              if (((pw = _this.opts.password) == null) && _this.opts.interactive && !_this.opts.bg) {
                (function(__iced_k) {
                  __iced_deferrals = new iced.Deferrals(__iced_k, {
                    parent: ___iced_passed_deferral,
                    filename: "/Users/max/src/keybase/node-client/src/pw.iced",
                    funcname: "PasswordManager.get_password"
                  });
                  _this.prompt_for_pw(is_new, __iced_deferrals.defer({
                    assign_fn: (function() {
                      return function() {
                        return pw = arguments[0];
                      };
                    })(),
                    lineno: 64
                  }));
                  __iced_deferrals._fulfill();
                })(__iced_k);
              } else {
                return __iced_k();
              }
            })(function() {
              return __iced_k(_this._pw = pw);
            });
          } else {
            return __iced_k();
          }
        });
      })(this)((function(_this) {
        return function() {
          return cb(_this._pw);
        };
      })(this));
    };

    return PasswordManager;

  })();

}).call(this);
