#!/bin/bash
# chkconfig: 345 80 30
#
# description: Daemon to automatically start the bwpci.ko driver
# processname: bwpcied
# Author:Derek Stark
#
### BEGIN INIT INFO
# Provides:            bwpcied
# Required-Start:      $local_fs $remote_fs $syslog
# Required-Stop:       $remote_fs $syslog
# Should-Start:        
# Should-Stop:         
# Default-Start:       345 80 30
# Default-Stop:        0 1 6
# Short-Description:   Bittware daemon
# Description:         Bittware daemon
### END INIT INFO

# Source function library.

if [ -e /etc/rc.d/init.d/functions ]
then
    . /etc/rc.d/init.d/functions
fi

RETVAL=0
VER=$(uname -r)

prog="bwpcied"

start() {
	# Start driver.
	RETVAL=0
	echo $"Starting $prog: "

	# Group: since distributions do it differently, look for wheel or use staff
	if grep -q '^staff:' /etc/group; then
	    group="staff"
	else
	    group="wheel"
	fi

	# invoke insmod with all arguments we got
	# and use a pathname, as insmod doesn't look in . by default
        if [ -e /etc/lsb-release ]; then
            # Ubuntu DKMS location
	    insmod /lib/modules/${VER}/updates/dkms/bwpci.ko
        else
            if [ -e /lib/modules/${VER}/extra/bwpci.ko.xz ]; then
	        insmod /lib/modules/${VER}/extra/bwpci.ko.xz
            else
	        insmod /lib/modules/${VER}/kernel/drivers/misc/bwpci.ko
            fi
        fi

	RETVAL=$?
#	action "" [ $RETVAL = 0 ] && touch /var/lock/subsys/bwpcied
	touch /var/lock/subsys/bwpcied
	return $RETVAL
}

stop() {
	# Stop driver.
	RETVAL=0
	echo $"Shutting down $prog: "
        rmmod -s bwpci
	
	RETVAL=$?
#	action "" [ $RETVAL = 0 ] && rm -rf /var/lock/subsys/bwpcied
	rm -rf /var/lock/subsys/bwpcied
	return $RETVAL
}

# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  restart)
	stop
	start
	RETVAL=$?
	;;
  status)
	status bwpcied
	RETVAL=$?
	;;
  *)
	echo $"Usage: $0 {start|stop|restart|status}"
	exit 1
esac

exit $RETVAL

