#!/usr/sbin/rsct/perl5/bin/perl
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
#  
#  
# Licensed Materials - Property of IBM 
#  
# (C) COPYRIGHT International Business Machines Corp. 2000,2007 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
#
# @(#)21   1.3         src/rsct/cfg_access/scaffold/ct_topology_info.pl, cfg.access, rsct_r510 12/10/00 22:24:43
#
# Usage: ct_topology_info [-l] [-w]
#    -l  to get the data from the local repository (no-use)
#    -w  to set the data to the repository (input from stdin)
# This will give the topology info

use Getopt::Std;

my %opts = ();
&getopts(':lw',\%opts);
my $mmsetting=(defined($opts{w}) ? 1 : 0);
$RSCTBIN="/usr/sbin/rsct/bin";

($dir,$progname) = $0 =~ /(.*\/)?(.*)/; # get basename of $0

#
# ValidKeys: list of all acceptable keys (either canset or not).
# In other words, the input keys must be in the list.
# Otherwise, an error will be issued.

$NON_SETTABLE = 0;
$SETTABLE = 1;

%ValidKeys = (
    "NETWORK_NAME"      => $NON_SETTABLE,
    "NETWORK_TYPE"      => $SETTABLE,
    "NETWORK_FREQ"      => $SETTABLE,
    "NETWORK_SENS"      => $SETTABLE,
    "NETWORK_NIM_EXEC"  => $SETTABLE,
    "NETWORK_NIM_PAR"   => $SETTABLE,
    "NETWORK_SRC_ROUTING" => $SETTABLE,
    "NETWORK_BCAST"     => $SETTABLE,
    "ADAPTER"           => $NON_SETTABLE
);



# main starts here
if($mmsetting) {
   #write the data to the repository
   &set_data_to_repository();
} else {
   my $outcnt = &get_data_from_repository();
   if($outcnt == 0) {
        &print_dbgmsg("No output");
        exit(1);
   }
}
exit(0);

#-------- functions -----------------------------------
# print_dbgmsg(list)
sub print_dbgmsg
{
  my @args = @_;
  if(!defined($ccal_log_inited)) {
	my $outfile = $ENV{"HA_CCAL_LOG"};
	$ccal_log_inited = 0;
	if(defined($outfile) && open(LOGOUT, ">>$outfile")) {
	    $ccal_log_inited = 1;
	    select LOGOUT;
	    $| = 1; # unbuffered
	    select STDOUT;
	}
  }
  if($ccal_log_inited) {
	my $dstr = scalar(localtime);
	my $msg = "$dstr $progname: " . join(" ", @args);
	print LOGOUT $msg, "\n" ;
  }
}

sub print_errmsg
{
   &print_dbgmsg(@_);
   print STDERR "# $progname: ", join(" ", @_), "\n";
}
#-----------------------------------------------------


