#!/usr/bin/env node

/******************************************************************************
 * Interactive RiveScript Shell for quickly testing your RiveScript bot.      *
 *                                                                            *
 * Usage: node shell.js /path/to/brain                                        *
 ******************************************************************************/

var readline = require("readline"),
	fs = require("fs"),
	RiveScript = require("./lib/rivescript");

//------------------------------------------------------------------------------
// Accept command line parameters.
//------------------------------------------------------------------------------

var opts = {
	debug: false,
	utf8: false,
	watch: false,
	brain: undefined
};

process.argv.slice(2).forEach(function(val, index, array) {
	
	if (val === "--debug") {
		opts.debug = true;
	}
	else if (val === "--utf8") {
		opts.utf8 = true;
	}
	else if (val === "--watch") {
		opts.watch = true;
	}
	else if (val.indexOf("-") === 0) {
		console.error("Unknown option: %s", val);
	}
	else if (opts.brain === undefined) {
		opts.brain = val;
	}
	else {
		console.error("Extra parameter ignored: %s", val);
	}
});

if (opts.brain === undefined) {
	console.log("Usage: node shell.js [--debug --utf8 --watch] </path/to/brain>");
	process.exit(1);
}

//------------------------------------------------------------------------------
// Initialize the RiveScript bot and print the welcome banner.
//------------------------------------------------------------------------------

var bot = null;
function loadBot() {
	bot = new RiveScript({
		debug: opts.debug,
		utf8:  opts.utf8,
	});
	bot.ready = false;
	bot.loadDirectory(opts.brain, loading_done, loading_error);
}
loadBot();

fs.watch(opts.brain, {recursive: true}, function() {
	console.log("");
	console.log('[INFO] Brain changed, reloading bot.');
	rl.prompt();
	loadBot();
});


//--------------------------------------------------------------------------
// Drop into the interactive command shell.
//--------------------------------------------------------------------------

console.log("      .   .       ");
console.log("     .:...::      RiveScript Interpreter (JavaScript)");
console.log("    .::   ::.     Library Version: v" + bot.version());
console.log(" ..:;;. ' .;;:..  ");
console.log("    .  '''  .     Type '/quit' to quit.");
console.log("     :;,:,;:      Type '/help' for more options.");
console.log("     :     :      ");
console.log("");
console.log("Using the RiveScript bot found in: " + opts.brain);
console.log("Type a message to the bot and press Return to send it.");
console.log("");

var rl = readline.createInterface({
	input: process.stdin,
	output: process.stdout
});

rl.setPrompt("You> ");
rl.prompt();
rl.on('line', function(cmd) {
	// Handle commands.
	if (cmd === "/help") {
		help();
	} else if (cmd.indexOf("/eval ") === 0) {
		eval(cmd.replace("/eval ", ""));
	} else if (cmd.indexOf("/log ") === 0) {
		console.log(eval(cmd.replace("/log ", "")));
	} else if (cmd === "/quit") {
		process.exit(0);
	} else {
		// Get a reply from the bot.
		var reply = (bot && bot.ready)
			? bot.reply("localuser", cmd)
			: "ERR: Bot Not Ready Yet";
		console.log("Bot>", reply);
	}
	
	rl.prompt();
}).on('close', function() {
	console.log("");
	process.exit(0);
});



function loading_done(batchNumber) {
	bot.sortReplies();
	bot.ready = true;
}

function loading_error(error, batchNumber) {
	console.error("Loading error: " + error);
}

function help() {
	console.log("Supported commands:");
	console.log("/help        : Show this text.");
	console.log("/eval <code> : Evaluate JavaScript code.");
	console.log("/log <code>  : Shortcut to /eval console.log(code).");
	console.log("/quit        : Exit the program.");
}



@@ -1821,19 +1821,20 @@
 ursive: 
-tru
+fals
 e%7D, func
@@ -2890,16 +2890,103 @@
 help();%0A
+%09%7D else if (cmd.indexOf(%22/data%22) === 0) %7B%0A%09%09console.log(bot.getUservars(%22localuser%22));%0A
 %09%7D else 
@@ -3022,16 +3022,28 @@
  0) %7B%0A%09%09
+console.log(
 eval(cmd
@@ -3065,16 +3065,17 @@
  %22, %22%22))
+)
 ;%0A%09%7D els

Make fs.watch non recursive (could be another option flag if needed)