// Read configuration from the environment, reporting anything that's missing.

var
  pkgcloud = require('pkgcloud'),
  childProcess = require('child_process'),
  info = require('../package.json');

var configuration = {
  rackspaceUsername: {
    env: "RACKSPACE_USERNAME"
  },
  rackspaceAPIKey: {
    env: "RACKSPACE_APIKEY"
  },
  rackspaceRegion: {
    env: "RACKSPACE_REGION"
  },
  rackspaceServiceNet: {
    env: "RACKSPACE_SERVICENET",
    def: "false"
  },
  adminAPIKey: {
    env: "ADMIN_APIKEY"
  },
  contentContainer: {
    env: "CONTENT_CONTAINER"
  },
  assetContainer: {
    env: "ASSET_CONTAINER"
  },
  mongodbURL: {
    env: "MONGODB_URL"
  },
  contentLogLevel: {
    env: "CONTENT_LOG_LEVEL",
    def: "info"
  },
  contentLogColor: {
    env: "CONTENT_LOG_COLOR",
    def: "false"
  }
};

var commit = childProcess.execSync("git rev-parse --short HEAD", { encoding: "utf-8" }).trim();

/**
 * @description Create a getter function for the named function.
 */
function makeGetter(settingName) {
  return function () {
    return configuration[settingName].value;
  };
}

exports.configure = function (env) {
  var missing = [];

  for (var name in configuration) {
    var setting = configuration[name];
    var value = env[setting.env];

    setting.value = value || setting.def;

    if (!setting.value) {
      missing.push(setting.env);
    }
  }

  // Normalize rackspaceServiceNet and contentLogColor as booleans.
  configuration.rackspaceServiceNet.value = (configuration.rackspaceServiceNet.value === "true");
  configuration.contentLogColor.value = (configuration.contentLogColor.value === "true");

  if (missing.length !== 0) {
    console.error("Required configuration values are missing!");
    console.error("Please set the following environment variables:");
    console.error("");
    missing.forEach(function (settingName) {
      console.error("  " + settingName);
    });
    console.error("");

    throw new Error("Inadequate configuration");
  }
};

// Export "getter" functions for each configuration option.
for (var name in configuration) {
  exports[name] = makeGetter(name);
}

// Re-export the package.json data and the current git commit.
exports.info = info;
exports.commit = commit;

@@ -76,18 +76,16 @@
 ng.%0A%0Avar
-%0A 
  pkgclou
@@ -107,19 +107,21 @@
 gcloud')
-,%0A 
+;%0Avar
  childPr
@@ -152,19 +152,48 @@
 rocess')
-,%0A 
+;%0Avar _ = require('lodash');%0Avar
  info = 
@@ -243,16 +243,72 @@
 ion = %7B%0A
+  storage: %7B%0A    env: %22STORAGE%22,%0A    def: %22remote%22%0A  %7D,%0A
   racksp
@@ -1453,16 +1453,436 @@
  %7D%0A  %7D%0A%0A
+  // Normalize storage as a lower-case string%0A  configuration.storage = configuration.storage.toLowerCase();%0A%0A  // If storage is not remote, remove remote-only-mandatory settings from the missing list.%0A  if (configuration.storage !== %22remote%22) %7B%0A    missing = _.without(missing,%0A      %22RACKSPACE_USERNAME%22, %22RACKSPACE_APIKEY%22, %22RACKSPACE_REGION%22,%0A      %22CONTENT_CONTAINER%22, %22ASSET_CONTAINER%22,%0A      %22MONGODB_URL%22);%0A  %7D%0A%0A
   // Nor
@@ -2484,24 +2484,277 @@
 ation%22);%0A  %7D
+%0A%0A  // Ensure that STORAGE is a recognized value.%0A  if (configuration.storage !== %22remote%22 && configuration.storage !== %22memory%22) %7B%0A    console.error(%22STORAGE must be either %5C%22remote%5C%22 or %5C%22memory%5C%22.%22);%0A%0A    throw new Error(%22Invalid configuration%22);%0A  %7D
 %0A%7D;%0A%0A// Expo

Add a STORAGE configuration variable.