#!/usr/bin/perl -w
use strict;
use DBI;
use lib qw(/home/jeremy/Documents/scripts/);
use tools;

my $file = "htout.tex";
open IN,$file;
my @data = <IN>;
close IN;

my @header = split(/\&/,$data[0]);
my %indexes = ();

my $t = 2;
foreach my $i (@header){
	my $slash = index($i,"/");
	if( $slash > -1 ){
		$indexes{substr($i,$slash+1,1)} = $t;
		$t++;
	}
}

sub sortString {
	my $str = $_[0];
	my @tmp = ();
	for(my $i=0;$i<length($str);$i++){
		push @tmp,substr($str,$i,1);
	}
	@tmp = sort @tmp;
	$str = '';
	foreach my $i (@tmp){
		$str .= $i;
	}
	$str =~ s/\s+//g;
	return($str);
}

sub getThisIdx {
	my $idx = $_[0];

	my $firstBrace = index($idx,"{");
	substr($idx,0,$firstBrace + 1) = '';

	my $secondBrace = index($idx,"}");
	substr($idx,$secondBrace,length($idx) - $secondBrace) = '';

	if( index($idx,"(") > -1 ){
		substr($idx,index($idx,"("),1) = '';
		substr($idx,index($idx,")"),1) = '';
	}

	$idx = sortString($idx);

	return($idx);
}

sub noEndMatter {
	my $str = $_[0];
	substr($str,index($str,"\\\\"),length($str)-index($str,"\\\\")) = '';
	return($str);
}

sub containedIn {
	my ($isin,$base) = @_;
	my $all = 1;

	for(my $i=0;$i<length($isin);$i++){
		my $s = substr($isin,$i,1);
		if( index($base,$s) < 0 ){
			#print "$isin\t$base\t$s\n";
			$all = 0;
		}
	}

	return($all);
}

my %texnames = ();

sub getType {
	my ($factor) = @_;
	my $interaction = substr($factor,1,index($factor,"_")-1);

	if( $interaction eq "\\varepsilon" ){
		return("\\sigma_{" . $interaction . "}^{2}");
	}

	open FAC,"factors";
	my @f = <FAC>;
	close FAC;

	my %list = ();
	foreach my $i (@f){
		chomp $i;
		my @line = split(/\t/,$i);
		$list{$line[0]} = $line[1];
	}

	my $random = 0;
	for( my $i=0;$i<length($interaction);$i++){
		my $letter = substr($interaction,$i,1);
		if((defined($list{$letter}))&&( $list{$letter} eq 'R' )){
			$random = 1;
		}
	}

	if( $random == 1 ){
		return("\\sigma_{" . $interaction . "}^{2}");
	}
	return("\\Phi(" . $interaction . ")");
}

open OUT,">postout.tex";

#print OUT "\\begin{center}\n\\begin{longtable}{lll}\n";
my @results = ();

for(my $i=(scalar(@data) - 1);$i>0;$i--){
	my @current = split(/\&/,noEndMatter($data[$i]));
	my $c_idx = getThisIdx($current[0]);
	my $EMS = '';

	for(my $j=(scalar(@data) - 1);$j>=$i;$j--){
		my @tmp = split(/\&/,noEndMatter($data[$j]));
		my $t_idx = getThisIdx($tmp[0]);
		my $factor = 1;

		if( containedIn($c_idx,$t_idx) > 0 ){
			foreach my $k (keys %indexes){
				if( index($c_idx,$k) < 0 ){
					$factor *= $tmp[$indexes{$k}];
				}
			}
		}else{
			$factor = 0;
		}

		if( ! defined($texnames{$tmp[0]}) ){
			$texnames{$tmp[0]} = getType($tmp[0]);
		}

		if( $factor > 1 ){
			$EMS .= $factor . $texnames{$tmp[0]} . " + ";
		}elsif( $factor == 1 ){
			$EMS .= $texnames{$tmp[0]} . " + ";
		}

	}
	substr($EMS,rindex($EMS," + "),3) = '';
	if( $current[1] == 0 ){
		$EMS = "- -";
	}
	my $outstr = StringTools::linetostr("&","\\\\",$current[0],$current[1],"\$$EMS\$");
	push @results,$outstr;
	#print OUT $outstr;
}

@results = reverse @results;

foreach my $i (@results){
	print OUT $i;
}

#print OUT "\\end{longtable}\n\\end{center}";

close OUT;
