#! /bin/ksh
# 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 
#
# sccsid = "@(#)02   1.15         src/rsct/pgs/cmds/cluster/cthags.sh, gsctrl, rsct_relgh, relghs001a 9/26/05 17:54:49"
#*======================================================================
#* DOMAIN TYPE = "CLUSTER"
#*======================================================================
#ARGS: [development_flag] 

print_message()
{
    MSGCMD="/usr/sbin/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=/usr/sbin/rsct/bin
RSCTPATH=/usr/sbin/rsct/bin:/usr/sbin/rsct/install/bin
export PATH=$RSCTPATH:$PATH:/bin:/usr/sbin:/usr/bin
export PGSD_PERL=/usr/sbin/rsct/perl5/bin/perl

ARGS=$*                 # Preserve parameters

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

#
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)
CLUSTER_NAME=${clinfo[0]}       # cluster-name
PGSD_NODE_NUMBER=${clinfo[1]}   # node-number
export PGSD_NODE_NUMBER
export PGSD_DAEMON=hagsd
export HA_DOMAIN_TYPE=CLUSTER
export PRM_DBGLVL=0

# set the default log file as /var/ct/<cluster>/log//cthags/cthags.default
DFLT_LOG_DIR=/var/ct/${CLUSTER_NAME}/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 -m unlimited
ulimit -s unlimited
ulimit -t unlimited

#
# 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_NAME}/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)
#-----------------------------------------------------

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

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

exit 1
