#
# Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

SCLOGGER=/usr/cluster/lib/sc/scds_syslog
PKG=SUNWscsag
METHOD=`basename $0`
LOGFILE=/var/tmp/${RESOURCE}_logfile

SCHA_RESOURCE_GET=/usr/cluster/bin/scha_resource_get
SCHA_RESOURCEGROUP_GET=/usr/cluster/bin/scha_resourcegroup_get
TASK_COMMAND=""

##### vars #####
#	needed for sag we will get them from the cluster in the future
#       but our time schedule calls for drastic measures... ;)
################
# 
# get user name to run sag under
#User=swnet #default
#GATEWAY_DIR=/global/data1/SWIFTAlliance/Gateway
#SagUser=Administrator
#SagPwd=Sunr1se2
preamble="su - ${User} -c "
if [ -f /opt/${PKG}/etc/settings ]
then
	. /opt/${PKG}/etc/settings
	if [ $GATEWAY_DIR == "not set" ]; then
		debug_message " settings file has not been adapted "
		return 101
	fi
	
else
	# problem ... settings have not been set up
	debug_message " settings file not good"	
		
fi		
		
# load SAG variables
. ${GATEWAY_DIR}/bin/sag_env.ksh


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

		# 
		# Ensure the while loop only reads a closed file
		#

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

                su $USERID -c "cat /dev/null > $LOGFILE" > /dev/null
		cat /dev/null > ${LOGFILE}.copy
        fi

        debug_message "Function: log_message - End"
}

create_dummy_process()
{
  debug_message "Function: create_dummy_process - Begin"
  $SET_DEBUG

  DUMMY_PROCESS="/tmp/${RESOURCE}_${RESOURCEGROUP}_dummy.csh"

  cat > ${DUMMY_PROCESS} <<EOF
#!/bin/csh

while (1)
        sleep 3600
end
EOF

  /bin/chmod 755 ${DUMMY_PROCESS}

  ${DUMMY_PROCESS} &

  debug_message "Function: create_dummy_process - End"
  return
}


standard_resource_get()
{
        debug_message "Function: standard_resource_get - Begin"
        $SET_DEBUG

        PROPERTY=$1

        ${SCHA_RESOURCE_GET} -R ${RESOURCE} -G ${RESOURCEGROUP} -O ${PROPERTY}

        s1=$?

        if [ "${s1}" -ne 0 ]; then
                debug_message "standard_resource_get - Retrievment of property ${PROPERTY} returned ${s1}"
        fi

        debug_message "Function: standard_resource_get - End"
        return ${s1}
}

standard_resourcegroup_get()
{
        debug_message "Function: standard_resourcegroup_get - Begin"
        $SET_DEBUG

        PROPERTY=$1

        ${SCHA_RESOURCEGROUP_GET} -R ${RESOURCE} -G ${RESOURCEGROUP} -O ${PROPERTY}

        s1=$?

        if [ "${s1}" -ne 0 ]; then
                debug_message "standard_resource_get - Retrievment of property ${PROPERTY} returned ${s1}"
        fi

        debug_message "Function: standard_resourcegroup_get - End"

        return ${s1}
}

