#! /bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
#  
#  
# Licensed Materials - Property of IBM 
#  
# (C) COPYRIGHT International Business Machines Corp. 1998,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 
#*===========================================================================*/
#*                                                                           */
#* Module Name:  grpsvcsctrl                                                 */
#*                                                                           */
#* Description:                                                              */
#*      Script to manage the Group Services Daemon as an SRC subsystem.      */
#*                                                                           */
#*      This file is formatted with tabstops of 4.                           */
#*===========================================================================*/
# @(#)95   1.7.1.11   src/rsct/pgs/cmds/aix/grpsvcsctrl.sh, gsctrl, rsct_relgh, relghs001a 1/29/07 22:04:15
#
# Note that we CANNOT use a "partition" (or domain) name in references in this
# script.  It is often the case that this script will be executed at HA install
# time when no domain name exists.  In the first release we could fall back on
# the SP partition name, but can do that no longer.  (See defect 37117.)
#

SS_PATH=/usr/sbin/rsct/bin      # path to subsystem daemon

DOMAIN_TYPE=HAES                # used to support the HA realm

SUBSYS=grpsvcs                  # name of subsystem
SCRIPT=hags
SUBSYSNAME=$SUBSYS
PROG=${SCRIPT}d                 # name of subsystem daemon
DFLT_LOG_DIR=/var/ha/log
DFLT_LCK_DIR=/var/ha/lck
DFLT_RUN_DIR=/var/ha/run
DFLT_SOC_DIR=/var/ha/soc

PGSD_LOGDIR=/var/ha/log
DFLT_LOG=${PGSD_LOGDIR}/grpsvcs.default   # name of default daemon log

OSname=$(uname -s)

if [ "$OSname" = "AIX" ]
then
SUBSYS2=grpglsm                 # name of subsystem group for global AM
SCRIPT2=hagsglsm
SUBSYSNAME2=$SUBSYS2            # name of subsystem on a node
PROG2=${SCRIPT2}d               # name of subsystem daemon
                                # name is appended to it
DFLT_LOG2=${PGSD_LOGDIR}/grpglsm.default  # name of default daemon log
fi     # if [ "$OSname" = "AIX" ]

CMD=$(basename $0)
SPMSG=${SS_PATH}/hadspmsg
MSGMAPPATH=/usr/sbin/rsct/msgmaps
export MSGMAPPATH

# Set path to known value

case "$OSname" in
  "AIX")
    PATH=/usr/sbin/rsct/bin:/usr/sbin/rsct/install/bin:/usr/es/sbin/cluster/utilities:/usr/bin:/usr/sbin
    ;;
  "Linux")
    PATH=/usr/sbin/rsct/bin:/usr/sbin/rsct/install/bin:/usr/es/sbin/cluster/utilities:/usr/bin:/usr/sbin:/bin
    ;;
  *)
    $SPMSG hagsctrl ha_gs.cat EMsg_Internal_error grpsvcsctrl
    exit 1
    ;;
esac

export PATH

#return 0 if exist
function SwitchExist
{
    ls /dev/css* >/dev/null 2>&1
    if [[ $? -eq 0 ]]
    then
        print $?
    else
        ls /dev/sn* >/dev/null 2>&1    
        print $?
    fi
}

# mainline code

# Parse command line
Domain_name=
Code_level=
Tracelist=
Argcount=0
while getopts ":askdcztorhgb" opt
do
	case $opt in
		a )	Op=add;      Argcount=$((Argcount + 1));;
		s )	Op=start;    Argcount=$((Argcount + 1));;
		k )	Op=stop;     Argcount=$((Argcount + 1));;
		d )	Op=delete;   Argcount=$((Argcount + 1));;
		c )	Op=clean;    Argcount=$((Argcount + 1));;
		z )	Op=deinstall;	Argcount=$((Argcount + 1));;
		t )	Op=traceon;  Tracelist="all"; Argcount=$((Argcount + 1));;
		o )	Op=traceoff; Tracelist="all"; Argcount=$((Argcount + 1));;
                r )	Op=refresh;  Argcount=$((Argcount + 1));;
                g )	Op=migrdone;  Argcount=$((Argcount + 1));;
                b )	Op=migrbegin;  Argcount=$((Argcount + 1));;
		h )	$SPMSG hagsctrl ha_gs.cat IMsg_hagsctrl_Usage $CMD
			# IMsg_hagsctrl_Usage needs to add "-z" for 51488
				 exit 0;;
		? )	$SPMSG hagsctrl ha_gs.cat IMsg_hagsctrl_Usage $CMD
				 exit 1;;
	esac
