#! /usr/bin/perl -w

# Build a readable log of what happened publically in a particular room
# on a Fuzzball or RF MUCK, from the horrible ugly server command logs.

# Run as: simplelog.pl <file> <room>
# Input file and room number are optional; it will ask if they're blank.

# Writes to stdout.  Messages go to stderr, so stdout can be redirected.

# People's say/osay messages as well as succ/osucc messages are lost,
# but the thread of commands is pretty well captured.

# Understands the pose, say, and spoof commands as well as some of the
# cambot object's commands.

# Copyright (c) 2001, Louis W. Erickson, All Rights Reserved.

my $file = "";
my $room = "";
my $argc = scalar @ARGV;

# Get command lines, if set
if($argc == 2)
{
	$file = $ARGV[0];
	$room = $ARGV[1];
}
else # Otherwise, ask the user.
{

	print "Log file to read? [log.txt]: ";
	$file = <STDIN>;
	chomp($file);
	if($file eq "")
	{
		$file = "log.txt";
	}

	print "Room to follow? (Full name or dbref) [Club Room]: ";
	$room = <STDIN>;
	chomp($room);
	if($room eq "")
	{
		$room = "Club Room";
	}
}

$room = "\L$room";

print STDERR "Reading $file...\n";

open(LOG, "$file") or die ("Can\'t read logfile $file.\n");

my $line = "";
my $lineCount = 0;

my %logentry = ();

my $compareroom = "";

while(<LOG>)
{
	$line = $_;
	chomp($line);

	$lineCount ++;

	%logentry = ();

	if($line =~ /(\d{2})\/(\d{2})\/(\d{2}) (\d{2}):(\d{2}):(\d{2}): WIZ: (.*)\((\d*)\) in (.*)\((\d*)\): (.*)/ or
	$line =~ /(\d{2})\/(\d{2})\/(\d{2}) (\d{2}):(\d{2}):(\d{2}): (.*)\((\d*)\) in (.*)\((\d*)\): (.*)/)
	{
		$logentry{"day"} = $1;
		$logentry{"month"} = $2;
		$logentry{"year"} = $3;

		$logentry{"hour"} = $4;
		$logentry{"minute"} = $5;
		$logentry{"second"} = $6;

		$logentry{"player"} = $7;
		$logentry{"playerid"} = $8;

		$logentry{"room"} = $9;
		$logentry{"roomid"} = $10;

		$logentry{"command"} = $11;

		$compareroom = $logentry{"room"};
		if($room eq "\L$compareroom" or $room eq $logentry{"roomid"})
		{
			runcmd(\%logentry);
		}
	}
	else
	{
		print "Unknown line: $line\n";
	}
}	

sub runcmd
{
	my $logentry = $_[0];

	my $cmdline = $$logentry{"command"};

	$cmdline =~ s/^\s+//;

	my $cmd = "";
	my $args = "";

	if($cmdline =~ /^\"([^\"].*)/)
	{
		$cmd = "say";
		$args = $1;
	}
	elsif($cmdline =~ /^:([^:].*)/)
	{
		$cmd = "pose";
		$args = $1;
	}
	else
	{
		$cmdline =~ /^([^ ]*)(.*)/;
		$cmd = $1;
		$args= $2;
	}

	$cmd = "\L$cmd";
	$cmd=~ s/^\s+//;
	$cmd=~ s/\s+$//;

	if($cmd eq "pose")
	{
		print $$logentry{"player"} . " " . $args . "\n";
	}
	elsif($cmd eq "say")
	{
		print $$logentry{"player"} . " says, \"" . $args . "\"\n";
	}
	elsif($cmd eq "spoof")
	{
		print "$args\n";
	}
	elsif($cmd eq "startlog")
	{
		print "***** Editor: The log was started.\n";
	}
	elsif($cmd eq "stoplog")
	{
		print "***** Editor: The log was stopped.\n";
	}
	elsif($cmd eq "changeline" or $cmd eq "deleteline" or $cmd eq "viewlast"
		or $cmd eq "viewlog" or $cmd eq "closeup")
	{
		print "***** Editor: " . $logentry{"player"} . " did: $cmd $args\n";
	}
	elsif($cmd eq "")
	{
		print "***** Editor: Unparsable line: $cmdline\n";
	}

}



