#!/usr/local/bin/perl -I../lib -w
 
#-----------------------------------------------------------------------

=head1 NAME

B<splitter> - Split the log file using the flex built lexical analyzers.

=head1 SYNOPSIS

  splitter
    [-help] |
    [-version] |
    [-verbose] 
    [-parser directory] 
    [-databaseName databaseName] 
    [-databaseMode GDBM | DBM | BSD] 

=head1 DESCRIPTION

B<splitter> reads the specification files for the dimensions
and creates the necessary databases.

=head1 EXAMPLE USAGE

Create the specifications.

 perl -I../lib splitter \
    -verbose \
    -parser parser \
    -databaseName databaseName \
    -databaseMode DBM \

See also L<IncompleteDataCube>.

=cut

#-----------------------------------------------------------------------

use strict;
require 5.002;

use Getopt::Long;
use IO::Pipe;
use IO::File;
use English;
use IncompleteDataCube;

#-----------------------------------------------------------------------

=head1 OPTIONS

=over 4

=item -help

Display a short help message with a reminder of supported
command-line options.

=item -version

Display the version of B<splitter>.

=item -verbose

Enable verbose reporting.

=item -parser parser

The name of the parser directory, defaults to parser.

=item -databaseName databaseName

The name of the database, overides the default name 
in L<IncompleteDataCube::Constants>.

=item -databaseMode databaseMode

The mode of the database, overides the default mode
in L<IncompleteDataCube::Constants>.

=back

=cut

#-----------------------------------------------------------------------

use vars qw($VERSION);

my $VERSION       = '1.00';
my $SHOW_VERSION  = 0;
my $VERBOSE       = 0;
my $HELP          = 0;
my $COMMAND_NAME  = 'splitter';
my $PARSER= 'parser';
my $LOG_FILE = '';

#-----------------------------------------------------------------------
# Parse the command line
#-----------------------------------------------------------------------
&ParseCommandLine();

#/**
#* Split the log file, input comes in on std input
#**/

my @fh;

for (my $i = 0; $i < $IncompleteDataCube::Constants::dimensions; $i++) {
  my $dimension = @$IncompleteDataCube::Constants::dimensionNames[$i];
  $fh[$i] = new IO::File "| $PARSER/$dimension > $PARSER/$dimension.out";
  die "$COMMAND_NAME: could not open $dimension." unless defined $fh[$i];
  }

my $logFileName = $IncompleteDataCube::Constants::logFileSplitFileName;
my $flog = new IO::File "$PARSER/$logFileName < $LOG_FILE |";
die "$COMMAND_NAME: could not open $LOG_FILE\n" unless defined $flog;

# read until done
while (<$flog>) {
  my $h = $fh[0];
  print $h "$_";
  for (my $i = 1; $i < $IncompleteDataCube::Constants::dimensions; $i++) {
    my $h = $fh[$i];
    my $s = <$flog>;
    print $h "$s";
    }
  } 

#------------------------------------------------------------------------
# ParseCommandLine() - handle command line
#------------------------------------------------------------------------
sub ParseCommandLine {
  my @switches = (
    'databaseMode=s', \$IncompleteDataCube::Constants::databaseMode,
    'databaseName=s', \$IncompleteDataCube::Constants::databaseName,
    'help',           \$HELP,
    'verbose',        \$VERBOSE,
    'version',        \$SHOW_VERSION,
    'parser',         \$PARSER,
    );

  &GetOptions(@switches) || die "use -help switch to display brief help\n";

  if ($SHOW_VERSION) {
    print "This is $COMMAND_NAME, version $VERSION\n";
    exit 0;
    }

  if ($HELP) {
    print <<HelpEnd;
    $COMMAND_NAME, v$VERSION - read the log files and split it 

    Usage: $COMMAND_NAME 
                         [-help] |
                         [-version] |
                         [-verbose]
                         [-parser directory]
                         [-databaseName databaseName]
                         [-databaseMode GDBM | DBM | BSD]
                         logFile

        -help            : display this message
        -verbose         : display verbose information as running
        -parser directory : where lieves the parser
        -databaseName name : name of the database
        -databaseMode mode : mode for the database 

HelpEnd
    exit 0;
    }

  $LOG_FILE = shift @ARGV || die "$COMMAND_NAME: Must input a log file name!";

}

