/**
 * Created by elad.benedict on 8/24/2015.
 */

var path = require('path');
var fs = require('fs');
var _ = require('underscore')
var hostname = require('./utils/hostname');

module.exports = (function(){

    var machineName = hostname.getLocalMachineHostname();

    var configTemplateContent = fs.readFileSync(path.join(__dirname, './config/config.json.template'), 'utf8');
    var updatedConfigContent = configTemplateContent.replace('@HOSTNAME@', machineName);

    var configObj = JSON.parse(updatedConfigContent);

    var mappingFilePath = path.join(__dirname, 'config', 'configMapping.json');
    if (fs.existsSync(mappingFilePath))
    {
        var mappingContent = fs.readFileSync(mappingFilePath, 'utf8');
        var mappingObj=JSON.parse(mappingContent);
        _.each(mappingObj, function(value, key) {
            console.log("Matching configurations arguments. Key: [%s] => Match: [%s]", key, machineName.match(key));
            if (machineName.match(key)) {
                assignValues(value, configObj);
            }
        });
    }
    

    function assignValues(configPropertiesObj, configOutputObj) {
        for (var p in configPropertiesObj) {
            if (configPropertiesObj.hasOwnProperty(p)) {
                if (!_.isArray(configPropertiesObj[p]) &&
                    _.isObject(configPropertiesObj[p]) &&
                    _.has(configOutputObj,p)) {
                    assignValues(configPropertiesObj[p], configOutputObj[p]);
                }
                else {
                    configOutputObj[p] = configPropertiesObj[p];
                }
            }
        }
    }
    var machineShortName = hostname.getLocalMachineHostname(false);
    let confString = JSON.stringify(configObj, null, 2).replace(/~/g,hostname.homedir()).replace('@HOSTNAME_SHORT@', machineShortName);
    fs.writeFileSync(path.join(__dirname, './config/config.json'), confString);

    var nconf = require('nconf');

    // Setup nconf to use (in-order):
    //   1. Command-line arguments
    //   2. Environment variables
    //   3. A file located at 'path/to/config.json'
    //
    nconf.argv()
        .env()
        .file({ file: path.join(__dirname, './config/config.json') });

    return nconf;
})();

@@ -206,27 +206,85 @@
 )%7B%0A%0A    
-var
+let machineName = hostname.getLocalMachineHostname();%0A    let
  machine
 Name = h
@@ -267,32 +267,37 @@
 %0A    let machine
+Short
 Name = hostname.
@@ -320,20 +320,24 @@
 ostname(
+false
 );%0A
-%0A
     var 
@@ -444,98 +444,8 @@
 ');%0A
-    var updatedConfigContent = configTemplateContent.replace('@HOSTNAME@', machineName);%0A%0A
     
@@ -475,21 +475,22 @@
 rse(
-updatedConfig
+configTemplate
 Cont
@@ -1607,75 +1607,8 @@
   %7D%0A
-    var machineShortName = hostname.getLocalMachineHostname(false);
 %0A   
@@ -1659,16 +1659,45 @@
 null, 2)
+;%0A    confString = confString
 .replace
@@ -1721,16 +1721,109 @@
 medir())
+;%0A    confString = confString.replace('@HOSTNAME@', machineName);%0A    confString = confString
 .replace

Change row order