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

SCLOGGER=/usr/cluster/lib/sc/scds_syslog
PKG=SUNWsc9ias
METHOD=`basename $0`
LOGFILE=/var/tmp/${RESOURCE}_logfile
LHOSTNAME="LHOSTNAME=${OIAS_LHOST}"
TASK_COMMAND=""
RESOURCE_PROJECT_NAME=""
SCHA_RESOURCEGROUP_GET=/usr/cluster/bin/scha_resourcegroup_get
SCHA_RESOURCE_GET=/usr/cluster/bin/scha_resource_get
SCHA_CLUSTER_GET=/usr/cluster/bin/scha_cluster_get

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

                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 $OIAS_USER -c "cat /dev/null > $LOGFILE" > /dev/null
                cat /dev/null > ${LOGFILE}.copy
        fi

        debug_message "Function: log_message - End"
}

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

        for i in RESOURCE RESOURCEGROUP OIAS_ORAHOME OIAS_ORASID OIAS_LHOST OIAS_USER OIAS_ADMIN OIAS_INFRA MODE OIAS_FQDN
        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;;

                        OIAS_ORAHOME)
                        if [ -z $OIAS_ORAHOME ]; then
                                logger -p daemon.err \
                                "ERROR: `basename $0` Option -O not set"
                                exit 1
                        fi;;

                        OIAS_ORASID)
                        if [ -z $OIAS_ORASID ]; then
                                logger -p daemon.err \
                                "ERROR: `basename $0` Option -S not set"
                                exit 1
                        fi;;

                        OIAS_LHOST)
                        if [ -z $OIAS_LHOST ]; then
                                logger -p daemon.err \
                                "ERROR: `basename $0` Option -H not set"
                                exit 1
                        fi;;

                        OIAS_USER)
                        if [ -z $OIAS_USER ]; then
                                logger -p daemon.err \
                                "ERROR: `basename $0` Option -U not set"
                                exit 1
                        fi;;

                        OIAS_ADMIN)
                        if [ -z $OIAS_ADMIN ]; then
                                logger -p daemon.err \
                                "ERROR: `basename $0` Option -P not set"
                                exit 1
                        fi;;

                        OIAS_INFRA)
                        if [ -z $OIAS_INFRA ]; then
                                logger -p daemon.err \
                                "ERROR: `basename $0` Option -E not set"
                                exit 1
                        fi;;

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

validate()
{
	# 
	# Validate
	# 

        debug_message "Function: validate - Begin"
        $SET_DEBUG

        rc_validate=0
	
        #
        # Validate that $ORACLE_HOME exists
        #

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

	#
	# Validate that $OIAS_USER exists
	#

        if cat /etc/passwd | awk -F: '{print $1}' | grep $OIAS_USER > /dev/null
        then
                debug_message "Validate - Infrastructure user ${OIAS_USER} exists"
        else
                scds_syslog -p daemon.err -t $(syslog_tag) -m \
                        "Validate - Infrastructure user <%s> does not exist" \
                        "${OIAS_USER}"
                rc_validate=1
        fi

        #
        # Validate that $OIAS_INFRA exists
        #

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

	# 
	# Validate Secure library and set LDPRELOAD
	#

	check_ias_version
	
	if [ "$rc_check_ias_version" -eq 1 ]
	then
		if [ -L /usr/lib/secure/libloghost_32.so.1 ] && [ -f /usr/lib/libloghost_32.so.1 ]
		then
			debug_message "Validate - 32 bit libloghost_32.so.1 is secure"
		else
        		scds_syslog -p daemon.err -t $(syslog_tag) -m \
               			"Validate - 32 bit libloghost_32.so.1 is not secure"
				rc_validate=1
		fi

		if [ -L /usr/lib/secure/sparcv9/libloghost_64.so.1 ] && [ -f /usr/lib/sparcv9/libloghost_64.so.1 ]
		then
			debug_message "Validate - 64 bit libloghost_64.so.1 is secure"
		else
        		scds_syslog -p daemon.err -t $(syslog_tag) -m \
               			"Validate - 64 bit libloghost_64.so.1 is not secure"
				rc_validate=1
		fi
	fi

        debug_message "Function: validate - End"
}

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 - Get property ${PROPERTY} rc<${s1}>, ignore if SC3.0"
        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 - Get property ${PROPERTY} rc<${s1}>, ignore if SC3.0"
        fi

        debug_message "Function: standard_resourcegroup_get - End"

	return ${s1}
}

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

        USER=$1

        #
        # 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_PROJECT_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 \
                        "Validate - 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
}

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"
}

