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

# Generic SunCluster Section

PKG=SUNWscsge
METHOD=`basename $0`
LOGFILE=/var/tmp/${RESOURCE}_logfile
TASK_COMMAND=""

SCLOGGER=/usr/cluster/lib/sc/scds_syslog
SCSTATUS=/usr/cluster/bin/scha_resource_setstatus
SCHA_RESOURCE_GET=/usr/cluster/bin/scha_resource_get
SCHA_RESOURCEGROUP_GET=/usr/cluster/bin/scha_resourcegroup_get

syslog_tag()
{
        #
        # Data Service message format
        #

        ${SET_DEBUG}

        print "SC[${PKG:-??}.${METHOD:-??}]:${RESOURCEGROUP:-??}:${RESOURCE:-??}"
}

scds_syslog()
{

        #
        # Log a message
        #

        ${SET_DEBUG}

        ${SCLOGGER} "$@" &
}

debug_message()
{
        #
        # Output a debug message to syslog if required
        #

        if [ "${DEBUG}" = "${RESOURCE}" -o "${DEBUG}" = "ALL" ]
        then
                SET_DEBUG="set -x"

                DEBUG_TEXT=$1

                scds_syslog -p daemon.debug -t $(syslog_tag) -m \
                        "%s" "$DEBUG_TEXT"
        else
                SET_DEBUG=
        fi
}

log_message()
{
        #
        # Output a message to syslog as required
        #

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

        if [ -s "${LOGFILE}" ]
        then
                PRIORITY=$1
                HEADER=$2

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

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

                cat /dev/null > ${LOGFILE}
		cat /dev/null > ${LOGFILE}.copy
        fi

        debug_message "Function: log_message - End"
}

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

        PROPERTY=$1

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

        s1=$?

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

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

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

        PROPERTY=$1

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

        s1=$?

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

        debug_message "Function: standard_resourcegroup_get - End"

        return ${s1}
}

srm_function()
{

        USER=$1

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


        #
        # If Solaris 8 just return
        #

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

        #
        # Retrieve RESOURCE_PROJECT_NAME
        #

        RESOURCE_PROJECT_NAME=`standard_resource_get RESOURCE_PROJECT_NAME`

        #
        # Retrieve RG_PROJECT_NAME if RESOURCE_PROJECT_NAME is not set
        #

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

                RESOURCE_PROJECT_NAME=`standard_resourcegroup_get RG_PROJECT_NAME`
        fi

        #
        # Return if no projects are defined
        #

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

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

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

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

        #
        # Set TASK_COMMAND
        #

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

        debug_message "Function: srm_function - End"

        return 0
}

set_status()
{
        #
        # Set appropriate resource status
        #

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

        STATUS_TYPE=$1

        ${SCSTATUS} -R ${RESOURCE} -G ${RESOURCEGROUP} -s ${STATUS_TYPE}

        rc_scha_resource_setstatus=$?

        if [ "$rc_scha_resource_setstatus" != 0 ]
        then
                scds_syslog -p daemon.err -t $(syslog_tag) -m \
                "set_status - rc<%s>  type<%s>" \
                "${rc_scha_resource_setstatus}" "${STATUS_TYPE}"
        fi

        debug_message "Function: set_status - End"
}

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

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

        for i in RESOURCE RESOURCEGROUP SGE_ROOT SGE_CELL
        do
                case $i in
                        RESOURCE)
                        if [ -z ${RESOURCE} ]; then
                                logger -p daemon.err \
                                "ERROR: `basename $0` Option -R not set"
                                exit 1
                        fi;;

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

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

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

                esac
        done

	if [ ! -d ${SGE_ROOT} ]
	then
	    scds_syslog -p daemon.err -t $(syslog_tag) -m \
	    "validate_options - Fatal: SGE_ROOT  ${SGE_ROOT} not a directory"
	    exit 1
	fi

	if [ ! -d ${SGE_ROOT}/${SGE_CELL} ]
	then
	    scds_syslog -p daemon.err -t $(syslog_tag) -m \
	    "validate_options -  Fatal: SGE_CELL  ${SGE_ROOT}/${SGE_CELL} not a directory"
	    exit 1
	fi

        debug_message "Function: validate_options - End"

}