srm_function()
{

        USER=$1

        debug_message "Function: srm_function - Begin"
        $SET_DEBUG


        #
        # If Solaris 8 just return
        #

        if [ `/usr/bin/uname -r` = "5.8" ];
        then
                return 0
        fi

        #
        # Retrieve RESOURCE_PROJECT_NAME
        #

        RESOURCE_PROJECT_NAME=`standard_resource_get RESOURCE_PROJEC
T_NAME`

        #
        # Retrieve RG_PROJECT_NAME if RESOURCE_PROJECT_NAME is not set
        #

        if [ -z "${RESOURCE_PROJECT_NAME}" ]; then

                RESOURCE_PROJECT_NAME=`standard_resourcegroup_get RG_PROJECT_NAME`
        fi

        #
        # Return if no projects are defined
        #

        if [ -z "${RESOURCE_PROJECT_NAME}" ]; then
                return 0
        fi

        #
        # Validate that $USER belongs to the project defined by
        # ${RESOURCE_PROJECT_NAME}
        #

        PROJ_MEMBER=`/usr/bin/projects ${USER} | /usr/bin/grep -w ${RESOURCE_PROJECT_NAME}`

        if [ -z "${PROJ_MEMBER}" ];
        then
             scds_syslog -p daemon.error -t $(syslog_tag) -m \
                        "srm_function - The user %s does not belongs to project %s" \
                        "${USER}" "${RESOURCE_PROJECT_NAME}" 
                return 1
        else
                debug_message "srm_function - User ${USER} belongs to project ${RESOURCE_PROJECT_NAME}"
        fi

        #
        # Set TASK_COMMAND
        #

        TASK_COMMAND="/usr/bin/newtask -p ${RESOURCE_PROJECT_NAME}"

        debug_message "Function: srm_function - End"

        return 0
}



Check_InterRG_dependency()
{
        if [ -z "`/usr/cluster/bin/scrgadm -pvv -j ${RESOURCE} | /bin/grep \
        Resource_dependencies_restart`" ]; then

                scds_syslog -p daemon.error -t $(syslog_tag) -m \
               "No Inter RG-dependency found, using internal dependency"

                USE_INTERNAL_DEP=TRUE
        fi
}


check_restart_dependency()
{

        $SET_DEBUG
        debug_message "Function: check_restart_dependency - Start called with the following arguments "$*

        #
        # Put here the specific application code to report if the dependent
	# application has been restarted or not
        #
	# Use hatimerun when calling an external program.
	#
        # Return 0 if the dependent resource has NOT been restarted.
        # Return 1 if the dependent resource has been restarted.


        return 0

        debug_message "Function: check_restart_dependency - End"

}


check_start_dependency()
{

	$SET_DEBUG
	debug_message "Function: check_start_dependency - Start called with the following arguments "$*

        #
        # Put here the specific application code to report if the dependent
        # application has been started or not
	#
	# Use hatimerun when calling an external program.
        #
        # Return 0 if the dependent resource is online
        # Return 1 if the dependent resource is not online



	return 0

	debug_message "Function: check_start_dependency - End"
	
}

start_dependency()
{
	debug_message "Function: start_dependency - Begin"
	$SET_DEBUG

	# RETRIEVE START_TIMEOUT

	START_TIMEOUT=`/usr/cluster/bin/scha_resource_get -O START_TIMEOUT -R ${RESOURCE} -G ${RESOURCEGROUP}`

	# 80 % OF THE START-TIMEOUT CAN BE SPEND ON WAITING

	MAX_START_TIMEOUT=`expr ${START_TIMEOUT} \* 80 \/ 100`

	# GET CURRENT TIME IN SEC ON 24H BASE
		
	CUR_HOUR=`date '+%H'`
	CUR_MIN=`date '+%M'`
	CUR_SEC=`date '+%S'`
	CUR_TIME=`expr ${CUR_HOUR} \* 3600 + ${CUR_MIN} \* 60 + ${CUR_SEC}`

	# RUN A TEST LOOP UNTIL THE DEPENDENT RESOURCE IS UP OR 
	# A TIMEOUT HAS OCCURED

	while [ 1 -eq 1 ]
	do

		# GET NEW CURRENT TIMEOUT
		NEW_HOUR=`date '+%H'`
		NEW_MIN=`date '+%M'`
		NEW_SEC=`date '+%S'`
		NEW_TIME=`expr ${NEW_HOUR} \* 3600 + ${NEW_MIN} \* 60 + ${NEW_SEC}`

		# HAVE WE EXEEDED TIMEOUT

		s1=`expr ${CUR_TIME} + ${MAX_START_TIMEOUT}`

		if [ ${s1} -le ${NEW_TIME} ]; then
			scds_syslog -p daemon.error -t $(syslog_tag) -m \
       			"start_dependency: Exeeded ${MAX_START_TIMEOUT} seconds for waiting on dependent resource for resource ${RESOURCE} to come online"

			St=1
			break
		fi

		# CALL check_start_dependency

		debug_message "Function: start_dependency - Call check_start_dependency function with argument "$*

		check_start_dependency $*
		St=$?

		if [ ${St} -eq 0 ]; then
		  St=0
		  break
		fi

		# Wait 5 seconds

		sleep 5
	done


	debug_message "Function: start_dependency - End"

	return ${St}
}

