#!/u01/app/perl/bin/perl -w


use strict;
use Data::Dumper;

use lib qw(./);

use TNSParser;

my $tnsfile=shift;

my $tnsdata  = new TNSParser($tnsfile);

#print Dumper($tnsdata);
#print Dumper($tnsdata->{TNSDATA});

#foreach my $tnsname ( keys %{$tnsdata->{TNSDATA}} ) { print "$tnsname\n" } 

#walk through entire tns list
while ( my $tns = $tnsdata->next  ) {

	#print Dumper($tns);
	my @key = keys %{$tns};
	my $tnsName = $key[0];

	print '=' x 60, "\n";
	print "==== $tnsName\n";

	# get Connect info
	print "   ===== connect info\n";
	foreach my $connectKey ( keys %{$tns->{$tnsName}{connect}} ){
		print "   key: $connectKey  value: $tns->{$tnsName}{connect}{$connectKey}\n";
	}

	# get host(s)
	#print Dumper($tns->{addresses});
	my $i=0;
	foreach my $addressHash ( @{$tns->{$tnsName}{addresses}} ) {
		print "   ===== address $i\n";
		foreach my $addressKey ( keys %{$addressHash} ) {
			print "   key: $addressKey  value: $addressHash->{$addressKey}\n";
		}
		$i++;
		#print Dumper($addressHash);
	}

	#print Dumper($tns->{$tnsName});
}

print '+' x 50, "\n\n";

# look up specific tnsname entries
foreach my $testTNS ( qw( agile testfail sample1 ) ) {

	print '=' x 60, "\n";
	print "==== $testTNS\n";

	my $tnsConnectInfo = $tnsdata->getTnsname($testTNS);
	#print Dumper($tnsConnectInfo) if defined $tnsConnectInfo; #->{$testTNS};

	# get Connect info
	print "   ===== connect info\n";
	foreach my $connectKey ( keys %{$tnsConnectInfo->{connect}} ){
		print "   key: $connectKey  value: $tnsConnectInfo->{connect}{$connectKey}\n";
	}

	# get host(s)
	#print Dumper($tns->{addresses});
	my $i=0;
	foreach my $addressHash ( @{$tnsConnectInfo->{addresses}} ) {
		print "   ===== address $i\n";
		foreach my $addressKey ( keys %{$addressHash} ) {
			print "   key: $addressKey  value: $addressHash->{$addressKey}\n";
		}
		$i++;
		#print Dumper($addressHash);
	}

}


