package diffutil;

BEGIN {
    die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
    die "GSDLOS not set\n" unless defined $ENV{'GSDLOS'};
    unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
    unshift (@INC, "$ENV{'GSDLHOME'}/perllib/cpan");
}
use util;



sub files_in_dir
{
    my ($dirname) =@_;

    opendir(DIR, $dirname) || die "can't opendir $dirname: $!";
    my $skipdirs_re = qr/^cached$/;
    @local_files = grep { /^\w/ && ((-d  &util::filename_cat($dirname,$_) && $_ !~ m/$skipdirs_re/) || -f &util::filename_cat($dirname,$_))} readdir(DIR);
    closedir DIR;

    return @local_files;
}

sub GetErrors
{
    my ($aryErrors,$aryReturnErrors,$intLineCount) = @_;
    for(my $intCounter = 0; $intCounter < $intLineCount ; $intCounter++)
    {
	if(my $strAError = shift(@{$aryErrors}))
	{
	    $strAError =~ s/^(<( )*)|^(>( )*)//g;
	    push(@{$aryReturnErrors},$strAError);
	}
	else {return 0;}
    }
    return 1;
}

sub IgnoreExp
{
    my ($strModel,$strTest,$strIgnoreExp) = @_;
    
    if($strIgnoreExp eq "")
    {
	return 0;
    }
    else
    {
	if($strModel =~ m/$strIgnoreExp/ && $strTest =~ m/$strIgnoreExp/) #if($strModel =~ m/$strIgnoreExp/mg && $strTest =~ m/$strIgnoreExp/mg)
	{
	    return 1;
	}
	else {return 0;}
    }
}

sub GenerateOutput
{
    my ($strResult,$strIgnoreExp) = @_;
    if($strResult eq "") {return "";}

    my @aryErrors = split ("\n",$strResult);
    my $strErrorMessage = "";
    my $intCounter = 0;
    my $hashptErrorLines;

    while(my $strFirstLine = shift(@aryErrors))
    {
	my $charResultChar;
	my @aryModelErrors;
	my @aryTestErrors;
	my $strAError = "";

	if($strFirstLine =~ m/(\d+,)*\d+[a-z]\d+(,\d+)*/)
	{
	    $charResultChar =  $strFirstLine;
	    $charResultChar =~ s/\d|\W//g;
	    my @aryRightLeft = split($charResultChar,$strFirstLine);
	    my @aryModelLines = split(",",$aryRightLeft[0]);
	    my @aryTestLines = split(",",$aryRightLeft[1]);
	    my $intModelLineCount = 0;
	    my $intTestLineCount = 0;

	    if(scalar(@aryModelLines)> 2 || scalar(@aryTestLines)> 2){die "Error Found!!\n";}

	    $intModelLineCount = (scalar(@aryModelLines) == 1) ? 1 : ($aryModelLines[1] - $aryModelLines[0] + 1);
	    $intTestLineCount = (scalar(@aryTestLines) == 1) ? 1 : ($aryTestLines[1] - $aryTestLines[0] + 1);
	    
	    if($charResultChar eq "c")
	    {
		die "Nothing to parse 1\n" unless(&GetErrors(\@aryErrors,\@aryModelErrors,$intModelLineCount) == 1);
		
		if($aryErrors[0] eq "---")
		{
		    my $strExtra = shift(@aryErrors);
		}
		else
		{
		    print "Incorrect format of diff program\n";
		}
		#die "Nothing to parse 2\n" unless(my $strExtra = shift(@aryErrors));
		#die "Error Occoured: $strResult\n" unless($strExtra eq "---");

		die "Nothing to parse 3\n" unless(&GetErrors(\@aryErrors,\@aryTestErrors,$intTestLineCount) == 1);

		if($intModelLineCount == 1 && $intTestLineCount == 1 && 
		   &IgnoreExp($aryModelErrors[0],$aryModelErrors[0],$strIgnoreExp) == 1)
		   #&IgnoreExp($aryModelErrors[0],$aryTestErrors[0],$strIgnoreExp) == 1) # 2nd param should be aryTestErrors?
		{
		    $strAError = "";
		}
		else
		{
		    $strAError .= "Differences found between Model and Test Collection\n";
		    $strAError .= "--------------------------------------------------------------------------\n";
		    
		    $strAError .= "Model Collection:\n";
		    for(my $intCounter = 0; $intCounter < $intModelLineCount ; $intCounter++)
		    {
			my $intOutLine = $aryModelLines[0] + $intCounter;
			$strAError .= " Line $intOutLine  \t($aryModelErrors[$intCounter])\n";
		    }
		    $strAError .= "Test Collection:\n";
		    for(my $intCounter = 0; $intCounter < $intTestLineCount ; $intCounter++)
		    {
			my $intOutLine = $aryTestLines[0] + $intCounter;		
			$strAError .= " Line $intOutLine  \t($aryTestErrors[$intCounter])\n";
		    }		
		}
	    }
	    
	    elsif($charResultChar eq "a")
	    {
		die "Nothing to parse 4\n" unless(&GetErrors(\@aryErrors,\@aryTestErrors,$intTestLineCount) == 1);
		$strAError .= "Missing in Model Collection\n"; 
		$strAError .= "--------------------------------------------------------------------------\n";
		$strAError .= "In Test Collection but not in Model Collection (Mismatch from Line $aryModelLines[0]):\n";
		for(my $intCounter = 0; $intCounter < $intTestLineCount ; $intCounter++)
		{
		    my $intOutLine = $aryTestLines[0] + $intCounter;	
		    $strAError .= " Line $intOutLine  \t($aryTestErrors[$intCounter])\n";
		}
	    }
	    
	    elsif($charResultChar eq "d")
	    {
		die "Nothing to parse 5:$strResult\n" unless( &GetErrors(\@aryErrors,\@aryModelErrors,$intModelLineCount) == 1);
		$strAError .= "Missing in Test Collection\n";
		$strAError .= "--------------------------------------------------------------------------\n";
		$strAError .= "In Model Collection but not in Test Collection (Mismatch from Line $aryTestLines[0]):\n";
		for(my $intCounter = 0; $intCounter < $intModelLineCount ; $intCounter++)
		{
		    my $intOutLine = $aryModelLines[0] + $intCounter;
		    $strAError .= " Line $intOutLine  \t($aryModelErrors[$intCounter])\n";
		}
	    }
	    else
	    {
		die "Missing comparsion type!!\n";
	    }
	    
	}
	else
	{
	    die "$strResult\n";
	}
	if($strAError ne ""){$strErrorMessage .= "\n".$strAError;}
    }

    return $strErrorMessage;
}

1;
