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

validate_options()
{
        debug_message "Function: validate_options - Begin"
	${SET_DEBUG}

        #
        # Ensure all options are set
        #

	rc_validate_options=0

        for i in RESOURCE RESOURCEGROUP ZONE SERVICE OPTIONS SERVICE_PROBE
        do
                case $i in
                        RESOURCE)
                        if [ -z ${RESOURCE} ]; then
                                scds_syslog -p daemon.err -t $(syslog_tag) -m \
                               	  "Function: validate_options - %s Option %s not set" \
                               	  "${MYNAME}" "-R"
				rc_validate_options=1
                        fi;;

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

                        ZONE)
                        if [ -z ${ZONE} ]; then
                                scds_syslog -p daemon.err -t $(syslog_tag) -m \
                               	  "Function: validate_options - %s Option %s not set" \
                               	  "${MYNAME}" "-Z"
				rc_validate_options=1
                        fi;;

                        SERVICE)
                        if [ -z ${SERVICE} ]; then
                                scds_syslog -p daemon.err -t $(syslog_tag) -m \
                               	  "Function: validate_options - %s Option %s not set" \
                               	  "${MYNAME}" "-S"
				rc_validate_options=1
                        fi;;

                        OPTIONS)
                        if [ -z ${OPTIONS} ]; then
                                scds_syslog -p daemon.err -t $(syslog_tag) -m \
                               	  "Function: validate_options - %s Option %s not set" \
                               	  "${MYNAME}" "-O"
				rc_validate_options=1
                        fi;;

                        SERVICE_PROBE)
                        if [ -z ${OPTIONS} ]; then
                                scds_syslog -p daemon.err -t $(syslog_tag) -m \
                               	  "Function: validate_options - %s Option %s not set" \
                               	  "${MYNAME}" "-P"
				rc_validate_options=1
                        fi;;
                esac
        done

	debug_message "Function: validate_options -returns ${rc_validate_options} - End"
	return ${rc_validate_options}
}

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

	rc_validate=0

	##################
	# Validate ${ZONE}
	
	get_zone_state ${ZONE}

	if [ "${ZONE_STATE}" = "installed" -o "${ZONE_STATE}" = "ready" -o "${ZONE_STATE}" = "running" ]
	then
		debug_message "Function: validate - ${ZONE} state is ${ZONE_STATE}"
	else
		scds_syslog -p daemon.err -t $(syslog_tag) -m \
		  "Function: validate - %s state is %" \
		  "${ZONE}" "${ZONE_STATE}"
		rc_validate=1
	fi
		
	#####################
	# Validate ${SERVICE}

	# Validate the service exists and any services it's dependent on are online
	# Output any svcs errors to ${LOGFILE}
	# Output any dependents and their state to ${STATE}
	# Note: The resource will not be allowed to register if validate fails

	if STATE=`${ZLOGIN} ${ZONE} svcs -Hd ${SERVICE} 2> ${LOGFILE} | awk '{if ($1 != "online") print $3 " which is " $1}'`
	then
		if [ -s "${LOGFILE}" ]
		then
			# e.g. svcs: Pattern 'X' doesn't match any instances
			log_message error validate
			rc_validate=1
		fi

		if [ "$STATE" ]
		then
			# e.g. X is dependent on Y which is disbaled
			echo "${SERVICE} is dependent on $STATE" > ${LOGFILE}
			log_message error validate
			rc_validate=1
		else
			debug_message "Function: validate - ${SERVICE} is valid - dependent services are online"
		fi
	else
		scds_syslog -p daemon.err -t $(syslog_tag) -m \
		  "Function: validate - Failied to get serivce state for %s" \
               	  "${SERVICE}"
		rc_validate=1
	fi

	###########################
	# Validate ${SERVICE_PROBE}

	myprobecmd=`echo ${SERVICE_PROBE} | awk '{print $1}'`

	if ${ZLOGIN} ${ZONE} /usr/bin/test -x ${myprobecmd}
	then
		debug_message "Function: validate - ${myprobecmd} is executable"
	else
		scds_syslog -p daemon.err -t $(syslog_tag) -m \
	  	  "Function: validate - %s is not executable" \
          	  "${myprobecmd}"
		rc_validate=1
	fi	

	debug_message "Function: validate - End"
	return ${rc_validate}
}

