#!/usr/bin/ksh
#
# Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# Method for the mysql agents smf manifwest and for the zsh component
#
# This method is called by the manifest, by the optional probe script of the smf method 
# and as start stop and probe for the zsh component.
#
# it is startet with options and up to 2 parameters:
#
# The options are used it smf_mysql is started for the zsh component.
# in example smf_mysql -R resource -G group -S timout -P project start
# in the example above are no agent specific options included, obviously they need 
# to be amended.
#
# if you use it for smf omit all the options and specify the parameters as stated below.
#
# $1 start stop or probe
# $2 is the smf service tag name. It is used only if the parameter $1 is probe
#

MYNAME=`basename ${0}`
MYDIR=`dirname ${0}`

. ${MYDIR}/functions
. ${MYDIR}/../etc/config
. ${MYDIR}/../lib/functions_static

if [ -f /lib/svc/share/smf_include.sh ]
then
	. /lib/svc/share/smf_include.sh
fi
debug_message "Method: ${MYNAME} ${1} - Begin"
${SET_DEBUG}

# get the options for gds and the zsh command, amend as appropriate

while getopts 'R:G:B:D:U:H:F:L:C' opt
do
        case "${opt}" in
                R)      RESOURCE=${OPTARG};;
                G)      RESOURCEGROUP=${OPTARG};;
                B)      MYSQL_BASEDIR=$OPTARG;;
                D)      MYSQL_DATADIR=$OPTARG;;
                U)      MYSQL_USER=$OPTARG;;
                H)      MYSQL_HOST=$OPTARG;;
                F)      MYSQL_FMUSER=$OPTARG;;
                L)      MYSQL_LOGDIR=$OPTARG;;
                C)      MYSQL_CHECK=TRUE;;
                *)      logger -p daemon.err \
                        "ERROR: ${MYNAME} Option ${OPTARG} unknown"
                        exit 1;;
        esac
done

# if no option is set ($OPTIND is 1 in this case), use the smf properties

if [ ${OPTIND} -gt 1 ]
then


	shift $((${OPTIND} - 1))
else
	
	# Setting SMF_FMRI in case of validate and probe
        
	if [ -z "${SMF_FMRI}" ]
	then
		SMF_FMRI=${2}	
	fi

	# getting the necessary parameters and filling the variables usually filled
	# in from options

	get_fmri_parameters

fi


# Source functions to nail down parameters which are set by options or get_fmri_parameters
. ${MYDIR}/functions
 
# set some generic variables
LOGFILE=/var/tmp/${RESOURCE}_logfile

case ${1} in
start)

	# start application mysql

	# exit from start, if the options are wrong

	validate_options
	rc_val=${?}
	if [ ${rc_val} -ne 0 ]
	then
		terminate ${1} ${rc_val}
	fi
	
	rm ${LOGFILE} 2>/dev/null

	variables_init
	
	if validate
	then

		start_mysql
		rc_val=${?}
		
		if [ ${rc_val} -eq 0 ]
		then
		        log_message notice "start_command rc<${rc_val}>"
		        debug_message "Method: ${MYNAME} - End (Exit 0)"
		else
		        log_message err "start_command rc<${rc_val}>"
		fi
	else
	        debug_message "Method: ${MYNAME} - End (Exit 1)"
		rc_val=1
	fi;;
stop)

	# stop application mysql

	# exit from stop, if the options are wrong

	validate_options
	rc_val=${?}
	if [ ${rc_val} -ne 0 ]
	then
		terminate ${1} ${rc_val}
	fi

	variables_init
	
	stop_mysql
	rc_val=${?}
	
	if [ "${rc_val}" -eq 0 ]
	then
	        log_message notice "stop_command rc<${rc_val}>"
	else
	        log_message err "stop_command rc<${rc_val}>"
	fi;;

probe)

	# probe application mysql

	# exit from probe, if the options are wrong

	validate_options
	rc_val=${?}
	if [ ${rc_val} -ne 0 ]
	then
		terminate ${1} ${rc_val}
	fi

	variables_init
	
	if ! validate
	then
	        rc_val=100
	else
		
		# spin off a project based smf probe if necessary, do the probe with the check function otherwise

		if [ "${Project}" != ":default" ] && [ -n "${SMF_FMRI}" ]
		then
			if /usr/bin/newtask -p ${Project} ${MYDIR}/probe_smf_mysql ${SMF_FMRI}
			then
				rc_val=0
			else
				rc_val=100
			fi
		else

			if check_mysql
			then
				rc_val=0
			else
				rc_val=100
			fi
		fi
	fi;;

validate)

	# validate the parameters for application mysql

	validate_options
	rc_val=${?}
	if [ ${rc_val} -ne 0 ]
	then
		terminate ${1} ${rc_val}
	fi
	
	rm ${LOGFILE} 2>/dev/null

	variables_init
	
	if validate
	then
	        rc_val=0
	else
	        rc_val=1
	fi;;
	
esac

# terminate with the right return code, either with an smf specific or the gds/zsh based
# return code

terminate ${1} ${rc_val}
