#!/usr/bin/perl
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
#  
#  
# Licensed Materials - Property of IBM 
#  
# (C) COPYRIGHT International Business Machines Corp. 2001,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 
#########################################################################
# cthagstune - script to tune the Group Services tunables. 
# Syntax:
#     cthagstune [-l log_length] [-d log_dirsize] [-o option_str] 
#     cthagstune [-h]
#
# Description: change and see Group Services tunables
#
#     options:
#     -h print usage message
#     -l maximum log file length. if the value is 0 or negative,
#        a default log file length will be used.
#     -d max. log dir size in KB. -1 for default 
#     -o comma-separated expandable option strings
#        (currently, none is defined)
# Returns
#	0 if sucessful
#	1 if error
#
# Note:
#     cthagstune will print the current tunables if no option is given
########################################################################
# sccsid = "@(#)43   1.4   src/rsct/pgs/cmds/cluster/cthagstune.perl, gsctrl, rsct_rady, rady2035a 11/12/15 16:44:53"

#
use Getopt::Std;

my %opts = ();
&getopts(':l:d:o:h', \%opts);

$RSCTBIN="/opt/rsct/bin";
($dir,$progname) = $0 =~ /(.*\/)?(.*)/; # get basename of $0


$gs_filelen = $opts{l};		#log file len
$gs_dirsize = $opts{d};		#log dir size
$gs_optstr = $opts{s};		#option string (currently ignored)
$gs_help = $opts{h};		#help?

#-----------------------------------------------------

# 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" ;
  }
}

#-----------------------------------------------------

if(defined($gs_help)) {
   $msgcmd = "$RSCTBIN/hadspmsg hagsctrl ha_gs.cat IMsg_cthagstune_Usage $progname $progname $progname";
   system($msgcmd);
   exit(1);
}
  
$HAGSINFOCMD="/opt/rsct/bin/ct_hags_info";

if(defined($gs_filelen) || defined($gs_dirsize)) {
   #update the options
   if(open(GSINFOFILE, "| ${HAGSINFOCMD} -w")) {
	if(defined($gs_filelen)) {
		&print_dbgmsg("LOGFILELEN $gs_filelen");
		print GSINFOFILE "LOGFILELEN $gs_filelen\n";
	}
	if(defined($gs_dirsize)) {
		&print_dbgmsg("LOGDIRSIZE $gs_dirsize");
		print GSINFOFILE "LOGDIRSIZE $gs_dirsize\n";
	}
	close $GSINFOFILE;
   } else {
	# Error in writing to '$HAGSINFOCMD -w'
        $msgcmd = "$RSCTBIN/hadspmsg hagsctrl ha_gs.cat EMsg_Write_Error $HAGSINFOCMD";
        system($msgcmd);
	exit(1);
   }
}

# list the current options

my %gsInfo = ();
if(open(GSINFOFILE, "${HAGSINFOCMD} |")) {
   @lines = <GSINFOFILE>;
   %gsInfo = &trans_list_to_hash(@lines);
   close GSINFOFILE;

   my $filelen = $gsInfo{"LOGFILELEN"};
   my $dirsize = $gsInfo{"LOGDIRSIZE"};
   if(defined($filelen)) {
	print "LOGFILELEN $filelen\n";
   }
   if(defined($dirsize)) {
	print "LOGDIRSIZE $dirsize\n";
   }
} else {
   #error in obtaining current info....
   # Error in reading from '$HAGSINFOCMD'
   $msgcmd = "$RSCTBIN/hadspmsg hagsctrl ha_gs.cat EMsg_Read_Error $HAGSINFOCMD";
   system($msgcmd);
   exit(1);
}


exit 0;


#
#
# Input:  list of a pair of <K> and <V>
#	eg: K1 V1
#	    K2 V2
# Output: hash table with pairs of {K}={V}
sub trans_list_to_hash
{
    my @lines = @_;
    my %outtbl = ();
    foreach $aline (@lines) {
	chomp($aline);
        my @kv=split(" ",$aline);
        $outtbl{$kv[0]} = join(" ",@kv[1..$#kv]);
    }
    return %outtbl;
}