#! /bin/ksh
# 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 = "@(#)02   1.37   src/rsct/pgs/cmds/cluster/cthags.sh, gsctrl, rsct_rady, rady2035a 10/30/16 22:21:13"
#*======================================================================
#* DOMAIN TYPE = "CLUSTER"
#*======================================================================
#ARGS: [development_flag] 

#Fix for Defect 181688
unalias -a

print_message()
{
    MSGCMD="/opt/rsct/bin/hadspmsg hagsctrl ha_gs.cat"
    print -u2 "`${MSGCMD} $*`"

#    if [[ X${PRINT_ENG_MSG} = XYES ]]
#    then
#        print -u2 "`LC_ALL=en_US ${MSGCMD} $*`"
#    fi
}



# Main starts here
# However, the src opens them separately, so they overwrite each other.
# To fix it, we make stderr a copy of stdout here.
#
#ARGS: [development_flag] [-p <cluster>] -d /var/ct/<cluster>/log/cthags.default

exec 2<&1   # make stderr a copy of stdout

#
RSCTBIN=/opt/rsct/bin
RSCTPATH=/opt/rsct/bin:/opt/rsct/install/bin
export PATH=$RSCTPATH:$PATH:/bin:/usr/sbin:/usr/bin
export PGSD_PERL=/usr/bin/perl

ARGS=$*                 # Preserve parameters

# set the correct environments for both development & production
development_flag=$1
if [[ -a /opt/rsct/bin/ct_devel_functions ]]; then
   . /opt/rsct/bin/ct_devel_functions ${development_flag}
fi

OSNAME=`uname -s 2> /dev/null`

#
umask 022

#
SUBSYS=cthags
HATS_SUBSYS=cthats

#
if [[ ! -z $CT_SUBSYS_SUFFIX ]]; then
   SUBSYS=${SUBSYS}.${CT_SUBSYS_SUFFIX}
fi

set -A clinfo $(${RSCTBIN}/ct_clusterinfo -c -n -i -d -r -M)
CLUSTER_NAME=${clinfo[0]}       # cluster-name
PGSD_NODE_NUMBER=${clinfo[1]}   # node-number
CLUSTER_ID=${clinfo[2]} 	# cluster-id
CLUSTER_DEFD_REALM=${clinfo[3]} # CAA or RPD
CLUSTER_CAA_CONFIGURED=${clinfo[4]}  # "CAA" if CAA is configured 
export PGSD_NODE_NUMBER

# For the compatibility with the older version, use the old path name
if [[ "$OSNAME" == "AIX" ]]
then
   export PGSD_DAEMON=/usr/sbin/rsct/bin/hagsd
else
   export PGSD_DAEMON=/opt/rsct/bin/hagsd
fi

export HA_DOMAIN_TYPE=CLUSTER
export PRM_DBGLVL=0
# For memory pre-alloc, set to 1 to enable, set to 0 to disable
export HAGSD_MEMORY_PRE_ALLOC=1
# For memory pre-alloc, size of memory to be pre-allocated
export HAGSD_MEMORY_PRE_ALLOC_SIZE_KB=3500

#Defect 170009: If CAA is defined but CAA is not configured or it's IW
# then exit and don't start hagsd.

if [[ ( "$CLUSTER_DEFD_REALM" == "CAA" && "$CLUSTER_CAA_CONFIGURED" != "CAA" ) || "$CLUSTER_NAME" == "IW" ]]
then
    echo "CLUSTER_DEFD_REALM=${CLUSTER_DEFD_REALM}, CLUSTER_CAA_CONFIGURED=${CLUSTER_CAA_CONFIGURED}, CLUSTER_NAME=${CLUSTER_NAME} exit."
    exit 0
fi

# set the default log file as /var/ct/<cluster>/log//cthags/cthags.default
DFLT_LOG_DIR=/var/ct/${CLUSTER_ID}/log/${SUBSYS}
if [[ ! -a ${DFLT_LOG_DIR} ]]; then
	mkdir -p ${DFLT_LOG_DIR}
fi
DFLT_LOG=${DFLT_LOG_DIR}/${SUBSYS}.default

# We set stdout and stderr to the same file in mkssys
# remove all limits: core, data, filesize, data, stack, cpu time
# both hard and soft
ulimit -c unlimited
ulimit -d unlimited
ulimit -f unlimited
ulimit -s unlimited
ulimit -t unlimited
if [[ $OSNAME != "Interix" && $OSNAME != "SunOS" ]]; then
    ulimit -m unlimited
fi

#
# Initialize FFDC for AIX LOG
. ${RSCTBIN}/fcinit.sh -l

RC=$?
if [[ $RC -ge 3 ]]
then
   # Can not initialize FFDC environment. RC=$RC
   print_message EMsg_Cannot_Initialize_FFDC ${PROGNAME} $RC
else
   export PGSD_FFDC_INIT_DONE=1
fi

FCLOGERR_X="ALPHA"
FCLOGERR_Y="100"

HB_SERVER_SOCKET=/var/ct/${CLUSTER_ID}/soc/${HATS_SUBSYS}/server_socket
export HB_SERVER_SOCKET

# hagsreap will remove hags logs and core files so that they consume
# no more than sizeLimit bytes.
# NOTE: This will determined in "hagsp"
# sizeLimit=${PGSD_REAP_SIZE:-$((5 * 1024 * 1024))} # default size is 5M
# export PGSD_REAP_SIZE=$sizeLimit