# GET DATA from the repository
sub get_data_from_repository
{
   $clstname=`$RSCTBIN/ct_clusterinfo -c`;
   chomp($clstname);	# chop '\n' if exists

   # get the topology_conf file name
   $topology_conf=$ENV{'CT_TOPOLOGY_CONF_FILE'};
   if( !defined($topology_conf) || ($topology_conf eq "") ) {
	$topology_conf="/var/ct/$clstname/cfg/topology.conf";
   }

   if(!open(TOPOCFG,$topology_conf)) {
	&print_errmsg("Error on opening $topology_conf");
	exit(1);
   }

   # read TOPOLCFG
   local(@topoContents) = <TOPOCFG>;
   close TOPOCFG;

   local($net_started) = 0;
   local($net_name)="";
   foreach $topoLine (@topoContents) {
	chomp($topoLine);		# chop '\n'
	&print_dbgmsg("$topoLine");
	my(@tks) = split(" ", $topoLine);	# separate ' ='
	if( $tks[0] eq "Network" && $tks[1] eq "Name" ) {
	    $net_started = 1;
	    $net_name = $tks[2];
	    print "NETWORK_NAME $net_name\n";
	} elsif( $tks[0] eq "NETWORK_NAME" ) {
	    $net_started = 1;
	    $net_name = $tks[1];
	    print "NETWORK_NAME $net_name\n";
	} elsif($tks[0] eq "Network" && $tks[1] eq "Type") {
	    print "NETWORK_TYPE $tks[2]\n";
	} elsif($tks[0] =~ /\*InstanceNumber/) {
	    @tks = split("=", $topoLine);
	    if($tks[0] eq "*InstanceNumber") {
		print "CONFIG_INST $tks[1]\n";
	    } else {
		# ignore
	    }
	} elsif($net_started && 
	        ($tks[0] =~ /\*!TS_/ || $tks[0] =~ /\*!NIM_/) ) {
	    @tks = split("=", $topoLine);
	    if($tks[0] eq "*!TS_Frequency") {
		print "NETWORK_FREQ $tks[1]\n";
	    } elsif($tks[0] eq "*!TS_Sensitivity") {
		print "NETWORK_SENS $tks[1]\n";
	    } elsif($tks[0] eq "*!NIM_Src_Routing") {
		print "NETWORK_SRC_ROUTING $tks[1]\n";
	    } elsif($tks[0] eq "*!NIM_Broadcast") {
		print "NETWORK_BCAST $tks[1]\n";
	    }
	} elsif($net_started && $tks[0] =~ /^[0-9]/) {
	    print "ADAPTER $tks[2] $tks[1] $tks[0] $net_name\n";
	} elsif( ($tks[0] eq "NETWORK_TYPE") ||
		 ($tks[0] eq "CONFIG_INST") ||
		 ($tks[0] eq "NETWORK_FREQ") ||
		 ($tks[0] eq "NETWORK_SENS") ||
		 ($tks[0] eq "NETWORK_SRC_ROUTING") ||
		 ($tks[0] eq "NETWORK_BCAST") ||
		 ($tks[0] eq "NETWORK_NIM_EXEC") ||
		 ($tks[0] eq "NETWORK_NIM_PAR") ||
		 ($tks[0] eq "ADAPTER") ) {

	    # print joined @tks to remove white blanks
	    print join(" ",@tks), "\n";
	} else {
	    # ignore
	}
   }
   return 1;	# many lines
}


# SET DATA to the repository 
sub set_data_to_repository
{
   my $count = 0;
   my %kvFields = ();
   my @fields;
   my ($key, $val, $canset);
   my $netname = "";
   my @mmInList = ();
   while( <STDIN> ) {
	@fields = split " ";	# split input
	$key = $fields[0];
	$val = join(" ",@fields[1..$#fields]);
	next if(!defined($key) || ($key eq "") || ($key =~ /^#/));
        $key = uc $key;		# upper case
	$canset = $ValidKeys{$key};	
	if(defined($canset)) {
	    if($key eq "NETWORK_NAME") {
		#flush the current saved paramters
		if(scalar(@mmInList) > 0 && $netname ne "") {
			# update the network info
			my $mmInStr = join(":",@mmInList);
			&print_dbgmsg("Update: $netname $mmInstr");
			$count++;
		}
		#new network starts
		@mmInList = ();		# make it empty
		$netname = $val;
	    } elsif($canset == $SETTABLE) {
		my $kvStr = "$key=$val";
		push @mmInList, ($kvStr);
	    } else {
	    	&print_dbgmsg("Unsettable field: $key $val");
	    }
	} else {
	    # not defined...unknown key
	    &print_errmsg("Unknown field: $key $val");
	}
   }

   #flush the current saved paramters
   if(scalar(@mmInList) > 0 && $netname ne "") {
	# update the network info
	my $mmInStr = join(":",@mmInList);
	&print_dbgmsg("Update: $netname $mmInstr");
	$count++;
   }

   return $count;
}