remove_dummy_process()
{
        debug_message "Function: remove_dummy_process - Begin"
        $SET_DEBUG
	
        DUMMY_PROCESS="/tmp/${RESOURCE}_${RESOURCEGROUP}_dummy.csh"

	for i in `pgrep -f ${DUMMY_PROCESS}`
	do
		kill -9 $i
	done

        debug_message "Function: remove_dummy_process - End"
}

check_ias_version()
{
	# 
	# Check if we are running 9iAS or 10gAS
	#
	
	debug_message "Function: check_ias_version - Begin"
	$SET_DEBUG

	VERSION=`grep Version= ${OIAS_ORAHOME}/config/ias.properties | awk -F= '{print $2}'`

	case "${VERSION}" in
		9.0.4*|10.*)    rc_check_ias_version=0
				;;
		*)              rc_check_ias_version=1
				;;
	esac

	debug_message "Function: check_ias_version - End"
}

resetiAS_passwd()
{
	#
	# Reset the iAS password
	#

	debug_message "Function: resetiAS_passwd - Begin"
	$SET_DEBUG

	execute_command resetiASpasswd.sh "cn=orcladmin $OIAS_ADMIN $OIAS_ORAHOME"

	if [ "$rc_execute_command" -eq 0 ]
	then
		log_message info "resetiAS_passwd"
	else
		log_message error "resetiAS_passwd"
	fi
		
	debug_message "Function: resetiAS_passwd - End"
}

reregister()
{
        #
        # Reset the iAS password
        #

        debug_message "Function: reregister - Begin"
        $SET_DEBUG

	PORT=`grep "Oracle Internet Directory(non-SSL)" ${OIAS_ORAHOME}/install/portlist.ini | awk -F= '{print $2}'`
	REREGISTER="/tmp/${RESOURCE}_${RESOURCEGROUP}_reregister.ksh"

	execute_command "${OIAS_ORAHOME}/bin/ldapsearch -h $OIAS_LHOST -p $PORT -D 'cn=orcladmin' -w '$OIAS_ADMIN' \
-b 'cn=IAS Infrastructure Databases,cn=IAS,cn=Products,cn=OracleContext' -s sub 'orclResourceName=orasso'" "orclpasswordattribute"

	OSSOPWD=`cat $LOGFILE | grep orclpasswordattribute | awk -F= '{print $2}'`

	REREGISTER="/tmp/${RESOURCE}_${RESOURCEGROUP}_reregister.ksh"

	cat > ${REREGISTER} <<-EOF
	#!/bin/ksh

	${OIAS_ORAHOME}/jdk/bin/java \
	-jar ${OIAS_ORAHOME}/sso/lib/ossoreg.jar \
	-site_name ${OIAS_ORASID}.${OIAS_FQDN} \
	-success_url http://${OIAS_FQDN}:7777/osso_login_success \
	-cancel_url http://${OIAS_FQDN}:7777 \
	-logout_url http://${OIAS_FQDN}:7777/osso_logout_success \
	-home_url http://${OIAS_FQDN}:7777 \
	-config_mod_osso TRUE \
	-oracle_home_path ${OIAS_ORAHOME} \
	-u root \
	-apache_server_root ${OIAS_ORAHOME}/Apache/Apache \
	-config_file ${OIAS_ORAHOME}/Apache/Apache/conf/osso/osso.conf \
	-sso_server_version v1.2 \
	-schema orasso \
	-pass $OSSOPWD
	EOF

        /bin/chmod 755 ${REREGISTER}
        /bin/chown ${OIAS_USER} ${REREGISTER}

	execute_command ${REREGISTER} ""

	if [ "$rc_execute_command" -eq 0 ]
	then
		log_message info "reregister"
	else
		log_message error "reregister"
	fi
		
        debug_message "Function: reregister - End"
}