Command=${PGSD_REAP_CMD:-Erase}
export PGSD_REAP_CMD=$Command

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

# PGSD_MAX_NODES_PER_BROADCAST: max # of nodes per broadcast
if [[ -z $PGSD_MAX_NODES_PER_BROADCAST ]]; then
        PGSD_MAX_NODES_PER_BROADCAST=64      # default 64 nodes
fi
export PGSD_MAX_NODES_PER_BROADCAST
echo "PGSD_MAX_NODES_PER_BROADCAST=${PGSD_MAX_NODES_PER_BROADCAST}"

# PGSD_REBROADCAST_NEXT_INTERVAL_MSECS: interval between broadcast
# The remainder nodes will be rebroadcast (in every 1.5 secs)
if [[ -z $PGSD_REBROADCAST_NEXT_INTERVAL_MSECS ]]; then
        PGSD_REBROADCAST_NEXT_INTERVAL_MSECS=1500   # default 1.5 sec
fi
export PGSD_REBROADCAST_NEXT_INTERVAL_MSECS
echo "PGSD_REBROADCAST_NEXT_INTERVAL_MSECS=${PGSD_REBROADCAST_NEXT_INTERVAL_MSECS}"
#

#-----------------------------------------------------
# No more PRM delay because of the above chunking broadcast
#
# export PRM_DELAY_USEC_PER_KB_SEND=100	# (usecs/KB)
#-----------------------------------------------------

#-----------------------------------------------------
# Get the somaxconn to set backlog for listen() call
if [[ -z $PGSD_SOCKET_MAXCONN ]]; then
   

   case "$OSNAME" in
     "AIX")
        export PGSD_SOCKET_MAXCONN=$(no -o somaxconn | awk '{print $3}')
       ;;
     "Linux")
        if [ -r /proc/sys/net/core/somaxconn ]
        then
          export PGSD_SOCKET_MAXCONN=$(cat /proc/sys/net/core/somaxconn)
        fi
       ;;

     "SunOS" )
	export PGSD_SOCKET_MAXCONN=$(/usr/sbin/ndd /dev/tcp tcp_conn_req_max_q)
	;;

     *)
	# Windows or other platforms - use a default constant 
	export PGSD_SOCKET_MAXCONN=128
       ;;
   esac
fi

if [[ -z $PGSD_SOCKET_MAXCONN ]]; then
   # Make sure maxconn has some value
   export PGSD_SOCKET_MAXCONN=128
fi
echo "PGSD_SOCKET_MAXCONN=${PGSD_SOCKET_MAXCONN}"
#-----------------------------------------------------


#
# Cleanup the topsvcs and grpsvcs subsystems and directories iff:
#
# - Migration indicates that cleanup is needed.
# - Migration from PowerHA is taking place.
# - The grpsvcs subsystem is inoperative.
#

RSCT_INSTALL_BIN=/opt/rsct/install/bin

migration_cleanup_needed=`$RSCT_INSTALL_BIN/ct_caa_is_migration_cleanup_needed`

if [[ "$migration_cleanup_needed" == "true" ]]
then
	migration_realm=`$RSCT_INSTALL_BIN/ct_caa_get_migration_realm`

	if [[ "$migration_realm" == "HACMP" ]]
	then
		migrated_GS_still_running=`$RSCT_INSTALL_BIN/ct_caa_migrated_GS_still_running`

		if [[ "$migrated_GS_still_running" == "false" ]]
		then
			#
			# Delete the grpsvcs and topsvcs SRC subsystems
			$RSCTBIN/grpsvcsctrl -d
			$RSCTBIN/topsvcsctrl -d
	                /opt/rsct/install/bin/ct_caa_set_migration_cleanup_complete
		fi
        else
                # non-HACMP realm
	        /opt/rsct/install/bin/ct_caa_set_migration_cleanup_complete
	fi
fi

# Determine if PowerHA is present and the cluster version if it is
PGSD_HAES_CODELEVEL=`$RSCT_INSTALL_BIN/ct_is_haes_present`
if [[ $? = 0 ]]
then
    export PGSD_HAES_CODELEVEL
    export PGSD_HAES_PRESENT=1
fi

# Make "causing an immediate system dump" (if daemons block and critical
# resources are present) the default behavior in AIX
if [[ "$OSNAME" == "AIX" ]]
then
	HA_IMMED_REBOOT_ON_GS_CLIENT_BLOCKED=${HA_IMMED_REBOOT_ON_GS_CLIENT_BLOCKED:-YES}
	export HA_IMMED_REBOOT_ON_GS_CLIENT_BLOCKED
fi


# goto hagsp with '-s <subsys> -p <clst> -d <log>'
exec ${PGSD_PERL} ${RSCTBIN}/cthagsp -s ${SUBSYS} -p ${CLUSTER_ID} -d ${DFLT_LOG}
# $ARGS

tmsg="exec $cmd failed! rc = $!, $(($!/256)) $(($!%256)) errno=$ERRNO "
tcmd="exec $cmd"
print_message EMsg_Command_Failed $tcmd $!
/opt/rsct/bin/fclogerr -x ${FCLOGERR_X} -y ${FCLOGERR_Y} \
	-d "$tmsg" -p $LINENO -s $0 -v "1.34" -l RSCT \
	-r $SUBSYS -t ERRID_GS_STARTERR_ER -e FFDC_ERROR \
	-i /opt/rsct/include/ha_gs.err.S.h \
	-b "cthags: $tmsg"

exit 1