text: // Generated by CoffeeScript 1.3.3
  (function() {
    var Configuration, Logger, async, compilePattern, fs, getFilenamesForHost, getUserEnv, libraryPath, mkdirp, path, rstat, sourceScriptEnv,
      __slice = [].slice;
  
    fs = require("fs");
  
    path = require("path");
  
    async = require("async");
  
    Logger = require("./logger");
  
    mkdirp = require("./util").mkdirp;
  
    sourceScriptEnv = require("./util").sourceScriptEnv;
  
    getUserEnv = require("./util").getUserEnv;
  
    module.exports = Configuration = (function() {
  
      Configuration.userConfigurationPath = path.join(process.env.HOME, ".powconfig");
  
      Configuration.loadUserConfigurationEnvironment = function(callback) {
        var _this = this;
        return getUserEnv(function(err, env) {
          var p;
          if (err) {
            return callback(err);
          } else {
            return path.exists(p = _this.userConfigurationPath, function(exists) {
              if (exists) {
                return sourceScriptEnv(p, env, callback);
              } else {
                return callback(null, env);
              }
            });
          }
        });
      };
  
      Configuration.getUserConfiguration = function(callback) {
        return this.loadUserConfigurationEnvironment(function(err, env) {
          if (err) {
            return callback(err);
          } else {
            return callback(null, new Configuration(env));
          }
        });
      };
  
      Configuration.optionNames = ["bin", "dstPort", "httpPort", "dnsPort", "timeout", "workers", "domains", "extDomains", "hostRoot", "logRoot", "rvmPath"];
  
      function Configuration(env) {
        if (env == null) {
          env = process.env;
        }
        this.loggers = {};
        this.initialize(env);
      }
  
      Configuration.prototype.initialize = function(env) {
        var _base, _base1, _ref, _ref1, _ref10, _ref11, _ref12, _ref13, _ref2, _ref3, _ref4, _ref5, _ref6, _ref7, _ref8, _ref9;
        this.env = env;
        this.bin = (_ref = env.POW_BIN) != null ? _ref : path.join(__dirname, "../bin/pow");
        this.dstPort = (_ref1 = env.POW_DST_PORT) != null ? _ref1 : 80;
        this.httpPort = (_ref2 = env.POW_HTTP_PORT) != null ? _ref2 : 20559;
        this.dnsPort = (_ref3 = env.POW_DNS_PORT) != null ? _ref3 : 20560;
        this.timeout = (_ref4 = env.POW_TIMEOUT) != null ? _ref4 : 15 * 60;
        this.workers = (_ref5 = env.POW_WORKERS) != null ? _ref5 : 2;
        this.domains = (_ref6 = (_ref7 = env.POW_DOMAINS) != null ? _ref7 : env.POW_DOMAIN) != null ? _ref6 : "dev";
        this.extDomains = (_ref8 = env.POW_EXT_DOMAINS) != null ? _ref8 : [];
        this.domains = (_ref9 = typeof (_base = this.domains).split === "function" ? _base.split(",") : void 0) != null ? _ref9 : this.domains;
        this.extDomains = (_ref10 = typeof (_base1 = this.extDomains).split === "function" ? _base1.split(",") : void 0) != null ? _ref10 : this.extDomains;
        this.allDomains = this.domains.concat(this.extDomains);
        this.allDomains.push(/\d+\.\d+\.\d+\.\d+\.xip\.io$/, /[0-9a-z]{1,7}\.xip\.io$/);
        this.supportRoot = libraryPath("Application Support", "Pow");
        this.hostRoot = (_ref11 = env.POW_HOST_ROOT) != null ? _ref11 : path.join(this.supportRoot, "Hosts");
        this.logRoot = (_ref12 = env.POW_LOG_ROOT) != null ? _ref12 : libraryPath("Logs", "Pow");
        this.rvmPath = (_ref13 = env.POW_RVM_PATH) != null ? _ref13 : path.join(process.env.HOME, ".rvm/scripts/rvm");
        this.dnsDomainPattern = compilePattern(this.domains);
        return this.httpDomainPattern = compilePattern(this.allDomains);
      };
  
      Configuration.prototype.toJSON = function() {
        var key, result, _i, _len, _ref;
        result = {};
        _ref = this.constructor.optionNames;
        for (_i = 0, _len = _ref.length; _i < _len; _i++) {
          key = _ref[_i];
          result[key] = this[key];
        }
        return result;
      };
  
      Configuration.prototype.getLogger = function(name) {
        var _base;
        return (_base = this.loggers)[name] || (_base[name] = new Logger(path.join(this.logRoot, name + ".log")));
      };
  
      Configuration.prototype.disableRvmDeprecationNotices = function() {
        return fs.writeFile(path.join(this.supportRoot, ".disableRvmDeprecationNotices"), "");
      };
  
      Configuration.prototype.enableRvmDeprecationNotices = function() {
        return fs.unlink(path.join(this.supportRoot, ".disableRvmDeprecationNotices"));
      };
  
      Configuration.prototype.findHostConfiguration = function(host, callback) {
        var _this = this;
        if (host == null) {
          host = "";
        }
        return this.gatherHostConfigurations(function(err, hosts) {
          var config, domain, file, _i, _j, _len, _len1, _ref, _ref1;
          if (err) {
            return callback(err);
          }
          _ref = _this.allDomains;
          for (_i = 0, _len = _ref.length; _i < _len; _i++) {
            domain = _ref[_i];
            _ref1 = getFilenamesForHost(host, domain);
            for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
              file = _ref1[_j];
              if (config = hosts[file]) {
                return callback(null, domain, config);
              }
            }
          }
          if (config = hosts["default"]) {
            return callback(null, _this.allDomains[0], config);
          }
          return callback(null);
        });
      };
  
      Configuration.prototype.gatherHostConfigurations = function(callback) {
        var hosts,
          _this = this;
        hosts = {};
        return mkdirp(this.hostRoot, function(err) {
          if (err) {
            return callback(err);
          }
          return fs.readdir(_this.hostRoot, function(err, files) {
            if (err) {
              return callback(err);
            }
            return async.forEach(files, function(file, next) {
              var name, root;
              root = path.join(_this.hostRoot, file);
              name = file.toLowerCase();
              return rstat(root, function(err, stats, path) {
                if (stats != null ? stats.isDirectory() : void 0) {
                  hosts[name] = {
                    root: path
                  };
                  return next();
                } else if (stats != null ? stats.isFile() : void 0) {
                  return fs.readFile(path, 'utf-8', function(err, data) {
                    if (err) {
                      return next();
                    }
                    data = data.trim();
                    if (data.length < 10 && !isNaN(parseInt(data))) {
                      hosts[name] = {
                        url: "http://localhost:" + (parseInt(data))
                      };
                    } else if (data.match("https?://")) {
                      hosts[name] = {
                        url: data
                      };
                    }
                    return next();
                  });
                } else {
                  return next();
                }
              });
            }, function(err) {
              return callback(err, hosts);
            });
          });
        });
      };
  
      return Configuration;
  
    })();
  
    libraryPath = function() {
      var args;
      args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
      return path.join.apply(path, [process.env.HOME, "Library"].concat(__slice.call(args)));
    };
  
    getFilenamesForHost = function(host, domain) {
      var i, length, parts, _i, _ref, _ref1, _results;
      host = host.toLowerCase();
      if (domain.test != null) {
        domain = (_ref = (_ref1 = host.match(domain)) != null ? _ref1[0] : void 0) != null ? _ref : "";
      }
      if (host.slice(-domain.length - 1) === ("." + domain)) {
        parts = host.slice(0, -domain.length - 1).split(".");
        length = parts.length;
        _results = [];
        for (i = _i = 0; 0 <= length ? _i < length : _i > length; i = 0 <= length ? ++_i : --_i) {
          _results.push(parts.slice(i, length).join("."));
        }
        return _results;
      } else {
        return [];
      }
    };
  
    rstat = function(path, callback) {
      return fs.lstat(path, function(err, stats) {
        if (err) {
          return callback(err);
        } else if (stats != null ? stats.isSymbolicLink() : void 0) {
          return fs.realpath(path, function(err, realpath) {
            if (err) {
              return callback(err);
            } else {
              return rstat(realpath, callback);
            }
          });
        } else {
          return callback(err, stats, path);
        }
      });
    };
  
    compilePattern = function(domains) {
      return RegExp("((^|\\.)(" + (domains.join("|")) + "))\\.?$", "i");
    };
  
  }).call(this);
  