"use strict";

let fs = require("fs"),
    env = process.env.NODE_ENV || "development";

/*
 * Toplevel config
 */
let config = {
  env: env
};

/*
 * Application
 */
let appConfig = readConfig("app");
config.app = {
  httpPort: appConfig.httpPort ||
    process.env.PORT ||
    (env === "production" ? 80 : 8080),
  sessionSecret: appConfig.sessionSecret ||
    process.env.SESSION_SECRET ||
    "omgsupersecretlol",
  passwordSecret: appConfig.passwordSecret ||
    process.env.PASSWORD_SECRET ||
    "omgsupersecretlol"
};

/*
 * Database
 */

let dbConfig = readConfig("db");
/*jshint camelcase:false*/
if (dbConfig.use_env_variable) {
  let dbInfo = process.env[dbConfig.use_env_variable].match(
      /([^:]+):\/\/([^:]+):([^@]+)@([^:]+):(\d+)\/(.+)/);
  dbConfig.database = dbInfo[6];
  dbConfig.username = dbInfo[2];
  dbConfig.password = dbInfo[3];
  dbConfig.host = dbInfo[4];
  dbConfig.port = dbInfo[5];
  dbConfig.dialect = dbInfo[1];
  dbConfig.protocol = dbInfo[1];
}
/*jshint camelcase:true*/

config.db = {
  username: dbConfig.username,
  password: dbConfig.password,
  database: dbConfig.database,
  host: dbConfig.host,
  dialect: dbConfig.dialect,
  protocol: dbConfig.protocol,
  maxConcurrentQueries: 100,
  logging: dbConfig.logging !== undefined ?
    dbConfig.logging :
    (env === "development" ?
     console.info : false),
  pool: { maxConnections: 50, maxIdleTime: 30 }
};

/*
 * Util
 */
function readConfig(name) {
  return JSON.parse(
    fs.readFileSync(
      __dirname + "/../../../config/"+name+".json"))[env];
}

/*
 * Export
 */
module.exports = config;

"use strict";

let fs = require("fs"),
    env = process.env.NODE_ENV || "development";

/*
 * Toplevel config
 */
let config = {
  env: env
};

/*
 * Application
 */
let appConfig = readConfig("app");
config.app = {
  httpPort: appConfig.httpPort ||
    process.env.PORT ||
    (env === "production" ? 80 : 8080),
  sessionSecret: appConfig.sessionSecret ||
    process.env.SESSION_SECRET ||
    "omgsupersecretlol",
  passwordSecret: appConfig.passwordSecret ||
    process.env.PASSWORD_SECRET ||
    "omgsupersecretlol"
};

/*
 * Database
 */

let dbConfig = readConfig("db");
/*jshint camelcase:false*/
if (dbConfig.use_env_variable || !Object.keys(dbConfig).length) {
  let dbInfo = process.env[dbConfig.use_env_variable || "DATABASE_URL"].match(
      /([^:]+):\/\/([^:]+):([^@]+)@([^:]+):(\d+)\/(.+)/);
  dbConfig.database = dbInfo[6];
  dbConfig.username = dbInfo[2];
  dbConfig.password = dbInfo[3];
  dbConfig.host = dbInfo[4];
  dbConfig.port = dbInfo[5];
  dbConfig.dialect = dbInfo[1];
  dbConfig.protocol = dbInfo[1];
}
/*jshint camelcase:true*/

config.db = {
  username: dbConfig.username,
  password: dbConfig.password,
  database: dbConfig.database,
  host: dbConfig.host,
  dialect: dbConfig.dialect,
  protocol: dbConfig.protocol,
  maxConcurrentQueries: 100,
  logging: dbConfig.logging !== undefined ?
    dbConfig.logging :
    (env === "development" ?
     console.info : false),
  pool: { maxConnections: 50, maxIdleTime: 30 }
};

/*
 * Util
 */
function readConfig(name) {
  let path = __dirname + "/../../../config/"+name+".json";
  return fs.existsSync(path) ? JSON.parse(fs.readFileSync(path))[env] : {};
}

/*
 * Export
 */
module.exports = config;

Return an empty object if the config file doesn't exist