#! /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 
#*===========================================================================*/
#*                                                                           */
#* Module Name:  cthagsctrl                                                  */
#*                                                                           */
#* Description:                                                              */
#*      Script to manage the Group Services Daemon as an SRC subsystem.      */
#*                                                                           */
#*      This file is formatted with tabstops of 4.                           */
#*===========================================================================*/
# sccsid = "@(#)03   1.22.1.21   src/rsct/pgs/cmds/cluster/cthagsctrl.sh, gsctrl, rsct_rady, rady2035a 11/12/15 16:44:55"

#########################################################################
# Currently, we inherit the locale according to the locale
# environment variables passed in the HATS script.
# However, we may need to set it to SP-admin locale if
# SP-admin locale becomes required.
# startsrc needs to pass environment variables explicitly. Make an
# environment variable list to pass locale information.
# 
# Create LOCALE_ENV that has all neccessary locale information
#
#########################################################################

#Fix for Defect 181688
unalias -a

function make_full_locale_env
{
   LOCALE_ENV=""
   if [ -n "$LANG" ]; then
	LOCALE_ENV="$LOCALE_ENV LANG=$LANG"; 
   fi
   if [ -n "$LC_COLLATE" ]; then
	LOCALE_ENV="$LOCALE_ENV LC_COLLATE=$LC_COLLATE"
   fi
   if [ -n "$LC_CTYPE" ]; then
	LOCALE_ENV="$LOCALE_ENV LC_CTYPE=$LC_CTYPE"
   fi
   if [ -n "$LC_MONETARY" ]; then
	LOCALE_ENV="$LOCALE_ENV LC_MONETARY=$LC_MONETARY"
   fi
   if [ -n "$LC_NUMERIC" ]; then
	LOCALE_ENV="$LOCALE_ENV LC_NUMERIC=$LC_NUMERIC"
   fi
   if [ -n "$LC_TIME" ]; then
	LOCALE_ENV="$LOCALE_ENV LC_TIME=$LC_TIME"
   fi
   if [ -n "$LC_MESSAGES" ]; then
	LOCALE_ENV="$LOCALE_ENV LC_MESSAGES=$LC_MESSAGES"
   fi
   if [ -n "$LC_ALL" ]; then
	LOCALE_ENV="$LOCALE_ENV LC_ALL=$LC_ALL"
   fi
}


#########################################################################
# Get the current Locale and determine whether English output is also needed
# Output: LOCALE
#	  PRINT_ENG_MSG
#########################################################################
function get_current_locale
{
   # Check if current locale is a English
   if [ -f /usr/bin/locale ]; then
	LOCALE=`/usr/bin/locale`
   elif [ -f /bin/locale ]; then
	# Interix
	LOCALE=`/bin/locale`
   else
	# assume LOCALE as POSIX
	LOCALE="LC_MESSAGES=\"POSIX\""
   fi
   # /usr/bin/locale may produce LC_MESSAGES=en_US or LC_MESSAGES="en_US".
   # We use two sed "s" commands, the first one gets rid of LC_MESSAGES=,
   # the second one removes the two surrounding '"' if they are there.
   LOCALE=`echo "$LOCALE" | grep LC_MESSAGES | sed "s/LC_MESSAGES=//;s/\"//g"`

   # Do not print English messages if the local language is English.
   # C and POSIX are special locales. Treat them as en_US.
   if [[ "$LOCALE" = "C" || "$LOCALE" = "POSIX" ]]; then
	PRINT_ENG_MSG="NO"
   else
	# A locale may look like en_US or en_US.ISO8859-1. Use cut command
	# to remove the language encoding.
	LOCALE=`echo "$LOCALE" | cut -f 1 -d "."`
	if [[ "$LOCALE" = "en_US" || "$LOCALE" = "En_US" || "$LOCALE" = "en_GB" || "$LOCALE" = "En_GB" ]]
	then
		# Local language is English.
		PRINT_ENG_MSG="NO"
	else
		# Local language is not English (including "$LOCALE"=""),
		# which should not happen in normal cases)
		PRINT_ENG_MSG="YES"
	fi
   fi
}

