/*eslint-env node */
'use strict';

/**
	Redbird ETCD Module
	This module handles automatic proxy registration via etcd
*/
var Etcd = require('node-etcd');

function ETCDModule(redbird, options){
  if (!(this instanceof ETCDModule)){
    return new ETCDModule(redbird, options);
  }

  // Create RedBird Instance and Log
  this.redbird = redbird;
  var log = redbird.log;
  var _this = this;

  // Create node-etcd Instance
  this.etcd = new Etcd(options.hosts,options.ssloptions);
  this.etcd_dir = (typeof options.path !== 'undefined') ? options.path : "redbird";

  // Create directory if not created
  this.etcd.get(this.etcd_dir,function(err,body,header){
    if (err && err.errorCode == 100){
      _this.etcd.mkdir(_this.etcd_dir,function(err){
        if (err){
          log.error(err, 'etcd backend error');
        }
        else{
          createWatcher();
        }
      });
    }
    else if(!err && body.node.dir){
      createWatcher();
    }
    else{
      log.error(err, 'etcd backend error');
    }
  });

  // Helper function to check if values contain settings
  function IsJsonString(str) {
      try {
          JSON.parse(str);
      } catch (e) {
          return false;
      }
      return true;
  }

  // Helper function to pretify etcd directory strings
  function removeEtcDir(str) {
    return str.replace(_this.etcd_dir,'').replace(/^\/+|\/+$/g, '');
  }

  function createWatcher(){
    // Watch etcd directory
    _this.watcher = _this.etcd.watcher(_this.etcd_dir,null,{recursive:true});

    // On Add/Update
    _this.watcher.on("change", function(body,headers){
      if(body.node.key && body.node.value && !IsJsonString(body.node.value)){
        _this.redbird.register(removeEtcDir(body.node.key),body.node.value);
      }
      else if(body.node.key && body.node.value && IsJsonString(body.node.value)){
        var config = JSON.parse(body.node.value);
        if (typeof config.docker !== 'undefined'){
          require('../').docker(_this.redbird).register(body.node.key,body.node.value.docker,body.node.value);
        }
        else {
          _this.redbird.register(removeEtcDir(body.node.key),config.hosts,config);
        }
      }
    });

    // On Delete
    _this.watcher.on("delete", function(body,headers){
      if(body.node.key){
        _this.redbird.unregister(removeEtcDir(body.node.key));
      }
    });

    // Handle Errors
    _this.watcher.on("error", function(err){
      log.error(err, 'etcd backend error');
    });
  }
}

module.exports = ETCDModule;

@@ -640,21 +640,23 @@
 ion(err,
+ 
 body,
+ 
 header)%7B
@@ -724,32 +724,33 @@
 (_this.etcd_dir,
+ 
 function(err)%7B%0A 
@@ -1350,16 +1350,17 @@
 tcd_dir,
+ 
 '').repl
@@ -1501,13 +1501,15 @@
 dir,
+ 
 null,
+ 
 %7Brec

Update etcd-backend.js