#!/bin/sh
#
# vhba: Virtual SCSI host bus adapter used by CDEmu daemon from userspace-cdemu suite
#
# chkconfig:    345 90 10 
# description:  Virtual SCSI host bus adapter used by CDEmu daemon from userspace-cdemu suite

### BEGIN INIT INFO
# Provides:       vhba
# Required-Start:
# Required-Stop:
# Default-Start:  3 4 5
# Default-Stop:   0 1 2 6
# Description:    virtual SCSI host bus adapter used by CDEmu daemon from userspace-cdemu suite
### END INIT INFO

# Source function library.
. /etc/init.d/functions

LOCKFILE=/var/lock/subsys/vhba
MODULENAME=vhba
RETVAL=0

start()
{
	action "Loading kernel module ($MODULENAME):" modprobe $MODULENAME
	RETVAL=$?
	[ $RETVAL = 0 ] && touch "$LOCKFILE" ||:
	return $RETVAL
}

stop()
{
	CDEMUDAEMON=`ps ax | grep 'cdemu-daemon' | grep -v 'grep' | wc -l`
	if [ $CDEMUDAEMON -ne 0 ]; then
		action "Stop CDemu daemon" /usr/bin/killall cdemu-daemon;
	fi	
	local RETVAL
	action "Unloading kernel module ($MODULENAME):" modprobe -r $MODULENAME; RETVAL=$?
	[ $RETVAL = 0 ] && rm -f "$LOCKFILE" ||:
	return $RETVAL
}

restart()
{
	stop
	start
}

status()
{
	if /sbin/lsmod | grep -qs "^$MODULENAME[[:space:]]"; then
		echo "$MODULENAME module is loaded"
		return 0
	elif [ -f "$LOCKFILE" ]; then
		echo "$MODULENAME module is not loaded, but subsystem is locked"
		return 2
	else
		echo "$MODULENAME service is stopped"
		return 3
	fi
}

# See how we were called.
case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	restart)
		restart
		;;
	condstop)
		if [ -e "$LOCKFILE" ]; then
			stop
		fi
		;;
	condrestart)
		if [ -e "$LOCKFILE" ]; then
			restart
		fi
		;;
	status)
		status
		RETVAL=$?
		;;
	*)
		msg_usage "${0##*/} {start|stop|restart|condstop|condrestart|status}"
		RETVAL=1
esac

exit $RETVAL
 
