#!/bin/bash
#
# chkconfig: 345 22 78
# description: Loads/Unloads dm-cmirror module
#
#	       
### BEGIN INIT INFO
# Provides: 
### END INIT INFO

. /etc/init.d/functions
[ -f /etc/sysconfig/cluster ] && . /etc/sysconfig/cluster

start()
{
	echo -n "Loading clustered mirror log:"

	# If gulm is in ccs, don't start
	# FIXME -- Should this be silent?  I think users should get some
	#          feedback, but others might not want added verbosity to
	#          the boot process.  Oh well... it's only one line :)
	if ! [ -r /etc/cluster/cluster.conf ]
	then
		# TODO -- cman can start w/out cluster.conf file.  This 
		#	  should not stop cman from starting up.
		initlog -n ${0##*/} -s "/etc/cluster/cluster.conf was not detected"
	elif grep -qE "<[[:space:]]*gulm([[:space:]]|[>]|$)" \
		/etc/cluster/cluster.conf 
	then
		warning "<gulm> section detected in /etc/cluster/cluster.conf."
		warning "Cluster mirrors only available to CMAN clusters."
		echo
		exit 0
	fi

	if ! [ -e /proc/cluster/status ]; then
	  	failure "cman has not been properly started."
		echo
		return 0
	fi

	rtrn=1

	modprobe dm-cmirror
	rtrn=$?
	
	if [ $rtrn -eq 0 ]; then
		success "startup"
	else
		failure "startup"
	fi
		
	# need the extra echo to properly terminate the line
	echo
	return $rtrn
}

stop()
{
	echo -n "Unloading clustered mirror log:"

	if grep cmirror /proc/modules >& /dev/null; then
	    rmmod dm-cmirror
	    rtrn=$?
	else
	    success "shutdown"
	    echo
	    return 1
	fi
	    
	
	if [ $rtrn -eq 0 ]; then
	    success "shutdown"
	else
	    failure "shutdown"
	fi

	# need the extra echo to properly terminate the line
	echo
	return $rtrn
}

cmirror_status()
{
	modprobe dm-cmirror
	if [ $? -ne 0 ]; then
		echo "Cluster log module is not loaded.  (Cluster mirrors will not work.)"
		return 1
	fi

	return 0
}

rtrn=1

# See how we were called.
case "$1" in
  start)
	start
	rtrn=$?
	;;

  stop)
	stop
	rtrn=$?
	;;

  restart)
	$0 stop
	$0 start 
	rtrn=$?
	;;

  status)
        cmirror_status
        rtrn=$?
        if [ $rtrn -eq 0 ]; then
                echo "cmirror is running."
        fi
        ;;

  *)
	echo $"Usage: $0 {start|stop|restart|status}"
	;;
esac

exit $rtrn