done

# Remove these references about the domain name.  If we are being called
# at install time, there is not yet a domain name.
#
#long_Domain_name=$(cldomain)	# get name of system domain we are in
#if [[ -z $long_Domain_name ]]
#then
#  $SPMSG hagsctrl ha_gs.cat EMsg_Cannot_Determine_Syspar_Name $CMD
#  exit 1
#fi
#
# Make host name short host name
#Domain_name=${long_Domain_name%%.*} # strip off . and all that follows

# We only use this to determine if we are on the "control workstation",
# which is not a valid HACMP/ES concept.
#
#Node_number=$(clnodenumber)
#if [[ -z $Node_number ]]
#then
#  $SPMSG hagsctrl ha_gs.cat EMsg_Cannot_Determine_Node_Number $CMD
#  exit 1
#fi

# Check for valid flags and/or arguments

if ((Argcount == 0))
then
  $SPMSG hagsctrl ha_gs.cat IMsg_hagsctrl_Usage $CMD
  exit 1
fi

if ((Argcount > 1))
then
  $SPMSG hagsctrl ha_gs.cat IMsg_hagsctrl_Usage $CMD
  exit 1
fi

# We only use this to determine if we are on the "control workstation",
# which is not a valid HACMP/ES concept.
#
#if [[ $Node_number -eq 0 ]]
#then
#  SUBSYSNAME=${SUBSYSNAME}.${Domain_name%%.*}		# append syspar name on CWS
#if [ "$OSname" = "AIX" ]
#then
#  SUBSYSNAME2=${SUBSYSNAME2}.${Domain_name%%.*}	# append syspar name on CWS
#fi     # if [ "$OSname" = "AIX" ]
#fi

# set service name for adding port to /etc/services
#SERVICENAME=${SUBSYS}.${Domain_name%%.*}
SERVICENAME=${SUBSYS}

case $Op in

# Add the subsystem to the SRC 

add )
	# Ensure subsystem is stopped if it is already in SRC

	# If INUCLIENTS is set then don't run lssrc to avoid having
        # to wait too long. (the subsystem should not be up anyway)

	if [[ -z "$INUCLIENTS" ]]; then
	    STATE=`LC_ALL=C lssrc -s "$SUBSYSNAME" \
		| sed -e "1d;s/.* \([a-zA-Z0-9]*\)$/\1/"`
	    if [[ X${STATE}X = XactiveX  ]]
	    then
		$SPMSG hagsctrl ha_gs.cat EMsg_Subsystem_Must_Be_Stopped \
				$CMD $SUBSYSNAME
		exit 1
	    fi

if [ "$OSname" = "AIX" ]
then
	    STATE=`LC_ALL=C lssrc -s "$SUBSYSNAME2" \
			| sed -e "1d;s/.* \([a-zA-Z0-9]*\)$/\1/"`
	    if [[ X${STATE}X = XactiveX  ]]
	    then
		$SPMSG hagsctrl ha_gs.cat EMsg_Subsystem_Must_Be_Stopped \
				$CMD $SUBSYSNAME2
		exit 1
	    fi
fi     # if [ "$OSname" = "AIX" ]
	fi

	if [[ ! -x $SS_PATH/$PROG ]]
	then
		$SPMSG hagsctrl ha_gs.cat EMsg_Not_Executable $CMD $SS_PATH/$PROG
		exit 1
	fi
if [ "$OSname" = "AIX" ]
then
	if [[ ! -x $SS_PATH/$PROG2 ]]
	then
		$SPMSG hagsctrl ha_gs.cat EMsg_Not_Executable $CMD $SS_PATH/$PROG2
		exit 1
	fi
fi     # if [ "$OSname" = "AIX" ]
	if [[ ! -x $SS_PATH/$SCRIPT ]]
	then
		$SPMSG hagsctrl ha_gs.cat EMsg_Not_Executable $CMD $SS_PATH/$SUBSYS
		exit 1
	fi
if [ "$OSname" = "AIX" ]
then
	if [[ ! -x $SS_PATH/$SCRIPT2 ]]
	then
		$SPMSG hagsctrl ha_gs.cat EMsg_Not_Executable $CMD $SS_PATH/$SUBSYS2
		exit 1
	fi
