#! /usr/xpg4/bin/sh
#
# ident "@(#)oracle_server_validate.ksh 1.22     11/05/12"
#
# Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
#
# VALIDATE method
# 
# Parameters passed:  -R <resource name> -T <type> -G <group>
#
#! /usr/bin/ksh
#
# ident "@(#)dbms_lib.ksh 1.32     07/03/13 SMI"
#
# Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
############################################################ 
#
# DBMS library
#
############################################################ 

set -ufp
MYDIR=`/bin/dirname $0`
BINDIR=`/bin/dirname $0`
ETCDIR="$BINDIR/../etc"
DEBUG_LEVEL=-1
unset PATH
export PATH="/bin:/usr/cluster/bin"
DEBUG_LOGFILE=""
DEBUG_LOG_SIZE=300000
ARGUMENTS="$0 ${@:-""}"
RESOURCE_NAME=""
RESOURCE_TYPE=""
RESOURCE_GROUP=""
RESOURCE_PROJECT_NAME=""
RG_PROJECT_NAME=""
SCLOGGER=/usr/cluster/lib/sc/scds_syslog
LOCALORADIR=/var/run/ha-oracle

TRAPSIGNALS="TERM KILL"
trap "trap - $TRAPSIGNALS; /usr/bin/pkill -g $$;exit 1" $TRAPSIGNALS


#############################################################
# syslog_tag
#
#	prints syslog_tag as SC[pkg.method]: GROUP:RESOURCE
#
#############################################################
syslog_tag()
{
	print "SC[${pkg:-??}.${method:-??}]:${RESOURCE_GROUP:-??}:${RESOURCE_NAME:-??}" 
	return 0
}

#############################################################
#  scds_syslog <priority> <formattext> <parameters> 
#############################################################

scds_syslog()
{
typeset priority=""
typeset msg_format=""
typeset tag=""
typeset msg

	$SCLOGGER "$@" & 

	while getopts :p:t:m: opt
	do
  	case $opt in
		p)      priority="$OPTARG";;
		t)      tag="$OPTARG";;
		m)      msg_format="$OPTARG";;
		*)	;;
  	esac
	done

	shift `expr $OPTIND - 1`

	msg=$(/usr/bin/printf "${msg_format}" ${@:-} )

	[ -z "${TRACE_INIT:-""}" ] && init_trace

	[ "${DEBUG_LOGFILE}"  != "/dev/console" ] && \
	    print -r -- "$(date '+%b %d %T') $tag: $msg" >> $DEBUG_LOGFILE

	return 0
}

#############################################################
#  write_stderr <formattext> <parameters> 
#
#  This functions writes output to stderr
#  Expected usage is from validate method.
#  
#############################################################

write_stderr()
{
	typeset msg_format=${1:-""}
	typeset msg

	shift

	msg=$(/usr/bin/printf "${msg_format}" "${@:-}" )

    	print -r -u2 -- "$msg" 

	return 0
}



#############################################################
#  write_trace <text> <parameters> 
#   Other information: TAG format 
#############################################################

write_trace()
{
typeset msg_text=${1:-""}
typeset msg

shift

	msg=$(/usr/bin/printf "${msg_text}" "${@:-}" )
	[ -z "${TRACE_INIT:-""}" ] && init_trace
	[ "${DEBUG_LOGFILE}"  != "/dev/console" ] && \
	    print -r -- "$(date '+%b %d %T') $(syslog_tag): $msg" >> $DEBUG_LOGFILE

	return 0
}

#############################################################
#  read_arguments()
#  
#  Set Globals variables:
#     RESOURCE_NAME, RESOURCE_TYPE RESOURCE_GROUP
#
#############################################################
read_arguments()
{

typeset rc=0
typeset invalid_option=""
typeset save_opt=""
typeset save_optarg=""

while getopts :R:T:G: opt
do
  case $opt in
       R)      	RESOURCE_NAME="$OPTARG";;
       T)      	RESOURCE_TYPE="$OPTARG";;
       G)      	RESOURCE_GROUP="$OPTARG";;
       *)      	invalid_option="YES"; 
		save_opt="$opt"
		save_optarg="$OPTARG"
		 ;;
  esac
done

	if [ -z "$RESOURCE_NAME" \
		-o -z "$RESOURCE_TYPE" \
		-o -z "$RESOURCE_GROUP"  \
		-o -n "$invalid_option" ]; then

		# SCMSGS
		# @explanation
		# Unable to process parameters passed to the call back method.
		# This is an internal error.
		# @user_action
		# Please report this problem.
		scds_syslog -p error -t $(syslog_tag) -m \
	 		"Initialization failed. Invalid command line  %s %s" \
			"$save_optarg" "$save_opt"
		rc=1;
	fi
	
	return $rc

}

#############################################################
# check_resource_setup
#
#############################################################
check_resource_setup()
{
        if [ -z "$RESOURCE_NAME" \
                -o -z "$RESOURCE_TYPE" \
                -o -z "$RESOURCE_GROUP" ]; then
		return 1
	else
		return 0
	fi
}


#############################################################
# error_exit()
#
#############################################################
error_exit()
{
  typeset exit_code="${1:-0}"
  exit $exit_code
}

#############################################################
# extract_property_value()
#
#  For extended properties, API returns data type followed by value
#  This function discards first line (data type) and prints 
#	remaining lines.
#
#############################################################
extract_property_value()
{
	typeset type
	typeset value
	typeset rc=0
	
	read type 
	rc=$?
	[ $rc -ne 0 ] && return $rc

	while read value
	do
		print $value
	done
		
	rc=$?
	return $rc
}

#############################################################
# read_extension_property_value()
# 
# This function reads extension property values
#
# Parameter 1: <property name>
# Parameter 2: log_flag  (optional)
#	"NOLOG" -> Don't log syslog message
#	any other value -> log message
#	Default "YES"
#
#############################################################
read_extension_property_value()
{

	typeset rc
	typeset property="${1:-""}"
	typeset log_flag="${2:-"YES"}"

	scha_resource_get -O Extension \
		 -R "$RESOURCE_NAME" -G "$RESOURCE_GROUP" "$property" \
		| extract_property_value

	rc=$?
	if [ $rc -ne 0 ]; then
                if [ "$log_flag" != "NOLOG" ]; then
			# SCMSGS
			# @explanation
			# Error occurred in API call scha_resource_get.
			# @user_action
			# Check syslog messages for errors logged from other
			# system modules. If error persists, please report the
			# problem.
			scds_syslog -p error -t $(syslog_tag) -m \
                	    "Error (%s) when reading extension property <%s>." "$rc" "${property}" 
		fi
	fi	
	return $rc
}

#############################################################
# read_standard_property_value()
#
# This function reads property values of standard resource properties
#
# Parameter 1: <property name> required
# Parameter 2: log_flag  (optional)
#	"NOLOG" -> Don't log syslog message
#	any other value -> log message
#	Default "YES"
#
#############################################################
read_standard_property_value()
{
	typeset rc=0

	typeset property="${1:-""}"
	typeset log_flag="${2:-"YES"}"
	
	scha_resource_get -O "$property" \
	 	-R "$RESOURCE_NAME" -G "$RESOURCE_GROUP"

	rc=$?
	if [ $rc -ne 0 ]; then
                if [ "$log_flag" != "NOLOG" ]; then
			# SCMSGS
			# @explanation
			# Error occurred in API call scha_resource_get.
			# @user_action
			# Check syslog messages for errors logged from other
			# system modules. If error persists, please report the
			# problem.
			scds_syslog -p error -t $(syslog_tag) -m \
                    	   "Error (%s) when reading standard property <%s>." "$rc" "${property}" 
		fi
	fi	
	return $rc
}

#############################################################
# read_standard_rg_property_value()
#
# This function reads property values of standard resource group
# properties
#
# Parameter 1: <property name> required
# Parameter 2: log_flag  (optional)
#	"NOLOG" -> Don't log syslog message
#	any other value -> log message
#	Default "YES"
#
#############################################################
read_standard_rg_property_value()
{
	typeset rc=0

	typeset property="${1:-""}"
	typeset log_flag="${2:-"YES"}"
	
	scha_resourcegroup_get -O "$property" -G "$RESOURCE_GROUP"

	rc=$?
	if [ $rc -ne 0 ]; then
                if [ "$log_flag" != "NOLOG" ]; then
			scds_syslog -p error -t $(syslog_tag) -m \
                    	   "Error (%s) when reading standard property <%s>." "$rc" "${property}" 
		fi
	fi	
	return $rc
}


