#! /usr/bin/sh
#
# ident	"@(#)sap_svc_abort_net.shi	1.4	98/07/24 SMI"
#
# Copyright (c) 1997 by Sun Microsystems, Inc.
# All rights reserved.
#
#
# This program stops sap r3. It stops app server, test, develop instances too.
#
#

VERSION=1.3
ARGV0=`basename $0`
PATH0=`dirname $0`
SYSLOG=`haget -f syslog_facility`
MASTERED="$1"
NOT_MASTERED="$2"
SAPCONF=/etc/opt/SUNWscsap/hadsconf
#HA_DEBUG=1

if [ $HA_DEBUG -ne 0 ];then
  logger -p ${SYSLOG}.info \
   "$0" "arg1=" "$1" "arg2=" "$2"
fi

prog_path=`dirname $0`
expr "$prog_path" : '.*/clust_progs' >/dev/null 2>&1
if [ $? -eq 0 ]; then
	PATH=${prog_path}:${PATH}
else
	PATH=${prog_path}:${prog_path}/../clust_progs:${PATH}
fi

# source in ha-services common utilities
. ds_utilities.1.3

source_env_file $SAPCONF

#
# SAP variables
#
NOTAVAIL=${_INST_1_PRIV_NOT_AVAIL} 
SAPSID=${_INST_1_PRIV_YOUR_SAP_SID}
CI=${_INST_1_PRIV_CI_LOGICAL_HOSTNAME}
DB=${_INST_1_PRIV_DB_LOGICAL_HOSTNAME}
NFS=${_INST_1_PRIV_NFS_LOGICAL_HOSTNAME}
SAPADM=${_INST_1_PRIV_SAP_ADMIN_LOGIN_NAME}
DBADM=${_INST_1_PRIV_DB_ADMIN_LOGIN_NAME}
USRSAP=${_INST_1_PRIV_SAP_MOUNT_POINT_A}
SAPMNT=${_INST_1_PRIV_SAP_MOUNT_POINT_B}
AUTO_NFSMOUNT=${_INST_1_PRIV_AUTO_NFS_MOUNT}


# Replace comma with space to have an sh word list:
NOT_MASTERED="`echo $NOT_MASTERED | tr ',' ' '`" 
MASTERED="`echo $MASTERED | tr ',' ' '`" 

if [ -z "$NOT_MASTERED" ]; then
	#
	# This physical host currently masters or will master logical host(s).
	#	
  	if [ $HA_DEBUG -ne 0 ];then
		logger -p ${SYSLOG}.info \
     		"Host does not give up mastery."
	fi
fi

STARTER=/usr/sap/${SAPSID}/SYS/exe/run/sapstart
SAPOSCOL=/usr/sap/${SAPSID}/SYS/exe/run/saposcol

MYLH=
NeedToStop=

# ###############################################################
#
# check if this is host is the master of CI.
# hareg -n will also shutdown CI, since it is NON_MASTERED.
#
# ###############################################################

for LH in $NOT_MASTERED ; do
	if [ "${LH}" = "${CI}" ]; then
		#
		# Host is not master of CI and indeed CI is running,
		# since SAPOSCOL or DW is running.
		# We need to stop the CI.
		#
		MYLH=$LH
		if ps -ef | egrep "${STARTER}" | grep -v grep >/dev/null ; then
			NeedToStop=y
			break
 		fi
	
		if ps -ef | egrep "${SAPOSCOL}" | grep -v grep >/dev/null ; then
  			NeedToStop=y
   		fi
   		break
  	fi
done

if [ "$NeedToStop" = "y" ]; then
   	if [ $HA_DEBUG -ne 0 ];then
  		logger -p ${SYSLOG}.info \
   		"Need to stop the CI."
  	fi
fi


# ###############################################################
#
# even we don't give up CI's mastery, hasap stop could request it
#
# ###############################################################

if [ -z "$MYLH" ]; then
  	#
  	# This host is not giving up mastery of the CI, but we need
  	# to check if data service state is off. If data service
  	# is off, we should turn off CI anyway. This could be a
  	# reconfiguration, per hasap stop's request.
  	#
	if [ $HA_DEBUG -ne 0 ];then
    		logger -p ${SYSLOG}.info \
     		"Checking Data Service status..."
  	fi

	if [ -n "$DS_STATE" -a "${DS_STATE}" = "n" ];then 
    		for LH in $MASTERED ; do
      			if [ "${LH}" = "${CI}" ]; then
				#
				# I am giving up CI and indeed CI is running
				# since SAPOSCOL or DW is running
				#
				if ps -ef | egrep "${STARTER}" | grep -v grep >/dev/null ; then
	  				NeedToStop=y
	  				break
				fi
	
				if ps -ef | egrep "${SAPOSCOL}" | grep -v grep >/dev/null ; then
	  				NeedToStop=y
					break
				fi
	  
      			fi
    		done
  	fi
fi

# ###############################################################
#
# When this point is reached, there is a stopping job to do.
# Do:
# 1. stop all app server/test/develop instances
# 2. stopsap r3
# 3. stop sap collector
# 4. clean up ipc
#
# ###############################################################

if [ "$NeedToStop" = "y" ];then 

	isRegistered=`haget -f service_is_on -s sap`
	if [ $isRegistered -ne 1 ]; then 
   		lognotice `gettext "Exit without turning off SAP."`
		exit 0
	fi

	if [ $HA_DEBUG -ne 0 ];then
   		lognotice "Stopping CI..."
  	fi

	#
	# This is an early starter. We will stop all instances before
	# we start the CI. However, if we can stop all instances ealier
	# cause less problem latter. stop_all_instances in start_net
	# is just a safety net for those cases that system crash.
	# If we stop app server/test/develp instances, we can release
	# the file system ealier. Or we will get umount nfs into trouble.
	#
        if [ -x ${PATH0}/hasap_stop_all_instances ]; then
                lognotice `gettext "Executing hasap_stop_all_instances in background"`
                ${PATH0}/hasap_stop_all_instances &
        fi

  	su - ${SAPADM}  -c "stopsap r3"
  	su - ${SAPADM}  -c "${SAPOSCOL} -k"
  	${PATH0}/hasap_ipcrm ${SAPADM}

  	if [ $HA_DEBUG -ne 0 ];then
   		lognotice "CI stopped"
  	fi

	exit 0
fi

#
# Prepare for mastery of the CI, which could need UFS mount, not NFS mount
#
if [ "$AUTO_NFSMOUNT" = "y" -o "$AUTO_NFSMOUNT" = "Y" ]; then
	${PATH0}/hasap_nfsumount
fi

if [ $HA_DEBUG -ne 0 ];then
     	logger -p ${SYSLOG}.info \
    	"Nothing to stop, exit!"
fi

exit 0
