#! /usr/bin/env node
'use strict';

var _ioAuthoringConfigService = require('./server/services/ioAuthoringConfigService');

var _ioAuthoringConfigService2 = _interopRequireDefault(_ioAuthoringConfigService);

var _ioauthoringConfigManager = require('./server/managers/ioauthoringConfigManager');

var _ioauthoringConfigManager2 = _interopRequireDefault(_ioauthoringConfigManager);

var _configUtil = require('./server/utils/configUtil');

var _configUtil2 = _interopRequireDefault(_configUtil);

var _winstonLogs = require('./server/utils/winstonLogs');

var _winstonLogs2 = _interopRequireDefault(_winstonLogs);

var _server = require('./server');

var _server2 = _interopRequireDefault(_server);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var packageJson = require('../package.json');

/*
 * *******************************************************************************
 *  * Copyright (c) 2018 Edgeworx, Inc.
 *  *
 *  * This program and the accompanying materials are made available under the
 *  * terms of the Eclipse Public License v. 2.0 which is available at
 *  * http://www.eclipse.org/legal/epl-2.0
 *  *
 *  * SPDX-License-Identifier: EPL-2.0
 *  *******************************************************************************
 *
 */

function main() {
  var key = void 0,
      value = void 0,
      args = process.argv.slice(2),
      argsString = args.toString(),
      commandInserted = argsString.replace(/,/g, ' ');

  var daemon = require("daemonize2").setup({
    main: "daemonServer.js",
    name: "ioauthoring",
    pidfile: "ioauthoring.pid",
    silent: true
  });

  daemon.on("starting", function () {
    console.log("Starting ioauthoring...");
  }).on("stopping", function () {
    console.log("Stopping ioauthoring...");
  }).on("stopped", function (pid) {
    console.log("ioauthoring stopped.");
  }).on("running", function (pid) {
    console.log("ioauthoring already running. PID: " + pid);
  }).on("notrunning", function () {
    console.log("ioauthoring is not running");
  }).on("error", function (err) {
    console.log("ioauthoring failed to start:  " + err.message);
  });

  _winstonLogs2.default.info('Command inserted: ioauthoring ' + commandInserted);

  switch (args[0]) {
    case 'start':
      daemon.start();
      break;

    case 'stop':
      daemon.stop();
      break;

    case 'status':
      var result = daemon.status();
      if (result == 0) {
        console.log('ioauthoring is not running.');
      } else {
        console.log('ioauthoring is running at pid:' + result);
      }
      break;

    case 'help':
      displayHelp();
      break;

    case 'version':
      console.log("Version: " + packageJson.version);
      console.log("License: " + packageJson.license.type + " (" + packageJson.license.url + ")");
      console.log(packageJson.description);
      break;

    case 'config':
      if (args[1]) {
        switch (args[1]) {
          case '-list':
            _ioAuthoringConfigService2.default.configList();
            break;

          case '-add':
            key = args[2];
            value = args[3];
            try {
              if (key && value) {
                var lowerKey = key.toLowerCase();
                _configUtil2.default.setConfigParam(lowerKey, value);
              } else {
                console.log('\nPlease provide values in following order:\n ioauthoring config -add <key> <value>');
              }
            } catch (exception) {
              console.log(exception);
            }
            break;

          case '-remove':
            key = args[2];
            try {
              if (key) {
                var lowerKey = key.toLowerCase();
                _ioauthoringConfigManager2.default.deleteConfig(lowerKey);
              } else {
                console.log('\nPlease provide values in following order:\n ioauthoring config -remove <key>');
              }
            } catch (exception) {
              console.log(exception);
            }
            break;
          default:
            console.log('Invalid flag "' + args[1] + '"');
        }
      } else {
        _ioAuthoringConfigService2.default.configList();
      }
      break;

    default:
      displayHelp();
      break;
  }
}

var displayHelp = function displayHelp() {
  var helpString = "\tUsage 1: ioauthoring [OPTION]\n" + "\tUsage 2: ioauthoring [COMMAND] <Argument>\n" + "\tUsage 3: ioauthoring [COMMAND] <key> <value>\n" + "\n\n" + "\tCommand          Arguments                                         Meaning\n" + "\t=======          =========                                         =======\n" + "\tconfig           -list                                             Displays Configuration information in CLI (config table content)\n" + "\t                 -add <key> <value>                                Set Configurations of ioauthoring\n" + "\t                                                                   (You can set one of these configurations: port, fog_controller_port, fog_controller_ip_address,\n" + "\t                                                                    fog_controller_protocol, ssl_key, ssl_cert, intermediate_cert, self_signed_certs)\n" + "\t                 -remove <key>                                     Deletes a Configuration with corresponding Key\n" + "\n\thelp                                                               Shows this message\n" + "\n\tstart                                                              Starts ioauthoring\n" + "\n\tstop                                                               Stops ioauthoring\n" + "\n\tversion                                                            Displays Version and License\n" + "\n\n" + "\tReport bugs to: edgemaster@iofog.org\n" + "\tioFog home page: http://iofog.org\n" + "\tFor users with Eclipse accounts, report bugs to: https://bugs.eclipse.org/bugs/enter_bug.cgi?product=iofog";
  console.log(helpString);
};

main();