#! /bin/sh

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="fencing domain"

test -x /usr/sbin/fence_tool || exit 0

if [ ! -e /etc/cluster/cluster.conf ]; then
	exit 0
fi

if [ -f /etc/default/fence ]; then
	. /etc/default/fence
fi

[ "$FENCED_START_TIMEOUT" ] || FENCED_START_TIMEOUT=20

set -e

case "$1" in
  start)
	echo -n "Starting $DESC: "
	if grep -qE "<[[:space:]]*gulm([[:space:]]|[>]|$)" /etc/cluster/cluster.conf; then
		echo "Not starting $DESC because of <gulm> section detected in /etc/cluster/cluster.conf"
		exit 0
	fi
	if [ ! -e /proc/cluster/status ]; then
		echo "Not starting $DESC because cman has not been properly started."
		exit 0
	fi
	fence_tool -t $FENCED_START_TIMEOUT join -w
	echo "done."
	;;
  stop)
	echo -n "Stopping $DESC: "
	fence_tool leave
	echo "done."
	;;
  restart|force-reload)
	;;
  *)
	N=/etc/init.d/$NAME
	echo "Usage: $N {start|stop}" >&2
	exit 1
	;;
esac

exit 0
