#!/usr/bin/perl
#
# usage:
#   ./parselog.pl --logdir=dirname
#   ./parselog.pl --onefile=filename


use strict;
use Getopt::Long;

package LogLine;
sub new {
	my $class = shift;
	my $self = {};
	bless $self, $class;
	return $self->parse(shift) if (@_);
}

sub parse {
	my $self = shift;
	$_ = shift;
	return undef if /^--/;  # logging software comments
	s/^(\d\d:\d\d)\s+\[notice\((\S+)\)\] \(#l5r\)/$1 -PugMajere:@#l5r-/i;
	if (/^(\d\d:\d\d)\s+(\* (\S+)|<[+@]?(\S+)>|-(\S+):[+@]?#\S+-) (.*)/) {
		$self->{text} = $6;
		$self->{nick} = $3 . $4 . $5;
		$self->{time} = $1;
		$self->{type} = $2 ? 'act' : 'say';
	} elsif (/^(\d\d:\d\d)\s+-!- (\S+) \[.*\] (has joined.*|has left.*|has quit.*|killed by.*)/) {
		$self->{text} = "$2 $3";
		$self->{nick} = "SERVER";
		$self->{time} = $1;
		$self->{type} = 'server';
	} elsif (/^(\d\d:\d\d)\s+-!- (\S+ is now known as \S+|\S+ was kicked.*|\S+ changed the topic.*)/) {
		$self->{text} = $2;
		$self->{nick} = "SERVER";
		$self->{time} = $1;
		$self->{type} = 'server';
	} elsif (/^\d\d:\d\d\s+-!- (Server)?[mM]ode\/#/) {
		return undef;  # ignore voicing/opping
	} elsif (/^\d\d:\d\d\s+-!- Netsplit/) {
		return undef;  # ignore Netsplits -- might want to track who's here, tho
	} elsif (/^\d\d:\d\d\s+-!- Irssi:/) {
		return undef;  # ignore program notes
	} else {
		print "couldn't understand log line: $_\n";
		return undef;
	}
	return $self;
}

sub stringwithnick {
	my $self = shift;
	return $self->{nick} . ' ' . $self->text;
}

sub stringnonick {
	my $self = shift;
	return $self->{text};
}	


package main;

my ($logdir, $onefile) = ('.', '');
GetOptions("logdir=s" => \$logdir, "onefile=s" => \$onefile);

my @files = $onefile ? ($onefile) : <$logdir/*>;
die "No files to use" unless @files;

my @lines;




my $baba =0;
foreach my $file (@files) {
	open READ, $file;
	while (<READ>) {
		my $line = LogLine->new($_);
		push @lines, $line if defined $line;

		if ( defined $line )
		{
		    print $baba++, ":", $line->{text}, "\n";
		}
	}
}
print scalar @lines, " total lines.\n";
