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

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

STOP_TIMEOUT=`$SCHA_RESOURCE_GET -O Stop_timeout \
                -R $RESOURCE -G $RESOURCEGROUP`

SCLH=`$SCHA_RESOURCE_GET -O Network_resources_used \
        -R $RESOURCE -G $RESOURCEGROUP`

LHOST=`scrgadm -pvv -j ${SCLH} | grep HostnameList | \
        grep value: | awk -F: '{print $4}' | tr -d " "`

LHOSTNAME="LHOSTNAME=${LHOST}"

syslog_tag()
{
        #
        # Data Service message format
        #

        $SET_DEBUG

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

scds_syslog()
{

        #
        # Log a message
        #

        $SET_DEBUG

        $SCLOGGER "$@" &
}

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

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

                DEBUG_TEXT=$1

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

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

        debug_message "Function: log_message - Begin"
        $SET_DEBUG

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

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

                su $CON_APPSUSER -c "cat /dev/null > $LOGFILE" > /dev/null
		cat /dev/null > ${LOGFILE}.copy
        fi

        debug_message "Function: log_message - End"
}

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

        for i in RESOURCE RESOURCEGROUP VERSION CON_COMNTOP CON_APPSUSER APP_SID
        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;;

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

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

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

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

                esac
        done

	#
        # Validate $VERSION
        #

        case "${VERSION}" in
                11.5.8)         debug_message "Validate - $VERSION"

                                # With 11.5.8 the SID didn't change

                                SID=${APP_SID}
                                ;;

                11.5.9)         debug_message "Validate - $VERSION"

                                # With 11.5.9 the SID changed to SID_<lhost>
                                # for some directory structures and files

                                SID=${APP_SID}
                                APP_SID=${APP_SID}_${LHOST}
                                ;;

                *)              scds_syslog -p daemon.err -t $(syslog_tag) -m \
                                        "Validate - %s is invalid" \
                                        $VERSION
                                ;;
        esac
}

validate()
{
        #
        # Validate rep
        #

        debug_message "Function: validate - Begin"
        $SET_DEBUG

        rc_validate=0

        #
        # Validate that ${CON_COMNTOP} exists
        #

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

	#
        # Validate that ${CON_COMNTOP}/admin/scripts/${APP_SID} exists
        #

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

        #
        # Validate that ${CON_APPSUSER} exists
        #

        if [ -z "`getent passwd ${CON_APPSUSER}`" ]
        then
                scds_syslog -p daemon.err -t $(syslog_tag) -m \
                        "Validate - Application user <%s> does not exist" \
                        "${CON_APPSUSER}"
                rc_validate=1
        else
                debug_message "Validate - Application user ${CON_APPSUSER} exists"
        fi

        debug_message "Function: validate - End"
}

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

        PROPERTY=$1

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

        s1=$?

        if [ "${s1}" -ne 0 ]; then
                debug_message "standard_resource_get - Get property ${PROPERTY} rc<${s1}>, ignore if SC3.0"
        fi

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

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

        PROPERTY=$1

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

        s1=$?

        if [ "${s1}" -ne 0 ]; then
                debug_message "standard_resource_get - Get property ${PROPERTY} rc<${s1}>, ignore if SC3.0"
        fi

        debug_message "Function: standard_resourcegroup_get - End"

        return ${s1}
}

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

        USER=$1

        #
        # If Solaris 8 just return
        #

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

        #
        # Retrieve RESOURCE_PROJECT_NAME
        #

        RESOURCE_PROJECT_NAME=`standard_resource_get RESOURCE_PROJECT_NAME`

        #
        # Retrieve RG_PROJECT_NAME if RESOURCE_PROJECT_NAME is not set
        #

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

                RESOURCE_PROJECT_NAME=`standard_resourcegroup_get RG_PROJECT_NAME`
        fi

        #
        # Return if no projects are defined
        #

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

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

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

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

        #
        # Set TASK_COMMAND
        #

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

        debug_message "Function: srm_function - End"

        return 0
}

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

        DUMMY_PROCESS="/tmp/${RESOURCE}_${RESOURCEGROUP}_dummy.csh"

        cat > ${DUMMY_PROCESS} <<-EOF
	#!/bin/csh

	while (1)
		sleep 3600
	end
	EOF

        /bin/chmod 755 ${DUMMY_PROCESS}

        ${DUMMY_PROCESS} &

        scds_syslog -p daemon.notice -t $(syslog_tag) -m \
                "Create_dummy_process - Reports Server was manually started" \
                "${QMGR}"

        debug_message "Function: create_dummy_process - End"
}

