/*
 * config.js: Configuration for the jitsu CLI.
 *
 * (C) 2010, Nodejitsu Inc.
 *
 */

var nconf = require('nconf'),
    path = require('path'),
    winston = require('winston'),
    optimist = require('optimist'),
    argv = optimist.argv,
    fs = require('fs');

var config = module.exports = Object.create(nconf.Provider.prototype);

//
// Set up our default config
//
var dir = process.cwd(),
    configPath = argv.jitsuconf || argv.j,
    looking = true;


var defaults = {
  'root': process.env.HOME,
  'protocol': 'http',
  'remoteHost': 'api.nodejitsu.com',
  'userconfig': '.jitsuconf',
  'loglevel': 'info',
  'tmproot': '/tmp',
  'tar': 'tar',
  'gzipbin': 'gzip'
};

//
// Make sure the file exists if it was set explicitly
//
config.findJitsuconf = function (filename) {
  //
  // Use pre-existing config file if already found
  //
  if(!looking) {
    return true;
  }
  filename = filename || '.jitsuconf';

  if (configPath) {
    try {
      var stat = fs.statSync(fs.realpathSync(configPath));
      looking = stat.isDirectory();
    }
    catch (ex) {
      return false;
    }
  }
  while (looking) {
    try {
      var stat = fs.statSync(fs.realpathSync(configPath = path.join(dir, filename)));
      looking = stat.isDirectory();
    }
    catch (e) {
      var olddir = dir;
      dir = path.dirname(dir);

      if (olddir === dir) {
        try {
          var stat = fs.statSync(fs.realpathSync(configPath = path.join(process.env.HOME, filename)));
          if(stat.isDirectory()) {
            configPath = undefined;
          }
        }
        catch (e) {
          //
          // Ignore errors
          //
        }
        looking = false;
      }
    }
  }

  if (!configPath) {
    fs.writeFileSync(path.join(process.env.HOME, filename),JSON.stringify(defaults));
  }

  winston.silly('Using config file ' + configPath.magenta);
  config.use('file', { file: configPath });
  return true;
};

var _get = config.get,
    _load = config.load;

Object.defineProperty(defaults, 'remoteUri', {
  get: function () {
    var port = optimist.argv.port || config.get('port') || this.port || '';
    if (port) {
      port = ':' + port;
    }

    return [this.protocol, '://', (optimist.argv.remoteHost || config.get('remoteHost') || defaults.remoteHost), port].join('');
  }
});

config.load = function (filename, callback) {
  if (!callback) {
    callback = filename;
    filename = null;
  }

  //
  // Find the `.jitsuconf` file to be used for this
  // jitsu session.
  //
  if (!config.findJitsuconf(filename)) {
    return callback(new Error(configPath.magenta + ' does not exist'), true);
  }

  _load.call(config, function (err, store) {
    if (err) {
      return callback(err);
    }

    if (filename) {
      config.set('userconfig', filename);
    }

    if (store.auth) {
      var auth = store.auth.split(':');
      config.clear('auth');
      config.set('username', auth[0]);
      config.set('password', auth[1]);
      return config.save(callback);
    }

    callback(null, store);
  });
};

config.get = function (key) {
  if (optimist.argv.hasOwnProperty(key)) {
    return optimist.argv[key];
  }

  var value = _get.call(config, key);
  return typeof value !== 'undefined' ? value :  defaults[key];
};

@@ -1630,32 +1630,66 @@
 rs%0A          //%0A
+          configPath = undefined;%0A
         %7D%0A      
@@ -1725,16 +1725,18 @@
   %7D%0A  %7D%0A
+  
 %0A  if (!
@@ -1745,24 +1745,79 @@
 nfigPath) %7B%0A
+    console.log(path.join(process.env.HOME, filename))%0A
     fs.write
@@ -1825,16 +1825,29 @@
 ileSync(
+configPath = 
 path.joi

fix for home directory bug just introduced #36