#!/usr/bin/perl
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
#  
#  
# Licensed Materials - Property of IBM 
#  
# (C) COPYRIGHT International Business Machines Corp. 2000,2019 
# 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 
#
# sccsid = "@(#)32   1.13   src/rsct/cfg_access/scaffold/ct_hags_info.perl, cfg.access, rsct_rady, rady2035a 11/12/15 16:44:05"
#
# Usage: ct_hags_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 hags subsystem information
#       REALM		CLUSTER
#	PORT 		<port number>
#	LOGFILELEN 	<logfile-len>		# optional
#	LOGDIRSIZE	<dir size in KB>	# optional
#
use Getopt::Std;

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

$RSCTBIN="/opt/rsct/bin";
$SUBSYS_PREFIX = "cthags";
($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
    &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
{
   # print REALM
   print "REALM         CLUSTER\n";

   # show the environment and AutoStart flags
   print "ENVIRONMENT    scaffold\n";
   print "START_AT_BOOT  1\n";
   print "RESTART_BY_SRC 1\n";

   if( ! -e "$RSCTBIN/ct_clusterinfo" ) {
   	&print_errmsg("No ${RSCTBIN}/ct_clusterinfo found.");
	exit(1);
   }

   $CLSTNAME = `$RSCTBIN/ct_clusterinfo -c`;
   chomp($CLSTNAME);
   if( !defined($CLSTNAME) || $CLSTNAME eq "" ) {
	&print_errmsg("No cluster name found.");
	exit(1);
   }

   $SUBSYSCONF = $ENV{"CT_SUBSYS_CONF_FILE"};
   if( !defined($SUBSYSCONF) ) {
	$SUBSYSCONF = "/var/ct/$CLSTNAME/cfg/subsys.conf";
   }

   if(!open(SUBFP, "<$SUBSYSCONF")) {
	&print_errmsg("No $SUBSYS found..");
	exit(1);
   }

   $PORT = 0;
   while (<SUBFP>) {
        chomp($_);       # Remove trailing '\n'
        # 2 simple s/// are faster than one complex s///.
        s/^[ \t\n]*//;   # Remove possible leading spaces
        s/#.*$//;       # Remove possible trailing comments
        ($keyword, $keyvalue) = split(/[ \t\n]+/, $_, 2);
	if($keyword eq $SUBSYS_PREFIX) {
		$PORT = $keyvalue;
	}
   }

   # print PORT
   if(defined($PORT) && ($PORT != 0)) {
	print "PORT         $PORT\n";
   }

   print "LOGFILELEN    -1\n";
   print "LOGDIRSIZE    -1\n";

   return 4;	# 4 lines of output
}


# SET DATA to the repository 
sub set_data_to_repository
{
   my $count = 0;
   my %kvFields = ();
   my @fields;
   my ($key, $val, $canset);
   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) {
		$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
	return 0;
   }

   # merge the 'kvFields' as the colon-separated list
   # For example, KA=V1:KB=V2:
   my @mmInList = ();
   my @kvpair;
   while( @kvpair = each %kvFields ) {
	my $kvStr = join("=",@kvpair);
	push @mmInList, ($kvStr);
   }
   my $mmInStr = join(":",@mmInList);
   &print_dbgmsg("Update: $mmInStr");
   
   return $count;
}