#!/bin/bash
#
# For RedHat and MontaVista
# chkconfig: 345 94 14 
# processname: sgdiskmon
# description: sgdisk is used to start/stop the sgdiskmon software raid1 daemon
#
### BEGIN SLES INIT INFO
# Provides: sgdisk
# Required-Start: $local_fs
# Default-Start:  3 4 5
# Default-Stop:   0 1 2 6
# Description: sgdisk is used to start/stop the sgdiskmon software raid1 daemon
### END INIT INFO

if [ -f /etc/SuSE-release ]; then
        osver=suse
elif [ -f /etc/redhat-release ]; then
        osver=redhat
elif [ -f /etc/mvl-release ]; then
        osver=mvl
else
        osver=unknown
fi

# Dont need functions any more
#if [ $osver = suse ]
#then
#	. /etc/rc.status
#else
#	. /etc/init.d/functions
#fi

case "$1" in 
"start")
	# Is sg module loaded?
	lsmod | grep sg >/dev/null
        if [ $? -ne 0 ]
	then
	   modprobe sg
	fi
	# Is it already started?
	rpid=`ps -ef |grep sgdiskmon |grep -v grep |awk '{print $2}'`
	if [ "${rpid}" != "" ]
	then
	    echo "sgdiskmon is already started"
	    exit 1
	fi
	sgdiskmon -b 
	touch /var/lock/subsys/sgdiskmon 
	;;
"stop")
	rpid=`ps -ef |grep sgdiskmon |grep -v grep |awk '{print $2}'`
	if [ "${rpid}" != "" ]
	then
	   kill $rpid
	fi
	rm -f /var/lock/subsys/sgdiskmon 
	;;
*)
        echo "Usage: $0 start|stop"
	exit 1
	;;
esac

exit 0