#############################################################
#
#############################################################
read_resource_project_name()
{
	typeset rc=0

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

	RESOURCE_PROJECT_NAME=$(read_standard_property_value RESOURCE_PROJECT_NAME)

	if [ -z "$RESOURCE_PROJECT_NAME" ]; then
		RESOURCE_PROJECT_NAME=$(read_standard_rg_property_value RG_PROJECT_NAME)
		rc=$?
	fi

	return $rc
}


#############################################################
# This function validates the resource project name.
#
# Parameter 1: <username> required
#
#############################################################
validate_resource_project_name()
{
	typeset PROJMEM=0
	typeset rc=0
	typeset USER="${1}"
	TASK_CMD=""

	if [ -z "$USER" ]; then
		# Internal usage error.
		return 1
	fi

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

	if [ -n "$RESOURCE_PROJECT_NAME" ]; then
		PROJMEM=`/usr/bin/projects $USER | /usr/bin/grep -w $RESOURCE_PROJECT_NAME | /usr/bin/wc -l`
	else
		if [ -n "$RG_PROJECT_NAME" ]; then
			RESOURCE_PROJECT_NAME=$RG_PROJECT_NAME
			PROJMEM=`/usr/bin/projects $USER | /usr/bin/grep -w $RESOURCE_PROJECT_NAME | /usr/bin/wc -l`
		fi
	fi

	if [ -n "$RESOURCE_PROJECT_NAME" ]; then
		if [ $PROJMEM -eq 0 ]; then
			scds_syslog -p error -t "$(syslog_tag)" -m \
		    	"Validation failed. User %s does not belong to the project %s." \
		     	$USER $RESOURCE_PROJECT_NAME
			write_stderr "$(gettext "Validation failed. User %s does not belong to the project %s.")"  \
		     	$USER $RESOURCE_PROJECT_NAME
			rc=1	
		else
			TASK_CMD="/usr/bin/newtask -p $RESOURCE_PROJECT_NAME"
		fi
	fi

	return $rc
}


#############################################################
# check_user_env
#
#############################################################
check_user_env()
{
	typeset rc=0

        if [ -z "${USER_ENV:-""}" ]; then
		return $rc
	fi

	if [ ! -f "$USER_ENV" ]; then
		# SCMSGS
		# @explanation
		# 'User_env' property was set when configuring the resource.
		# File specified in 'User_env' property does not exist or is
		# not readable. File should be specified with fully qualified
		# path.
		# @user_action
		# Specify existing file with fully qualified file name when
		# creating resource. If resource is already created, please
		# update resource property 'User_env'.
		scds_syslog -p error -t $(syslog_tag) -m \
	 	    "file specified in USER_ENV parameter %s does not exist" \
		    $USER_ENV

		write_stderr "$(gettext "file specified in USER_ENV parameter %s does not exist")" \
		    $USER_ENV
		rc=1
        fi
	return $rc
}