restart_dependency()
{
	debug_message "Function: restart_dependency - Begin"
	$SET_DEBUG


	# CALL check_restart_dependency

	debug_message "Function: start_dependency - Call check_restart_dependency function with argument "$*
	check_restart_dependency $*
	St=$?

	if [ ${St} -ne 0 ]; then
           scds_syslog -p daemon.error -t $(syslog_tag) -m \
           "restart_dependency - Dependent resource to resource %s has been restarted, restart this resource %s" \
           "${RESOURCE}" "${RESOURCE}"

	   St=100
        else
           St=0
	fi

	debug_message "Function: restart_dependency - End"

	return ${St}
}


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

	SCRIPTNAME=`basename $0`
        for i in RESOURCE RESOURCEGROUP USE_INTERNAL_DEP
        do
                case $i in
                        RESOURCE)
                        if [ -z $RESOURCE ]; then
                                scds_syslog -p daemon.err -t $(syslog_tag) -m \
                                "validate_options: %s Option -R not set" \
                                "${SCRIPTNAME}"
                                exit 1
                        fi;;

                        RESOURCEGROUP)
                        if [ -z $RESOURCEGROUP ]; then
                                scds_syslog -p daemon.err -t $(syslog_tag) -m \
                                "validate_options: %s Option -G not set" \
                                "${SCRIPTNAME}"
                                exit 1
                        fi;;

                        USE_INTERNAL_DEP)
                        if [ ${USE_INTERNAL_DEP} = "FALSE" ]; then
				Check_InterRG_dependency
                        fi;;
                esac
        done
}

validate()
{
	#
	# Validate xxx
	#
	filename=$1
        debug_message "Function: validate - Begin"
	$SET_DEBUG

	rc_validate=0

	#
	# Validate that ${filename} exists
	#

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

	#
	# More validate checks here
	#

	debug_message "Function: validate - End"
}

start_sag()
{
	#
	# Start xxx
	#

        debug_message "Function: start_sag - Begin"
	$SET_DEBUG

	# start a dummy loop, to allow pmf .... to be faked out
	su - ${SagUser} -c "/opt/SUNWscsag/bin/loop.sh &"
	#####
	##### starting SAG actually requires multiple steps...
	
	su - ${User} -c "${GATEWAY_DIR}/bin/sag_bootstrap start"
	. ${GATEWAY_DIR}/bin/sag_env.ksh
	if [ $? != 0 ]; then
		# execution of bootstrap failed... bailing out
		debug_message " failed first part of the start"
		return 100
	fi
	count=0
	while true; do
		# check if bootstrap is ready
		${preamble} ". ${SAG_HOME}/bin/sag_env.ksh;${SAG_HOME}/bin/sag_system -SagUser ${SagUser} -SagPwd ${SagPwd} -- status system | grep stopped"
		if [ $? != 0 ]; then
			#bootstrap not finished yet
			sleep 5
			#
			# count=count+1
			# if count > 500 -> failed
			# if not loop
			# 
			debug_message " bootstrap not yet finished... looping"
		else 
			# ok sag base is there, ready to be started
			debug_message " bootstrap started fine"
			break
		fi
			
	done
	
	debug_message "first start was successfull"
	set_status "UNKNOWN"
	# we have started the backend.. let it settle
	${preamble} "$SAG_HOME/bin/sag_system -SagUser ${SagUser} -SagPwd ${SagPwd} -- start"
	# let it settle ... takes time to actually start
	while true; do
		# loop to wait until running
		${preamble} ". ${SAG_HOME}/bin/sag_env.ksh;${SAG_HOME}/bin/sag_system -SagUser ${SagUser} -SagPwd ${SagPwd} -- status system | grep started"
		if [ $? != 0 ]; then
			# not yet started! 
			debug_message " waiting until sag is ready "
			sleep 10
		else
			# break loop ... started
			debug_message " sag is running "
			break
		fi
	done
	# depend on the probe to actually test the app running
	set_status OK

	debug_message "Function: start_sag - End"
}

