#!/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 INIT INFO
# Provides: sgdisk
# Required-Start: $local_fs
# Required-Stop: $local_fs
# Default-Start:  3 5
# Default-Stop:   0 1 2 6
# Short-Description: sgdiskmon daemon
# Description: sgdisk is used to start/stop the sgdiskmon software raid1 daemon
### END INIT INFO

if [ -f /etc/SuSE-release ] || fgrep -siq suse /etc/os-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")
	echo -n "Starting sgdiskmon "
	# 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"
	    rc_status -v
	    rc_exit
	fi
	sgdiskmon -b 
	rc_status -v
	;;
"stop")
	echo -n "Stopping sgdiskmon "
	rpid=`ps -ef |grep sgdiskmon |grep -v grep |awk '{print $2}'`
	if [ "${rpid}" != "" ]
	then
           echo -n "(pid $rpid) "
	   kill $rpid
	fi
	rc_status -v
	;;
"status")
	echo -n "Checking for sgdiskmon "
	rpid=`ps -ef |grep sgdiskmon |grep -v grep |awk '{print $2}'`
	if [ "${rpid}" != "" ]
	then
	  echo "(pid $rpid)"
  	else
      rc_failed 3
	fi
	rc_status -v
	;;
*)
        echo "Usage: $0 start|stop"
	exit 1
	;;
esac

rc_exit
