var droonga = require('express-droonga'),
    fs      = require('fs'),
    yaml    = require('js-yaml'),
    path    = require('path');

var baseDir = path.resolve(process.env.DROONGA_BASE_DIR || ".");

var engineConfigs = {};

function setBaseDir(baseDir) {
  var configs = {};

  var configFile = path.resolve(baseDir, "droonga-http-server.yaml");
  if (fs.existsSync(configFile)) {
    configs = yaml.safeLoad(fs.readFileSync(configFile, 'utf8'));
    if (!('daemon' in configs))
      configs.daemon = true;
  }

  var engineConfigFile = path.resolve(baseDir, "droonga-engine.yaml");
  if (fs.existsSync(engineConfigFile)) {
    engineConfigs = yaml.safeLoad(fs.readFileSync(engineConfigFile, 'utf8'));
  }

  configs.baseDir = baseDir;
  configs = defineDefaultConfigs(configs);
  configs.setBaseDir = setBaseDir;
  return configs;
}

function define(slot, path, value) {
  var keys = path.split('.');
  keys.some(function(key, index) {
    if (index == keys.length - 1) {
      if (!(key in slot))
        slot[key] = value;
    } else {
      if (!(key in slot))
        slot[key] = {};
      slot = slot[key];
    }
  });
}

function defineDefaultConfigs(configs) {
define(engineConfigs, 'host', '127.0.0.1');
define(engineConfigs, 'port', 10031);
define(engineConfigs, 'tag',  'droonga');

define(configs, 'port',               10041);
define(configs, 'cache_size',         100);
define(configs, 'enable_trust_proxy', false);
define(configs, 'plugins', [
  droonga.API_REST,
  droonga.API_GROONGA,
  droonga.API_DROONGA
]);
define(configs, 'environment', process.env.NODE_ENV || 'development');

if (configs.daemon) {
  define(configs, 'pid_file',        'droonga-http-server.pid');
  define(configs, 'access_log_file', 'droonga-http-server.access.log');
  define(configs, 'system_log_file', 'droonga-http-server.system.log');
} else {
  define(configs, 'daemon',          false);
  define(configs, 'access_log_file', '-');
  define(configs, 'system_log_file', '-');
}

define(configs, 'engine.host',            engineConfigs.host);
define(configs, 'engine.port',            engineConfigs.port);
define(configs, 'engine.tag',             engineConfigs.tag);
define(configs, 'engine.default_dataset', 'Default');
// We can use the host name of the droogna-engine as the receiver host
// of the http server, because this computer works as a droonga-engine
// node and it is guaranteed that this computer can be accessed with
// the host name.
define(configs, 'engine.receiver_host',   engineConfigs.host);

return configs;
}

module.exports = setBaseDir(baseDir);

@@ -219,16 +219,137 @@
 gs = %7B%7D;
+%0Avar engineServiceUserName = 'droonga-engine';%0Avar engineServiceBaseDir  = '/home/' + engineServiceUserName + '/droonga';
 %0A%0Afuncti
@@ -631,16 +631,255 @@
 e;%0A  %7D%0A%0A
+  var engineServiceConfigFile = path.resolve(engineServiceBaseDir, %22droonga-engine.yaml%22);%0A  if (fs.existsSync(engineServiceConfigFile)) %7B%0A    engineConfigs = yaml.safeLoad(fs.readFileSync(engineServiceConfigFile, 'utf8'));%0A  %7D%0A  else %7B%0A  
   var en
@@ -933,32 +933,34 @@
 -engine.yaml%22);%0A
+  
   if (fs.existsS
@@ -980,32 +980,34 @@
 figFile)) %7B%0A    
+ 
+ 
 engineConfigs = 
@@ -1060,24 +1060,30 @@
 , 'utf8'));%0A
+    %7D%0A
   %7D%0A%0A  confi

Load droonga-engine.yaml for the service