#!/bin/sh
#
# Copyright (c) 2015 Jose A. Rivera <jarrpa@redhat.com>
#   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:

if [ -n "${OCF_DEBUG_LIBRARY}" ]; then
        . ${OCF_DEBUG_LIBRARY}
else
        : ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/lib/heartbeat}
        . ${OCF_FUNCTIONS_DIR}/ocf-shellfuncs
fi

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

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

<longdesc lang="en">
The ganesha_trigger Resource Agent trigger the TAKE_IP GRACE action of the
ganesha nfs daemon.
</longdesc>
<shortdesc lang="en">ganesha TAKE_IP trigger</shortdesc>

<parameters/>

<actions>
<action name="start"        timeout="40" />
<action name="stop"         timeout="40" />
<action name="status"       depth="0" timeout="20" interval="10" />
<action name="monitor"      depth="0" timeout="20" interval="10" />
<action name="notify"       timeout="20" />
<action name="meta-data"    timeout="20" />
<action name="validate-all" timeout="20" />
</actions>
</resource-agent>
END
}

#######################################################################
CMD=`basename $0`

trigger_usage() {
	cat <<END
usage: $CMD {start|stop|status|monitor|validate-all|meta-data}

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

trigger_start() {
	ha_pseudo_resource "${OCF_RESOURCE_INSTANCE}" start
	return $OCF_SUCCESS
}

grace() {
	rc=1
	# Loop until success or timeout
	while [ rc != 0 ]; do
		dbus-send --print-reply --system --dest=org.ganesha.nfsd \
		/org/ganesha/nfsd/admin org.ganesha.nfsd.admin.grace \
		string:${1}
		rc=$?
		if [ $rc -ne 0 ]; then
			ocf_log warn "FAILED: dbus-send --print-reply --system "\
"--dest=org.ganesha.nfsd /org/ganesha/nfsd/admin "\
"org.ganesha.nfsd.admin.grace string:${1}"
			sleep 1;
		fi
	done
}

trigger_notify() {
	# since this is a clone RA we should only ever see pre-start
	# or post-stop
	mode="${OCF_RESKEY_CRM_meta_notify_type}-${OCF_RESKEY_CRM_meta_notify_operation}"
	case "${mode}" in
	pre-start)
		grace "5:$OCF_RESKEY_CRM_meta_notify_start_uname"
		;;
	post-stop)
		grace "2:$OCF_RESKEY_CRM_meta_notify_stop_uname"
		;;
	esac

	return $OCF_SUCCESS
}

trigger_stop() {
	ha_pseudo_resource "${OCF_RESOURCE_INSTANCE}" stop
	return $OCF_SUCCESS
}

# Make sure meta-data and usage always succeed
case $__OCF_ACTION in
meta-data)		meta_data
				exit $OCF_SUCCESS
				;;
usage|help)		usage
				exit $OCF_SUCCESS
				;;
esac

case $__OCF_ACTION in
status|monitor|validate-all)	exit $OCF_SUCCESS;;
start)	trigger_start;;
stop)	trigger_stop;;
notify) trigger_notify;;
usage|help)	trigger_usage
		exit $OCF_SUCCESS
		;;
*)		trigger_usage
		exit $OCF_ERR_UNIMPLEMENTED
		;;
esac

rc=$?
ocf_log debug "${OCF_RESOURCE_INSTANCE} $__OCF_ACTION : $rc"
exit $rc