fi     # if [ "$OSname" = "AIX" ]

	rmssys -s $SUBSYSNAME >/dev/null		# ensure subsystem is out of SRC
if [ "$OSname" = "AIX" ]
then
	rmssys -s $SUBSYSNAME2 >/dev/null		# ensure subsystem is out of SRC
fi     # if [ "$OSname" = "AIX" ]

#	mkssys -s $SUBSYSNAME -a "$DOMAIN_TYPE -p $Domain_name -d ${DFLT_LOG}$Domain_name" \
#		-p $SS_PATH/$SCRIPT -u 0 \
#		-o ${DFLT_LOG}$Domain_name -e ${DFLT_LOG}$Domain_name \
#		-R -Q -K -d -w 30 -G $SUBSYS
	mkssys -s $SUBSYSNAME -a "$DOMAIN_TYPE -d ${DFLT_LOG}" \
		-p $SS_PATH/$SCRIPT -u 0 \
		-o ${DFLT_LOG} -e ${DFLT_LOG} \
		-O -Q -K -d -w 30 -G $SUBSYS
	if [[ $? -ne 0 ]]
	then
		$SPMSG hagsctrl ha_gs.cat EMsg_Could_Not_Add $CMD $SUBSYSNAME SRC
		exit 1
	fi

if [ "$OSname" = "AIX" ]
then
#	mkssys -s $SUBSYSNAME2 \
#		-a "$DOMAIN_TYPE -p $Domain_name -d ${DFLT_LOG2}$Domain_name" \
#		-p $SS_PATH/$SCRIPT2 -u 0 \
#		-o ${DFLT_LOG2}$Domain_name -e ${DFLT_LOG2}$Domain_name \
#		-R -Q -K -d -w 30 -G $SUBSYS
# Make grpglsm restartable (78529)
        SwitchExist 
        if [[ $? -eq 0 ]]
        then
            mkssys -s $SUBSYSNAME2 \
                -a "$DOMAIN_TYPE -d ${DFLT_LOG2}" \
                -p $SS_PATH/$SCRIPT2 -u 0 \
                -o ${DFLT_LOG2} -e ${DFLT_LOG2} \
                -R -Q -K -d -w 30 -G $SUBSYS
            if [[ $? -ne 0 ]]
            then
                $SPMSG hagsctrl ha_gs.cat EMsg_Could_Not_Add $CMD $SUBSYSNAME2 S
RC
                exit 1
            fi
        fi
fi     # if [ "$OSname" = "AIX" ]
        ;;

# start the subsystem 

start )
	startsrc -s $SUBSYSNAME
if [ "$OSname" = "AIX" ]
then
        # if /dev/css* exist start grpglsm
        STATE=`LC_ALL=C lssrc -s "$SUBSYSNAME2" \
                        | sed -e "1d;s/.* \([a-zA-Z0-9]*\)$/\1/"` 
        swExist=$(SwitchExist)
        if [[ X${STATE}X = XX && $swExist -eq 0 ]]
        then
            #switch exist but subsystem is not defined
            #... Create subsystem
            mkssys -s $SUBSYSNAME2 \
                -a "$DOMAIN_TYPE -d ${DFLT_LOG2}" \
                -p $SS_PATH/$SCRIPT2 -u 0 \
                -o ${DFLT_LOG2} -e ${DFLT_LOG2} \
                -R -Q -K -d -w 30 -G $SUBSYS
            if [[ $? -ne 0 ]]
            then
                $SPMSG hagsctrl ha_gs.cat EMsg_Could_Not_Add $CMD $SUBSYSNAME2 SRC
                exit 1
            fi
        fi
        # start the subsystem if switch exist
        if [[ $swExist -eq 0 ]]
        then
            startsrc -s $SUBSYSNAME2
        fi
fi     # if [ "$OSname" = "AIX" ]
        exit $?

        ;;

# stop the subsystem.  use cancel option to assure that the subsystem really
# does stop.  SRC gives subsystem X seconds, then sends kill -9.  Ensure that
# the subsystems are running before actually stopping them.

stop )

if [ "$OSname" = "AIX" ]
then
	# grpglsm
	STATE=`LC_ALL=C lssrc -s "$SUBSYSNAME2" \
                        | sed -e "1d;s/.* \([a-zA-Z0-9]*\)$/\1/"`
	if [[ X${STATE}X = XactiveX  ]] 
	then
		stopsrc -c -s $SUBSYSNAME2
		if (( $? != 0))
		then
			exit $?
		fi
	fi
