#!/usr/local/bin/perl
#
# Program to find if there is any difference in the steady state.
# 
sub mkmatrix {
    my($rows, $cols) = @_;
   # --$rows; --$cols;
    my $count = 1;
    my @mx = ();
    foreach (0 .. $rows) {
    my @row = ();
    $row[$_] = $count++ foreach (0 .. $cols);
    push(@mx, \@row);
    }
    return(\@mx);
}


sub print_topo
{

    my ($num, $topo_mat) = @_;
    
    
for($myi=0;$myi<$num;$myi++)
{
	
	for($j=0;$j<$num;$j++)
	{
	 
	    print  " $topo_mat->[$myi][$j]";
	 
  	}
	print"\n";

}



}



#opening the file
$input_file_name = $ARGV[0];
$topo_file_name = $ARGV[1];
open(INFILE, $input_file_name);		
open(TOPOFILE, $topo_file_name);		

@file = <INFILE>;
@topo = <TOPOFILE>;
close(INFILE);
close(TOPOFILE);


@numnodes=split(/ /,$topo[0]);
$correct_topo_mat= mkmatrix($numneodes[0],$numnodes[0]);

$linecount=1;

for($i=0;$i<$numnodes[0];$i++)
  {
    @thisline = split(/ /,$topo[$linecount]);
    for($j=0;$j<$numnodes[0];$j++)
      {
	$correct_topo_mat->[$i][$j] = $thisline[$j];
	
      }
    $linecount++;
  }







$max =-1;
for($i=0;$i<=$#file;$i++)
{
    $_ = $file[$i];

    if(/topology\[1]\[(.*)\]\[(.*)\]/)
    {
	if($1 > $max)
	{
	    $max= $1;
	}
	
    }
}



$diff_count=0;

for($k=0;$k<=$#file;$k++)
{
    $_ = $file[$k];
    if(/%STARTSTATEDESC/)
{
#add all the intermediate lines to murph
$#murph=0;


while(!(/%ENDSTATEDESC/))
{
    push(@murph,$file[$k]);
	 $k++;
	$_ = $file[$k]; 
}

$topo_mat= mkmatrix($max,$max);

for($g=0;$g<=$#murph;$g++)
{
    $_ = $murph[$g];
   

    s/routing_tables\[(.*)\]\[(.*)\]\.distance:(.*)//g;
 
    $topo_mat->[$1-1][$2-1]=$3;

  }






$flag =0;
for($ik=0;$ik<$numnodes[0];$ik++)
{
    for($jk=0;$jk<$numnodes[0];$jk++)
    {
    if($topo_mat->[$ik][$jk]!= $correct_topo_mat->[$ik][$jk])
    {
    $flag =1;
    }  
}

}
if($flag ==1)
{

    $diff_count++;
    print_topo($max,$topo_mat);
    print "\n";
}
if ($flag != 1)
  {
    #print_topo($max,$topo_mat);
  }
}

}

printf("\nThe number of different topologies are $diff_count");

