start_rep()
{
	#
	# Start rep
	#

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

        if getent passwd ${CON_APPSUSER} | awk -F: '{print $7}' | grep csh > /dev/null
        then
                su ${CON_APPSUSER} -c "$TASK_COMMAND ${CON_COMNTOP}/admin/scripts/${APP_SID}/adrepctl.sh start \
                        >& ${LOGFILE}" > /dev/null
                rc_start_rep=$?
        else
                su ${CON_APPSUSER} -c "$TASK_COMMAND ${CON_COMNTOP}/admin/scripts/${APP_SID}/adrepctl.sh start \
                        >${LOGFILE} 2>&1" > /dev/null
                rc_start_rep=$?
        fi

	debug_message "Function: start_rep - End"
}

stop_rep()
{
       	#
	# Stop rep
	#

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

        if getent passwd ${CON_APPSUSER} | awk -F: '{print $7}' | grep csh > /dev/null
        then
                su ${CON_APPSUSER} -c "$TASK_COMMAND ${CON_COMNTOP}/admin/scripts/${APP_SID}/adrepctl.sh stop \
                        >& ${LOGFILE}" > /dev/null
                rc_stop_rep=$?
        else
                su ${CON_APPSUSER} -c "$TASK_COMMAND ${CON_COMNTOP}/admin/scripts/${APP_SID}/adrepctl.sh stop \
                        >${LOGFILE} 2>&1" > /dev/null
                rc_stop_rep=$?
        fi

        if [ "$rc_stop_rep" -ne 0 ]
        then
                log_message err stop_rep
        fi

	debug_message "Function: stop_rep - End"
}

cleanup_rep()
{
        #
        # Cleanup rep
        #

        debug_message "Function: Cleanup_rep - Begin"
        $SET_DEBUG

        # Wait upto 30 seconds before we kill what's left

	for pid in `pgrep -f -u ${CON_APPSUSER} rwmts60`
	do
		kill -9 $pid
	done

        debug_message "Function: Cleanup_rep - End"
}

orderly_stop_rep()
{
	#
	# Orderly stop rep 
	#
	
        debug_message "Function: orderly_stop_rep - Begin"
        $SET_DEBUG

        for func in stop_rep \
                    cleanup_rep
        do
                case $func in
                        stop_rep)       ((FORCE_TIME=$STOP_TIMEOUT *65/100));;
                        cleanup_rep)    ((FORCE_TIME=$STOP_TIMEOUT *5/100));;
                esac

                LOOP_TIME=$FORCE_TIME

                $func

                # Test if any ${CON_APPSUSER} rwmts60 pids are still running

                while (( $LOOP_TIME > 0 ))
                do
                        if pgrep -f -u ${CON_APPSUSER} rwmts60 > /dev/null
                        then
                                sleep 1
                                LOOP_TIME=`expr $LOOP_TIME - 1`
                        else
                                break
                        fi
                done

                # Test if we broke out of the loop or the time
                # allocated for the function has expired.

                if pgrep -f -u ${CON_APPSUSER} rwmts60 > /dev/null
                then
                        continue
                else
                        # Break out of running all the functions
                        break
                fi
        done

        debug_message "Function: orderly_stop_rep - End"
}

check_rep()
{
	#
	# Check the Oracle Report Server
	# 

        debug_message "Function: check_rep - Begin"
	$SET_DEBUG
	
	rc_check_rep=0

	if pgrep -f -u ${CON_APPSUSER} rwmts60 > /dev/null
	then
		continue
	else
		rc_check_rep=100
	fi

	debug_message "Function: check_rep - End"
}

set_status()
{
        #
        # Set appropriate resource status
        #

        debug_message "Function: set_status - Begin"
        $SET_DEBUG

        STATUS_TYPE=$1

        scha_resource_setstatus -R $RESOURCE -G $RESOURCEGROUP -s $STATUS_TYPE

        rc_scha_resource_setstatus=$?

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

        debug_message "Function: set_status - End"
}