fi     # if [ "$OSname" = "AIX" ]

	#grpsvcs
        STATE=`LC_ALL=C lssrc -s "$SUBSYSNAME" \
                        | sed -e "1d;s/.* \([a-zA-Z0-9]*\)$/\1/"`
        if [[ X${STATE}X = XactiveX  ]]
        then
                stopsrc -c -s $SUBSYSNAME
                if (( $? != 0))
                then
                        exit $?
                fi
       fi

       exit 0

       ;;

# Delete the subsystem from the SRC. We do not remove the associated port
# number from the SDR, since it may still be needed by this subsystem on
# other nodes. However, we do remove the port number from /etc/services.

delete )
	# Ensure subsystem is stopped 

        STATE=`LC_ALL=C lssrc -s "$SUBSYSNAME" \
                        | sed -e "1d;s/.* \([a-zA-Z0-9]*\)$/\1/"`
        if [[ X${STATE}X = XactiveX  ]]
        then
		$SPMSG hagsctrl ha_gs.cat EMsg_Subsystem_Must_Be_Stopped \
				$CMD $SUBSYSNAME
		exit 1
	fi

if [ "$OSname" = "AIX" ]
then
        STATE=`LC_ALL=C lssrc -s "$SUBSYSNAME2" \
                        | sed -e "1d;s/.* \([a-zA-Z0-9]*\)$/\1/"`
        if [[ X${STATE}X = XactiveX  ]]
        then
		$SPMSG hagsctrl ha_gs.cat EMsg_Subsystem_Must_Be_Stopped \
				$CMD $SUBSYSNAME2
		exit 1
	fi
fi     # if [ "$OSname" = "AIX" ]

	rmssys -s $SUBSYSNAME >/dev/null		# take subsystem out of SRC
if [ "$OSname" = "AIX" ]
then
	rmssys -s $SUBSYSNAME2 >/dev/null		# take subsystem out of SRC
fi     # if [ "$OSname" = "AIX" ]


	;;

# Unconfigure all elements of the subsystem in all partitions. However, DO NOT
# remove anything from the SDR. This function is used in preparation to
# restoring the subsystem to a known state - said state defined by the contents
# of the SDR.

clean)

	# ensure all subsystem daemons are stopped

	stopsrc -c -g $SUBSYS >/dev/null

	# remove all SRC entries

	LC_ALL=C lssrc -g $SUBSYS | sed 1d |
	while read ssname junk
	do
		rmssys -s $ssname >/dev/null		# take subsystem out of SRC
	done

	;;

# This flag is used in unpost_i.sh to clean up all stuff created at
# run-time before de-install the Group Services subsystem.