#########################################################################
#                                                                       #
# Function: print_message                                               #
# Description: wrapper for message printing. All messages go to stderr. #
#     For now, all messages are printed in both English and the current #
#     language unless current language is en_US, C, or POSIX.           #
#                                                                       #
#     This subroutine makes it easy to print messages in  either English#
#     or current language or both. Also, it ensure all messages will be #
#     printed to the same stream.                                       #
#########################################################################

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

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

#########################################################################
#									#
# Function: PSSP_running						#
# Description: Check if PSSP is running correctly.			#
# This script heavily depends on PSSP. We need a good way to find out if#
# PSSP is correctly installed and running correctly.			#
# Currently, we have no official way to do the task. Since SDR is an	#
# important component of PSSP, we assume PSSP is running if SDR is	#
# running. There is a PSSP command SDR_test that test if SDR runs.	#
# However, SDR_test requires root privilege. We make this a function so	#
# that we can change it easily when a better way is found.		#
# Return code:								#
#       0 : PSSP is running correctly					#
#       1 : PSSP is not running correctly				#
#									#
#########################################################################

PSSP_running() {
       	return 0
}

#########################################################################
function print_dbgmsg
{
   if [[ -n $HA_CCAL_LOG ]]; then
        print `date` ${CMD}: "$*" >> $HA_CCAL_LOG
   fi
}

#########################################################################
# Main starts here
#########################################################################

# Set path to known value
PATH=/opt/rsct/bin:/opt/rsct/install/bin:/usr/bin:/usr/sbin:/bin
export PATH

RSCTBIN=/opt/rsct/bin      # path to subsystem daemon
SPMSG=${RSCTBIN}/hadspmsg
export MSGMAPPATH=/opt/rsct/msgmaps
CMD=$(basename $0)
OSNAME=`uname -s 2> /dev/null`

# Function that tells whether the installation is on chroot directory(ex. SuSE kiwi environment) or not.
is_chroot_mode()
{
   OS=$(uname -s)
   if [[ $OS = "Linux" ]]; then
        if [[ "$(stat -c %d:%i /)" != "$(stat -c %d:%i /proc/1/root/.)" ]]; then
            return 1
        fi
   fi
   return 0
}

# Use SYSTEMCTL
USE_SYSTEMCTL=0
if [[ $OSNAME = "Linux" || $OSNAME = "Interix" || $OSNAME = "SunOS" ]]
then
   is_chroot_mode
   if [[ $?  -ne 0 ]]
   then
        # Under chroot. Check the file existence
        if [[ -x /usr/bin/systemctl ]] || [[ -x /bin/systemctl ]]; then
            USE_SYSTEMCTL=1
        fi
   else
        if [[ `systemctl 2>/dev/null` == *-\.mount* ]]
        then
            USE_SYSTEMCTL=1
        fi
   fi
fi



# set umask
umask 022

#
if [[ X${OSNAME}X = XLinuxX || X${OSNAME}X = XAIXX  ]]
then
    GLSMOSPossible=1	# possibly enabled on these platform
    GLSMEnabled=1	# this will be re-evaluated inside Obtain_cluster_name_and_realm()
else
    GLSMOSPossible=0
    GLSMEnabled=0
fi

ARGS=$*				# save the parameters

print_dbgmsg "started: $ARGS"

# Parse command line
Code_level=
Tracelist=
Argcount=0
while getopts ":ap:skdczutorhgb" opt
do
    case $opt in
	a )	Op=add;      Argcount=$((Argcount + 1));;
	p ) 	PORT_NUMBER=$OPTARG;;	# portnumber
	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));;
	u )	Op=unconfig; 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


# 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

if [[ -n $PORT_NUMBER && $Op != add ]]; then
	# invalid parameter
        $SPMSG hagsctrl ha_gs.cat IMsg_hagsctrl_Usage $CMD
	exit 1
fi


# Include the environment functions
if [[ -a /opt/rsct/bin/ct_devel_functions ]]; then
   # make sure the argument as ""
   . /opt/rsct/bin/ct_devel_functions ""           # pass "" arguments
   # This provides CT_SUBSYS_SUFFIX and CT_DEVELOPMENT_FLAG
fi

#

# include the common functions, e.g., to handle /etc/services
# a more robust method of updating /etc/services
. /opt/rsct/bin/ct_etc_functions

