// Generated by IcedCoffeeScript 1.7.1-g
(function() {
  var BaseCommand, GPG, GetIndex, Installer, KeySetup, KeyUpgrade, SoftwareUpgrade, chain, check_node_async, colors, constants, find_and_set_cmd, fullname, iced, keyring, log, make_esc, mkdir_p, npm, path, request, __iced_k, __iced_k_noop, _ref, _ref1,
    __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; };

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

  BaseCommand = require('./base').BaseCommand;

  _ref = require('gpg-wrapper'), find_and_set_cmd = _ref.find_and_set_cmd, keyring = _ref.keyring, GPG = _ref.GPG;

  _ref1 = require('iced-error'), chain = _ref1.chain, make_esc = _ref1.make_esc;

  request = require('./request');

  fullname = require('./package').fullname;

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

  KeySetup = require('./key_setup').KeySetup;

  KeyUpgrade = require('./key_upgrade').KeyUpgrade;

  GetIndex = require('./get_index').GetIndex;

  SoftwareUpgrade = require('./software_upgrade').SoftwareUpgrade;

  log = require('./log');

  npm = require('./npm');

  mkdir_p = require('iced-utils').fs.mkdir_p;

  path = require('path');

  colors = require('colors');

  check_node_async = require('badnode').check_node_async;

  exports.Installer = Installer = (function(_super) {
    __extends(Installer, _super);

    function Installer(argv) {
      Installer.__super__.constructor.call(this, argv);
    }

    Installer.prototype.cleanup = function(cb) {
      var e2, ___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-installer/src/installer.iced",
            funcname: "Installer.cleanup"
          });
          _this.config.cleanup(__iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                return e2 = arguments[0];
              };
            })(),
            lineno: 28
          }));
          __iced_deferrals._fulfill();
        });
      })(this)((function(_this) {
        return function() {
          if (typeof e2 !== "undefined" && e2 !== null) {
            log.error("In cleanup: " + e2);
          }
          if ((typeof err === "undefined" || err === null) && (_this["package"] != null)) {
            log.info("Succesful install: " + (_this["package"].filename()));
          }
          return cb();
        };
      })(this));
    };

    Installer.prototype.make_install_dir = function(cb) {
      var err, made, p, ___iced_passed_deferral, __iced_deferrals, __iced_k;
      __iced_k = __iced_k_noop;
      ___iced_passed_deferral = iced.findDeferral(arguments);
      err = null;
      (function(_this) {
        return (function(__iced_k) {
          if (((p = _this.config.install_prefix()) != null) && p.length) {
            (function(__iced_k) {
              __iced_deferrals = new iced.Deferrals(__iced_k, {
                parent: ___iced_passed_deferral,
                filename: "/Users/max/src/keybase/node-installer/src/installer.iced",
                funcname: "Installer.make_install_dir"
              });
              mkdir_p(p, 0x1ed, __iced_deferrals.defer({
                assign_fn: (function() {
                  return function() {
                    err = arguments[0];
                    return made = arguments[1];
                  };
                })(),
                lineno: 39
              }));
              __iced_deferrals._fulfill();
            })(function() {
              return __iced_k((err == null) && made ? log.warn("Created install directory: " + p) : void 0);
            });
          } else {
            return __iced_k();
          }
        });
      })(this)((function(_this) {
        return function() {
          return cb(err);
        };
      })(this));
    };

    Installer.prototype.test_gpg = function(cb) {
      var alt, cmd, err, lines, version, ___iced_passed_deferral, __iced_deferrals, __iced_k;
      __iced_k = __iced_k_noop;
      ___iced_passed_deferral = iced.findDeferral(arguments);
      alt = this.config.set_alt_gpg();
      log.debug("+ Installer::test_gpg");
      (function(_this) {
        return (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/max/src/keybase/node-installer/src/installer.iced",
            funcname: "Installer.test_gpg"
          });
          find_and_set_cmd(alt, __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                version = arguments[1];
                return cmd = arguments[2];
              };
            })(),
            lineno: 49
          }));
          __iced_deferrals._fulfill();
        });
      })(this)((function(_this) {
        return function() {
          if (typeof err !== "undefined" && err !== null) {
            lines = [];
            if (alt != null) {
              lines.push("The GPG command you specified `" + alt + "` wasn't found; see this page for help installing `gpg`:");
            } else {
              lines.push("The commands `gpg2` and `gpg` weren't found; you need to install it. See this page for more info:");
            }
            lines.push("\t   https://keybase.io/docs/command_line/prerequisites");
            err = new Error(lines.join("\n"));
          } else {
            log.debug("| Found '" + cmd + "' @ " + version);
          }
          log.debug("- Installer::test_gpg -> " + (err != null ? 'FAILED' : 'OK'));
          return cb(err);
        };
      })(this));
    };

    Installer.prototype.test_npm = function(cb) {
      var c, cmd, err, ___iced_passed_deferral, __iced_deferrals, __iced_k;
      __iced_k = __iced_k_noop;
      ___iced_passed_deferral = iced.findDeferral(arguments);
      cmd = this.config.get_cmd('npm');
      log.debug("+ Installer::test_npm");
      (function(_this) {
        return (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/max/src/keybase/node-installer/src/installer.iced",
            funcname: "Installer.test_npm"
          });
          npm.check(__iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                return err = arguments[0];
              };
            })(),
            lineno: 74
          }));
          __iced_deferrals._fulfill();
        });
      })(this)((function(_this) {
        return function() {
          if (typeof err === "undefined" || err === null) {

          } else if ((c = _this.config.get_alt_cmd('npm')) != null) {
            err = new Error("The npm command you specified `" + c + "` wasn't found");
          } else {
            err = new Error("Couldn't find an `npm` command in your path");
          }
          log.debug("- Installer::test_npm -> " + (err != null ? 'FAILED' : 'OK'));
          return cb(err);
        };
      })(this));
    };

    Installer.prototype.test_npm_install = 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: "/Users/max/src/keybase/node-installer/src/installer.iced",
            funcname: "Installer.test_npm_install"
          });
          npm.test_install(__iced_deferrals.defer({
            assign_fn: (function(__slot_1) {
              return function() {
                err = arguments[0];
                return __slot_1._install_prefix = arguments[1];
              };
            })(_this),
            lineno: 86
          }));
          __iced_deferrals._fulfill();
        });
      })(this)((function(_this) {
        return function() {
          return cb(err);
        };
      })(this));
    };

    Installer.prototype.welcome_message = function(cb) {
      var cmd, dir;
      dir = path.join(this._install_prefix, "bin");
      cmd = path.join(dir, "keybase");
      console.log("=====================================================================\n\nWelcome to keybase.io!\n\nYou have successfully installed the command-line client to\n\n====>    " + (colors.bold(cmd)) + "    <=======\n\nPlease make sure " + (colors.bold(dir)) + " is in your PATH.\n\nIf you're new to the service run:\n\n     $ keybase signup        # signup for a new account\n     $ keybase push          # to push your public key to the server\n         -- or --\n     $ keybase gen           # generate a new key and push it\n\nIf you already signed up via the Web or another keybase client, try:\n\n     $ keybase login         # establish a session with the server, and pull down keys\n\nOnce you're configured, you can:\n\n     $ keybase prove twitter # prove your twitter identity\n     $ keybase id max        # to identify a friend\n     $ keybase track max     # to track them and write a proof to the server\n\nAnd then attempt crypto actions like enc/dec/verify/sign.  See `keybase --help` for\nmore details.\n");
      return cb(null);
    };

    Installer.prototype.run = function(cb) {
      var esc, ___iced_passed_deferral, __iced_deferrals, __iced_k;
      __iced_k = __iced_k_noop;
      ___iced_passed_deferral = iced.findDeferral(arguments);
      log.debug("+ Installer::run");
      cb = chain(cb, this.cleanup.bind(this));
      esc = make_esc(cb, "Installer::run");
      (function(_this) {
        return (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/max/src/keybase/node-installer/src/installer.iced",
            funcname: "Installer.run"
          });
          check_node_async(null, esc(__iced_deferrals.defer({
            lineno: 135
          })));
          __iced_deferrals._fulfill();
        });
      })(this)((function(_this) {
        return function() {
          (function(__iced_k) {
            __iced_deferrals = new iced.Deferrals(__iced_k, {
              parent: ___iced_passed_deferral,
              filename: "/Users/max/src/keybase/node-installer/src/installer.iced",
              funcname: "Installer.run"
            });
            _this.test_gpg(esc(__iced_deferrals.defer({
              lineno: 136
            })));
            __iced_deferrals._fulfill();
          })(function() {
            _this.config.set_alt_npm();
            npm.set_config(_this.config);
            (function(__iced_k) {
              __iced_deferrals = new iced.Deferrals(__iced_k, {
                parent: ___iced_passed_deferral,
                filename: "/Users/max/src/keybase/node-installer/src/installer.iced",
                funcname: "Installer.run"
              });
              _this.make_install_dir(esc(__iced_deferrals.defer({
                lineno: 141
              })));
              __iced_deferrals._fulfill();
            })(function() {
              (function(__iced_k) {
                __iced_deferrals = new iced.Deferrals(__iced_k, {
                  parent: ___iced_passed_deferral,
                  filename: "/Users/max/src/keybase/node-installer/src/installer.iced",
                  funcname: "Installer.run"
                });
                _this.test_npm(esc(__iced_deferrals.defer({
                  lineno: 142
                })));
                __iced_deferrals._fulfill();
              })(function() {
                (function(__iced_k) {
                  __iced_deferrals = new iced.Deferrals(__iced_k, {
                    parent: ___iced_passed_deferral,
                    filename: "/Users/max/src/keybase/node-installer/src/installer.iced",
                    funcname: "Installer.run"
                  });
                  _this.test_npm_install(esc(__iced_deferrals.defer({
                    lineno: 143
                  })));
                  __iced_deferrals._fulfill();
                })(function() {
                  _this.config.set_actual_prefix(_this._install_prefix);
                  (function(__iced_k) {
                    __iced_deferrals = new iced.Deferrals(__iced_k, {
                      parent: ___iced_passed_deferral,
                      filename: "/Users/max/src/keybase/node-installer/src/installer.iced",
                      funcname: "Installer.run"
                    });
                    _this.config.make_tmpdir(esc(__iced_deferrals.defer({
                      lineno: 147
                    })));
                    __iced_deferrals._fulfill();
                  })(function() {
                    (function(__iced_k) {
                      __iced_deferrals = new iced.Deferrals(__iced_k, {
                        parent: ___iced_passed_deferral,
                        filename: "/Users/max/src/keybase/node-installer/src/installer.iced",
                        funcname: "Installer.run"
                      });
                      _this.config.init_keyring(esc(__iced_deferrals.defer({
                        lineno: 148
                      })));
                      __iced_deferrals._fulfill();
                    })(function() {
                      (function(__iced_k) {
                        __iced_deferrals = new iced.Deferrals(__iced_k, {
                          parent: ___iced_passed_deferral,
                          filename: "/Users/max/src/keybase/node-installer/src/installer.iced",
                          funcname: "Installer.run"
                        });
                        _this.key_setup(esc(__iced_deferrals.defer({
                          lineno: 149
                        })));
                        __iced_deferrals._fulfill();
                      })(function() {
                        (function(__iced_k) {
                          __iced_deferrals = new iced.Deferrals(__iced_k, {
                            parent: ___iced_passed_deferral,
                            filename: "/Users/max/src/keybase/node-installer/src/installer.iced",
                            funcname: "Installer.run"
                          });
                          _this.get_index(esc(__iced_deferrals.defer({
                            lineno: 150
                          })));
                          __iced_deferrals._fulfill();
                        })(function() {
                          (function(__iced_k) {
                            __iced_deferrals = new iced.Deferrals(__iced_k, {
                              parent: ___iced_passed_deferral,
                              filename: "/Users/max/src/keybase/node-installer/src/installer.iced",
                              funcname: "Installer.run"
                            });
                            _this.key_upgrade(esc(__iced_deferrals.defer({
                              lineno: 151
                            })));
                            __iced_deferrals._fulfill();
                          })(function() {
                            (function(__iced_k) {
                              __iced_deferrals = new iced.Deferrals(__iced_k, {
                                parent: ___iced_passed_deferral,
                                filename: "/Users/max/src/keybase/node-installer/src/installer.iced",
                                funcname: "Installer.run"
                              });
                              _this.software_upgrade(esc(__iced_deferrals.defer({
                                lineno: 152
                              })));
                              __iced_deferrals._fulfill();
                            })(function() {
                              (function(__iced_k) {
                                __iced_deferrals = new iced.Deferrals(__iced_k, {
                                  parent: ___iced_passed_deferral,
                                  filename: "/Users/max/src/keybase/node-installer/src/installer.iced",
                                  funcname: "Installer.run"
                                });
                                _this.welcome_message(esc(__iced_deferrals.defer({
                                  lineno: 153
                                })));
                                __iced_deferrals._fulfill();
                              })(function() {
                                log.debug("- Installer::run");
                                return cb(null);
                              });
                            });
                          });
                        });
                      });
                    });
                  });
                });
              });
            });
          });
        };
      })(this));
    };

    Installer.prototype.key_setup = function(cb) {
      return (new KeySetup(this.config)).run(cb);
    };

    Installer.prototype.get_index = function(cb) {
      return (new GetIndex(this.config)).run(cb);
    };

    Installer.prototype.key_upgrade = function(cb) {
      return (new KeyUpgrade(this.config)).run(cb);
    };

    Installer.prototype.software_upgrade = function(cb) {
      return (new SoftwareUpgrade(this.config)).run(cb);
    };

    return Installer;

  })(BaseCommand);

}).call(this);