execute_command()
{
	#
	# Generic execute command 
	#

	debug_message "Function: execute_command - Begin"
	$SET_DEBUG

	COMMAND=$1
	ARG=$2

	rc_execute_command=0 

	check_ias_version

	if [ "$rc_check_ias_version" -eq 0 ]
	then
		# Logical Host interposing not required for 9.0.4
		LDP32=""
		LDP64=""
	else
		LDP32="LD_PRELOAD_32=libloghost_32.so.1"
		LDP64="LD_PRELOAD_64=libloghost_64.so.1"
	fi

	LDLP="LD_LIBRARY_PATH=${OIAS_ORAHOME}/lib"
	OHOME="ORACLE_HOME=$OIAS_ORAHOME"
	OSID="ORACLE_SID=$OIAS_ORASID"
	RUN_PATH="PATH=${OIAS_ORAHOME}/bin:$PATH"
	DISPLAY="DISPLAY=${OIAS_LHOST}:0"
	
	srm_function ${OIAS_USER}

        if getent passwd ${OIAS_USER} | awk -F: '{print $7}' | grep csh > /dev/null
        then
                su ${OIAS_USER} -c "$TASK_COMMAND env $LHOSTNAME env $LDP32 env $LDP64 env $LDLP \
env $OHOME env $OSID env $RUN_PATH env $DISPLAY $COMMAND $ARG >& ${LOGFILE}" > /dev/null
		rc_execute_command=$?
        else
                su ${OIAS_USER} -c "$TASK_COMMAND env $LHOSTNAME env $LDP32 env $LDP64 env $LDLP \
env $OHOME env $OSID env $RUN_PATH env $DISPLAY $COMMAND $ARG >${LOGFILE} 2>&1" > /dev/null
		rc_execute_command=$?
	fi

	debug_message "Function: execute_command - End"
}

execute_oidmon()
{
	#
	# Execute Oracle Internet Directory monitor
	#

	debug_message "Function: execute_oidmon - Begin"
	$SET_DEBUG
	
	ARG=$1

        if [ "$ARG" = "stop" ]; then
		# Allow oidmon some time to finish processing when stopping
		sleep 10
	fi
		
	execute_command "${OIAS_ORAHOME}/bin/oidmon sleep=5" $ARG

	debug_message "Function: execute_oidmon - End"
}

build_sql()
{
	#
	# Build the Select and Truncate SQL scripts
	#

	debug_message "Function: build_sql - Begin"
	$SET_DEBUG

	SQLSELECT="/tmp/${RESOURCE}_${RESOURCEGROUP}_SQLSelect.ksh"
	SQLTRUNCATE="/tmp/${RESOURCE}_${RESOURCEGROUP}_SQLTruncate.ksh"

	cat > ${SQLSELECT} <<-EOF
	#!/bin/ksh

	${OIAS_ORAHOME}/bin/sqlplus -s ods/ods@${OIAS_ORASID} <<-END
	set echo off verify off termout off feedback off head off
	select pid from ods.ods_process where configset=0 and instance=1;
	END

	EOF

	cat > ${SQLTRUNCATE} <<-EOF
	#!/bin/ksh

	${OIAS_ORAHOME}/bin/sqlplus -s ods/ods@${OIAS_ORASID} <<-END
	set echo off verify off termout off feedback off head off
	truncate table ods.ods_process;
	END

	EOF

	/bin/chmod 755 ${SQLSELECT}
	/bin/chmod 755 ${SQLTRUNCATE}
	/bin/chown ${OIAS_USER} ${SQLSELECT}
	/bin/chown ${OIAS_USER} ${SQLTRUNCATE}

	debug_message "Function: build_sql - End"
}

