#!/bin/sh
#
#
#	Infoset OCF Resource Agent. 
#
# Copyright (c) 2006 Andrew Beekhof
# Copyright (c) 2012. Synology, Inc. All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it would be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# Further, this software is distributed without any warranty that it is
# free of the rightful claim of any third person regarding infringement
# or the like.  Any license provided herein, whether implied or
# otherwise, applies only to this software file.  Patent licenses, if
# any, provided herein do not apply to combinations of this program with
# other software, or any other product whatsoever.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write the Free Software Foundation,
# Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
#

#######################################################################
# Initialization:

: ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/lib/heartbeat}
. ${OCF_FUNCTIONS_DIR}/ocf-shellfuncs
CRM_MASTER="${USR_BIN_DIR}/crm_master -l reboot"

. $prefix/etc.defaults/rc.subr
BOOTSEQ_BIN="/usr/syno/bin/synobootseq"
SYNO_HA_TMP_DIR=$HA_TMP_DIR
REMOTE_STANDBY_ING="${SYNO_HA_TMP_DIR}remote_standby_ing"
SYNOHA_CIB_INFO_REMOTE_FILES=$CIB_INFO_REMOTE_FILES

#######################################################################

meta_data() {
	cat <<END
<?xml version="1.0"?>
<!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
<resource-agent name="Infoset" version="1.0">
<version>1.0</version>

<longdesc lang="en">
This is an example resource agent that impliments two states
</longdesc>
<shortdesc lang="en">Example infoset resource agent</shortdesc>

<parameters>

<parameter name="state" unique="1">
<longdesc lang="en">
Location to store the resource state in
</longdesc>
<shortdesc lang="en">State file</shortdesc>
<content type="string" default="${HA_RSCTMP}/Infoset-{OCF_RESOURCE_INSTANCE}.state" />
</parameter>

</parameters>

<actions>
<action name="start"   timeout="20s" />
<action name="stop"    timeout="20s" />
<action name="promote" timeout="20s" />
<action name="demote"  timeout="20s" />
<action name="monitor" timeout="20" interval="31" role="Slave" depth="0" />
<action name="monitor" timeout="20" interval="29" role="Master" depth="0" />
<action name="meta-data"  timeout="5" />
<action name="validate-all"  timeout="20s" />
</actions>
</resource-agent>
END
	exit $OCF_SUCCESS
}

#######################################################################

infoset_usage() {
	cat <<END
usage: $0 {start|stop|promote|demote|monitor|validate-all|meta-data}

Expects to have a fully populated OCF RA-compliant environment set.
END
	exit $1
}

infoset_update() {
	echo $1 > ${OCF_RESKEY_state}
}

infoset_check_state() {
    target=$1
    if [ -f ${OCF_RESKEY_state} ]; then
	state=`cat ${OCF_RESKEY_state}`
	if [ "x$target" = "x$state" ]; then
	    return 0
	fi

    else
	if [ "x$target" = "x" ]; then
	    return 0
	fi
    fi

    return 1
}

infoset_start() {
    infoset_check_state master
    if [ $? = 0 ]; then
       	# CRM Error - Should never happen
	return $OCF_RUNNING_MASTER
    fi
    infoset_update slave
    $CRM_MASTER -v 5

    return 0
}

infoset_cib()
{
	{
	if $BOOTSEQ_BIN --is-ready &> /dev/null ; then
		if ! /bin/ps axw | grep "synoha --cib-hdinfo-set" | grep -v grep &> /dev/null ; then
			${HA_SBIN_DIR}/synoha --cib-hdinfo-set
		fi
		if ! /bin/ps axw | grep "synoha --cib-mdinfo-set" | grep -v grep &> /dev/null ; then
			${HA_SBIN_DIR}/synoha --cib-mdinfo-set
		fi
		if ! /bin/ps axw | grep "synoha --cib-nodeinfo-set" | grep -v grep &> /dev/null ; then
			${HA_SBIN_DIR}/synoha --cib-nodeinfo-set
		fi

		for (( i=1; i<=6; i=i+1 )) do
			if ! /bin/ps axw | grep "synoha --cib-ifinfo-set" | grep -v grep &> /dev/null ; then
				${HA_SBIN_DIR}/synoha --cib-ifinfo-set
			fi
			sleep 5
		done
	fi
	}&
}

infoset_demote() {
    infoset_check_state 
    if [ $? = 0 ]; then
       	# CRM Error - Should never happen
	return $OCF_NOT_RUNNING
    fi
    infoset_update slave
    $CRM_MASTER -v 5
    infoset_cib
    return 0
}

infoset_promote() {
    infoset_check_state 
    if [ $? = 0 ]; then
	return $OCF_NOT_RUNNING
    fi
    infoset_update master
    $CRM_MASTER -v 10
    return 0
}

infoset_stop() {
    $CRM_MASTER -D
    infoset_check_state master
    if [ $? = 0 ]; then
       	# CRM Error - Should never happen
	return $OCF_RUNNING_MASTER
    fi
    if [ -f ${OCF_RESKEY_state} ]; then
	rm ${OCF_RESKEY_state}
    fi
    return 0
}

infoset_monitor() {
	infoset_cib
	{
		local REMOTE_STATUS=`${HA_SBIN_DIR}/synoha --remote-status | cut -d':' -f2`
		if ! echo $REMOTE_STATUS | grep -q "\<online\>" ; then
			rm -f $SYNOHA_CIB_INFO_REMOTE_FILES
		fi
		if echo $REMOTE_STATUS | grep -q "\<offline\>" ; then
			#if ping `$HA_SBIN_DIR/synoha --remote-ip` -c 3 -W 1 &>/dev/null; then
				#ocf_log err "Remote offline but ping OK"
			#else
				#ocf_log err "Standby offline remote"
				if [ ! -e $REMOTE_STANDBY_ING ]; then
					touch $REMOTE_STANDBY_ING &>/dev/null
					${HA_SBIN_DIR}/synoha --standby-remote
					rm -rf $REMOTE_STANDBY_ING &>/dev/null
				fi
			#fi
		fi
	}&

    infoset_check_state "master"
    if [ $? = 0 ]; then
	return $OCF_RUNNING_MASTER
    fi

    infoset_check_state "slave"
    if [ $? = 0 ]; then
	return $OCF_SUCCESS
    fi

    if [ -f ${OCF_RESKEY_state} ]; then
	echo "File '${OCF_RESKEY_state}' exists but contains unexpected contents"
	cat ${OCF_RESKEY_state}
	return $OCF_ERR_GENERIC
    fi
    return 7
}

infoset_validate() {
	exit $OCF_SUCCESS
}

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

case $__OCF_ACTION in
meta-data)	meta_data;;
start)		synoha_log notice "infoset start";   infoset_start;;
promote)	synoha_log notice "infoset promote"; infoset_promote;;
demote)		synoha_log notice "infoset demote";  infoset_demote;;
stop)		synoha_log notice "infoset stop";    infoset_stop;;
monitor)	infoset_monitor;;
validate-all)	infoset_validate;;
usage|help)	infoset_usage $OCF_SUCCESS;;
*)		infoset_usage $OCF_ERR_UNIMPLEMENTED;;
esac

exit $?

