#
# Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
###################
# Swift Alliance Access integration
#  Kris Buggenhout SUN PS Belgium
###################
set -x 
SCLOGGER=/usr/cluster/lib/sc/scds_syslog
PKG=SUNWscsaa
METHOD=`basename $0`
LOGFILE=/var/tmp/${RESOURCE}_logfile
export PATH=/usr/bin:/bin:/usr/sbin
export USR_SWA=/usr/swa                 # SWA instance information
export ALL_ADM INST_NAME
export ARCH=SunOS
syslog_tag()
{
        #
        # Data Service message format
        #

        $SET_DEBUG

        print "SC[${PKG:-??}.${METHOD:-??}]:${RESOURCEGROUP:-??}:${RESOURCE:-??}"
}

scds_syslog()
{

        #
        # Log a message
        #

        $SET_DEBUG

        $SCLOGGER "$@" &
}

debug_message()
{
        #
        # Output a debug message to syslog if required
        #

        if [ "$DEBUG" = "$RESOURCE" -o "$DEBUG" = "ALL" ]
        then
                SET_DEBUG="set -x"

                DEBUG_TEXT=$1

                scds_syslog -p daemon.debug -t $(syslog_tag) -m \
                        "%s" "$DEBUG_TEXT"
        else
                SET_DEBUG=
        fi
}

log_message()
{
        #
        # Output a message to syslog as required
        #

        debug_message "Function: log_message - Begin"
        $SET_DEBUG

        if [ -s "${LOGFILE}" ]
        then
                PRIORITY=$1
                HEADER=$2

                while read MSG_TXT
                do
                        scds_syslog -p daemon.${PRIORITY} -t $(syslog_tag) -m \
                                "%s - %s" \
                                "${HEADER}" "${MSG_TXT}"
                done < $LOGFILE

                su - $USERID -c "cat /dev/null > $LOGFILE" > /dev/null
        fi

        debug_message "Function: log_message - End"
}

validate_options()
{
        #
        # Ensure all options are set
        #

        for i in RESOURCE RESOURCEGROUP 
        do
                case $i in
                        RESOURCE)
                        if [ -z $RESOURCE ]; then
                                logger -p daemon.err \
                                "ERROR: `basename $0` Option -R not set"
                                exit 1
                        fi;;

                        RESOURCEGROUP)
                        if [ -z $RESOURCEGROUP ]; then
                                logger -p daemon.err \
                                "ERROR: `basename $0` Option -G not set"
                                exit 1
                        fi;;
                esac
        done
}

validate()
{
	#
	# Validate SAA
	#
	
        debug_message "Function: validate - Begin"
	$SET_DEBUG

	rc_validate=0

	#
	# Validate that ${filename} exists
	#
	filename="/usr/swa/insts"

	if [ ! -f "${filename}" ]
	then
   		scds_syslog -p daemon.error -t $(syslog_tag) -m \
			"Validate - file %s does not exist" \
			"${filename}"
		rc_validate=1
	else
		debug_message "Validate - ${filename} exists"
	fi

	#
	# More valodate checks here
	#

	debug_message "Function: validate - End"
}

start_dce()
{
	#
	# Start dce
	#

        debug_message "Function: start_dce - Begin"
	$SET_DEBUG
	
	#
	# This should take care of sh, ksh, csh, tcsh and bash
	#
	/opt/dce/tcl/start.dce
	rc_start_command=$?
	debug_message "Function: start_dce - End"
}

stop_dce()
{
	#
	# Stop dce
	#

        debug_message "Function: stop_dce - Begin"
	$SET_DEBUG

	/opt/dce/tcl/stop.dce
	rc_stop_command=$?
	
	debug_message "Function: stop_dce - End"
}

check_dce()
{
	# 
	# Probe dce
	#

        debug_message "Function: check_dce - Begin"
	$SET_DEBUG

	rc_check_dce=0

	RESULT=`/opt/dce/tcl/show.cfg | grep RPC | awk ' {print $3}'`

	case "$RESULT" in
'		Not')
        		#can not connect to dced assuming it is dead
        		#print "ERROR    dced is dead"
        		rc_check_command=1
        	;;
		'Running')
        		#no problemmo  dced is running fine
        		rc_check_command=0
        	;;
	esac
	
	debug_message "Function: check_dce - End"
}



