#! /usr/bin/ksh
#
# ident "@(#)oracle_rac_server_validate.ksh 1.8     05/01/07 SMI"
#
# Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# Oracle RAC server VALIDATE method
# 
# Parameters passed:  -R <resource name> -T <type> -G <group>
#
#! /usr/bin/ksh
#
# ident "@(#)dbms_lib.ksh 1.26     04/06/07 SMI"
#
# Copyright 2004 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

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

		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
			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
			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
		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
                        	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
                        		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
		#

		if [[ "$param" == "PATH" ]]; then
			if [ "$syslog_flag" = "YES" ]; then
                        	scds_syslog -p warning -t $(syslog_tag) -m \
				    "Ignoring PATH set in environment file %s" \
				    "${USER_ENV}"
			fi

			write_stderr "$(gettext "Ignoring PATH set in environment file %s")" \
				"${USER_ENV}"
			continue
		fi


		#
		# Do not allow Environment_file to override ORACLE_HOME
		# or ORACLE_SID
		#

		if [[ "$param" == "ORACLE_HOME" ]]; then
			if [ "$syslog_flag" = "YES" ]; then
                        	scds_syslog -p warning -t $(syslog_tag) -m \
				    "Ignoring ORACLE_HOME set in environment file %s" \
				    "${USER_ENV}"
			fi

			write_stderr "$(gettext "Ignoring ORACLE_HOME set in environment file %s")" \
				"${USER_ENV}"
			continue
		fi
		if [[ "$param" == "ORACLE_SID" ]]; then
			if [ "$syslog_flag" = "YES" ]; then
                        	scds_syslog -p warning -t $(syslog_tag) -m \
				    "Ignoring ORACLE_SID set in environment file %s" \
				    "${USER_ENV}"
			fi

			write_stderr "$(gettext "Ignoring ORACLE_SID set in environment file %s")" \
				"${USER_ENV}"
			continue
		fi


		# 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
				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
                        	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
				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

	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
}


#############################################################
# 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
	   	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
	   	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
	   	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
	   	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
#
#! /usr/bin/ksh
#
# ident "@(#)oracle_rac_server_lib.ksh 1.5     05/01/07 SMI"
#
# Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# Oracle RAC Library routines
#
#
#
initialize()
{

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

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

	DEBUG_LOG_SIZE=300000

	MONITOR_TAG_PREFIX=RACSERV_MON

	MONITOR_NAME=""
}

#############################################################
#
#############################################################
server_running()
{
 typeset running
	running=$(/bin/ps -u $ORACLE_OWNER -o comm | \
			/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)

	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

	THOROUGH_PROBE_INTERVAL=$(read_standard_property_value Thorough_probe_interval)
	[ -z "$THOROUGH_PROBE_INTERVAL" ] && THOROUGH_PROBE_INTERVAL=30
}


#############################################################
#
#############################################################
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

	return 0

}

#############################################################
#
#############################################################
monitor_setup()
{
	MONITOR_TAG="${MONITOR_TAG_PREFIX}_${RESOURCE_NAME}"
	MONITOR_NAME="$BINDIR/oracle_rac_server_monitor"

	return 0

}

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

	return $rc
}

