#!/bin/sh
#
#   Support:      linux-ha-dev@lists.tummy.com
#   License:      GNU General Public License (GPL)
#
#	This script is called to politely request that a resource be given up
#	to us.
#
#	At the end of $TIMEOUT, we take it anyway.
#
#	It could be that we already have taken it, in which case it should
#	do nothing.
#
#

# set -x
prefix=@prefix@
exec_prefix=@exec_prefix@
HA_DIR=@sysconfdir@/ha.d; export HA_DIR
. $HA_DIR/shellfuncs

#
#	This code is the "ask nicely" case for when we first start up a node.
#	The case of the other node failing or being dead when we start up
#	is handled by mach_down which invokes takegroup just like we do
#	except it never asks for permission.
#
#	The nice_failback = yes ($HA_NICEFAILBACK) case coordinates things
#	so always know the other guy doesn't have the resources before we
#	get here, so we don't need to ever wait in that case...
#
#	If no one else is up, then we set HA_DONTASK in the environment.
#	It is possible that the TIMEOUT case should be removed now...
#	This is a failsafe case, but it's kind of a dangerous failsafe
#	case...
#
TIMEOUT=1200

RESOURCE=$1

#
#	Do we already have this resource?
#

$HA_NOARCHBIN/ResourceManager status $RESOURCE && exit 0

#
#	Now give our ip-request-message...
#
#		"ip-request"		Message type
#		ip-address		IP address requested
#						DENY if we won't/can't
#
case $HA_debug in
 [123458789]*)	ha_log "debug: in $0 $*"
		ha_log "debug: dont_ask: $HA_DONTASK nice_failback: $HA_NICEFAILBACK";;
esac

#
#	The nice failback case only takes over from dead machines...
#
case $HA_NICEFAILBACK in
  yes)	HA_DONTASK=yes;;
esac

CMD=ip-request
case $HA_DONTASK in
  yes)
	#
	#	Pretend the current owner gave it up peacefully
	#
	ha_clustermsg <<-!MSG
		t=$CMD-resp
		dest=$HA_CURHOST
		ipaddr=$RESOURCE
		weown=yes
		ok=OK
!MSG
	;;

  *)
	: "Ask Nicely ;-)"
	ha_clustermsg <<-!MSG
	t=$CMD
	ipaddr=$RESOURCE
!MSG

	#	NOTE!
	#	The original dangerous failsafe takeover of resources
	#	has been disabled!!

	exit 0


  # Normally they will have already replied to us before we get here
  # and we will have already taken things over.  This is to allow
  # for the case that they somehow never tell us to go ahead...
  # Heartbeat should kill us if it shuts down before we finish.
  # For that to work we can't go into the background...

	sleep $TIMEOUT
	$HA_NOARCHBIN/ResourceManager status "$RESOURCE" ||
	$HA_NOARCHBIN/ResourceManager takegroup "$RESOURCE"
	;;
esac