#############################################################
# set_user_env
#
#############################################################
set_user_env()
{
        typeset param
        typeset val
        typeset val1
	typeset aa_line

        [ -z "${USER_ENV:-""}" ] && return 0

	# Following flag will be set to "NO" in *server_manage methods.
        syslog_flag="${syslog_flag:-"YES"}"

	check_user_env || return $?

        while read aa_line
        do
        	[ -z ${aa_line:-""} ] && continue

		if [[ "$aa_line" == \#* ]]; then
		  continue
		fi

                # Now ensure that the line read is of the form
                # VARIABLE=VALUE

                if [[ "$aa_line" != *\=* ]]; then
			if [ "$syslog_flag" == "YES" ]; then
                        	# SCMSGS
                        	# @explanation
                        	# HA-Oracle reads the fle specified in
                        	# USER_ENV property and exports the variables
                        	# declared in the file.
                        	#
                        	# Syntax for declaring the variables is :
                        	#         VARIABLE=VALUE
                        	#
                        	# Lines starting with "#" are treated as
                        	# comment lines.
                        	#
                        	# VARIABLE is expected to be a valid Korn
                        	# shell variable that starts with alphabet or
                        	# "_" and contains alphanumerics and "_".
                        	# @user_action
                        	# Please check the environment file and
                        	# correct the syntax errors. Do not use export
                        	# statement in environment file.
                        	scds_syslog -p warning -t $(syslog_tag) -m \
				    "Incorrect syntax in the environment file %s. Ignoring %s"\
				    "${USER_ENV}" "${aa_line}"
			fi
                        
			write_stderr "$(gettext "Incorrect syntax in the environment file %s. Ignoring %s")" \
                            "${USER_ENV}" "${aa_line}"
                        continue
                fi

        	val="${aa_line#*=}"
        	param="${aa_line%%=*}"


		if [[ "$val" == *\"* ]]; then
			if [[ "$val" != \"*\" ]]; then
				if [ "$syslog_flag" == "YES" ]; then
                        		# SCMSGS
                        		# @explanation
                        		# HA-Oracle reads the file specified
                        		# in USER_ENV property and exports the
                        		# variables declared in the file.
                        		#
                        		# Syntax for declaring the variables
                        		# is : VARIABLE=VALUE
                        		#
                        		# VALUE may be a single-quoted or
                        		# double-quoted string. The string
                        		# itself may not contain any quotes.
                        		# @user_action
                        		# Please check the environment file
                        		# and correct the syntax errors.
                        		scds_syslog -p warning \
					    -t $(syslog_tag) -m \
					    "Ignoring string with misplaced quotes in the entry for %s" \
					    "${param}"
				fi

				write_stderr "$(gettext "Ignoring string with misplaced quotes in the entry for %s")" \
				"${param}"
				continue
			else
				#
				# Strip any double quotes around the value
				#
					val1="${val#*\"}"
					val="${val1%\"}"
			fi
		fi


		if [[ "$val" == *\'* ]]; then
			if [[ "$val" != \'*\' ]]; then
				if [ "$syslog_flag" == "YES" ]; then
                        		scds_syslog -p warning \
					    -t $(syslog_tag) -m \
					    "Ignoring string with misplaced quotes in the entry for %s" \
					    "${param}"
				fi

				write_stderr "$(gettext "Ignoring string with misplaced quotes in the entry for %s")" \
				"${param}"
				continue
			else
				#
				# Strip any single quotes around the value
				#
				val1="${val#*\'}"
				val="${val1%\'}"
			fi
		fi


		if [[ "$val" == *\"* || "$val" == *\'* ]]; then
			if [ "$syslog_flag" == "YES" ]; then
                       		scds_syslog -p warning \
				    -t $(syslog_tag) -m \
				    "Ignoring string with misplaced quotes in the entry for %s" \
				    "${param}"
			fi

			write_stderr "$(gettext "Ignoring string with misplaced quotes in the entry for %s")" \
			"${param}"
			continue
		fi


		#
		# Do not allow Environment_file to override PATH,
		# LD_LIBRARY_PATH, ORACLE_HOME, or ORACLE_SID
		#
		case "$param" in
			PATH|LD_LIBRARY_PATH|ORACLE_HOME|ORACLE_SID)
			if [ "$syslog_flag" == "YES" ]; then
				#
				# SCMSGS
				# @explanation
				# HA-Oracle methods read the file specified by
				# the USER_ENV property and set the
				# environment variables declared in this file.
				# If an entry in the file specified by the
				# USER_ENV property tries to set a critical
				# environment variable, that entry is ignored.
				# @user_action
				# Check USER_ENV and remove any entries which
				# attempt to set the environment variable
				# specified in the error message.
				#
                        	scds_syslog -p warning -t $(syslog_tag) -m \
				    "Ignoring %s set in environment file %s" \
				    "$param" "${USER_ENV}"
			fi

			write_stderr "$(gettext "Ignoring %s set in environment file %s")" \
				"$param" "${USER_ENV}"
			continue
			;;
		esac


		# Ensure that Variable is a valid KSH variable

		if [[ "$param" != @([A-Z]|[a-z]|_)*([A-Z]|[a-z]|[0-9]|_) ]];then
			if [ "$syslog_flag" == "YES" ]; then
				# SCMSGS
				# @explanation
				# HA-Oracle reads the file specified in
				# USER_ENV property and exports the variables
				# declared in the file.
				#
				# Syntax for declaring the variables is :
				# VARIABLE=VALUE
				#
				# Lines starting with "#" are treated as
				# comment lines.
				#
				# VARIABLE is expected to be a valid Korn
				# shell variable that starts with alphabet or
				# "_" and contains alphanumerics and "_".
				# @user_action
				# Please check the environment file and
				# correct the syntax errors. Do not use export
				# statement in environment file.
				scds_syslog -p warning -t $(syslog_tag) -m \
				    "Invalid variable name in the Environment file %s. Ignoring %s" \
				    "${USER_ENV}" "${aa_line}"
			fi

			write_stderr "$(gettext "Invalid variable name in the Environment file %s. Ignoring %s")" \
			     "${USER_ENV}" "${aa_line}"
			continue
		fi

		# Ignore unset variables used in USER_ENV file 
		set +u

		if [[ "$val" == *\`* ]]; then
			if [ "$syslog_flag" == "YES" ]; then
				#
				# SCMSGS
				# @explanation
				# HA-Oracle reads the file specified in
				# USER_ENV property and exports the variables
				# declared in the file.
				#
				# Syntax for declaring the variables is :
				# VARIABLE=VALUE
				#
				# If a command execution is attempted using
				# `<command>`, the VARIABLE is ignored.
				# @user_action
				# Please check the environment file and
				# correct the syntax errors by removing any
				# entry containing a back-quote (`) from it.
                        	#
                        	scds_syslog -p warning -t $(syslog_tag) -m \
				    "Ignoring command execution \`<command>\`"
			fi

			write_stderr "$(gettext "Ignoring command execution %s")" "\`<command>\`"
			continue
		elif [[ "$val" == *\$\(* ]]; then
			if [ "$syslog_flag" == "YES" ]; then
				#
				# SCMSGS
				# @explanation
				# HA-Oracle reads the file specified in
				# USER_ENV property and exports the variables
				# declared in the file.
				#
				# Syntax for declaring the variables is :
				# VARIABLE=VALUE
				#
				# If a command execution is attempted using
				# $(command), the VARIABLE is ignored.
				# @user_action
				# Please check the environment file and
				# correct the syntax errors by removing any
				# entry containing a $(command) construct
				# from it.
				#
				scds_syslog -p warning -t $(syslog_tag) -m \
				    "Ignoring command execution \$(command)"
			fi

			write_stderr "$(gettext "Ignoring command execution \$(command)")"
			continue
		fi

		if [ "$syslog_flag" == "NO" ]; then
			# This trace message is logged only from
			# *server_manage methods and not validate
			# method. stderr is redirected to
			# DEBUG_LOGFILE. 
			print -r -u2 -- \
				"Setting environment variable: ${param}=${val}"
		fi

		eval ${param}=\"${val}\"

		export $param

		# Reenable flag to check unset variables
		set -u

	done < $USER_ENV

	return 0

}


########################################################################
# export_env()
# Takes care of 64-bit and 32-bit libraries as well as other environment
# variables required by other methods.
#
########################################################################
export_env()
{
	LD_LIBRARY_PATH=${ORACLE_HOME}/lib64:${ORACLE_HOME}/lib:${ORACLE_HOME}/lib32:/usr/cluster/lib/sparcv9:/usr/cluster/lib${LD_LIBRARY_PATH_64:+:$LD_LIBRARY_PATH_64}${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}

	export LD_LIBRARY_PATH

	export ORACLE_HOME

	# Note that the := will initialize the parameter on the 
	# left hand side in case it is not defined. We are 
	# initializing it to empty string below.


	if [ -n "${ORACLE_OWNER:=""}" ]; then
		export ORACLE_OWNER
	fi
	if [ -n "${ORACLE_SID:=""}" ]; then
		export ORACLE_SID
	fi
	if [ -n "${PARAMETER_FILE:=""}" ]; then
		export PARAMETER_FILE
	fi
	if [ -n "${USER_ENV:=""}" ]; then
		export USER_ENV
	fi
	if [ -n "${CONNECT_STRING:=""}" ]; then
		export CONNECT_STRING
	fi
	if [ -n "${LISTENER_NAME:=""}" ]; then
		export LISTENER_NAME
	fi
	if [ -n "${LISTENER_PATH_LENGTH:=""}" ]; then
		export LISTENER_PATH_LENGTH
	fi

	# adding the following for dataguard support. 

	if [ -n "${DATAGUARD_ROLE:=""}" ]; then
		export DATAGUARD_ROLE
	fi

	if [ -n "${STANDBY_MODE:=""}" ]; then
		export STANDBY_MODE
	fi
}


#############################################################
# init_trace()
#
#############################################################
init_trace()
{
	typeset debugdir

        [ ${DEBUG_LEVEL} -eq -1 ] && DEBUG_LEVEL=$(read_extension_property_value DEBUG_LEVEL NOLOG)

	[ -z "${DEBUG_LEVEL}" ] && DEBUG_LEVEL=0

	TRACE_INIT="done"

	if [ -z "${DEBUG_LOGFILE}" ]; then
		DEBUG_LOGFILE="/dev/console"
		return
	fi

	debugdir=$(/bin/dirname "${DEBUG_LOGFILE}") 

	[ -n "$debugdir" ] && /bin/mkdir -p "$debugdir"

	if [[ "$DEBUG_LOGFILE" == *\. ]];then
   		DEBUG_LOGFILE="${DEBUG_LOGFILE}${RESOURCE_NAME:-""}"
	fi

	[ -z "$DEBUG_LOG_SIZE" ] && DEBUG_LOG_SIZE=300000 

	touch $DEBUG_LOGFILE 2> /dev/null

	if [ "${redirect_stderr:-}" != "NO" } ]; then
       		exec 2>> $DEBUG_LOGFILE
	fi

	rotate_log 

	return 0
}

#############################################################
# rotate_log()
#
#############################################################
rotate_log()
{
        typeset log_size=0

	[ ! -f "$DEBUG_LOGFILE" ] && return
	( 
        log_size=$(/bin/ls -l "$DEBUG_LOGFILE" | /bin/awk '{print $5}')
        [ $log_size -lt $DEBUG_LOG_SIZE ] && return

        [ -f ${DEBUG_LOGFILE}.1 ] &&  /bin/rm -f "${DEBUG_LOGFILE}.1"
        [ -f ${DEBUG_LOGFILE}.0 ] && /bin/mv ${DEBUG_LOGFILE}.0 ${DEBUG_LOGFILE}.1
        /bin/mv "${DEBUG_LOGFILE}" "${DEBUG_LOGFILE}.0"
        /bin/touch "${DEBUG_LOGFILE}"
	) &
}

#############################################################
# set_status
#   Sets resource status
#	Parameter 1: <status> (not validated)
#		 OK,  DEGRADED,  FAULTED, UNKNOWN, or OFFLINE. 
#	Parameter 2: <message> (optional)
#############################################################
set_status()
{
	typeset rstatus="${1:-UNKNOWN}"
	typeset rmsg=${2:-""}
	typeset prev_status
	typeset rc=0

	prev_status=$(read_standard_property_value Status)

	# WARNING: Assumption here is that all previous scha_resource_setstatus
	# commands did NOT use the -m option. If it was ever used, more
	# parsing of prev_status is required here
	
	if [ "$prev_status" == "$rstatus" ]; then
		return 0
	fi

	if [ -n "${rmsg}" ]; then
		scha_resource_setstatus -s $rstatus -m "$rmsg" \
			-R $RESOURCE_NAME -G $RESOURCE_GROUP
		rc=$?
	else
		scha_resource_setstatus -s $rstatus \
			-R $RESOURCE_NAME -G $RESOURCE_GROUP
		rc=$?
	fi

	return $rc
}

#############################################################
#
# hastorageplus_validation
#
# Arguments: Arguments to be passed to hasp_check program
# 
# This function checks if resource is dependent on SPlus resource and 
# state of SPlus resource.
#
# This function is expected to be called from validate or monitor_check
# methods, if some validations failed.
# 
# If this resource is dependent on SPlus resource, validations 
# are possible only on the node that has SPlus resource online.
# 
# return 0 - if resource depends on HAStoragePlus resource
#		and HASP resource is on-line on some other node
# 
#        1 - All other cases
# 
#############################################################

hastorageplus_validation() 
{
  	typeset rc=0

	#
	# hasp_check return codes 
	# 1 - Internal error
	#
	# 2 - No dependency on HASToragePlus resource
	#
	# 3 - One or more of the SUNW.HAStoragePlus resources
	#     that this resource depends on is in a different RG.
	#
	# 4 - One or more of the SUNW.HAStoragePlus resources
	#     that this resource depends on is not online anywhere.
	#
	# 5 - All the SUNW.HAStoragePlus resources that this 
	#     resource depends on are not online on this node. 
	#
	# 6 - All the SUNW.HAStoragePlus resources that this
	#     resource depends on are online on the local node. 	
	#


	if [ ! -f /usr/cluster/bin/hasp_check ]; then

		# hasp_check binary does not exist
		# return error
		
		return 1
	fi

	/usr/cluster/bin/hasp_check "$@"
 	hasp_status=$?
 
	case "$hasp_status" in
 
	   1)	rc=1
	   	# SCMSGS
	   	# @explanation
	   	# Internal error occured in hasp_check.
	   	# @user_action
	   	# Check the errors logged in the syslog messages by
	   	# hasp_check. Please verify existance of
	   	# /usr/cluster/bin/hasp_check binary. Please report this
	   	# problem.
	   	scds_syslog -p error -t $(syslog_tag) -m \
	    	   "Error in hasp_check. Validation failed."

	   	write_stderr "$(gettext "Error in hasp_check. Validation failed.")"
	   	;;
		   	
	   3)	rc=1
	   	# SCMSGS
	   	# @explanation
	   	# The resource depends on a HAStoragePlus resource that is
	   	# configured in a different resource group. This configuration
	   	# is not supported.
	   	# @user_action
	   	# Please add this resource and the HAStoragePlus resource in
	   	# the same resource group.
	   	scds_syslog -p error -t $(syslog_tag) -m \
	    	   "This resource depends on a HAStoragePlus resouce \
that is in a different Resource Group. This configuration is not supported."

	    	write_stderr "$(gettext "This resource depends on a HAStoragePlus resouce that is in a different Resource Group. This configuration is not supported.")" 
	   	;;

	   4)	rc=1
	   	# SCMSGS
	   	# @explanation
	   	# The resource depends on a HAStoragePlus resource that is not
	   	# online on any node. Some of the files required for
	   	# validation checks are not accessible. Validations cannot be
	   	# performed on any node.
	   	# @user_action
	   	# Enable the HAStoragePlus resource that this resource depends
	   	# on and reissue the command.
	   	scds_syslog -p error -t $(syslog_tag) -m \
	    	   "This resource depends on a HAStoragePlus resouce \
that is not online. Unable to perform validations."

		write_stderr "$(gettext "This resource depends on a HAStoragePlus resouce that is not online. Unable to perform validations.")"
	   	;;


	   5)	rc=0
	   	# SCMSGS
	   	# @explanation
	   	# The resource depends on a HAStoragePlus resource. Some of
	   	# the files required for validation checks are not accessible
	   	# from this node, because HAStoragePlus resource in not online
	   	# on this node. Validations will be performed on the node that
	   	# has HAStoragePlus resource online. Validation errors are
	   	# being ignored on this node by this callback method.
	   	# @user_action
	   	# Check the validation errors logged in the syslog messages.
	   	# Please verify that these errors are not configuration
	   	# errors.
	   	scds_syslog -p warning -t $(syslog_tag) -m \
		   "This resource depends on a HAStoragePlus resouce \
that is not online on this node. Ignoring validation errors." 

		write_stderr "$(gettext "This resource depends on a HAStoragePlus resouce that is not online on this node. Ignoring validation errors.")" 
	   	;; 

	   # Return code 2, 6 or any other.
 	   *) 	rc=1;; 
	esac

	return $rc

}
#include_dbms_lib
#

#
# ident "@(#)oracle_server_lib.ksh 1.36     11/05/12"
#

# Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
#
# HA-Oracle Library routines
#
#
#
initialize()
{

	#
	# uncomment to turn on tracing in all functions
	# typeset -ft $(typeset +f)
	#
	
	pkg=SUNWscor.oracle_server

	DEBUG_LOGFILE="/var/opt/SUNWscor/oracle_server/message_log."

	DEFAULT_ACTION_FILE=${ETCDIR}/oracle_server_monitor_action

	CUSTOM_ACTION_FILE=""

	ONLINE_CHECK_CONFIG_FILE=${ETCDIR}/oracle_server_online_check

	SUPPORT_FILE=${ETCDIR}/oracle_server_support

	DEBUG_LOG_SIZE=300000

	MONITOR_RETRY_COUNT=1

	MONITOR_RETRY_INTERVAL=5

	MONITOR_TAG_PREFIX=ORASERV_MON

	MONITOR_DELAY=0

	MONITOR_NAME=""

	RESOURCE_PROJECT_NAME=""

	RG_PROJECT_NAME=""

	if [ -f /opt/SUNWscor/.use_su ]; then
		EXECUTE_METHOD="su"
	else
		EXECUTE_METHOD="run_setuid_prog"
	fi

	CLANG="/usr/bin/env LANG=C LC_ALL=C"

}

#############################################################
#
#############################################################
server_running()
{
 typeset running
 typeset PSZARGS
 typeset ZGREP

	if [ -x /usr/bin/zonename ]; then
		PSZARGS="zone,"
		ZGREP="/bin/grep -w `/usr/bin/zonename`"
	else
		PSZARGS=""
		ZGREP="/bin/tee"
	fi

	running=$(/bin/ps -u $ORACLE_OWNER -o ${PSZARGS}comm | \
			${ZGREP} | /bin/grep -w ora_pmon_${ORACLE_SID})

	if [ -n "$running" ]; then
		return 0
	fi

	return 1
}


#############################################################
#
#############################################################
read_parameters()
{
	ORACLE_HOME=$(read_extension_property_value ORACLE_HOME)

	ORACLE_SID=$(read_extension_property_value ORACLE_SID)

	WAIT_FOR_ONLINE=$(read_extension_property_value WAIT_FOR_ONLINE)

	PARAMETER_FILE=$(read_extension_property_value PARAMETER_FILE)

	USER_ENV=$(read_extension_property_value USER_ENV)

	CONNECT_STRING=$(read_extension_property_value CONNECT_STRING)

 	# Read and set default, if not set.

	STOP_TIMEOUT=$(read_standard_property_value STOP_TIMEOUT)
	[ -z "$STOP_TIMEOUT" ] && STOP_TIMEOUT=300

	START_TIMEOUT=$(read_standard_property_value START_TIMEOUT)
	[ -z "$START_TIMEOUT" ] && START_TIMEOUT=300

	AUTO_END_BKP=$(read_extension_property_value AUTO_END_BKP NOLOG)
	[ -z "$AUTO_END_BKP" ] && AUTO_END_BKP=FALSE

	RESTART_TYPE=$(read_extension_property_value  RESTART_TYPE NOLOG)
	[ -z "$RESTART_TYPE" ] && RESTART_TYPE="RESOURCE_GROUP_RESTART"

	# New parameters to read in the values of the extension properties
 	# Dataguard_role and standby_mode that support dataguard instances.

	DATAGUARD_ROLE=$(read_extension_property_value DATAGUARD_ROLE NOLOG)
	[ -z "$DATAGUARD_ROLE" ] && DATAGUARD_ROLE="NONE"

	STANDBY_MODE=$(read_extension_property_value STANDBY_MODE NOLOG)
	[ -z "$STANDBY_MODE" ] && STANDBY_MODE="LOGICAL"

	ON_OFF_SWITCH=$(read_standard_property_value On_off_switch)

	MONITORED_SWITCH=$(read_standard_property_value Monitored_switch)

	DB_UNIQUE_NAME=$(read_extension_property_value DB_UNIQUE_NAME NOLOG)
}


#############################################################
#
#############################################################
validate_parameters()
{
	ORACLE_OWNER=""

	if [ ! -d "$ORACLE_HOME" ]; then
		scds_syslog -p error -t "$(syslog_tag)" -m \
		    "Validation failed. ORACLE_HOME %s does not exist" \
		    $ORACLE_HOME

		write_stderr "$(gettext "Validation failed. ORACLE_HOME %s does not exist")" \
		    $ORACLE_HOME
		return 1
	fi

	if [ ! -f "$ORACLE_HOME/bin/oracle" ]; then
		scds_syslog -p error -t "$(syslog_tag)" -m \
		    "Validation failed. ORACLE binaries not found ORACLE_HOME=%s" \
		    $ORACLE_HOME

		    write_stderr "$(gettext "Validation failed. ORACLE binaries not found ORACLE_HOME=%s")" \
		    $ORACLE_HOME
		return 1
	else
		ORACLE_OWNER=`/bin/ls -ld ${ORACLE_HOME}/bin/oracle | /bin/nawk '{print $3}'`
	fi

	if [ ! -f "$ORACLE_HOME/bin/sqlplus" ]; then
		scds_syslog -p error -t "$(syslog_tag)" -m \
		    "Validation failed. ORACLE_HOME/bin/sqlplus not found ORACLE_HOME=%s" \
		    $ORACLE_HOME

		    write_stderr "$(gettext "Validation failed. ORACLE_HOME/bin/sqlplus not found ORACLE_HOME=%s")" \
		    $ORACLE_HOME
		return 1
	fi

	if [ -z "$ORACLE_SID" ]; then
		scds_syslog -p error -t "$(syslog_tag)" -m \
		    "Validation failed. ORACLE_SID is not set"

		    write_stderr "$(gettext "Validation failed. ORACLE_SID is not set")"
		return 1
	fi

        # Note that within an 11gR2 Grid Infrastructure for Clusters deployment
        # the CONNECT_STRING extension property is no longer required.

	check_grid_infrastructure || \
	if echo ${CONNECT_STRING} | /usr/xpg4/bin/grep -qe "^/@"
	then
		# TNS-03505 Failed to resolve name

		if ${ORACLE_HOME}/bin/tnsping ${CONNECT_STRING#/@} | /usr/xpg4/bin/grep -q "TNS-03505"
		then
			# SCMSGS
			# @explanation
			# CONNECT_STRING property for the resource is
			# specified incorrectly for wallet use. In order
			# to use an Oracle wallet to connect to the database
			# you must specify a valid SERVICE NAME you specified
			# for the database within tnsnames.ora
			# @user_action
			# Specify CONNECT_STRING in the specified wallet format.
			scds_syslog -p error -t "$(syslog_tag)" -m \
			    "Validation failed. CONNECT_STRING not in specified wallet format"
			write_stderr "$(gettext "Validation failed. CONNECT_STRING not in specified wallet format")"

			return 1
		fi
	else
	    if [ "$CONNECT_STRING" != "/" ]; then
		if [ `echo $CONNECT_STRING | grep -c "/"` -ne 1 ]; then
			# SCMSGS
			# @explanation
			# CONNECT_STRING property for the resource is not
			# specified in the correct format. The format could be
			# either 'username/password' or '/' (if operating
			# system authentication is used).
			# @user_action
			# Specify CONNECT_STRING in the specified format.
			scds_syslog -p error -t "$(syslog_tag)" -m \
			    "Validation failed. CONNECT_STRING not in specified format"
			write_stderr "$(gettext "Validation failed. CONNECT_STRING not in specified format")"

			return 1
		fi

		USERNAME=`echo $CONNECT_STRING | cut -d/ -f1`
		if [ -z "$USERNAME" ]; then
			# SCMSGS
			# @explanation
			# USERNAME is missing in the specified CONNECT_STRING.
			# The format could be either 'username/password' or
			# '/' (if operating system authentication is used).
			# @user_action
			# Specify CONNECT_STRING in the specified format.
			scds_syslog -p error -t "$(syslog_tag)" -m \
			    "Validation failed. USERNAME missing in CONNECT_STRING"
			write_stderr "$(gettext "Validation failed. USERNAME missing in CONNECT_STRING")"

			return 1
		fi

		PASSWORD=`echo $CONNECT_STRING | cut -d/ -f2`
		if [ -z "$PASSWORD" ]; then
			# SCMSGS
			# @explanation
			# PASSWORD is missing in the specified CONNECT_STRING.
			# The format could be either 'username/password' or
			# '/' (if operating system authentication is used).
			# @user_action
			# Specify CONNECT_STRING in the specified format.
			scds_syslog -p error -t "$(syslog_tag)" -m \
			    "Validation failed. PASSWORD missing in CONNECT_STRING"
			write_stderr "$(gettext "Validation failed. PASSWORD missing in CONNECT_STRING")"

			return 1
		fi
	    fi
	fi

	if check_grid_infrastructure
	then
		if [[ ! -x ${ORA_CRS_HOME}/bin/crsctl ]]; then
			# SCMSGS
			# @explanation
			# ${ORA_CRS_HOME}/bin/crsctl is either not found or not executable.
			# @user_action
			# Verify that the Grid Infrastructure for Clusters installation completed
			# successfully on all cluster nodes. If the problem persists contact
			# your Oracle support representative for further assistance.
			scds_syslog -p error -t $(syslog_tag) -m \
				"%s/bin/crsctl is either not found or not executable." \
				"${ORA_CRS_HOME}"

			write_stderr "$(gettext "%s/bin/crsctl is either not found or not executable.")" \
			"${ORA_CRS_HOME}"

			return 1
		fi

		if [[ ! -x ${ORACLE_HOME}/bin/srvctl ]]; then
			# SCMSGS
			# @explanation
			# ${ORACLE_HOME}/bin/srvctl is either not found or not executable.
			# @user_action
			# Verify that the Grid Infrastructure for Clusters installation completed
			# successfully on all cluster nodes. If the problem persists contact
			# your Oracle support representative for further assistance.
			scds_syslog -p error -t $(syslog_tag) -m \
				"%s/bin/srvctl is either not found or not executable." \
				"${ORACLE_HOME}"

			write_stderr "$(gettext "%s/bin/srvctl is either not found or not executable.")" \
			"${ORACLE_HOME}"

			return 1
		fi

		if (( ${#DB_UNIQUE_NAME} == 0 )); then
			# SCMSGS
			# @explanation
			# Extension property DB_UNIQUE_NAME needs to be set when Grid
			# Infrastructure for Clusters is deployed. 
			# @user_action
			# Ensure that extension property DB_UNIQUE_NAME reflects the 
			# database unique name for the database you want to manage with 
			# this resource.
			scds_syslog -p error -t "$(syslog_tag)" -m \
				"Validation failed. DB_UNIQUE_NAME is not set"

			write_stderr "$(gettext "Validation failed. DB_UNIQUE_NAME is not set")"

			return 1
		else
			if [[ ${DB_UNIQUE_NAME} != $(/bin/su ${ORACLE_OWNER} -c "${CLANG} \
				${ORACLE_HOME}/bin/srvctl config db -d ${DB_UNIQUE_NAME}" |\
				/usr/xpg4/bin/awk -F: '/^Database unique name:/{print $2}' | /usr/xpg4/bin/tr -d ' ') ]]; then
	
				# SCMSGS
				# @explanation
				# Extension property DB_UNIQUE_NAME is not correct.
				# @user_action
				# Verify that the Grid Infrastructure for Clusters installation completed
				# successfully on all cluster nodes and that a Grid Infrastructure for
				# Clusters database resource exists. Finally, verify that the extension
				# property DB_UNIQUE_NAME correctly reflects the database unique name,
				# for the database that you want to manage with this Oracle Solaris Cluster
				# resource. If the problem persists, contact your Oracle support representative
				# for further assistance.

				scds_syslog -p error -t "$(syslog_tag)" -m \
					"Validation failed. DB_UNIQUE_NAME=%s is not correct" \
					"${DB_UNIQUE_NAME}"

				write_stderr "$(gettext "Validation failed. DB_UNIQUE_NAME=%s is not correct")" \
				"${DB_UNIQUE_NAME}"

				return 1
			fi


			typeset -l db_unique_name=${DB_UNIQUE_NAME}
			typeset -i CHECK_INTERVAL=1

			# Note that the following typeset will set a variable=value pair for CHECK_INTERVAL.
			# The typeset above is simply to have a defined value for the subsequent test.

			typeset CRSOUT=$(/bin/su ${CRS_OWNER} -c "${CLANG} \
				${ORA_CRS_HOME}/bin/crsctl stat res ora.${db_unique_name}.db -p")
			typeset -i $(/bin/printf "%s\n" "${CRSOUT}" | /usr/xpg4/bin/awk '/^CHECK_INTERVAL=/{print $0}')
		
			if (( ${CHECK_INTERVAL} == 0 )); then

				# Note that we rely on the Grid Infrastructure database CHECK entry point
				# to report the database state. As such, if CHECK_INTERVAL=0 is set then
				# STATE_DETAILS will not be updated whenever the resource is changing state.

				# SCMSGS
				# @explanation
				# Attribute CHECK_INTERVAL has been set to 0.
				# @user_action
				# Setting CHECK_INTERVAL=0 for the database resource is now allowed when
				# Oracle Solaris Cluster is also managing the database. Modify the database
				# resource so that at least CHECK_INTERVAL=1 is set.

				scds_syslog -p error -t "$(syslog_tag)" -m \
					"Validation failed. CHECK_INTERVAL=0 is not allowed when Oracle Solaris Cluster is managing database %s" \
					"${DB_UNIQUE_NAME}"

				write_stderr "$(gettext "CHECK_INTERVAL=0 is not allowed when Oracle Solaris Cluster is managing database %s")" \
				"${DB_UNIQUE_NAME}"

				return 1
			fi
		fi

	fi


	return 0

}

#############################################################
#
#############################################################
monitor_setup()
{
	if [ ! -f "$SUPPORT_FILE" ]; then	
		# SCMSGS
		# @explanation
		# This is an internal error. Support file is used by HA-Oracle
		# to determine the fault monitor information.
		# @user_action
		# Please report this problem.
		scds_syslog -p error -t "$(syslog_tag)" -m \
		    "Setup error. SUPPORT_FILE %s does not exist" \
		    "$SUPPORT_FILE"
		return 1
	fi

	MONITOR_TAG="${MONITOR_TAG_PREFIX}_${RESOURCE_NAME}"

	return 0

}	
#############################################################
#  Check if fault monitor is running
#############################################################
monitor_running()
{
	typeset rc
	pmfadm -q $MONITOR_TAG
	rc=$?

	return $rc
}

#############################################################
#  read_support_file
#
#   Input:
#	Parameter 1: key in support file.
#		Deault value is "default"
#
#   Output:
#       sets MONITOR_NAME
#
#   Return codes:
#	0 - Success
#	1 - Failure
#	
#############################################################
read_support_file()
{
	typeset rc
	typeset support_line
	typeset match="default"
	typeset monitor

	monitor=""

	[ -n "${1:-""}" ] && match="$1"

        /bin/grep -v "^#" ${SUPPORT_FILE} | \
        while read support_line ; do

	    [ -z "$support_line" ] && continue

	    set $support_line
	    [ $# -lt 2 ] && continue

            if [ "$1" == "$match" ] ; then
		monitor="$2"

		# support file contains monitor program name.
		# append monitor program name to bin directory
		# to get full file name (including path)
	
		monitor="$BINDIR/$monitor"
		if [  -d $monitor -o  ! -x $monitor ]; then
			# SCMSGS
			# @explanation
			# Fault monitor program specified in support file is
			# not executable or does not exist. Recheck your
			# installation.
			# @user_action
			# Please report this problem.
			scds_syslog -p error -t "$(syslog_tag)" -m \
 			    "Fault monitor does not exist or is not executable : %s" \
        		    $monitor
			return 1
		fi
		MONITOR_NAME="$monitor"
		return 0
            fi
        done

	# SCMSGS
	# @explanation
	# This is an internal error. Could not determine fault monitor program
	# name.
	# @user_action
	# Please report this problem.
	scds_syslog -p error -t "$(syslog_tag)" -m \
 	    "Internal Error. Unable to get fault monitor name"

	write_trace "Internal error when searching for fault monitor: %s" \
		"$match"

        return 1;
}

#############################################################
#  get_monitor_name
#
#   Input:
#		none
#
#   Output:
#	prints out fault monitor name
#	(as printed by read_support_file)
#
#   Return codes:
#	(as returned by read_support_file)
#	0 - Success
#	1 - Failure
#
#   Assumptions:
#	$ORACLE_HOME is set and validated
#
#   This function decides which support file key is to be used
#   to retrieve fault monitor name. At this time fault monitors
#   are selected based on 32 bit or 64 bit architecture.
#   In case version dependent + architecture dependent fault monitors
#   are to be selected, selection can be done in this function.
#	
#############################################################
get_monitor_name()
{
	typeset rc
        typeset architecture
        typeset monitor_name
        typeset oracle_version

	architecture=$(get_architecture)
	oracle_version=$(get_oracle_version)

	read_support_file "${oracle_version}_${architecture}"
}

#############################################################
#  get_architecture
#
#   Input:
#               none
#
#   Output:
#       prints out the os type (32 or 64 bit in use)
#
#   Return codes:
#        32 or 64
#
#   Assumptions:
#       $ORACLE_HOME is set and validated
#
#   This function decides which architecture the system is using,
#    32 bit or 64 bit
#
#############################################################
get_architecture()
{
	typeset arch

	arch="`LC_MESSAGES=C /bin/file ${ORACLE_HOME}/bin/oracle | \
		/bin/awk '/(32|64)-/ {print substr($3,0,2);}'`"

	print $arch
}

#############################################################
#  get_oracle_version
#
#   Input:
#               none
#
#   Output:
#       prints out oracle version oracle8, oracle9 or oracle10
#
#   Return codes:
#	NA
#
#   Assumptions:
#       $ORACLE_HOME is set and validated
#
#############################################################
get_oracle_version()
{
	typeset version

	if [ -f $ORACLE_HOME/lib/libcore8.a ]; then
		version="oracle8"
	elif [ -f $ORACLE_HOME/lib/libcore9.a ]; then
		version="oracle9"
	elif [ -f $ORACLE_HOME/lib/libcore10.a ]; then
		version="oracle10"
	elif [ -f $ORACLE_HOME/lib/libcore11.a ]; then
		version="oracle11"
	else
		version="unknown"
	fi

	print $version
}

check_db_open()
{

	# This function calls the check_db_open option of
	# oracle_server_manage.ksh. This function is the
	# interface for all other programs that wish to use
	# the check_db_open option of oracle_server_manage.

	typeset rc

	export_env

	export DEBUG_LOGFILE MONITOR_DELAY DEBUG_LOG_SIZE

	write_trace "Checking the instance's status and role."

	if [ "$EXECUTE_METHOD" == "su" ]; then
		su $ORACLE_OWNER -c "$TASK_CMD \
		    $BINDIR/oracle_server_manage check_db_open \
		    ${pkg} ${method} ${RESOURCE_NAME} ${RESOURCE_GROUP}" \
		    >> $DEBUG_LOGFILE 2>&1
	else
		$BINDIR/run_setuid_prog -f $ORACLE_HOME/bin/oracle -c \
		    "$BINDIR/oracle_server_manage check_db_open \
		    ${pkg} ${method} ${RESOURCE_NAME} ${RESOURCE_GROUP}" \
		    >> $DEBUG_LOGFILE 2>&1
  	fi

	rc=$?

	return $rc

}


#############################################################
# The following function grants rights to the views that will
# need to be queried by the fault monitor user configured
# during resource creation.
#############################################################

grant_rights()
{
	typeset rc

	typeset arch

	export_env

	export DEBUG_LOGFILE MONITOR_DELAY DEBUG_LOG_SIZE

	if [ "$EXECUTE_METHOD" == "su" ]; then
		su $ORACLE_OWNER -c "$TASK_CMD \
		    $BINDIR/oracle_server_manage grant_rights" \
		    >> $DEBUG_LOGFILE 2>&1
	else
		$BINDIR/run_setuid_prog -f $ORACLE_HOME/bin/oracle -c \
		    "$BINDIR/oracle_server_manage grant_rights" \
		    >> $DEBUG_LOGFILE 2>&1	
	fi

	rc=$?
	
	if [ $rc -eq 0 ]; then
		set_status OK
		return 0
	else
		set_status FAULTED

		# SCMSGS
		# @explanation
		# Access rights on v_$views could not be granted to the fault
		# monitor user that the extension property connect_string
		# specifies.
		# @user_action
		# Check that the fault monitor user has been created, and that
		# the procedure for creating this user in the user
		# documentation for the Oracle data service has been followed
		# correctly. If the fault monitor user has not been created
		# correctly, repeat the procedure for creating this user. If
		# the fault monitor user has been created correctly, check
		# that the database is in writable mode. If the problem
		# persists, contact your Sun support representative for
		# further assistance.
		scds_syslog -p info -t $(syslog_tag) -m \
                "Grant of access rights on v_$ views failed."

		write_trace "Grant of access rights on v_$ views failed."

		set_status FAULTED
		return $rc
	fi
}

check_grid_infrastructure()
{
	# [1] We need to allow a pre-11gR2 single instance database to
	# be managed by this Oracle Solaris Cluster agent, regardless
	# if Grid Infrastructure for Clusters is deployed.
	#
	# As such, if a pre-11gR2 version is found we return 1 to 
	# indicate that the pre-11gR2 single instance database is not
	# managed by Grid Infrastructure for Clusters and instead will
	# be managed by this Oracle Solaris Cluster agent.
	#
	# [2] We need to check that Grid Infrastructure for Clusters
	# is deployed by checking if server pools exist.
	#
	# Note that /var/opt/oracle/olr.loc is introduced with 11gR2.
	#
	# -bash-3.00$ srvctl status srvpool | grep PRKO
	# PRKO-2012 : srvpool object is not supported in Oracle Restart
	# -bash-3.00$
	#
	# Note, we only exit 1 if the srvcrtl status srvpool is successful
	# and PRKO-2012 is found, being indicative that Oracle Restart or
	# Grid Infrastructure for Stand-Alone Servers is deployed.
	#
	# Furthermore, this check is for Grid Infrastructure where ASM
	# is also expected. As such, SUNW.scalable_asm_instance_proxy
	# will also ensure that Grid Infrastructure for Clusters is
	# deployed and prevent a clustered ASM resource creation if 
	# Grid Infrastructure for Stand-Alone Servers is deployed.
	#
	# [3] We need to allow a single instance database in ZoneCluster to
	# be managed by this Oracle Solaris Cluster agent, regardless
	# if Grid Infrastructure for Clusters is deployed (bug 7082865).

	[[ `/usr/bin/zonename` != 'global' ]] && return 1

	typeset -i rc=0
	export ORACLE_HOME

	ORACLE_OWNER=$(/bin/ls -ld ${ORACLE_HOME}/bin/oracle | /bin/nawk '{print $3}')

        typeset ORACLE_VERSION=$(/bin/su ${ORACLE_OWNER} -c "${CLANG} \
		${ORACLE_HOME}/bin/sqlplus -v 2>/dev/null" | /bin/awk '{if (NF >= 3) print $3}')

	if (( ${#ORACLE_VERSION} != 0 )) 
	then
		(( $(echo ${ORACLE_VERSION} | /bin/cut -d. -f1,2 | /usr/xpg4/bin/tr -d .) < 112 )) && return 1
	fi

	if [[ -f /var/opt/oracle/olr.loc ]]
	then
		ORA_CRS_HOME=$(/usr/xpg4/bin/awk -F= '/^crs_home=/{print $2}' /var/opt/oracle/olr.loc)
		CRS_OWNER=$(/bin/ls -ld ${ORA_CRS_HOME}/bin/crsctl | /bin/nawk '{print $3}')

		if [[ -x ${ORA_CRS_HOME}/bin/srvctl ]]
		then
			if $(/bin/su ${CRS_OWNER} -c "${CLANG} \
				${ORA_CRS_HOME}/bin/srvctl status srvpool" | \
				/usr/xpg4/bin/grep -iqw PRKO-2012)
			then
				# SCMSGS
				# @explanation
				# Deployment with Grid Infrastrusture for Stand-Alone Servers is not supported.
				# @user_action
				# Ensure that Grid Infrastructure for Clusters is installed.
				scds_syslog -p error -t $(syslog_tag) -m \
                			"Deployment with Grid Infrastrusture for Stand-Alone Servers is not supported."

				write_stderr "$(gettext "Deployment with Grid Infrastrusture for Stand-Alone Servers is not supported.")"
				exit 1
			fi
		else
			rc=1
		fi
	else
		rc=1
	fi

	return ${rc}
}
#include_oracle_lib

method=validate
redirect_stderr="NO"
export TEXTDOMAINDIR=/opt/SUNWscor/oracle_server/lib/locale
export TEXTDOMAIN=SUNW_SC_ORACLE_SERVER

#############################################################
# set_var()
#
#############################################################
set_var()
{
	typeset -u param
	typeset val
	[ -z ${1:-""} ] && return

	val="${1##*=}"
	param="${1%%=*}"
	eval ${param}=\"${val}\"

	return;
}

#############################################################
# check_arguments()
#
#############################################################
check_arguments()
{

typeset rc
typeset invalid_option=""
typeset save_opt=""
typeset save_optarg=""

RESOURCE_NAME=""
RESOURCE_TYPE=""
RESOURCE_GROUP=""
operation="VALIDATE"

	while getopts ugcr:R:T:G:x: opt
	do
	  case $opt in
		R)      RESOURCE_NAME="$OPTARG";;
		T)      RESOURCE_TYPE="$OPTARG";;
		G)      RESOURCE_GROUP="$OPTARG";;
		x)      ;;
		r)      ;;
		c)      operation="VALIDATE" ;;
		g)      ;;
		u)      operation="UPDATE" ;;
		*)      invalid_option="YES";  
                	save_opt="$opt"
                	save_optarg="$OPTARG"
                 ;;
	  esac
	done

	check_resource_setup
	rc=$?
        if [ $rc -ne 0 -o -n "$invalid_option" ]; then
		scds_syslog -p error -t $(syslog_tag) -m \
		"Validation failed. Invalid command line parameter %s %s" \
                "$save_opt" "$save_optarg"

		write_stderr "$(gettext "Validation failed. Invalid command line parameter %s %s")" \
                "$save_opt" "$save_optarg"
                rc=1;
        fi
 
	return $rc
}

#############################################################
# read_method_arguments()
#
#############################################################
read_method_arguments()
{

typeset rc

     if [ $operation = "UPDATE" ]; then

	# Read previous property values
	#
	PARAMETER_FILE=$(read_extension_property_value PARAMETER_FILE)
	ALERT_LOG_FILE=$(read_extension_property_value ALERT_LOG_FILE)
	ORACLE_SID=$(read_extension_property_value ORACLE_SID)
	ORACLE_HOME=$(read_extension_property_value ORACLE_HOME)
	USER_ENV=$(read_extension_property_value USER_ENV)
	CONNECT_STRING=$(read_extension_property_value CONNECT_STRING)
	CUSTOM_ACTION_FILE=$(read_extension_property_value CUSTOM_ACTION_FILE NOLOG)
	DATAGUARD_ROLE=$(read_extension_property_value DATAGUARD_ROLE NOLOG)
	STANDBY_MODE=$(read_extension_property_value STANDBY_MODE NOLOG)

	check_grid_infrastructure && DB_UNIQUE_NAME=$(read_extension_property_value DB_UNIQUE_NAME)
	
	read_resource_project_name
     fi

     	while getopts ucg:r:R:T:G:x: opt
	do
	  case $opt in
		R)      RESOURCE_NAME="$OPTARG";;
		T)      RESOURCE_TYPE="$OPTARG";;
		G)      RESOURCE_GROUP="$OPTARG";;
		x)      set_var "$OPTARG";;
		r)      set_var "$OPTARG";;
		g)      set_var "$OPTARG";;
		c)      ;;
		u)      ;;
		*)      ;;
	  esac
     	done

     	check_resource_setup
	rc=$?
        
	return $rc
}
#############################################################
# validate_oracle_setup()
#
#############################################################
validate_oracle_setup()
{

typeset rc=0

	validate_parameters || return 1

	if ! /bin/test -u ${ORACLE_HOME}/bin/oracle; then
		scds_syslog -p error -t $(syslog_tag) -m \
		    "Permissions incorrect for %s. s bit not set." \
		    $ORACLE_HOME/bin/oracle

		write_stderr "$(gettext "Permissions incorrect for %s. s bit not set.")" \
		    $ORACLE_HOME/bin/oracle
		rc=1
	fi

	return $rc
}