stop_sag()
{
	#
	# Stop sag
	#

        debug_message "Function: stop_sag - Begin"
	$SET_DEBUG
	# source the settings otherwise SAG_HOME will be unknown
	. ${GATEWAY_DIR}/bin/sag_env.ksh

	#
	# This should take care of sh, ksh, csh, tcsh and bash
	#

	pkill -U ${SagUser} -9 loop.sh
	#settings 
	# check running :
	
	${preamble} "$SAG_HOME/bin/sag_bootstrap stop"
	if [ $? != 0 ]; then
		# bootstrap returned faul : rc -> 102
		return 102
		exit
	fi
	while true; do
		# loop until we know there is no sag anymore
		#
		#
		#
		proccount=`pgrep -U ${User} | wc -l`
		if [ $proccount = 0 ]; then
			# ok no more processes
			sleep 5
			break
		else
			if [ -f /tmp/sagkill ] ; then
				# emergency stop- too many loops .. manual kill
				debug_message " emergency breakout"
				break
			fi
				
			# still processes
			#cnt1=`ps -ef | grep ${User} |grep -v ora | wc -l `
			#if [ $cnt1 = 0] ; then
			#	# only oracle processes still running
			#	# normally this should not happen..
			#	# but the behaviour persisted
			#	 ${preamble} "$SAG_HOME/bin/stop_oracle"
			#else
				# there is more than oracle processes
				debug_message "waiting for all swnet processes to dissapear"
				sleep 10
			#fi
		fi
	done

	debug_message "Function: stop_sag - End"
}

check_sag()
{
	# 
	# Probe xxx
	#

        debug_message "Function: check_xxx - Begin"
	$SET_DEBUG

	rc_check_sag=0

        ################
	##
	##  probing consists of a few checks
	## provided by swift to perform the checks
	##
	################
	# for 4.0.20 only  5.0 
	${preamble} "$SAG_HOME/bin/sag_system -SagUser ${SagUser} -SagPwd ${SagPwd} -- status system | grep Running"
	retcode=$?
	if [ $retcode = 0 ]; then
		set_status OK
	fi
	if [ $retcode != 0 ]; then
		# check did not return a running process
		debug_message " ! no running status returned on first check "
		sleep 5
		set_status DEGRADED
	
		
		sleep 5
		# badness in sytem state checking
		# recheck ... some false positives have been observed
		${preamble} "$SAG_HOME/bin/sag_system -SagUser ${SagUser} -SagPwd ${SagPwd} -- status system | grep Partial"
		if [ $? != 0 ]; then
			# check did not return a running process
			#  : flag not running sag system
			debug_message " no running status returned on second check "
			set_status FAULTED
			return 100
		else
			# clear the state ... running ...
			debug_message " we got a partial  ..."
			
		fi
	fi
	# all is hunky dory if we arrive at this spot
	#############################################

	debug_message "Function: check_sag - End"
}

set_status()
{
        #
        # Set appropriate resource status
        #

        debug_message "Function: set_status - Begin"
        $SET_DEBUG

        STATUS_TYPE=$1

        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.err -t $(syslog_tag) -m \
                "set_status - rc<%s>  type<%s>" \
                "${rc_scha_resource_setstatus}" "${STATUS_TYPE}"
        fi

        debug_message "Function: set_status - End"
}

