#!/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 
#
# @(#)22   1.12         src/rsct/cfg_access/gpfs/ct_hags_info.perl, cfg.access, rsct_relgh, relghs001a 1/25/03 13:55:09
#
# This will give the hags subsystem information for GPFS clusters
# Usage: ct_hags_info [-l] [-w]
#    -l  to get the data from the local repository (performance)
#    -w  to set the data to the repository (input from stdin)
# Input/Output:
#	REALM		CLUSTER
#       PORT            <port number>
#       LOGFILELEN      <logfile-len>           # optional
#       LOGDIRSIZE      <dir size in KB>        # optional
#
use Getopt::Std;

my %opts = ();
&getopts(':lw',\%opts);
my $mmrefresh=(defined($opts{l}) ? "norefresh" : "");
my $mmsetting=(defined($opts{w}) ? 1 : 0);


$RSCTBIN="/usr/sbin/rsct/bin";
$MMHA="/usr/lpp/mmfs/bin/mmha";
($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 = (
    "ENVIRONMENT"       => $NON_SETTABLE,
    "START_AT_BOOT"      => $NON_SETTABLE,
    "RESTART_BY_SRC"      => $NON_SETTABLE,
    "REALM" 		=> $NON_SETTABLE,
    "PORT"		=> $SETTABLE,
    "LOGFILELEN"	=> $SETTABLE,
    "LOGDIRSIZE"	=> $SETTABLE 
);

# main starts here
if($mmsetting) {
   #write the data to the repository
   &print_dbgmsg("Set data to repository");
   &set_data_to_repository();
} else {
   # show the environment and AutoStart flags
   print "ENVIRONMENT    gpfs\n";
   print "START_AT_BOOT  1\n";
   print "RESTART_BY_SRC 1\n";

   my $outcnt = &get_data_from_repository();
   if($outcnt <= 0) {
	&print_dbgmsg("No fields are read.");
	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
{
   my $outcnt = 0;
   $MMHAGETCMD="$MMHA haGetHagsInfo all $mmrefresh";
   &print_dbgmsg("$MMHAGETCMD");
   if(!open(MMCMDFILE, "${MMHAGETCMD} |")) {
	&print_errmsg("Error on '${MMHAGETCMD}'");
	exit(1);
   }

   local($line);
   my @pair = ();
   while($line = <MMCMDFILE>) {
	&print_dbgmsg("Rep: $line");
	my %kvFields = &trans_mmha_to_hash($line);
	while( @pair = each %kvFields ) {
             print "$pair[0] $pair[1]\n";
	     $outcnt++;
	}
   }
   close MMCMDFILE;
   return $outcnt;
}

# SET DATA to the repository 
sub set_data_to_repository
{
   my $count = 0;
   my %kvFields = ();
   my @fields;
   my ($key, $val, $canset);
   $MMHAPUTCMD="$MMHA haPutHagsInfo";
   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)) {
	    # process the field
	    if($canset == $SETTABLE) {
		&print_dbgmsg("Setting field: $key $val");
		$kvFields{$key} = $val;
		$count++;
	    } else {
	    	&print_dbgmsg("Unsettable field: $key $val");
	    }
	} else {
	    # not defined...unknown key
	    &print_errmsg("Unknown field: $key $val");
	}
   }
   
   if($count == 0) {
	#no fields will be updated
	&print_dbgmsg("No fields will be updated");
	return 0;
   }

   # merge the 'kvFields' as the colon-separated list
   # For example, KA=V1:KB=V2:
   my @mmInList = ();
   while( ($key,$val) = each %kvFields ) {
        if($val eq "") { $val = " "; }	# make sure 'blank' exists
        my $kvStr = join("=",($key,$val));
	push @mmInList, ($kvStr);
   }
   my $mmInStr = join(":",@mmInList);
   &print_dbgmsg("Update: $mmInStr");
   
   #update the inputs
   `$MMHAPUTCMD \"$mmInStr\"`;
   $rc = $?;
   &print_dbgmsg("$MMHAPUTCMD $mmInStr RETURNS $rc");
   
   return $count;
}

#


#
# Input:  a colon-separated mmha output line
#         eg: KA=V1:KB=V2:KC=V3:
# Output: hash table with pairs of {K}={V}
sub trans_mmha_to_hash
{
    my $parm = $_[0];
    chomp($parm);
    my @fields = split(":",$parm);
    my %outtbl = ();
    foreach $item (@fields) {
	my @kv=split("=",$item);
	if(scalar(@kv) >= 2) {
            $outtbl{$kv[0]} = join("=",@kv[1..$#kv]);
	} elsif(scalar(@kv) > 0) {
	    &print_dbgmsg("Unkown field $item");
	}
    }
    return %outtbl;
}