#############################################################
# The following function checks for the existence of the
# parameter file.
#############################################################

validate_parameter_file()
{

        typeset rc
        rc=0

        if [ -n "$PARAMETER_FILE" ]; then
                # If Server Parameter file is being used in this install,
                # PARAMETER_FILE $ORACLE_HOME/dbs/init$ORACLE_SID.ora may
                # not exist. So validate PARAMETER_FILE only if it is
                # specified as a property

                # Check if the parameter file exists
                if [ ! -f "$PARAMETER_FILE" ]; then
                        scds_syslog -p error -t $(syslog_tag) -m \
                                "Validation failed. PARAMETER_FILE: %s does not exist" \
                                "$PARAMETER_FILE"

                        write_stderr "$(gettext "Validation failed. PARAMETER_FILE: %s does not exist")" \
                                "$PARAMETER_FILE"
                        rc=1
                fi
        else
                # PARAMETER_FILE has not been specified. Check to see
                # whether either spfile<sid>.ora or init<sid>.ora exists
                # in $ORACLE_HOME/dbs/ folder.

                if [ ! -f "${ORACLE_HOME}/dbs/init${ORACLE_SID}.ora" ] && \
                  [ ! -f "${ORACLE_HOME}/dbs/spfile${ORACLE_SID}.ora" ]; then
			# SCMSGS
			# @explanation
			# Neither the parameter file init<sid>.ora nor the
			# server parameter file spfile<sid>.ora is present in
			# the default location $ORACLE_HOME/dbs/ folder.
			# @user_action
			# Create a valid parameter file or server parameter
			# file for that Oracle instance in the default
			# location, or provide a valid parameter file in the
			# PARAMETER_FILE extension property of the HA-Oracle
			# resource.
                        scds_syslog -p error -t $(syslog_tag) -m \
                                "Validation failed. Neither default PARAMETER_FILE: %s " \
				"nor server PARAMETER_FILE: %s exists" \
                                "${ORACLE_HOME}/dbs/init${ORACLE_SID}.ora" "${ORACLE_HOME}/dbs/spfile${ORACLE_SID}.ora"

                        write_stderr "$(gettext "Validation failed. Neither default "\
				"PARAMETER_FILE: %s nor server PARAMETER_FILE: %s exists")" \
				"${ORACLE_HOME}/dbs/init${ORACLE_SID}.ora" "${ORACLE_HOME}/dbs/spfile${ORACLE_SID}.ora"

                        rc=1
                fi
        fi

	return $rc

}

