#!/bin/sh
#
# to be managed by heartbeat.
# description: Mounts and unmounts the cluster filesystem
#	  
# Copyright (C) 1999 2000, Philipp Reisner <philipp@linuxfreak.com>.
# Initial author.
#
# Copyright (C) 2000, Fbio Oliv Leite <olive@conectiva.com.br>
# Changes to support multiple devices and different distributions.
# Changes to increase robustness and exit with proper error values.
#
# Copyright (C) 2000, Thomas Mangin <thomas.mangin@free.fr>
# Losts of changes in the code
# moved the most of the code to drbdc only keep the start, stop, restart section
# Heartbeat integration, much more
#
# Copyright (C) 2000, Omar Kilani <omar@aurore.net>
# Bug fixing.


INSTALL="/etc/ha.d/resource.d"
. $INSTALL/drbd_common $*

# Parameter :
# The resource to wait for
# The time to wait
waitForPrimary()
{
		haveRemotePrimary $1
		RPRIMARY=$?

		havetowait=0
		counter=0

		while [ $RPRIMARY -eq 1 -a $counter -lt $2 ]; do
			havetowait=1
			logger -t "datadisk" "Waiting for the other node to become secondary"
			sleep 1

			counter=`expr $counter + 1`
			
			haveRemotePrimary $1
			RPRIMARY=$?
		done


		if [ $havetowait -eq 1 ]; then
			if [ $RPRIMARY -eq 1 ]; then
				logger -t "datadisk" "The other node didn't release the resource after $2 second"
				logger -t "datadisk" "Forcing the to become primary"
			else
				logger -t "datadisk" "The other node is now secondary"
			fi
		else
			logger -t "datadisk" "The node can become primary without waiting"
		fi
}


# See how we were called.
case "$OPTION" in
	start)
        if [ -z "$RESOURCE" ]; then
            runForAll start
            exit $RETVAL
        fi

		setLocal $CONFIG $RESOURCE
		$INSTALL/drbd $RESOURCE start

		if [ "$SAFE" != "false" ]; then
			if [ -e $STATE/"$RESOURCE"_safe ]; then
				logger -t "datadisk" "Safe mode : It seems that the resource $RESOURCE didn't stop properly"
				echo "It seems that the node didn't stop properly"
				ACTION="loop"

				while [ "$ACTION" != "c" -a "$ACTION" != "C" -a "$ACTION" != "r" -a "$ACTION" != "R" ]; do
					echo -n "Do you want to continue or reboot [C/R] " 
					read ACTION
					echo
				done

				if [ $ACTION != "c" -o $ACTION != "C" ]; then
					logger -t "datadisk" "User requested us to continue"
				else
					logger -t "datadisk" "User requested us to reboot the server"
					/sbin/shutdown -t3 -r now
					sleep 10
					echo "Oups, shutdown failed !"
					logger -t "datadisk" "shutdown is not in /sbin .. exiting."
					exit 0
				fi
			fi
			touch $STATE/"$RESOURCE"_safe
		else
			rm -f $STATE/"$RESOURCE"_safe 2> /dev/null
		fi

		is_force=`echo "$MODE" | grep -i "force" | wc -l | sed -e"s/ //g"`
		is_restore=`echo "$MODE" | grep -i "restore" | wc -l | sed -e"s/ //g"`
		is_negotiate=`echo "$MODE" | grep -i "negotiate" | wc -l | sed -e"s/ //g"`

		if [ $is_force -eq 1 ]; then
			waitForPrimary $RESOURCE $TIME
			command="master"
		elif [ $is_restore -eq 1 ]; then
			isMasterBoot $RESOURCE
			MASTER=$?

			isRemotePrimary $RESOURCE
			REMOTE_PRIMARY=$?

			if [ "$MASTER" -eq 1 -a "$REMOTE_PRIMARY" -ne 1 ]; then
				command="master"
			else
				command="slave"
			fi
		elif [ $is_negotiate -eq 1 ]; then
			haveRemotePrimary $RESOURCE
			RPRIMARY=$?

			haveRemoteSecondary $RESOURCE
			RSECONDARY=$?

			if [ "$RSECONDARY" -eq 1 ]; then
				command="master"
			elif [ "$RPRIMARY" -eq 1 ]; then
				command="slave"
			else
				# The other node must not be here
				command="slave"
			fi
		else
			failed "$0 unknow mode '$MODE' configured"
		fi

		$INSTALL/drbdc $RESOURCE $command

		RV=$?
		[ $RETVAL -eq 0 ] && RETVAL=$RV
		;;

	stop)
        if [ -z "$RESOURCE" ]; then
       	   runForAll stop
            exit $RETVAL
       	fi

		setLocal $CONFIG $RESOURCE

		if [ "$SAFE" != "false" ]; then
			logger -t "datadisk" "Safe mode : Clean shutdown for $RESOURCE"
			rm -f $STATE/"$RESOURCE"_safe 2> /dev/null
		fi

		$INSTALL/drbdc $RESOURCE slave

		RV=$?
		[ $RETVAL -eq 0 ] && RETVAL=$RV
		;;
		
	restart)
		$0 $RESOURCE stop
		$0 $RESOURCE start

		# TODO: A real return check
		RV=$?
		[ $RETVAL -eq 0 ] && RETVAL=$RV
		;;
	status)
		$INSTALL/drbdc $RESOURCE status
		RETVAL=$?
		;;

	*)
		echo "Usage: $0 [resource] {start|stop|restart|status}"
		RETVAL=1
		;;
esac

exit $RETVAL
