#!/bin/sh
# Copyright (c) 2012. Synology, Inc. All Rights Reserved.
#
# OCF Resource Agent resource script wrapper for service(s).
#
# OCF instance parameters
#       OCF_RESKEY_logfile
#       OCF_RESKEY_errlogfile
#       OCF_RESKEY_monitor_hook
#       OCF_RESKEY_type

: ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/lib/heartbeat}
. ${OCF_FUNCTIONS_DIR}/ocf-shellfuncs

PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/syno/sbin:/usr/syno/bin
PATH=$PATH:$prefix/sbin:$prefix/bin

. $prefix/etc.defaults/rc.subr

wrapper="$OCF_RESOURCE_INSTANCE"
logfile="$OCF_RESKEY_logfile"
errlogfile="$OCF_RESKEY_errlogfile"

: ${OCF_RESKEY_state=${HA_RSCTMP}/ocf_wrapper_serv-${OCF_RESOURCE_INSTANCE}.state}

ocf_wrapper_meta()
{
	cat << END
<?xml version="1.0"?>
<!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
<resource-agent name="$1">
	<version>1.0</version>
	<longdesc lang="en">This is a generic OCF resource agent wrapper.</longdesc>
	<shortdesc lang="en">Manages an arbitrary service</shortdesc>
	<parameters>
		<parameter name="type" required="0" unique="1">
			<longdesc lang="en">Type of services (base/monitor/other).</longdesc>
			<shortdesc lang="en">Type of services (base/monitor/other).</shortdesc>
			<content type="string" default="" />
		</parameter>
		<parameter name="logfile" required="0">
			<longdesc lang="en">File to write STDOUT to</longdesc>
			<shortdesc lang="en">File to write STDOUT to</shortdesc>
			<content type="string" />
		</parameter>
		<parameter name="errlogfile" required="0">
			<longdesc lang="en">File to write STDERR to</longdesc>
			<shortdesc lang="en">File to write STDERR to</shortdesc>
			<content type="string" />
		</parameter>
		<parameter name="monitor_hook">
			<longdesc lang="en">Command to run in monitor operation</longdesc>
			<shortdesc lang="en">Command to run in monitor operation</shortdesc>
			<content type="string" />
		</parameter>
	</parameters>
	<actions>
		<action name="start"        timeout="120s" />
		<action name="stop"         timeout="120s" />
		<action name="monitor"      timeout="60s" interval="60" depth="0" />
		<action name="meta-data"    timeout="5" />
		<action name="validate-all" timeout="5" />
	</actions>
</resource-agent>
END

	exit $OCF_SUCCESS
}

# According to LSB 3.1 (ISO/IEC 23360:2006), if the `status` action is
# requested, the init scripts will return the following exit status codes.
#
# 0             program is running or service is OK
# 1             program is dead and /var/run pid file exists
# 2             program is dead and /var/lock lock file exists
# 3             program is not runnning
# 4             program or service status is unknown
# 5-99          reserved for future LSB use
# 100-149       reserved for distribution use
# 150-199       reserved for application use
# 200-254       reserved

# For all other init-scripts actions, the init script shall return an exit
# status of zero if the action was successful. Otherwise, the exit status
# shall be non-zero, as defined below. In addition to straightforward success
# , the following situations are also to be considered successful:
#
# - restarting a service (instead of reloading it) with the force-reload argument
# - running `start` on a service already running
# - running `stop` on a service already stopped or not running
# - running `restart` on a service already stopped or not running
# - running `try-restart` on a service already stopped or not running
#
# In case of an error while processing any init-script action except for
# `status`, the init script shall print an error message and exit with a
# non-zero code:
#
# 0             successful
# 1             generic or unspecified error
# 2             invalid or excess argument(s)
# 3             unimplemented feature (for example, `reload`)
# 4             user had insufficient privilege
# 5             program is not installed
# 6             program is not configured
# 7             program is not running
# 8-99          reserved for future LSB use
# 100-149       reserved for distribution use
# 150-199       reserved for application use
# 200-254       reserved

ocf_wrapper_start()
{
	if [ -f ${OCF_RESKEY_state} ]; then
		return $OCF_SUCCESS
	fi
	touch ${OCF_RESKEY_state}

	StartServicesHA START
	StartServicesHA BASE
	StartServicesHA 3RD
	StartServicesHA DONE
	# Serviecs restart was denied when IPaddr2 need to restart rc.network.
	allow_services_restart

	return $OCF_SUCCESS
}

ocf_wrapper_stop()
{
	if [ ! -f ${OCF_RESKEY_state} ]; then
		return $OCF_SUCCESS
	fi
	rm -fr ${OCF_RESKEY_state}
	StopServiceHA

	return $OCF_SUCCESS
}

ocf_wrapper_monitor()
{
	if [ -f "$SYNO_HA_STOPPED" -o -f "$SYNO_HA_STOPPING" ]; then
		logger -p err "System is shutting down. Skip ocf service check."
		return $LSB_SUCCESS
	fi

	if [ ! -f ${OCF_RESKEY_state} ]; then
		return $LSB_NOT_RUNNING
	fi
	{
		if [ -f $SYNO_HA_STATUS_CHECK_LOG ]; then
			cp $SYNO_HA_STATUS_CHECK_LOG /var/lib/ha/ha_status_check_error
			return $LSB_SUCCESS
		fi
		touch $SYNO_HA_STATUS_CHECK_LOG
		ServiceStatusHA
		rm -f $SYNO_HA_STATUS_CHECK_LOG
	}&
	return $LSB_SUCCESS
}

ocf_wrapper_validate()
{
	return $OCF_SUCCESS
}

ocf_wrapper_notify()
{
	return $OCF_ERR_UNIMPLEMENTED
}

ocf_wrapper_demote()
{
	return $OCF_ERR_UNIMPLEMENTED
}

ocf_wrapper_promote()
{
	return $OCF_ERR_UNIMPLEMENTED
}

ocf_wrapper_reload()
{
	return $OCF_ERR_UNIMPLEMENTED
}

case "$1" in
	meta-data|metadata|meta_data)
		ocf_wrapper_meta
		;;
	start)
		exec &>/dev/null
		synoha_log "ocf_wrapper_serv start."
		ocf_wrapper_start
		;;
	stop)
		exec &>/dev/null
		synoha_log "ocf_wrapper_serv stop."
		ocf_wrapper_stop
		;;
	monitor)
		ocf_wrapper_monitor
		;;
	validate-all)
		ocf_wrapper_validate
		;;
	notify)
		ocf_wrapper_notify
		;;
	demote)
		ocf_wrapper_demote
		;;
	promote)
		ocf_wrapper_promote
		;;
	reload)
		ocf_wrapper_reload
		;;
	*)
		ocf_log err "$0 was called with unsupported arguments: $*"
		exit $OCF_ERR_UNIMPLEMENTED
		;;
esac

# vim:ft=sh