execute_oidldapd()
{
	#
	# Execute Oracle Internet Directory Server
	#

	debug_message "Function: execute_oidldapd - Begin"
	$SET_DEBUG

	# The oidctl utility issues run-server and stop-server commands that are
	# interpreted and processed by the OID monitor process, via table
	# ods.ods_process.

	ARG=$1
	SAVEARG=$ARG
	PID=

        if [ "$ARG" = "start" ]
        then
		build_sql
		execute_command ${SQLSELECT} ""
		ARG=$SAVEARG

		# If $LOGFILE is empty then "no rows selected" would have been returned

		if [ -s $LOGFILE ]
		then
			PID=`tail -1 $LOGFILE | awk '{print $1}'`

			ps -p $PID 2>&1 >/dev/null
			rc=$?
			
			# Should only hit rc=1, i.e. On failover ods.ods_process is wrong
			# If we hit rc=0 then just let it run and let oidmon fix it

			case $rc in
				0)	continue;;
				*)	# ods.ods_process has lost it's pid or bad sql
					execute_command ${SQLTRUNCATE} ""
					ARG=$SAVEARG
					;;
			esac
		fi
	fi

	# On Start, if the oidldap server is already running, oidmon will report 
	# 	*** Instance Number already in use. ***
	# 	*** Please try a different Instance number. ***
	# which is fine as we'll let oidmon sort it out.

	execute_command "${OIAS_ORAHOME}/bin/oidctl server=oidldapd configset=0 instance=1" $ARG

	debug_message "Function: execute_oidldapd - End"
}

execute_em()
{
	#
	# Execute Oracle Enterprise Manager
	#

	debug_message "Function: execute_em - Begin"
	$SET_DEBUG

	ARG=$1

	# If the mid tier is also installed onto infrastructure failover nodes
	# then /var/opt/oracle will contain mid tier entries. So, before the
	# mid tier is installed the infrastructure entries in /var/opt/oracle
	# must be saved, i.e cp -rp /var/opt/oracle /var/opt/oracle_infra
	# before installing a mid tier instance. /var/opt/oracle_infra is then
	# referenced when the resource is registered as ${OIAS_INFRA}
	#
	# This needs to be done on each infrastructure failover node where the 
	# mid tier will be installed.
	# 
	# oidctl runs checkActiveEmdRoot() against EMDROOT using /var/opt/oracle
	# entries and as such the infrastructure entry emtab needs to be restored
	# prior to issuing oidctl.

	check_ias_version

	# 10gAS v9.0.4 does not create or use an emtab file

	if [ "$rc_check_ias_version" -eq 1 ]
	then
		# Save the current emtab 

		cp /var/opt/oracle/emtab /var/opt/oracle/emtab_${RESOURCE}

		# Copy the infrastructure emtab and allow for 
		# ${OIAS_INFRA}/emtab to be identical to /var/opt/oracle/emtab
		# i.e. if ${OIAS_INFRA} is set to /var/opt/oracle

		cp ${OIAS_INFRA}/emtab /var/opt/oracle/emtab 2> /dev/null
	fi

	if [ "$ARG" = "stop" ]
	then
		if [ "$rc_check_ias_version" -eq 0 ]
		then
			execute_command "${OIAS_ORAHOME}/bin/emctl stop iasconsole" "&"
		else
			execute_command "/usr/bin/echo $OIAS_ADMIN | ${OIAS_ORAHOME}/bin/emctl stop" "&"
		fi

		sleep 10 

		if /usr/ucb/ps auxww | grep EMDROOT | grep ${OIAS_ORAHOME} > /dev/null
		then
			for i in `pgrep -f "emctl stop"`
			do
				kill -9 $i
			done

			# Cleanup EM if it's still running
			for i in `/usr/ucb/ps auxww | grep EMDROOT | grep ${OIAS_ORAHOME} | grep -v grep | awk '{print $2}'`
			do
				kill -9 $i
			done
		fi
	else
		if [ "$rc_check_ias_version" -eq 0 ]
		then
			execute_command "${OIAS_ORAHOME}/bin/emctl $ARG" "iasconsole"
		else
			execute_command "${OIAS_ORAHOME}/bin/emctl" $ARG
		fi
	fi

	if [ "$rc_check_ias_version" -eq 1 ]
	then
		# Reinstate the previously saved emtab

		cp /var/opt/oracle/emtab_${RESOURCE} /var/opt/oracle/emtab
	fi

	debug_message "Function: execute_em - End"
}