#
MYNAME=`basename $0`            # name invoked by
SUBSYS=${MYNAME%%ctrl}          # name of subsystem group
if [[ ! -z $CT_SUBSYS_SUFFIX ]]; then
	SUBSYS=${SUBSYS}.${CT_SUBSYS_SUFFIX}
fi
SUBSYSNAME=$SUBSYS              # name of subsystem on a node
#ALT_SUBSYSNAME=hags

SUBSYS2=${SUBSYS}glsm           # name of subsystem group for global AM
SUBSYSNAME2=$SUBSYS2            # name of subsystem on a node
PROG2=${SUBSYS2}d               # name of subsystem daemon

#
DOMAIN_TYPE=CLUSTER                # used to support the SP realm

#
CLUSTER_REALM=CLUSTER		# default CLUSTER

# obtain a cluster name & Realm
# Out:
#    CLUSTER_NAME 
#    CLUSTER_ID
#    CLUSTER_REALM = "CAA" or "CLUSTER"
function Obtain_cluster_name_and_realm
{
   if [[ ! -a ${RSCTBIN}/ct_clusterinfo ]]; then
	print_dbgmsg "${RSCTBIN}/ct_clusterinfo does not exist"
        $SPMSG hagsctrl ha_gs.cat EMsg_No_ct_clusterinfo ct_clusterinfo
	return
   fi

   set -A clinfo $(${RSCTBIN}/ct_clusterinfo -c -r -i -M)
   CLUSTER_NAME=${clinfo[0]}	# cluster name
   CLUSTER_REALM=${clinfo[1]}	# realm (CAA or CLUSTER)
   CLUSTER_ID=${clinfo[2]}	# cluster id
   if [[ ! -z $CLUSTER_ID ]]; then
	DFLT_LOG_DIR=/var/ct/$CLUSTER_ID/log
	DFLT_LCK_DIR=/var/ct/$CLUSTER_ID/lck
	DFLT_RUN_DIR=/var/ct/$CLUSTER_ID/run
	DFLT_SOC_DIR=/var/ct/$CLUSTER_ID/soc
   fi

   # check whether it is in CAA
   if [[ ! -z $CLUSTER_REALM && $CLUSTER_REALM = "CAA" ]]; then
	GLSMEnabled=0	
   fi
}


function Add_HAGSGLSM_Subsys
{
    mkdir -p $DFLT_RUN_DIR/${SUBSYS2} 2> /dev/null
    mkdir -p $DFLT_LOG_DIR/${SUBSYS} 2> /dev/null
    DFLT2_LOG=$DFLT_LOG_DIR/${SUBSYS}/${SUBSYS2}.default

    mkssys -s $SUBSYSNAME2 \
	-a "$DOMAIN_TYPE -p $CLUSTER_ID -d $DFLT2_LOG" \
	-p $RSCTBIN/${SUBSYS2} -u 0 -o ${DFLT2_LOG} \
	-e ${DFLT2_LOG} -R -Q -K -d -w 30 -G $SUBSYS

    return $?
}

#
# setup the proper locale environment
make_full_locale_env

# find the current working locale
get_current_locale



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

#
case $Op in

  # Add the subsystem to the SRC 
