#!/bin/ksh
#
# Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
#
#  This script takes 2 options, both are optional.
#  -f filename states a config file different from ha_mysql_config.
#     This file will be sourced instead of ha_mysql_config if -f filename is specified
#
#  -z smf/zsh 
#     This option  is used for local zone instances only. smf stands for a 
#     Sun Cluster Agent for Containder SMF Component, zsh stands for the 
#     zsh component


# Set generic variables:

BINDIR=/opt/SUNWscmys/bin
UTILDIR=/opt/SUNWscmys/util
SMFUTILDIR=/opt/SUNWsczone/sczsmf/util
ZSHUTILDIR=/opt/SUNWsczone/sczsh/util

global_zone()
{

# function to register a resource in the global zone or on solaris 8 or 9

	scrgadm -a -j $RS -g $RG -t SUNW.gds \
	-x Start_command="/opt/SUNWscmys/bin/start_mysql \
	-R $RS -G $RG -B $BASEDIR -D $DATADIR -U $MYSQLUSER \
	-H ${MYSQLHOST} -F ${FMUSER}%${FMPASS} -L $LOGDIR ${CHECK}" \
	-x Probe_command="/opt/SUNWscmys/bin/probe_mysql \
	-R $RS -G $RG -B $BASEDIR -D $DATADIR -U $MYSQLUSER \
	-H ${MYSQLHOST} -F ${FMUSER}%${FMPASS} -L $LOGDIR ${CHECK}" \
	-x Stop_command="/opt/SUNWscmys/bin/stop_mysql \
	-R $RS -G $RG -B $BASEDIR -D $DATADIR -U $MYSQLUSER \
	-H ${MYSQLHOST} -F ${FMUSER}%${FMPASS} -L $LOGDIR ${CHECK}" \
	-y Port_list=23/tcp -y Network_resources_used=$LH \
	-x Stop_signal=9 \
	-y Resource_dependencies=$HAS_RS
	
	St=$?
	
	if [ "${St}" -ne 0 ]; then
	        echo "Registration of resource ${RS} failed, please correct the wrong parameters."
	
	        return 1
	else
	        echo "Registration of resource ${RS} succeeded."
	fi
	
	# VALIDATE RESOURCE
	
	echo "Validate resource ${RS} in resourcegroup ${RG}"
	
	${BINDIR}/control_mysql -R ${RS} -G ${RG} -B $BASEDIR -D $DATADIR -U $MYSQLUSER \
        -H ${MYSQLHOST} -F ${FMUSER}%${FMPASS} -L $LOGDIR ${CHECK} validate
	
	St=$?
	
	if [ "${St}" -ne 0 ]; then
	        echo "Validation of resource ${RS} failed, please correct the wrong parameters."
	        echo "Removing resource ${RS} from the cluster configuration."
	
	        /usr/cluster/bin/scrgadm -r -j ${RS}
	
	        return 1
	else
	        echo "Validation of resource ${RS} succeeded."
	fi

}