deinstall )
	# Stop Group Services daemon
	TIMEOUT=60
	PROCLIST=""
	LC_ALL=C lssrc -g ${SUBSYS} 2> /dev/null | grep "active" |
	while read junk junk PID1 junk
	do
	    PROCLIST=${PROCLIST},${PID1}
	done
	if [ -n "${PROCLIST}" ]
	then
	    PROCLIST=${PROCLIST#,}
	    stopsrc -c -g ${SUBSYS} > /dev/null
	    (( i = ${TIMEOUT} ))
	    while (( $i > 0 ))
	    do
		/bin/ps -p ${PROCLIST} > /dev/null
		if [[ $? -ne 0 ]]
		then
		    break
		else
		    sleep 1
		    (( i = $i - 1 ))
		fi
	    done
	    /bin/ps -p ${PROCLIST} > /dev/null
	    if [[ $? -eq 0 ]]
	    then
		# Add a new message for 51488
		# ${SUBSYS} is not fully stopped in ${TIMEOUT} seconds.
		$SPMSG hagsctrl ha_gs.cat EMsg_Subsys_Not_Fully_Stopped $SUBSYS $TIMEOUT 
                
	    fi
	fi

	# Remove SRC entries
	LC_ALL=en_US lssrc -g ${SUBSYS} | sed 1d |
	while read ssname junk
	do
	    rmssys -s ${ssname} > /dev/null	# take subsystem out of SRC
	done

	# Remove /etc/services entry
	# HACMP creates /etc/services entries at installation time in
	# cluster.es.server.utils.post_i.sh and removes the entries in
	# cluster.es.server.utils.unpost_i.sh.

	# Remove /etc/inittab entry
	# HACMP doesn't have a /etc/inittab entry for Group Services

	# Remove ODM entries.

	# Remove log/lck/run/soc files.
	if [ -n "${PGSD_LOGFILE}" ]
	then
	    rm -f ${PGSD_LOGFILE} 2> /dev/null
	else
	    rm -f ${DFLT_LOG_DIR}/${SUBSYS}.*  2> /dev/null
if [ "$OSname" = "AIX" ]
then
	    rm -f ${DFLT_LOG_DIR}/${SUBSYS2}.* 2> /dev/null
fi     # if [ "$OSname" = "AIX" ]
	fi

	if [ -n "${PGSD_TID_DIR}" ]
	then
	    rm -rf ${PGSD_TID_DIR}/${SUBSYS}.tid.*  2> /dev/null
if [ "$OSname" = "AIX" ]
then
	    rm -rf ${PGSD_TID_DIR}/${SUBSYS2}.tid.* 2> /dev/null
fi     # if [ "$OSname" = "AIX" ]
	else
	    rm -rf ${DFLT_LCK_DIR}/${SUBSYS}.tid.*  2> /dev/null
if [ "$OSname" = "AIX" ]
then
	    rm -rf ${DFLT_LCK_DIR}/${SUBSYS2}.tid.* 2> /dev/null
fi     # if [ "$OSname" = "AIX" ]
	fi

	if [ -n "${PGSD_RUN_DIR}" ]
	then
	    rm -rf ${PGSD_RUN_DIR}/${SUBSYS}.*  2> /dev/null
if [ "$OSname" = "AIX" ]
then
	    rm -rf ${PGSD_RUN_DIR}/${SUBSYS2}.* 2> /dev/null
fi     # if [ "$OSname" = "AIX" ]
	else
	    rm -rf ${DFLT_RUN_DIR}/${SUBSYS}.*  2> /dev/null
if [ "$OSname" = "AIX" ]
then
	    rm -rf ${DFLT_RUN_DIR}/${SUBSYS2}.* 2> /dev/null
fi     # if [ "$OSname" = "AIX" ]
	fi

	# Client socket directory cannot be specified by environment variable.
	rm -rf ${DFLT_SOC_DIR}/${SUBSYS}.clients.* 2> /dev/null

	# Group Services socket
	if [ -n "${PGSD_SUPP_SOCK}" ]
	then
	    rm -f ${PGSD_SUPP_SOCK}/${SUBSYS}/${SUBSYS}dsocket.* 2> /dev/null
	else
	    rm -f ${DFLT_SOC_DIR}/${SUBSYS}dsocket.* 2> /dev/null
	fi

	;;
								
traceon)

	# turn on all tracing in Group Services daemon for this system partition

	traceson -l -s $SUBSYSNAME
	exit $?

	;;

traceoff)

	# turn off all (but default) tracing in Group Services daemon for this
        # system partition

	tracesoff -s $SUBSYSNAME
	exit $?

	;;

refresh)

	# refresh the subsystem. 
	refresh -s $SUBSYSNAME
if [ "$OSname" = "AIX" ]
then
        # if /dev/css* exist start grpglsm
        STATE=`LC_ALL=C lssrc -s "$SUBSYSNAME2" \
                        | sed -e "1d;s/.* \([a-zA-Z0-9]*\)$/\1/"` 
        swExist=$(SwitchExist)

        if [[ X${STATE}X = XX && $swExist -eq 0 ]]
        then
            #switch exist but subsystem is not defined
            #... Create subsystem
            mkssys -s $SUBSYSNAME2 \
                   -a "$DOMAIN_TYPE -d ${DFLT_LOG2}" \
                   -p $SS_PATH/$SCRIPT2 -u 0 \
                   -o ${DFLT_LOG2} -e ${DFLT_LOG2} \
                   -R -Q -K -d -w 30 -G $SUBSYS
            if [[ $? -ne 0 ]]
            then
                $SPMSG hagsctrl ha_gs.cat EMsg_Could_Not_Add $CMD $SUBSYSNAME2 SRC
                exit 1
            fi
        fi
        # refresh the subsystem if switch exist
        if [[ $swExist -eq 0 ]]
        then
            refresh -s $SUBSYSNAME2
        fi
fi     # if [ "$OSname" = "AIX" ]
        ;;

migrdone)

        # migration has completed.

        hagsmigrate -a "done `clmixver`" -s $SUBSYSNAME

        ;;

migrbegin)

        # migration has completed.

        hagsmigrate -a begin -s $SUBSYSNAME

        ;;

esac


exit 0