add )
	# Obtain the current cluster info
	Obtain_cluster_name_and_realm

	#Check HAGSGLSM
	GLSMActive=0
	if [[ $GLSMEnabled -ne 0 ]]; then
		STATE=`LC_ALL=C lssrc -s "$SUBSYSNAME2" \
			| LC_ALL=C sed -e "1d;s/.* \([a-zA-Z0-9]*\)$/\1/"`
		if [[ X${STATE}X = XactiveX  ]]; then
			GLSMActive=1
		fi
	fi

	# Ensure subsystem is stopped if it is already in SRC
	STATE=`LC_ALL=C lssrc -s "$SUBSYSNAME" \
		| LC_ALL=C 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

		if [[ $GLSMEnabled -ne 0 && $GLSMActive -eq 0 ]]; then
			# add HAGSGLSM if needed
			if [[ -n $CLUSTER_NAME  && $CLUSTER_NAME != "IW" ]]
			then
				Add_HAGSGLSM_Subsys
			fi
		fi
		exit 1
	fi

	if [[ $GLSMEnabled -ne 0 && $GLSMActive -ne 0 ]]; then
		$SPMSG hagsctrl ha_gs.cat EMsg_Subsystem_Must_Be_Stopped \
			$CMD $SUBSYSNAME2
		exit 1
        fi

	#

	if [[ -z $CLUSTER_NAME ]]; then
		# cluster ($CLUSTER_NAME) does not exist
                $SPMSG hagsctrl ha_gs.cat EMsg_No_Cluster_Name $CMD $CLUSTER_NAME 
		exit 1
	fi

	# get the port number if -p option is not present
	if [[ -z $PORT_NUMBER ]]; then
		if [[ ! -z $CLUSTER_REALM && $CLUSTER_REALM = "CAA" ]]; then
			# default port number = 12348
			PORT_NUMBER=12348
		else
			PORT_NUMBER=$(${RSCTBIN}/ct_hags_info | \
			    grep PORT | awk '{print $2}')
		fi
	fi

        # if no port number
	if [[ -z $PORT_NUMBER ]]; then
            $SPMSG hagsctrl ha_gs.cat EMsg_Port_Number_Not_Found \
            $CMD $SUBSYSNAME
            exit 1
	fi

	# 79514: updservices funtion will update the entry only 
	# when it is necessary...And no reason to remove it first.
	#
        # if $PORT_NUMBER is the NULL string, set_port_number will pick a
        # new port number and update the SDR, as well as /etc/services
	updservices -s $SERVICENAME -p $PORT_NUMBER -t udp
	rc=$?
	if [[ $rc != 0 ]]; then
		if [[ -z $PORT_NUMBER ]]; then
			PORT_NUMBER=NOPORT
		fi
		$SPMSG hagsctrl ha_gs.cat EMsg_Cannot_Register_Service_Name \
			$CMD $SERVICENAME udp $PORT_NUMBER
		exit 1
	fi

        # ensure subsystems are out of SRC
	rmssys -s $SUBSYSNAME >/dev/null
	if [[ $GLSMOSPossible -ne 0 ]]; then
		rmssys -s $SUBSYSNAME2 >/dev/null
	fi

	# create run&log dir
	mkdir -p $DFLT_RUN_DIR/${SUBSYS} 2> /dev/null
	mkdir -p $DFLT_LOG_DIR/${SUBSYS} 2> /dev/null
	DFLT_LOG=$DFLT_LOG_DIR/$SUBSYS/${SUBSYS}.default

	# get whether HAGS restartable
	if [[ ! -z $CLUSTER_REALM && $CLUSTER_REALM = "CAA" ]]; then
		RESTART_BY_SRC=1         # default (restartable by SRC)
	else
		RESTART_BY_SRC=$( ${RSCTBIN}/ct_hags_info | \
			grep -w "RESTART_BY_SRC" | awk '{print $2}')
	fi	
	if [[ -z $RESTART_BY_SRC ]]; then
		RESTART_BY_SRC=1         # default (restartable by SRC)
	fi

	(( RESTART_BY_SRC = $RESTART_BY_SRC )) # make sure it must be 0 or 1
	if (( $RESTART_BY_SRC != 0 ))
	then
		# restartable by SRC	
		RESTART_SRC_FLAG="-R"
	else
		# do not restart HAGS when it fails
		RESTART_SRC_FLAG="-O"
	fi

	# pass CT_DEVELOPMENT_FLAG, subsys & its alternate name
	mkssys -s $SUBSYSNAME -a "$CT_DEVELOPMENT_FLAG" \
		-p $RSCTBIN/$SUBSYS -u 0 -o ${DFLT_LOG} \
		-e ${DFLT_LOG} ${RESTART_SRC_FLAG} -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

	#HAGSGLSM
	if [[ $GLSMEnabled -ne 0 ]]; then
		Add_HAGSGLSM_Subsys
		if [[ $? -ne 0 ]]; then
			$SPMSG hagsctrl ha_gs.cat EMsg_Could_Not_Add \
				$CMD $SUBSYSNAME2 SRC
			exit 1
		fi
	fi

        # Add an entry to the inittab file for this subsystem, if it is not
        # already there. The new entry will be put at the end of /etc/inittab
        # However, do not add the entry if this is a HACWS
        # Assume adding entry to /etc/inittab
	# LINUX...DO NOT ADD
	noitab=1                
	#
	if (( noitab == 0 ))
	then
		lsitab $SUBSYS > /dev/null 2>&1
                # if entry not there
		if (( $? != 0 ))
		then
			mkitab \
				"$SUBSYS:2:once:/usr/bin/startsrc -g $SUBSYS \
				> /dev/console 2>&1" \
				> /dev/null 2>&1
			if (( $? != 0))
			then
				$SPMSG hagsctrl ha_gs.cat EMsg_Could_Not_Add \
					$CMD $SUBSYSNAME inittab
				exit 1
			fi
		fi
	fi

        #
        # All the necessary steps have been taken to add the subsystem.
        # Finally, vfyservices is called to verify the addition to the 
        # /etc/services file.  The vfyservices function verifies that
        # programs using getservbyname() and getservbyport() will actually
        # see the information this script has put into /etc/services.
        # If this fails, and NIS is in use, the system administrator may need
        # to update /etc/services on the NIS master server.
        #

	vfyservices -s $SERVICENAME -t udp -p $PORT_NUMBER
	if [[ $? -ne 0 ]]
	then
		$SPMSG hagsctrl ha_gs.cat EMsg_Cannot_Verify_Service_Name \
			$CMD $SERVICENAME udp $PORT_NUMBER
		exit 1
	fi

	if [[ X${OSNAME}X = XLinuxX || X${OSNAME}X = XSunOSX ]]
	then
		#START_AT_BOOT=$( ${RSCTBIN}/ct_hags_info | \
		#	grep -w "START_AT_BOOT" | awk '{print $2}')
		#if [[ -z $START_AT_BOOT ]]; then
		#	START_AT_BOOT=1		# default (start at boot)
		#fi
		## make sure the value must be either 0 or 1
		#(( START_AT_BOOT = $START_AT_BOOT ))
		#print_dbgmsg "HAGS AUTOSTART = $START_AT_BOOT"
		#
		#$RSCTBIN/ctinitmgr -a $RSCTBIN/rc.$SUBSYSNAME \
		#		   -b $START_AT_BOOT $SUBSYSNAME 
		#
		# ctinitmgr no longer manipulates symbolic links for SysV
		# init scripts. START_AT_BOOT is taken care by rc.cthags.
        if [ $USE_SYSTEMCTL -eq 0 ]
        then
		    $RSCTBIN/ctinitmgr -a $RSCTBIN/rc.$SUBSYSNAME $SUBSYSNAME
        fi
	fi

	;;

  # start the subsystem 

start )
        #
        Obtain_cluster_name_and_realm

        if [[ -z $CLUSTER_NAME ]]; then
                # cluster ($CLUSTER_NAME) does not exist
                $SPMSG hagsctrl ha_gs.cat EMsg_No_Cluster_Name $CMD $CLUSTER_NAME 
                exit 1
        fi

	startsrc -s $SUBSYSNAME
	RetCode=$?
	if [[ $RetCode -eq 0 ]]
	then
		# Some Linux distributions, e.g. RedHat, need to keep tracking
		# which service is up to make it stopped automatically at
		# machine shutdown time.
		if [[ X${OSNAME}X = XLinuxX || X${OSNAME}X = XSunOSX ]]
		then
            if [ $USE_SYSTEMCTL -eq 0 ]
            then
			   $RSCTBIN/ctinitmgr -s $SUBSYSNAME
            fi
		fi
	fi

	if [[ $GLSMEnabled -ne 0 ]]; then
		startsrc -s $SUBSYSNAME2
	fi
	exit $RetCode
	;;

  # 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 [[ $GLSMOSPossible -ne 0 ]]; then
	   STATE=`LC_ALL=C lssrc -s "$SUBSYS2" \
		| LC_ALL=C sed -e "1d;s/.* \([a-zA-Z0-9]*\)$/\1/"`
	   if [[ X${STATE}X = XactiveX  ]]; then
		stopsrc -c -s $SUBSYSNAME2
	   fi
	fi

	STATE=`LC_ALL=C lssrc -s "$SUBSYS" \
		| LC_ALL=C 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
	# Some Linux distributions, e.g. RedHat, need to keep tracking
	# which service is up to make it stopped automatically at
	# machine shutdown time.
	# ctinitmgr -k should be called when the subsystem is not active
	# no matter if it is stopped by this script or not.
	if [[ X${OSNAME}X = XLinuxX || X${OSNAME}X = XSunOSX ]]
	then
        if [ $USE_SYSTEMCTL -eq 0 ]
        then
		    $RSCTBIN/ctinitmgr -k $SUBSYSNAME
        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 "$SUBSYS" \
                | LC_ALL=C 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 [[ $GLSMOSPossible -ne 0 ]]; then
		STATE=`LC_ALL=C lssrc -s "$SUBSYS2" \
                        | LC_ALL=C 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

                # if okay....
		rmssys -s $SUBSYSNAME2 >/dev/null
        fi

	rmssys -s $SUBSYSNAME >/dev/null      # take subsystem out of SRC
	remservices -s $SERVICENAME -t udp    # take port out of /etc/services

	if [[ X${OSNAME}X = XLinuxX || X${OSNAME}X = XSunOSX ]]
	then
        if [ $USE_SYSTEMCTL -eq 0 ]
        then
		    $RSCTBIN/ctinitmgr -d $SUBSYSNAME
        fi
	fi

	;;

#
# Clean or unconfigure all elements of the subsystem

clean|unconfig)

	# If the option is clean, or the option is unconfig
	#
        # Stop Group Services daemons
	stopsrc -c -g $SUBSYS > /dev/null	# stop hags/hagsglsm
	if [[ $GLSMOSPossible -ne 0 ]]; then
		rmssys -s $SUBSYSNAME2 >/dev/null
	fi
	rmssys -s $SUBSYSNAME >/dev/null       # take subsystem out of SRC
	remservices -s $SERVICENAME -t udp    # take port out of /etc/services

	# Some Linux distributions, e.g. RedHat, need to keep tracking
	# which service is up to make it stopped automatically at
	# machine shutdown time.
	if [[ X${OSNAME}X = XLinuxX || X${OSNAME}X = XSunOSX ]]
	then
        if [ $USE_SYSTEMCTL -eq 0 ]
        then
		    $RSCTBIN/ctinitmgr -d $SUBSYSNAME
        fi
	fi

	;;


deinstall )

	# Stop Group Services daemons
	stopsrc -c -g $SUBSYS > /dev/null	# stop hags/hagsglsm
        if [[ $GLSMOSPossible -ne 0 ]]; then
                rmssys -s $SUBSYSNAME2 >/dev/null
        fi
        rmssys -s $SUBSYSNAME >/dev/null       # take subsystem out of SRC
        remservices -s $SERVICENAME -t udp     # take port out of /etc/services

	# obtain the cluster name
	Obtain_cluster_name_and_realm

	# remove files
	if [[ -n $CLUSTER_NAME ]]; then
	    rm -rf ${DFLT_LOG_DIR}/${SUBSYS}  2> /dev/null
	    rm -rf ${DFLT_LCK_DIR}/${SUBSYS}  2> /dev/null
	    rm -rf ${DFLT_RUN_DIR}/${SUBSYS}  2> /dev/null
	    rm -rf ${DFLT_SOC_DIR}/${SUBSYS}  2> /dev/null
	    rm -rf ${DFLT_SOC_DIR}/${SUBSYS}.clients  2> /dev/null
	    rm -rf ${DFLT_SOC_DIR}/${SUBSYS2}  2> /dev/null
	fi

	# Some Linux distributions, e.g. RedHat, need to keep tracking
	# which service is up to make it stopped automatically at
	# machine shutdown time.
	if [[ X${OSNAME}X = XLinuxX || X${OSNAME}X = XSunOSX ]]
	then
        if [ $USE_SYSTEMCTL -eq 0 ]
        then
		    $RSCTBIN/ctinitmgr -d $SUBSYSNAME
        fi
	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 [[ $GLSMEnabled -ne 0 ]]; then
		refresh -s $SUBSYSNAME2
	fi
        ;;

migrdone)

        # migration has completed.

        hagsmigrate -a done -s $SUBSYSNAME

        ;;

migrbegin)

        # migration is beginning.
        hagsmigrate -a begin -s $SUBSYSNAME

        ;;

esac

exit 0