#!/usr/bin/env node

var amok = require('..');
var program = require('commander');

program.version(require('../package.json').version);

program.usage('[OPTION ...] <URL | FILE>');

program.option('--debug-host <HOST>', 'specify debug host', 'localhost');
program.option('--debug-port <PORT>', 'specify debug port', 9222);

program.option('-b, --browser <BROWSER>', 'specify browser', function(value) {
  var args = process.env[value.toUpperCase() + '_FLAGS'];
  if (args) {
    args = args.split(' ');
  }

  var command = process.env[value.toUpperCase() + '_BIN'];
  if (!command) {
    command = value;
  }

  return {
    command: command,
    args: args
  };
});

program.option('--http-host <HOST>', 'specify http host', 'localhost');
program.option('--http-port <PORT>', 'specify http port', 9966);

program.option('-c, --compiler <COMPILER>', 'specify compiler');
program.option('-w, --watch <GLOB>', 'specify watch pattern');
program.option('-t, --hot [GLOB]', 'enable script hot patching');
program.option('-i, --interactive', 'enable interactive mode');
program.option('-d, --cwd <DIR>', 'change working directory', process.cwd());

program.parse(process.argv);

amok.set('cwd', program.cwd);
amok.set('port', program.debugPort);
amok.set('host', program.debugHost);

program.url = program.args.filter(function(arg) {
  return arg.match(/^(http|https|file|about:blank)/);
})[0];

program.scripts = program.args.filter(function(arg) {
  return arg.match(/(.js|.ts|.coffee)$/);
});

if (program.url) {
  amok.set('url', program.url);
} else if (program.scripts.length > 0) {
  amok.set('scripts', program.scripts.reduce(function(object, value, key) {
    object[value] = value;
    return object;
  }, {}));

  amok.use(amok.server(program.httpPort, program.httpHost));
} else {
  program.help();
}

if (program.compiler) {
  amok.use(amok.compiler(program.compiler, program.args, process.stderr));
}

if (program.watch) {
  amok.use(amok.watch(program.watch));
}

if (program.hot) {
  amok.use(amok.hotpatch(program.hot));
}

if (program.browser) {
  amok.use(amok.browser(program.browser.command, program.browser.args, process.stderr));
}

if (program.interactive) {
  amok.use(amok.repl(process.stdin, process.stdout));
} else {
  amok.use(amok.print(process.stdout));
}

amok.on('connect', function() {
  amok.client.on('error', function(error) {
    process.stderr.write(error.description + '\n');
  });
});

amok.connect(amok.get('port'), amok.get('host'));

process.on('SIGTERM', function() {
  process.exit(0);
});

process.on('SIGINT', function() {
  process.exit(0);
});

process.on('exit', function() {
  amok.close();
});

@@ -2389,19 +2389,15 @@
 ror.
-description
+message
  + '

Print client error message instead of description