execute_opmn()
{
	#
	# Execute Oracle Process Management Notification
	#

	debug_message "Function: execute_opmn - Begin"
	$SET_DEBUG

	ARG=$1

	if [ "$ARG" = "start" ]
	then
		OPMN_COMPONENTS=`echo $OIAS_OPMN | tr "/" " "`

		for i in $OPMN_COMPONENTS
		do
			if [  "$i" = "ALL" ]  ||  [ "$i" = "All" ] || [ "$i" = "all" ]
			then
				execute_command "${OIAS_ORAHOME}/opmn/bin/opmnctl" "startall"
			else

				check_ias_version
	
				if [ "$rc_check_ias_version" -eq 0 ]
				then
					# Start opmn daemon without starting opmn managed processes 

					if ! pgrep -f -u ${OIAS_USER} opmn >/dev/null
					then
						execute_command "${OIAS_ORAHOME}/opmn/bin/opmnctl" "start"
					fi
					
					# Start the ias-component OID as it is a dependent process for other ias-components

					execute_command "${OIAS_ORAHOME}/opmn/bin/opmnctl status | grep OID | grep Alive" ""
			
					if [ "$rc_execute_command" -eq 1 ]
					then
						execute_command "${OIAS_ORAHOME}/opmn/bin/opmnctl startproc ias-component=OID" ""
					fi

					# Start the ias-componets as listed

					execute_command "${OIAS_ORAHOME}/opmn/bin/opmnctl startproc ias-component=$i" ""
				else
					if [  "$i" = "ohs" ]
					then
						execute_command "${OIAS_ORAHOME}/dcm/bin/dcmctl start -ct ohs" ""
					else
						# If we get a typo component, then it will just get ignored
						execute_command "${OIAS_ORAHOME}/dcm/bin/dcmctl start -co $i" ""
					fi
				fi
			fi
		done
	else
		execute_command "${OIAS_ORAHOME}/opmn/bin/opmnctl" $ARG
	fi

	debug_message "Function: execute_opmn - End"
}

check_oidldapd()
{
        #
        # Check the Oracle Internet Directory is working
        #

	debug_message "Function: check_oidldapd - Begin"
	$SET_DEBUG

	rc_check_oidldapd=0

	PORT=`grep "Oracle Internet Directory(non-SSL)" ${OIAS_ORAHOME}/install/portlist.ini | awk -F= '{print $2}'`

	execute_command "${OIAS_ORAHOME}/bin/ldapsearch -v -p $PORT -b '' -s base '(objectclass=*)'" ""

	# If oidldap didn't respond then report back 50 to allow oidmon to restart the oidldapd server

	if [ "$rc_execute_command" -ne 0 ]
	then
		rc_check_oidldapd=50
	fi

	debug_message "Function: check_oidldapd - End"
}

