// Generated by CoffeeScript 2.3.2
(function() {
  var Automist, ConfigBuilder, add, fs, isArray, load, merge, minimist, path, unit, wrap, yaml;

  fs = require("fs");

  path = require("path");

  merge = require("deepmerge");

  minimist = require("minimist");

  yaml = require('js-yaml');

  Automist = require("automist");

  ({ConfigBuilder} = require("@l.degener/irma-config"));

  ({unit, add, load, wrap} = ConfigBuilder);

  ({isArray} = require("util"));

  require("coffeescript/register");

  module.exports = function(...args) {
    var addDirsToTypePath, argv, automist, checkLegacyIo, generateManpage, loadConfigFilesFromCommandLine, printHelp, printInfo, processCommandLineOverrides, readme, startServer, toCamelCase;
    toCamelCase = function(x) {
      var key, o, value;
      if (typeof x === "string") {
        return x.replace(/\W+(\w)/g, function(_, c) {
          return c.toUpperCase();
        });
      } else {
        o = {};
        for (key in x) {
          value = x[key];
          o[toCamelCase(key)] = value;
        }
        return o;
      }
    };
    readme = yaml.load(fs.readFileSync(path.resolve(__dirname, "../README.yaml")));
    automist = Automist(readme);
    argv = toCamelCase(minimist(args, automist.options()));
    checkLegacyIo = function(cfg) {
      return unit(cfg).then(function({stderr, stdout}) {
        if ((stderr != null) || (stdout != null)) {
          throw new Error("WARNING: you are trying to setup stdout/stderr via the environment\n( probably in some wrapper code that uses the ConfigBuilder to programatically\n  create/run an irma configuration ).\n\nSupport for this method has been removed, so THIS WILL NOT WORK.\n\nInstead, Io can now be configured / overridden using the regular\nconfiguration subsystem. For example, you could put something like this in your\nirma.yaml:\n\n  io:\n    stderr: path/to/err.log\n\nIf you create your configuration programmatically, you can also create a writeable\nyourself and pass it as io.std[err|out].\n\nWe appologize for the inconvenience!");
        }
      });
    };
    printHelp = function(cfg) {
      return unit(cfg).then(function(_, {io}) {
        return io.stderr.write(automist.help());
      });
    };
    generateManpage = function(cfg) {
      return unit(cfg).then(function(_, {io}) {
        return io.stdout.write(automist.manpage());
      });
    };
    addDirsToTypePath = function(cfg) {
      var dirs, ref;
      dirs = (ref = argv.configTypes) != null ? ref : [];
      if (!isArray(dirs)) {
        dirs = [dirs];
      }
      return unit(cfg).typePath(...dirs.slice().reverse());
    };
    loadConfigFilesFromCommandLine = function(cfg) {
      var cb;
      return cb = argv._.slice().reverse().reduce((function(cb, file) {
        return cb.load(file);
      }), unit(cfg));
    };
    processCommandLineOverrides = function(cfg) {
      var hostName, portNumber, portString;
      return unit(cfg).bind(argv.listen ? ([hostName, portString] = argv.listen.split(':'), portNumber = parseInt(portString), add({
        host: hostName,
        port: portNumber
      })) : void 0).bind(argv.esHost != null ? ([hostName, portString] = argv.esHost.split(':'), portNumber = parseInt(portString), add({
        elasticSearch: {
          host: hostName,
          port: portNumber
        }
      })) : void 0).bind(argv.esIndex != null ? add({
        elasticSearch: {
          index: argv.esIndex
        }
      }) : void 0);
    };
    startServer = function(cfg) {
      return unit(cfg).then(function({Server}, settings) {
        return Server(settings).start();
      });
    };
    printInfo = function(cfg) {
      return unit(cfg).then(function(_, settings) {
        var host, io, port;
        ({host, port, io} = settings);
        io.stderr.write(`IRMa listening at http://${host}:${port}\n`);
        return settings;
      });
    };
    if (argv.help) {
      return printHelp;
    } else if (argv.manpage) {
      return generateManpage;
    } else {
      return function(cfg) {
        return unit(cfg).bind(checkLegacyIo).bind(addDirsToTypePath).bind(loadConfigFilesFromCommandLine).bind(processCommandLineOverrides).bind(startServer).bind(printInfo);
      };
    }
  };

}).call(this);