local_zone_smf()
{

# function to register a smf resource

	SERVICE_TAG=svc:/application/sczone-agents:${RS}

	if [ ! -f ${SMFUTILDIR}/sczsmf_config ]
	then
		echo  ${SMFUTILDIR}/sczsmf_config does not exist. Make sure, that \
		      Sun Cluster HA for Solaris container is intalled in the global zone
		return 1
	fi

# prepare the config file

	cp ${SMFUTILDIR}/sczsmf_config ${SMFUTILDIR}/sczsmf_config.work
	cat << EOF > ${SMFUTILDIR}/sczsmf_config 
#
# Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# This file will be sourced in by sczsmf_register and the parameters
# listed below will be used.
#
# These parameters can be customized in (key=value) form
#
#               RS - Name of the resource
#               RG - Name of the resource group containing RS
#         SCZBT_RS - Name of the SC Zone boot resource
#             ZONE - Name of the Zone
#
#       For SERVICE, RECURSIVE and STATE, refer to the svcadm(1M)
#                 man page
#
#          SERVICE - {FMRI | pattern}
#               FMRI - Fault management resource identifier
#               pattern - Pattern matching a service
#
#        RECURSIVE - {false | true}     Default: true
#               False - Just enable the service and no dependents
#               True - Enable the service and recursively enable
#                 its dependents
#
#               RECURSIVE=true equates to svcadm enable "-r"
#
#            STATE - {false | true}     Default: true
#               False - Do not wait until service state is reached
#               True - Wait until service state is reached
#
#               STATE=true equates to svcadm enable/disable "-s"
#
#    SERVICE_PROBE - Script to check the SMF service
#
#               The optional parameter, SERVICE_PROBE, provides the
#                 ability to check that the SMF service is working.
#                 This must be a script within the zone and must
#                 adhere to these return codes,
#
#                 0 - The SMF service is working
#               100 - The SMF service should be restarted
#               201 - The SMF service should initiate a failover of
#                       the Resource Group
#
#               Note: That return code 201, requires that this resource
#                 has an appropriate extension property value for
#                 FAILOVER_MODE and FAILOVER_ENABLED=TRUE
#
#               For FAILOVER_MODE refer to the r_properties(5) man page.
#

RS=${RS}
RG=${RG}
SCZBT_RS=${ZONE_BT}
ZONE=${ZONE}
SERVICE=${SERVICE_TAG}
RECURSIVE=false
STATE=true
SERVICE_PROBE="${BINDIR}/control_mysql probe ${SERVICE_TAG}"
  
EOF

	# determine if a working copy of the config file was created if yes use the -f option

	REGOPT=""
	if [ -f ${UTILDIR}/ha_mysql_config.work ]
	then
		REGOPT="-f ${UTILDIR}/ha_mysql_config.work"
	fi
	
	echo clean up the manifest / smf resource
	${UTILDIR}/ha_mysql_smf_remove ${REGOPT} 2>/dev/null
	

	# register the manifest

	if /usr/sbin/zlogin ${ZONE} ${UTILDIR}/ha_mysql_smf_register ${REGOPT}
	then
		echo "Manifest svc:/application/sczone-agents:${RS} was created in zone ${ZONE}"
		echo "Registering the zone smf resource"
		ksh ${SMFUTILDIR}/sczsmf_register
		ret_code=${?}
                mv ${SMFUTILDIR}/sczsmf_config.work ${SMFUTILDIR}/sczsmf_config
	else
		echo "The registration of the manifest did not complete, fix the errors and retry"
		ret_code=0
	fi
	return ${ret_code}

}

MYCONFIG=

typeset opt

while getopts 'f:' opt
do
        case "${opt}" in
                f)      MYCONFIG=${OPTARG};;
                *)      echo "ERROR: ${MYNAME} Option ${OPTARG} unknown - early End. Only -f is valid"
                        exit 1;;
        esac
done

# Sourcing the specified config file, either the default one,
# or the one supplied with -f

if [ -n "${MYCONFIG}" ] && [ -f "${MYCONFIG}" ]
then
	echo "sourcing ${MYCONFIG} and create a working copy under ${UTILDIR}/ha_mysql_config.work"
	cp ${MYCONFIG} ${UTILDIR}/ha_mysql_config.work
	. ${MYCONFIG}
else
	PKGCONF=`dirname $0`/ha_mysql_config
	echo "sourcing ${PKGCONF}"
	. ${PKGCONF}
fi

if [ "${CHECK}" = "YES" ]; then
   CHECK=" -C"
else
   CHECK=""
fi


# Registering the resource

if [ -z "${ZONE}" ]
then
	global_zone
else
	local_zone_smf
fi

if [ -f ${UTILDIR}/ha_mysql_config.work ]
then
        echo " remove the working copy ${UTILDIR}/ha_mysql_config.work"
	rm ${UTILDIR}/ha_mysql_config.work
fi

exit 0