check_em()
{
        #
        # Check the Oracle Enterprise Manager is working
        #

	debug_message "Function: check_em - Begin"
	$SET_DEBUG

	rc_check_em=0

	if ps -e -o args | grep start-em | grep $RESOURCE | grep -v grep >/dev/null
	then
		debug_message "Function: check_em - EM is starting, check delayed"
		rc_check_em=100
	else
		check_ias_version

		if [ "$rc_check_ias_version" -eq 1 ]
		then
			# Copy the infrastructure emtab and allow for 
			# ${OIAS_INFRA}/emtab to be identical to /var/opt/oracle/emtab
			# i.e. if ${OIAS_INFRA} is set to /var/opt/oracle

			cp ${OIAS_INFRA}/emtab /var/opt/oracle/emtab 2> /dev/null
		fi

		if /usr/ucb/ps auxww | grep -v grep | grep EMDROOT > /dev/null
		then
			if [ "$rc_check_ias_version" -eq 0 ]
			then
				execute_command emctl "status iasconsole"
				
				if ! grep "10g Application Server Control is running." ${LOGFILE} > /dev/null
				then
					rc_check_em=100
				else
					rc_check_em=0
				fi
			else
				execute_command emctl status

				if ! grep "EMD is up and running" ${LOGFILE} > /dev/null
				then
					rc_check_em=100
				else
					rc_check_em=0
				fi
			fi
		else
			rc_check_em=100
		fi

		if [ "$rc_check_ias_version" -eq 1 ]
		then
			# /var/opt/oracle/emtab_${RESOURCE} should already exist.
			# i.e. It was created when the EM resource was started (see execute_em)

			# Reinstate the previously saved emtab

			cp /var/opt/oracle/emtab_${RESOURCE} /var/opt/oracle/emtab
		fi
	fi

	debug_message "Function: check_em - End"
}

check_opmn()
{
        #
        # Check the OPMN managed processes
        #

	debug_message "Function: check_opmn - Begin"
	$SET_DEBUG
	
	rc_check_opmn=0

	if ps -e -o args | grep start-opmn | grep $RESOURCE | grep -v grep >/dev/null
	then
		debug_message "Function: check_opmn - OPMN is starting, check delayed"
		rc_check_opmn=100
	else
		if ! pgrep -f -u ${OIAS_USER} opmn >/dev/null
		then
			rc_check_opmn=100
		else
			check_ias_version
	
			if [ "$rc_check_ias_version" -eq 0 ]
			then
				execute_command "${OIAS_ORAHOME}/opmn/bin/opmnctl" "status" 

				if [ "$rc_execute_command" -ne 0 ]
				then
					rc_check_opmn=100
				fi
			else
				execute_command "${OIAS_ORAHOME}/dcm/bin/dcmctl" "getstate -v" 

				if [ "$rc_execute_command" -eq 0 ]
				then
					# Issue a start request for each OPMN component that we expect to be Up, yet is Down
					# If by chance the component was Down and now is Partially up then issuing the start will be ignored
	
					OPMN_COMPONENTS=`echo $OIAS_OPMN | tr "/" " "`
					OPMN_COMPONENTS_UP=`grep Up ${LOGFILE} | grep -v Component | awk '{if (NF == 6) print $4; else if (NF == 5 ) print $2}'`

					for i in $OPMN_COMPONENTS
					do
						if ! echo $OPMN_COMPONENTS_UP | grep $i >/dev/null
						then
							if [  "$i" = "ohs" ]
							then
								execute_command "${OIAS_ORAHOME}/dcm/bin/dcmctl start -ct ohs" ""
							else
								execute_command "${OIAS_ORAHOME}/dcm/bin/dcmctl start -co $i" ""
							fi
						fi
					done
				else
					rc_check_opmn=100
				fi
			fi
		fi
	fi

	debug_message "Function: check_opmn - End"
}

check_oidmon()
{
        #
        # Check OIDMON
        #

	debug_message "Function: check_oidmon - Begin"
	$SET_DEBUG

	rc_check_oidmon=0 

	if ! pgrep -f -u ${OIAS_USER} oidmon > /dev/null
	then
		rc_check_oidmon=100
	fi

	debug_message "Function: check_oidmon - 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"
}

