// Generated by IcedCoffeeScript 1.8.0-c
(function() {
  var check_cmd, exec, fs, iced, log, npm, os, path, prng, run, strip, __iced_k, __iced_k_noop, _config;

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

  run = require('iced-spawn').run;

  exec = require('child_process').exec;

  path = require('path');

  fs = require('fs');

  log = require('./log');

  prng = require('crypto').prng;

  strip = require('./util').strip;

  os = require('os');

  _config = null;

  exports.set_config = function(c) {
    return _config = c;
  };

  exports.npm = npm = function(_arg, cb) {
    var args, err, name, out, p, ___iced_passed_deferral, __iced_deferrals, __iced_k;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    args = _arg.args;
    name = _config.get_cmd('npm');
    p = _config.install_prefix();
    if ((p != null) && p.length) {
      args = ["--prefix", p].concat(args);
    }
    (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/npm.iced"
        });
        run({
          args: args,
          name: name
        }, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              return out = arguments[1];
            };
          })(),
          lineno: 23
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        return cb(err, out);
      };
    })(this));
  };

  exports.check = check_cmd = 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/npm.iced"
        });
        npm({
          args: ["--version"]
        }, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              return err = arguments[0];
            };
          })(),
          lineno: 29
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        return cb(err);
      };
    })(this));
  };

  exports.test_install = function(cb) {
    var dirname, err, out, r, test, tmp, ___iced_passed_deferral, __iced_deferrals, __iced_k;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    log.debug("+ Installer::test_npm_install");
    dirname = null;
    (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/npm.iced",
          funcname: "test_install"
        });
        _this.npm({
          args: ["get", "prefix"]
        }, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              return out = arguments[1];
            };
          })(),
          lineno: 38
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        (function(__iced_k) {
          if (typeof err !== "undefined" && err !== null) {
            return __iced_k(log.error("Failed to get a prefix from npm"));
          } else {
            dirname = strip(out.toString('utf8'));
            log.debug("| Testing install directory: " + dirname);
            r = prng(10).toString('hex');
            test = path.resolve(dirname, ".keybase_test_install_" + r);
            log.debug("| Writing temporary file, to see if install will work: " + test);
            (function(__iced_k) {
              __iced_deferrals = new iced.Deferrals(__iced_k, {
                parent: ___iced_passed_deferral,
                filename: "/Users/max/src/keybase/node-installer/src/npm.iced",
                funcname: "test_install"
              });
              fs.writeFile(test, new Buffer([]), {
                mode: 0x180
              }, __iced_deferrals.defer({
                assign_fn: (function() {
                  return function() {
                    return err = arguments[0];
                  };
                })(),
                lineno: 47
              }));
              __iced_deferrals._fulfill();
            })(function() {
              (function(__iced_k) {
                var _ref;
                if (typeof err !== "undefined" && err !== null) {
                  return __iced_k((_ref = err.code) === 'EACCES' || _ref === 'EPERM' ? os.platform() === 'win32' ? err = new Error("Permission denied - Node was installed as Admin.\n" + "\nWindows solution: launch another command window by right-clicking" + "\nand selecting \"Run as Administrator\"," + "\nthen run `keybase-installer`. (Then you may close the window.)\n") : err = new Error("Permission denied installing to " + dirname + ": try running `sudo keybase-installer` or use the `--prefix` option to install locally") : err = new Error("Can't write to directory " + dirname + ": " + err.code));
                } else {
                  (function(__iced_k) {
                    __iced_deferrals = new iced.Deferrals(__iced_k, {
                      parent: ___iced_passed_deferral,
                      filename: "/Users/max/src/keybase/node-installer/src/npm.iced",
                      funcname: "test_install"
                    });
                    fs.unlink(test, __iced_deferrals.defer({
                      assign_fn: (function() {
                        return function() {
                          return tmp = arguments[0];
                        };
                      })(),
                      lineno: 60
                    }));
                    __iced_deferrals._fulfill();
                  })(function() {
                    return __iced_k(typeof tmp !== "undefined" && tmp !== null ? log.warn("Failed to unlink temporary file: " + test) : log.debug("| Unlinking file: " + test));
                  });
                }
              })(__iced_k);
            });
          }
        })(function() {
          log.debug("- Installer::test_npm_install");
          return cb(err, dirname);
        });
      };
    })(this));
  };

}).call(this);
