'use strict';

var Command = require('ronin').Command;
var HttpAPI = require('../../http-api');
var debug = require('debug');
var log = debug('cli:daemon');
log.error = debug('cli:daemon:error');

var httpAPI = void 0;

module.exports = Command.extend({
  desc: 'Start a long-running daemon process',

  run: function run(name) {
    console.log('Initializing daemon...');
    httpAPI = new HttpAPI();
    httpAPI.start(function (err) {
      if (err) {
        return log.error(err);
      }
      console.log('Daemon is ready');
    });

    process.on('SIGINT', function () {
      console.log('Received interrupt signal, shutting down..');
      httpAPI.stop(function (err) {
        if (err) {
          return log.error(err);
        }
        process.exit(0);
      });
    });
  }
});