'use strict';
/*
 *  Please DO NOT edit this file.
 *
 *  Edit auto-generated 'user.config.js' file instead.
 *
 */

// #start config-auto-export
var config = {};

/*
 * Configure host and port.
 *  Please for ports < 1000 use authbind. DO NOT RUN nodejs as root.
 *  $ authbind nodejs index.js
 */
config.host = 'localhost';
config.port = 3000;

config.debomatic = {};
config.debomatic.path = '/srv/debomatic-amd64';
config.debomatic.jsonfile = '/var/log/debomatic-json.log';

config.routes = {};
config.routes.debomatic = '/debomatic';
config.routes.distribution = '/distribution';
config.routes.preferences = '/preferences';
config.routes.commands = '/commands';

// web configuration
config.web = {};

// debomatic configuration exportable for web
config.web.debomatic = {};
config.web.debomatic.admin = {};
config.web.debomatic.admin.name = 'Your Name';
config.web.debomatic.admin.email = 'you AT debian DOT org'; // please use this SPAMFREE form - it will be converted client side by javascript
config.web.debomatic.architecture = 'amd64';

config.web.debomatic.dput = {};
config.web.debomatic.dput.incoming = config.debomatic.path;
config.web.debomatic.dput.host = config.host;
config.web.debomatic.dput.login = 'debomatic';
config.web.debomatic.dput.method = 'scp';
config.web.debomatic.dput.unsigned_uploads = false;

// header title and description
config.web.title = 'Deb-o-Matic web.ui';
config.web.description = 'This is a web interface for debomatic over ' + config.web.debomatic.architecture;

// list of files get preview
config.web.file = {};
config.web.file.preview = ['buildlog'];
config.web.file.num_lines = 25;

// default ui settings
config.web.preferences = {};
config.web.preferences.autoscroll = true;
config.web.preferences.header = true;
config.web.preferences.sidebar = true;
config.web.preferences.glossy_theme = true;
config.web.preferences.file_background = true;
config.web.preferences.file_fontsize = 13; // valid values are [13..16]
config.web.preferences.debug = 0; // debug level - 0 means disabled

// #end config-auto-export


// DO NOT TOUCH these ones

config.version = '0.5.1';

config.events = {};
config.events.error = 'error';
config.events.broadcast = {};
config.events.broadcast.distributions = 'b.distributions';
config.events.broadcast.status_update = 'b.status_update';
config.events.broadcast.status_debomatic = 'b.status_debomatic';

config.events.client = {};
config.events.client.distribution_packages = 'c.distribution_packages';
config.events.client.distribution_packages_status = 'c.distribution_packages_status';
config.events.client.package_files_list = 'c.package_files_list';
config.events.client.file = 'c.file';
config.events.client.file_newcontent = 'c.file_newcontent';
config.events.client.status = 'c.status';

// debomatic status according with JSONLogger.py module
config.status = {};
config.status.build = 'build';
config.status.create = 'create';
config.status.update = 'update';
config.status.success = true;
config.status.fail = false;

// read user configuration and merge it
/*
 * update object1 with object2 values
 */
function _merge(object1, object2) {
    var result = {};
    for (var p in object1) {
        if (object2.hasOwnProperty(p)) {
            if (typeof object1[p] === 'object' && typeof object2[p] === 'object') {
                result[p] = _merge(object1[p], object2[p]);
            } else {
                result[p] = object2[p];
            }
        } else {
            result[p] = object1[p];
        }
    }
    return result;
}

try {
    var Parser = require('./parser.js');
    var parser = new Parser();
    var user_config = parser.getUserConfig();
    if (user_config) {
        console.log('Reading user configutation ...');
        config = _merge(config, require(user_config));
    } else {
        console.log('No user config specified. Using global settings.');
    }
} catch (err) {
    if (err.code == 'MODULE_NOT_FOUND') {
        console.log('File %s not found.', user_config);
        process.exit(1);
    } else {
        console.error('Error reading user configutation', err);
        process.exit(1);
    }
} finally {
    // export some variable
    config.web.paths = config.routes;
    config.web.events = config.events;
    config.web.status = config.status;
    config.web.host = config.host;

    // calculate pidfile
    config.debomatic.pidfile = "/var/run/debomatic-" +
        require('crypto')
        .createHash('sha256')
        .update(config.debomatic.path)
        .digest('hex');

    module.exports = config;
}