start_sczsmf()
{
	#
	# Start sczsmf
	#

        debug_message "Function: start_sczsmf - Begin"
	${SET_DEBUG}

	rc_start_sczsmf=0

	${ZLOGIN} ${ZONE} svcadm -v enable -${OPTIONS} ${SERVICE} > $LOGFILE 2>&1
	rc_start_sczsmf=$?
	
	START_TIMEOUT=`${SCHA_RESOURCE_GET} -O START_TIMEOUT -R ${RESOURCE} -G ${RESOURCEGROUP}`
	sleep ${START_TIMEOUT} &

	if pmfadm -q $RESOURCEGROUP,$RESOURCE,0.svc
	then
		pmfadm -s $RESOURCEGROUP,$RESOURCE,0.svc	
	fi

	debug_message "Function: start_sczsmf - End"
	return ${rc_start_sczsmf}
}

stop_sczsmf()
{
	#
	# Stop sczsmf
	#

        debug_message "Function: stop_sczsmf - Begin"
	${SET_DEBUG}

	OPTIONS=`echo ${OPTIONS} | tr -d "r"`

	if ${ZLOGIN} ${ZONE} svcadm -v disable -${OPTIONS} ${SERVICE} > $LOGFILE 2>&1
	then
		rc_stop_sczsmf=0

		STOP_TIMEOUT=`${SCHA_RESOURCE_GET} -O STOP_TIMEOUT -R ${RESOURCE} -G ${RESOURCEGROUP}`
		MAX_STOP_TIMEOUT=`expr ${STOP_TIMEOUT} \* 60 \/ 100`

		while  [ "${MAX_STOP_TIMEOUT}" -ge 2 ]
		do
			sleep 2
			if get_svc_state ${ZONE} ${SERVICE}
			then
				debug_message "Function stop_sczsmf: ${SERVICE} state is ${SVC_STATE}, timeout ${MAX_STOP_TIMEOUT}"

				if [ "${SVC_STATE}" = "disabled" ]
				then
					MAX_STOP_TIMEOUT=0
				else
					MAX_STOP_TIMEOUT=`expr ${MAX_STOP_TIMEOUT} - 3`
				fi
			else
				MAX_STOP_TIMEOUT=`expr ${MAX_STOP_TIMEOUT} - 3`
			fi
		done

	else
		rc_stop_sczsmf=1
	fi

	if [ "${SVC_STATE}" != "disabled" ] -o [ "${rc_stop_sczsmf}" = 1 ]
	then
		if PIDS=`${ZLOGIN} ${ZONE} svcs -o fmri -p ${SERVICE} | awk '{if (NF == 3) print $2}'`
		then
			# Kill the service pids as reported by svcs
			for i in `echo $PIDS`
			do
				kill -9 $i
			done
	
			echo "${SERVICE} pids killed as reported by svcs" > ${LOGFILE}
		else
			# Kill the service pids as reported by pgrep
			pkill -9 -f -z ${ZONE} ${SERVICE}
	
			echo "${SERVICE} pids killed as reported by pgrep" > ${LOGFILE}
		fi
	fi

	if ${PMFADM} -q ${RESOURCEGROUP},${RESOURCE},0.svc
	then
		${PMFADM} -s ${RESOURCEGROUP},${RESOURCE},0.svc KILL 2> /dev/null
	fi

	debug_message "Function: stop_sczsmf - End"
	return 0
 }

check_sczsmf()
{
	# 
	# Probe sczsmf
	#

        debug_message "Function: check_sczsmf - Begin"
	${SET_DEBUG}

	rc_check_sczsmf=0

        if ps -z global | grep -w gds_svc_start | grep -w ${RESOURCE} >/dev/null
        then
                debug_message "Function: check_sczsmf - start program is still running "
                rc_check_sczsmf=100
	else
		get_svc_state ${ZONE} ${SERVICE}
		debug_message "Function: check_sczsmf - ${SERVICE}: State is ${SVC_STATE}"

		if [ "${SVC_STATE}" = "disabled" ]
		then
                	rc_check_sczsmf=100
		else
			# Don't react if ${SERVICE} is being restarted by the SMF
			# restarter. i.e. An asterisk (*) is appended for instances
			# in transition, e.g. online* or offline*

			if [ "${SVC_STATE}" = "online*" -o "${SVC_STATE}" = "offline*" ]
			then
				rc_check_sczsmf=0
			else
				${ZLOGIN} ${ZONE} ${SERVICE_PROBE} >/dev/null
				rc_check_sczsmf=$?
			fi
		fi
        fi

	debug_message "Function: check_sczsmf - End"
	return ${rc_check_sczsmf}
}
