#!/bin/sh
#
# dlm_controld
#
# chkconfig: 21 79
# description: starts and stops dlm_controld
#


### BEGIN INIT INFO
# Provides: dlm_controld
# Required-Start: $network $remote_fs $time $syslog corosync
# Required-Stop: $remote_fs $syslog
# Should-Start:
# Should-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts and stops dlm_controld
# Description: starts and stops dlm_controld
### END INIT INFO

DESC="dlm control daemon"
NAME="dlm_controld"
DAEMON_OPTS=""

progdir="cluster"
lockfile="/var/run/$progdir/$NAME.pid"
exec="/usr/sbin/$NAME"

[ -x $exec ] || exit 0

. /lib/init/vars.sh
. /lib/lsb/init-functions

[ -f /etc/sysconfig/dlm ] && . /etc/sysconfig/dlm

setup() {
	modprobe dlm > /dev/null 2>&1
	if [ ! -d /sys/kernel/config ]; then
		mount -t configfs none /sys/kernel/config > /dev/null 2>&1
	fi
}

do_start() {
	breakpoint="$1"

	[ -x $exec ] || exit 5

	if [ ! -d /var/run/$progdir ]; then
		mkdir -p /var/run/$progdir
	fi

	setup

	[ "$breakpoint" = "setup" ] && exit 0

	start-stop-daemon --start --quiet --exec $exec -- $DAEMON_OPTS || status="$?"
	status=$?
	[ $status -eq 0 ]
	return $status
}

do_stop() {
        start-stop-daemon --stop --quiet --name dlm_controld
	status=$?
	return $status
}

wait_for_stop() {
	while [ -e $lockfile ]; do
		sleep .5
	done
}

case "$1" in
	start)
		# start the dlm control daemon
		log_daemon_msg "Starting $DESC" "$NAME"
		do_start $2
                status=$?
                case "$status" in
			0) log_end_msg 0 ;;
			1) log_action_msg " already running" ; log_end_msg 0 ;;
			*) log_end_msg 1 ;;
		esac
		exit $status
	;;
	stop)
		# stop the dlm control daemon
		log_daemon_msg "Stopping $DESC" "$NAME"
		do_stop
		status=$?
		case "$status" in
			0) log_end_msg 0 ;;
			1) log_action_msg " already stopped" ; log_end_msg 0 ;;
			*) log_end_msg 1 ;;
		esac
		exit $status
	;;
	restart|force-reload)
		$0 stop
		wait_for_stop
		$0 start
	;;
	reload)
		$0 status
		status=$?
		if [ ! $status -eq 0 ]; then
			exit 7
		fi
		$0 restart
	;;
	condrestart|try-restart)
		$0 status
		status=$?
		[ $status -eq 0 ] || exit 0
		$0 restart
	;;
	status)
		pid=$( pidof $exec )
		if [ -n "$pid" ]; then
			log_action_msg "$DESC is running"
		else
			log_action_msg "$DESC is not running"
			exit 3
		fi
		exit 0
	;;
	*)
		echo $"Usage $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
		exit 2
esac
exit $?