#############################################################
#  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"
	else
		version="unknown"
	fi

	print $version
}
#include_oracle_rac_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)
	ORACLE_SID=$(read_extension_property_value ORACLE_SID)
	ORACLE_HOME=$(read_extension_property_value ORACLE_HOME)
	USER_ENV=$(read_extension_property_value USER_ENV)
	RESOURCE_DEPENDENCIES=$(read_standard_property_value RESOURCE_DEPENDENCIES)
	NODELIST=`scha_resourcegroup_get -O NODELIST -G "$RESOURCE_GROUP"`
	FAILBACK=`scha_resourcegroup_get -O FAILBACK -G "$RESOURCE_GROUP"`
	RG_AFFINITIES=`scha_resourcegroup_get -O RG_AFFINITIES \
	  -G "$RESOURCE_GROUP"`
     fi

     	while getopts ug:cr: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

	ORACLE_BIN_PERMS=`/bin/ls -l ${ORACLE_HOME}/bin/oracle | nawk '{print $1}'`
	if [[ $ORACLE_BIN_PERMS != ???s??[sx]??? ]]; 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
}

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

	validate_oracle_setup
	[ $? -ne 0 ] && rc=1

	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
        fi

        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

	check_properties
	[ $? -ne 0 ] && return 1

	check_dependencies
	[ $? -ne 0 ] && return 1

	write_trace "Validation successful"

	return $rc

}
#############################################################
# check_properties
#
#############################################################
check_properties()
{
	if [ "`echo $NODELIST | grep ,`" ]; then
		scds_syslog -p error -t $(syslog_tag) -m \
		  "Validation failed. Resource group property NODELIST must contain only 1 node"

		write_stderr "$(gettext "Error: Validation failed. Resource group property NODELIST must contain only 1 node")"

		return 1
	fi

	if [ "$FAILBACK" != "FALSE" ]; then
		scds_syslog -p error -t $(syslog_tag) -m \
		  "Validation failed. Resource group property FAILBACK must be FALSE"

		write_stderr "$(gettext "Error: Validation failed. Resource group property FAILBACK must be FALSE")"

		return 1
	fi

	return 0
}
#############################################################
# check_dependencies()
#
#############################################################
check_dependencies()
{
	typeset rc=0
	typeset FOUND=0
	#
	# This resource group should have RG_AFFINITIES set to a strong
	# positive affinity with the scalable rac framework RG
	#
	if [ -z "$RG_AFFINITIES" -o \
	    -z `echo $RG_AFFINITIES | grep "^++[A-Z,a-z]"` ]; then

		scds_syslog -p error -t $(syslog_tag) -m \
		  "Validation failed. Resource group property RG_AFFINITIES should specify a STRONG POSITIVE affinity (++) with the SCALABLE resource group containing the RAC framework resources"

		write_stderr "$(gettext "Error: Resource group property RG_AFFINITIES should specify a STRONG POSITIVE affinity (++) with the SCALABLE resource group containing the RAC framework resources")"

		return 1
	fi

	#
	# Remove the "++" from the begining of RG_AFFINITIES
	# to get the resource group name
	#
	RG_AFFINITIES=`echo $RG_AFFINITIES | /usr/bin/sed 's/^++//'`
	TYPE=`scha_resourcegroup_get -O RG_MODE -G "$RG_AFFINITIES"`

	if [ "$TYPE" != "Scalable" ]; then
		scds_syslog -p error -t $(syslog_tag) -m \
		  "Validation failed. Resource group property RG_AFFINITIES should specify a SCALABLE resource group containing the RAC framework resources"

		write_stderr "$(gettext "Error: Resource group property RG_AFFINITIES should specify a SCALABLE resource group containing the RAC framework resources")"

		return 1
	fi

	#
	# This resource should have a RESOURCE_DEPENDENCIES list
	# consisting of at least the SUNW.rac_framework resource
	#
	if [ -z "$RESOURCE_DEPENDENCIES" ]; then
		scds_syslog -p error -t $(syslog_tag) -m \
		  "Validation failed. Resource property RESOURCE_DEPENDENCIES should contain at least the rac_framework resource"

		write_stderr "$(gettext "Error: Resource property RESOURCE_DEPENDENCIES should contain at least the rac_framework resource")"

		return 1
	fi

	#
	# Process each resource in the RESOURCE_DEPENDENCIES list
	#
	IFS=$IFS","
	for RESOURCE in $RESOURCE_DEPENDENCIES
	do
		TYPE=`scha_resource_get -O TYPE -R "$RESOURCE"`

		if [[ "$TYPE" = SUNW.rac_framework:* ]]; then
			FOUND=1

			#
			# The resource should belong to the same group
			# specified by RG_AFFINITIES
			#
			compare_group "$RESOURCE" "$RG_AFFINITIES"

			if [ $? != 0 ]; then
				scds_syslog -p error -t $(syslog_tag) -m \
				  "Validation failed. The rac_framework resource %s, specified in the RESOURCE_DEPENDENCIES property, does not belong to the group specified in the resource group property RG_AFFINITIES" $RESOURCE

				write_stderr "$(gettext "Error: The rac_framework resource %s, specified in the RESOURCE_DEPENDENCIES property, does not belong to the group specified in the resource group property RG_AFFINITIES")" \
				  $RESOURCE

				return 1
			fi
		fi

	done

	if [ $FOUND -eq 0 ]; then
		scds_syslog -p error -t $(syslog_tag) -m \
		  "Validation failed. Resource property RESOURCE_DEPENDENCIES should contain at least the rac_framework resource"

		write_stderr "$(gettext "Error: Resource property RESOURCE_DEPENDENCIES should contain at least the rac_framework resource")"

		return 1
	fi

	return 0
}

#############################################################
# compare_group()
#
#############################################################
compare_group()
{
	RES=$1
	GRP=$2

	if [ "`scha_resource_get -O GROUP -R $RES`" = "$GRP" ]; then
		return 0
	else
		return 1
	fi
}

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

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

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

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

	return 0

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

