#!/usr/bin/perl

use strict;
use vars;
my $OPTIONS;

    if (@ARGV < 2)
    {
        print "Usage: $0 <logfilename> <outputPrefix> [ <formatOptions> [ <formatOptions> ] ]\n\n";
        print "Extracts tables from querystream logfiles, formats and splits them into files which can easily be identified by the query number.\n";
        print "\tlogfilename   The name of the logfile for the querystream containing the tables.\n";
        print "\toutputPrefix  A prefix for the filename added before the query number, i.e. with prefix 'q' the output files are called q1.out - q22.out\n";
        print "\tformatOptions List of format options for in following format q<QUERY>c<COLUMN>o<OPTIONS> i.e. q1c1oZ q2c4oR q3c1oRZ\n";
        print "\t              Options:\n";
        print "\t                  R - right trunc\n";
        print "\t                  Z - remove .00 at the end of numbers\n";
        exit (0);
    }
    elsif (@ARGV > 2)
    {
        for(my $i=2;$i<@ARGV;$i++)
        {
            my $opt = $ARGV[$i];
            if ($opt =~ /^q(\d+)c(\d+)o([RZ]+)$/)
            {
                my $q = $1;
                my $c = $2;
                my $o = $3;
                $OPTIONS->{$q}->{$c} = $o;
            } else
            {
                die "Wrong option format: $opt";
            }
        }
    }
    
    my $LOGFILE = $ARGV[0];
    my $PREFIX = $ARGV[1];
    
    open(INF, $LOGFILE) or die "Can't open file $LOGFILE!";
    my $CQ = 0; #current query
    my $OUTF;
    my $mode = 0; # 0 - query name search; 1 - table header search; 2 - table body print
    my $pline = "";
    while (<INF>)
    {
        if ($mode == 0)
        { #query
            if (/--\s+TPC-H Query (\d+)/)
            {
                $CQ = $1;
                print "Q$CQ\n";
                close($OUTF) if ($OUTF);
                open($OUTF, ">$PREFIX$CQ.out") or die;
                $mode = 1;
            }
        } 
        elsif ($mode == 1)
        { #header
            if (/^=+$/) # match ^=================...=====================$
            {
                print $OUTF $pline;
                print $OUTF $_;
                $mode  = 2;
            }
            $pline = $_;
        } 
        elsif ($mode == 2)
        { #table
            if (/^\s+$/) # match white spaces line 
            {
                #found empty line => end of table
                $mode = 0;
            } else
            {
                if ($OPTIONS->{$CQ})
                {
                    my @cols = split(/\|/, $_);
                    for(my $c=0;$c<@cols;$c++)
                    {
                        my $opt = $OPTIONS->{$CQ}->{$c};
                        if ($opt)
                        {
                            $cols[$c] =~ s/\.00$// if ($opt =~ /Z/); #remove .00
                            $cols[$c] =~ s/\s+$// if ($opt =~ /R/); #right trim
                        }
                    }
               
                    print $OUTF join('|', @cols);
                } else
                {
                    print $OUTF $_;
                }
            }
        }
    }
    close($OUTF) if ($OUTF);
    close(INF);
    
    
