#!/usr/bin/perl -w

use strict;
use warnings;
use lib "./lib";
use RiveScript;

print "Welcome to the Perl RiveScript Interpreter. This script is a demonstration\n"
	. "of RiveScript. The bot's replies are taken from the files in the\n"
	. "'RiveScript/demo' directory, which by default are based on some of Eliza's\n"
	. "triggers and responses. To load a different set of replies, provide\n"
	. "a path to a directory on the command line, e.g. rsdemo /opt/rs/brain\n\n";

# Get a directory name from the command line.
my $brain = undef;
my @opts = ();
my $help  = 0;
if (@ARGV) {
	foreach my $v (@ARGV) {
		if ($v =~ /^\-*?debug$/i) {
			push (@opts, 'debug', 1);
			next;
		}
		elsif ($v =~ /^\-*?(?:debug|)(?:f|file)=(.+?)$/i) {
			push (@opts, 'debugfile', $1);
			next;
		}
		elsif ($v =~ /^\-*?(v|verbose)=(.+?)$/i) {
			push (@opts, 'verbose', $1);
			next;
		}
		elsif ($v =~ /^\-*?(h|help|\?)$/i) {
			$help = 1;
			next;
		}
		else {
			if (-d $v) {
				$brain = $v;
			}
			else {
				warn "Can't load brain from $v: not a directory\n";
			}
		}
	}
}

if ($help) {
	print "Usage: rsdemo [--debug] [directory]\n";
	exit(0);
}

my $rs = new RiveScript(@opts);

# Read the test directory.
my $replies = (defined $brain ? $brain : ($RiveScript::basedir . "/demo"));
print "Loading RiveScript brain from directory:\n$replies\n\n";
$rs->loadDirectory ($replies);
$rs->sortReplies();

print "You\'re now chatting with the RiveScript bot. Why not say hello? When\n"
	. "you get tired of this, type \"quit\" to exit this demonstration.\n\n";

while (1) {
	print "You> ";
	chomp (my $msg = <STDIN>);

	if ($msg =~ /^quit/i) {
		exit(0);
	}

	my $reply = $rs->reply ('localuser',$msg);

	print "Bot> $reply\n";
}

=head1 NAME

rsdemo - Command-line demonstration and development tool for RiveScript.

=head1 SYNOPSIS

  Usage: rsdemo
         rsdemo --debug
         rsdemo /path/to/replies
         rsdemo --debug /path/to/replies

=head1 DESCRIPTION

B<rsdemo> is a program for testing and developing RiveScript code via the
command line. Run with no arguments, rsdemo loads the default set of RiveScript
replies that are installed in your Perl lib. The default set is based on the
classic Eliza bot's personality, with additional triggers for learning and
repeating user information.

If you have a different directory containing RiveScript documents, pass the
path to that directory on the command line, and C<rsdemo> will load replies
from there instead.

=head1 OPTIONS

=over 4

=item --debug

This will enable RiveScript debug mode. A B<lot> of information is printed to
the terminal when debug mode is active.

=item --debugfile=?, --file=?, -f=?

Specify an external file for debug lines to be printed to. Since a lot of debug
information gets printed, you might want to use this in conjunction with
C<--verbose=0>.

=item --verbose=?, -v=?

Enable or disable verbose (debug) mode. This option only has an effect if debug
mode is on. If verbose is C<1> (the default), all debug information is printed
to the terminal. Set verbose to C<0> and this information will NOT go to the
terminal.

If C<debugfile> is provided, all debug information will (also) be printed to the
debug file.

=item --help

Prints the usage of the command.

=back

=head1 DEBUGGING

The C<rsdemo> tool can be used for debugging a custom set of RiveScript replies.
If you pass the C<--debug> option, debug mode is activated. By default, all
debug information will be printed to the terminal, which is likely going to be
more lines than your scrollback buffer can display. Unless you have a very small
amount of replies you're debugging, it'll be more practical to pipe the debug
information into a file and not display it on the terminal.

Here's an example:

  rsdemo --debug --verbose=0 --file=debug.txt /path/to/replies

Or a shortened example:

  rsdemo --debug -v=0 -f=debug.txt /path/to/replies

In this case, the terminal would act as normal and allow you to chat with the
bot, and all debug information would be written to debug.txt.

=cut