#############################################################
# validate_setup()
#
#############################################################
validate_setup()
{
	typeset rc=0

	write_trace "Validate method invoked on resource $RESOURCE_NAME."

	validate_oracle_setup || rc=1

	# Note that within an 11gR2 Grid Infrastructure for Clusters deployment
	# the ALERT_LOG_FILE extension property is no longer required.

	check_grid_infrastructure || \
	if [ -n "$ALERT_LOG_FILE" ]; then
		if [ ! -f "$ALERT_LOG_FILE" ]; then
			# SCMSGS
			# @explanation
			# File specified in resource property 'Alert_log_file'
			# does no exist. HA-Oracle requires correct Alert Log
			# file for fault monitoring.
			# @user_action
			# Check 'Alert_log_file' property of the resource.
			# Specify correct Oracle Alert Log file when creating
			# resource. If resource is already created, please
			# update resource property Alert_log_file'.
			scds_syslog -p error -t $(syslog_tag) -m \
			    "ALERT_LOG_FILE %s doesn't exist" \
			    "$ALERT_LOG_FILE" 

			write_stderr "$(gettext "ALERT_LOG_FILE %s doesn't exist")" \
			    "$ALERT_LOG_FILE" 
			rc=1
		fi
	fi

	validate_parameter_file || rc=1

        if [ -n "$USER_ENV" ]; then
		if [ ! -f "$USER_ENV" ]; then
			scds_syslog -p error -t $(syslog_tag) -m \
				"file specified in USER_ENV %s does not exist" \
				"$USER_ENV"
			rc=1
		else

			# Check user environment
			# Set environment variables in a separate shell.
			# In case there are syntax errors in user_env file
			# set_user_env file will print errors and warnings
			(set_user_env)
		fi
        fi

	if [ -n "$CUSTOM_ACTION_FILE" ]; then

		# Check if custom action file exists
		if [ ! -f "$CUSTOM_ACTION_FILE" ]; then
			# SCMSGS
			# @explanation
			# The file specified in property 'Custom_action_file'
			# does not exist.
			# @user_action
			# Please make sure that 'Custom_action_file' property
			# is set to an existing action file. Reissue command
			# to create/update.
			scds_syslog -p warning -t "$(syslog_tag)" -m \
			    "Validation failed. CUSTOM_ACTION_FILE: %s does not exist" \
		    "$CUSTOM_ACTION_FILE"

			write_stderr "$(gettext "Validation failed. CUSTOM_ACTION_FILE: %s does not exist")" \
		    "$CUSTOM_ACTION_FILE"
			rc=1
		fi
	fi

	if [ -s "$CUSTOM_ACTION_FILE" ]; then

		# Custom actions are found

		# SCMSGS
		# @explanation
		# This message indicates that a custom monitor action file has
		# been configured. The entries in this file will determine the
		# actual action that is taken for the specified errors. In
		# case any error is specified in both, the custom and the
		# default action files, the action specified in the custom
		# action file will be chosen.
		# @user_action
		# None.
		scds_syslog -p info -t "$(syslog_tag)" -m \
		    "Custom monitor actions found in %s. These will override the default actions." \
		    "$CUSTOM_ACTION_FILE"

		# Check syntax of the custom action file
		${BINDIR}/monitor_action_check \
			-R "$RESOURCE_NAME" -G "$RESOURCE_GROUP" \
			-T "$RESOURCE_TYPE" -f "$CUSTOM_ACTION_FILE" \
			-c -l -e -d $DEBUG_LEVEL
		if [ $? -ne 0 ]; then
			# SCMSGS
			# @explanation
			# The custom action file that is specified contains
			# errors which need to be corrected before it can be
			# processed completely.
			# @user_action
			# Please ensure that all entries in the custom monitor
			# action file are valid and follow the correct syntax.
			# After the file is corrected, validate it again to
			# verify the syntax.
			scds_syslog -p error -t "$(syslog_tag)" -m \
			    "Custom action file %s contains errors." \
			    "$CUSTOM_ACTION_FILE"

			write_stderr "$(gettext "Custom action file %s contains errors.")" \
			    "$CUSTOM_ACTION_FILE"
			rc=1
		fi
	fi

	if [ $rc -eq 0 ]; then
		validate_resource_project_name "$ORACLE_OWNER"
		rc=$?
	fi

	if [ $rc -eq 0 ]; then
		write_trace "Validation successful"
	else
		# Validations failed. Check if this resource is dependent
		# on SPlus resource and state of SPlus resource
		# set return code to Success (0) if SPlus resource is
		# on-line not local.

		hastorageplus_validation "$@"

                rc=$?

	fi

	return $rc

}

#############################################################
#
#############################################################
main()
{
	initialize

	check_arguments "${@:-}" || error_exit $?

	read_method_arguments "${@:-}" || error_exit $?

	validate_setup "${@:-}" || error_exit $?

	return 0

}
main "${@:-}"
exit 0 

