#!/usr/bin/perl -w
#=========================================================#
# onis 0.3.3                             2002-03-29 20:41 #
# http://verplant.org/onis/                               #
# Florian Forster <flo@verplant.org>                      #
#                                                         #
# onis                                                    #
#=========================================================#

BEGIN
{
	unshift (@INC, ('.', 'plugins', 'parser'));
}

use strict;
use Config;
use Getopt::Long;

use vars qw(	$starttime $user $debug
		@input $output $channel
		$data
		%color %user2id
		%id2user
		$VERSION $AUTHOR
		%plugins
		@nicklist %allnicks
	);

# setting the defaults/constants that will be used
# in the program. Setting $debug to two will give
# you way more information that you would ever
# want so be carefull..
$debug = 0;

$VERSION = '0.3.3';
$AUTHOR = 'Florian Forster <flo@verplant.org>';

%plugins = (
	action	=>	[],
	text	=>	[],
	quit	=>	[],

	calculate	=>	[],
	output		=>	[]
);

@nicklist = ();
%allnicks = ();

Getopt::Long::config ('pass_through');
my $result = GetOptions (
		'debug|verbose|v+'	=>	\$debug,
		'output|o=s'		=>	\$output,
		'channel|c=s'		=>	\$channel,
		'type|logtype|t=s'	=>	\$Config::config->{'type'},
		'overwrite|w'		=>	\$Config::config->{'overwrite'},
		'generator|g=s'		=>	\$user
	);

print "Running in debug-mode\n" if $debug;

foreach (Config->plugins ())
{
	print "Including: $_.pm\n" if $debug;
	require "$_.pm";
}

# checking for options from @ARGV. I'm pretty sure I'll change this some day
# since it's not very user-friendly..
@input = @ARGV;
#if ((scalar @input < 3 and !$channel and !$output) or (scalar @input < 2 and (!$channel or !$output)) or (!@input))
if (!defined $output or !@input)
{
	die <<EOF
Usage: ./onis [options] <logfile> [logfile] ...

	-o	--output <file>		Defines the file to write the HTML to.
	-w	--overwrite		Overwrites files without prompting.
	-c	--channel <channel>	Defines the channel's name.
	-t	--logtype <type>	Defines the logfile's type.
					See 'config' for a complete list.
	-g	--generator <name>	Define's the generator's name.
	-v	--verbose		Prints lots of debugging messages.

EOF
}

if (-e $channel or -e $output)
{
	if (-e $channel)
	{
		print 'Are you sure you channel\'s name is "' . $channel . '"? (y/N) ';
		my $answer = <STDIN>;
		exit (1) unless $answer =~ /[yzj]/i;
	}
	if ((-e $output) and !($Config::config->{'overwrite'}))
	{
		print "The output file ($output) already exists. Are you sure you want to overwrite it? (Y/n) ";
		my $answer = <STDIN>;
		print "To disable this dialogue you have to select the 'overwrite' option in the config file.\n";
		exit (1) if $answer =~ /n/i;
	}
}

$starttime = time ();

print "Input: " . join (', ', @input) . "\n" if $debug;
print "Output: $output\n" if $debug;
print "Channel: $channel\n" if ($debug and $channel);

require $Config::config->{'type'} . '.pm';

foreach (@input)
{
	open (LOGFILE, '< ' . $_) or die $!;

	foreach my $line (<LOGFILE>)
	{
		$line =~ s/\n|\r//g;
		parser::parse ($line);
	}

	close LOGFILE;
}
print "All data read.\n" if $debug;

require 'html.pm';

foreach (@{$plugins{'calculate'}})
{
	&$_ ();
}

while (!$channel)
{
	print 'Could not detect the channel\'s name. Please enter it manually: ';
	$channel = <STDIN>;
	chomp ($channel);
}

if ($Config::config->{'html_header'} != 0) { html::start (); }
html::head ();

foreach (@{$plugins{'output'}})
{
	&$_ ();
}

if ($Config::config->{'copyleft'} == 1) { html::copyleft (); }
if ($Config::config->{'html_header'} == 1) { html::end (); }
print "Status: $output successfully generated\n" if $debug;

exit (0);
