'use strict';

const os = require('os');
const fs = require('fs');
const path = require('path');
const merge = require('merge');
const url = require('url');
const _ = require('lodash');

const ENV = process.env;
const PLATFORM = os.platform();
const DIRNAME = '.storj-billing';
const HOME = PLATFORM === 'win32' ? ENV.USERPROFILE : ENV.HOME;
var storjbillingPath = ENV.STORJ_BILLING_DIR || HOME;
const DATADIR = path.join(storjbillingPath, DIRNAME);
const CONFDIR = path.join(DATADIR, 'config');
const ITEMDIR = path.join(DATADIR, 'items');
const CONSTANTS = require('./constants');

const MONGO_URL = process.env.MONGO_URL || ('mongodb://127.0.0.1:27017/__storj-bridge-' + process.env.NODE_ENV || 'development');
const MONGO_URL_OBJ = url.parse(MONGO_URL);
const MONGO_USERNAME = MONGO_URL_OBJ.auth && MONGO_URL_OBJ.auth.split(':')[0];
const MONGO_PASSWORD = MONGO_URL_OBJ.auth && MONGO_URL_OBJ.auth.split(':')[1];
const MONGOS = process.env.MONGOS && JSON.parse(process.env.MONGOS);
const MONGO_SSL = process.env.MONGO_SSL && JSON.parse(process.env.MONGO_SSL);

if (!fs.existsSync(DATADIR)) {
  fs.mkdirSync(DATADIR);
}

if (!fs.existsSync(CONFDIR)) {
  fs.mkdirSync(CONFDIR);
}

if (!fs.existsSync(ITEMDIR)) {
  fs.mkdirSync(ITEMDIR);
}

/**
 * Represents a configuration
 * @constructor
 * @param {String|Object} env
 */
function Config(env) {
  if (!(this instanceof Config)) {
    return new Config(env);
  }

  var config;

  if (typeof env === 'string') {
    var envConfigPath = path.join(CONFDIR, env);

    if (!fs.existsSync(envConfigPath)) {
      fs.writeFileSync(envConfigPath, JSON.stringify(Config.DEFAULTS, null, 2));
    }

    const reduceConfig = function (result, overrideValue, overrideKey) {
      if (overrideValue != null && typeof(overrideValue) === 'object') {
        result[overrideKey] = _.reduce(overrideValue, reduceConfig, {});
      } else if (overrideValue !== null && typeof(overrideValue) !== 'undefined') {
        result[overrideKey] = overrideValue;
      }
      return result;
    };
    const configEnvOverrides = _.reduce(Config.ENV_OVERRIDES, reduceConfig, {});

    config = merge.recursive(
      Config.DEFAULTS,
      JSON.parse(fs.readFileSync(envConfigPath)),
      configEnvOverrides
    );

  } else {
    config = merge(Object.create(Config.DEFAULTS), env);
  }

  for (let prop in config) {
    if (config.hasOwnProperty(prop)) {
      this[prop] = config[prop];
    }
  }
}

Config.ENV_OVERRIDES = {
  storage: {
    mongoURI: MONGO_URL,
    mongoOptions: {
      auth: {
        user: process.env.MONGO_USERNAME || MONGO_USERNAME,
        pass: process.env.MONGO_PASSWORD || MONGO_PASSWORD
      },
      mongos: MONGOS,
      ssl: MONGO_SSL
    }
  }
};

Config.DEFAULTS = {
  application: {
    mirrors: 6
  },
  storage: {
    mongoURI: '127.0.0.1:27017/__storj-billing-' + (process.env.NODE_ENV || 'develop'),
    mongoOptions: {
      auth: {
        user: null,
        pass: null
      },
      mongos: false,
      ssl: false
    }
  },
  server: {
    host: '127.0.0.1',
    port: parseInt(process.env.PORT, 10) || 3000,
    timeout: 240000,
    ssl: {
      cert: null,
      key: null,
      ca: [],
      redirect: 80
    },
    public: {
      host: '127.0.0.1',
      port: 80
    }
  },
  mailer: {
    host: '127.0.0.1',
    port: 465,
    secure: true,
    auth: {
      user: 'username',
      pass: 'password'
    },
    from: 'robot@storj.io'
  },
  logger: {
    level: CONSTANTS.LOG_LEVEL_INFO
  }
};

module.exports = Config;

@@ -2764,16 +2764,72 @@
  6%0A  %7D,%0A
+  billing: %7B%0A    storage: .015,%0A    bandwidth: .05%0A  %7D,%0A
   storag

Add billing prices to config