start_SAA()
{
	#
	# Start SAA
	#

        debug_message "Function: start_SAA - Begin"
	$SET_DEBUG
	
	while read TEMP1 TEMP2 TEMP3 ALL_ADM TEMP5 INST_NAME TEMP7
	do

	####for INST_NAME in `cat /usr/swa/insts | awk '{print $6}'`
	####do
		if [ -f $USR_SWA/alliance_init ]
		then
        		. $USR_SWA/alliance_init -s -i "$INST_NAME"
			$ALLIANCE/INS/bin/$ARCH/rc.alliance start &
			rc_start_command=?$
		else
			rc_start_command=100
			
		fi
		####################
		# loop until a_start_server dissapears ... to be shure not to return before saa is really started
		####################
		# first wait for a_start_server to appear
		####################
		while true
		do
			if pgrep -f -u ${ALL_ADM} a_start_server > /dev/null
			then
				break
			else 
				sleep 5
			fi
		done

		while true
		do
			if pgrep -f -u ${ALL_ADM} a_start_server > /dev/null
			then
				debug_message "saa server still starting... wait"
				sleep 5
			else
				debug_message "saa server start script is not running anymore"
				break
			fi
		done
	done < $USR_SWA/insts

	debug_message "Function: start_SAA - End"
}

check_hostrename()
{

rc_check_hostrename=0
while read TEMP1 TEMP2 TEMP3 ALL_ADM TEMP5 INST_NAME TEMP7
do
	if [ -f "/usr/swa/alliance_init" ]; then
		# call routine to see if the instance is running
		check_inst_running
			if [ "$rc_check_running" -ne 0 ]; then
				logger -p user.notice "instance $INST_NAME servers is still running"
			else
				/bin/su $ALL_ADM -c '
				export HOME=~$ALL_ADM
				. $USR_SWA/alliance_init -s -i "$INST_NAME"
				# Clean-up Alliance db (rename host)
				logger -p user.debug "Making SWIFTAlliance $INST_NAME ready"
				TO_HOST=`hostname`
				FROM_HOST=`cat $ALLIANCE_DB/.alliance_host`
				$ALLIANCE/BSS/bin/$ARCH/alliance hostrename "$FROM_HOST" "$TO_HOST"
				exit
				'
				log_message notice "ended host rename "
			fi
			
	else
		log_message error "can not find alliance_init in ~ALL_ADM for instance $INST_NAME"
		rc_check_hostrename=100
	fi
	done < $USR_SWA/insts
}

check_inst_running()
{	
	/bin/su ${ALL_ADM} -c '
 		export HOME=~$ALL_ADM
		. $USR_SWA/alliance_init -s -i "$INST_NAME"
		logger -p user.debug "Checking SWIFTAlliance $INST_NAME running"
		export LIBPATH=$ALLIANCE/common/lib/$ARCH:$ALLIANCE/BSS/lib/$ARCH
		$ALLIANCE/BSS/bin/$ARCH/alliance -E $ALLIANCE/BSS/bin/$ARCH/is_listening -- -- -P /BS_csys -T 10
		if [ $? -ne 0 ]
		then
			rc_check_running=100
			logger -p user.debug "instance $INST_NAME still running"
		else
			logger -p user.debug "ok instance $INST_NAME not running"
			rc_check_running=0
		fi
		'
}

start_swa_rpcd()
{
	#check if file exists
	if [ -f $USR_SWA/swa_rpcd ]
	then 
		
		debug_message "Starting SWIFTAlliance rpc process"
		$USR_SWA/swa_rpcd
		rc_start_swa_rpcd=$?
	else
		debug_message "can not find $USR_SWA/swa_rpcd"
		rc_start_swa_rpcd=100
	fi
}

