#!/usr/bin/perl
#
# Petr Simandl, www.simandl.cz
# v 0.0.2

print "Content-type: text/html\n\n";

print "<HTML><HEAD><META HTTP-EQUIV=Pragma CONTENT=no-cache></HEAD><BODY>\n";

$date = `date`;
print "<BR>$date\n<BR>";

print "<pre>";

@ol_names=`cat /home/www/cgi-bin/netmap/data/jablonecka.comp`;
@ol_database=`/home/www/cgi-bin/ospfd_database.cgi`;
#print @ol_names;

foreach $ol_line (@ol_names)
{
@ol_pole=split(";",$ol_line);
#print "$ol_pole[0] $ol_pole[1]\n";
}

print "Vypis routeru s jednotlivymi linky\n";
#print @ol_database;

@ol_routers=();
@ol_links=();
$ol_router="";
$ol_link="";

#picking up routers and their links into fields ol_routers and ol_links
foreach $ol_line (@ol_database)
{
  if ($ol_line =~ /Advertising/)
  {	@ol_string=split(" ",$ol_line);
	print "\nrouter $ol_string[2]\n";
	#storing previous router 
	if ($ol_router eq "")
	#no previous found yet
	{	print "prvni polozka\n";
	}
	else
	{
		print "zapis routeru\n";
		push(@ol_routers, $ol_router );
#		print "@ol_routers\n";
#		print "@ol_links\n";
	}

	#prefilling with IP
	$ol_router=$ol_string[2];
	#using netmap config to substittute names instead IPs
        foreach $ol_name (@ol_names)
	{   @ol_name_split=split(";",$ol_name);
	    #compare IPs
    	    if ("$ol_name_split[1]" eq "$ol_string[2]")
    		{ print "jsou stejne $ol_name_split[0] a $ol_string[2]\n";
		  #name found so we will store it
		  $ol_router=$ol_name_split[0];
	        }
        }

	
	
#	$ol_router=$ol_string[2];
	$ol_link="";
  }

  #looking for first (central) side of link
  if ($ol_line =~ /Designated/)
  {  	@ol_string=split(" ",$ol_line);
#	print "ifA $ol_string[5]\n";
	$ol_link=$ol_string[5];
  }

  #looking for second (local) side of link and storing this link into the ol_links
  if ($ol_line =~ /Interface/)
  {  	@ol_string=split(" ",$ol_line);
#	print "ifB $ol_string[5]\n";
	#adding only second local side
	$ol_router=$ol_router." ".$ol_string[5];
	$ol_link=$ol_link." ".$ol_string[5];
	push(@ol_links, $ol_link );
	$ol_link="";
  }
}

#storing the last router
print "Posledni zapis routeru\n";
push(@ol_routers, $ol_router );
print "@ol_routers\n";


print "\n\nVypis pole routeru\n";

print "@ol_routers";

print "\n\nVypis pole linku\n";
print "@ol_links";

####################################################

print "\n\nVypis pole linku redukovanych\n";

#kicking links with same begin and end
@ol_links_reduced=();

foreach $ol_line (@ol_links)
{   @ol_string=split(" ",$ol_line);
    if ("$ol_string[0]" eq "$ol_string[1]")
        { #print "jsou stejne $ol_string[0] a $ol_string[1]\n";
	}
	else
	{
	push(@ol_links_reduced, $ol_line );
	#print "added $ol_line\n";
	}

}


print "@ol_links_reduced";

#prvni cast linku je vzdy designated
#seradit podle prvni casti linku
#roztridit do skupin podle prvni casti linku
#pokud chceme -zatim nedelat -  tak ve skupine vybrat nejmensi ip a vlozit ho za designated
#stale mame dvojice linku
#jdeme v seznamu routeru ol_routers a pekne v seznamu ol_links zamenime hlavni IP routeru za IP jeho rozhrani 

print "\n\nVytvareni jmennych linku\n";

#going through all links and fill router names into links instead of interface IPs 
@ol_links_named=();
foreach $ol_link (@ol_links_reduced)
{   @ol_ips=split(" ",$ol_link);
    print "$ol_link";
    #initially we fill both sides of link with real IPs
    $ol_ipa=$ol_ips[0];
    $ol_ipb=$ol_ips[1];
    foreach $ol_router_line (@ol_routers)
    {   @ol_router_ifaces=split(" ",$ol_router_line);
        $ol_router_name=shift(@ol_router_ifaces);
	#print "$ol_router_name ma @ol_router_ifaces\n";
        foreach $ol_iface (@ol_router_ifaces)
        { #print "$ol_iface\n";
	  if ("$ol_iface" eq "$ol_ips[0]")
	  { $ol_ipa=$ol_router_name;
	  }
	  if ("$ol_iface" eq "$ol_ips[1]")
	  { $ol_ipb=$ol_router_name;
	  }
	}
    }
    #
    push(@ol_links_named, $ol_ipa." ".$ol_ipb );
}

print "\n\n@ol_links_named\n";
foreach $ol_link (@ol_links_named)
{   @ol_name=split(" ",$ol_link);
    print "02;$ol_name[0];$ol_name[1];\n";
}



print "</pre></BODY></HTML>";

exit