stop_swa_rpcd()
{
	
	SWA_PID=`ps -ef | grep swa_rpcd | grep -v grep | awk ' { print $2 } '`
	if [ -n "$SWA_PID" ]
	then
		if [ -z $SWA_PID ]
		then
			debug_message "no swa_rpcd running"
			rc_stop_swa_rpcd=0
		else
        		kill -KILL $SWA_PID
        		if [ $? -ne 0 ]
        		then
        		        debug_message "Cannot kill swa_rpcd process (pid $SWA_PID)"
        		        rc_stop_swa_rpcd=100
        		else
        		        sleep 2
				rc_stop_swa_rpcd=0
        		fi
		fi
	fi
}

stop_SAA()
{
	#
	# Stop SAA
	#

        debug_message "Function: stop_SAA - Begin"
	$SET_DEBUG

	while read TEMP1 TEMP2 TEMP3 ALL_ADM TEMP5 INST_NAME TEMP7
	do
		check_inst_running
		if [ "$rc_check_running" -ne 0 ]
		then
			debug_message "$INST_NAME not running"
		else
			debug_message "Stopping SWIFTAlliance $INST_NAME"
			. $USR_SWA/alliance_init -s -i "$INST_NAME"
			su $ALL_ADM -c '$ALLIANCE/BSS/bin/$ARCH/alliance stop_server

			if [ $? -ne 0 ]
			then
				logger  -p user.error "could not stop running server for $INST_NAME"
				exit 1
			else
				logger -p user.debug "successfully stopped $INST_NAME"
			fi

			if [ -f $ALLIANCE/BSS/bin/$ARCH/select_swnet_inst ]
			then
			SELECTED=`$ALLIANCE/BSS/bin/$ARCH/alliance -E $ALLIANCE/BSS/bin/$ARCH/select_swnet_inst -- -- -E -R 2>&1 1>/dev/null`
			if [ $? -ne 0 ]
			then
				logger -p user.debug "SWIFTAlliance $SELECTED  no SNL "
			else
				logger -p user.debug "Setting SNL environment -  $SELECTED"
				eval $SELECTED
				export PATH=$PATH:$SWNET_HOME/bin
				. swiftnet init
				logger -p user.debug "Stopping SNL"
				swiftnet stop

				# clean_ipc does not exist when RA is used
				if [ -f $SWNET_HOME/bin/clean_ipc ]
				then
					clean_ipc 1>/dev/null
			fi
			'
		fi
		
	#done < $USR_SWA/insts
	#### loop over the processes until they dont exist.
	###################################################
	while true
	do
		if pgrep -f -u ${ALL_ADM} a_stop_server > /dev/null
		then
			debug_message "In STOP waiting for stop of processes"
			sleep 10
		else
			break
		fi
	done
	
	done < $USR_SWA/insts
	debug_message "Function: stop_SAA - End"
}

check_SAA()
{
	# 
	# Probe SAA
	#

        debug_message "Function: check_SAA - Begin"
	$SET_DEBUG

	rc_check_SAA=0
	###################################
	# as required by SWIFT no monitoring of the application is done 
	# since application control is kept in the SAA admin console
	# The cluster is used to be able to automate the SAA failover in case of 
	# failures of the framework... Sun Cluster should not care about the application
	# but fail it over, on demand, or when network failure occurs.
	#
	# This could change in the future if we can convince the people of SWIFT to do so...
	# or have them integrate cluster commands in their admin scripts, which would be the 
	# better way to do it ... disabling and enabling fault monitoring if needed from 
	# the SAA admin console.
	###################################
	
	debug_message "Function: check_SAA - End"
}

set_status()
{
        #
        # Set appropriate resource status
        #

        debug_message "Function: set_status - Begin"
        $SET_DEBUG

        STATUS_TYPE=$1

        /usr/cluster/bin/scha_resource_setstatus -R $RESOURCE -G $RESOURCEGROUP -s $STATUS_TYPE

        rc_scha_resource_setstatus=$?

        if [ "$rc_scha_resource_setstatus" != 0 ]
        then
                scds_syslog -p daemon.error -t $(syslog_tag) -m \
                "set_status - rc<%s>  type<%s>" \
                "${rc_scha_resource_setstatus}" "${STATUS_TYPE}"
        fi

        debug_message "Function: set_status - End"
}

