#!/bin/sh
#
# hdapsd		Starts the hdapsd daemon
#
# chkconfig:	345 44 56
# description:	protect harddriver
# processname:	hdapsd
#

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

HDAPS_DEVICE=""
HDAPS_SENSITIVITY=""

# Configuration file.
[ -f /etc/sysconfig/hdapsd ] && . /etc/sysconfig/hdapsd

start() {
	# Start daemons.
	if [ -f /var/lock/subsys/hdapsd ]; then
		msg_already_running hdapsd
		return
	fi

	msg_starting hdapsd
  	# check whether kernel is hdaps_protect enabled
	if [ ! -f /sys/devices/platform/hdaps/position ]; then
		# it is not, try to load hdaps module and see if that helps
		_modprobe hdaps
		if [ ! -f /sys/devices/platform/hdaps/position ]; then
			fail
			exit 1
		fi;
	fi;
	
	daemon hdapsd ${HDAPS_DEVICE:+-d ${HDAPS_DEVICE}} ${HDAPS_SENSITIVITY:+-s ${HDAPS_SENSITIVITY}} ${HDAPS_OPTIONS} -b -l
	RETVAL=$?
	[ $RETVAL -eq 0 ] && touch /var/lock/subsys/hdapsd
}

stop() {
	# Stop daemons.
	if [ ! -f /var/lock/subsys/hdapsd ]; then
		msg_not_running hdapsd
		return
	fi

	msg_stopping hdapsd
	killproc hdapsd
	rm -f /var/lock/subsys/hdapsd >/dev/null 2>&1
}

condrestart() {
	if [ -f /var/lock/subsys/hdapsd ]; then
		stop
		start
	else
		msg_not_running hdapsd
		RETVAL=$1
	fi
}

RETVAL=0
# See how we were called.
case "$1" in
  start)
  	start
	;;
  stop)
  	stop
	;;
  restart)
	stop
	start
	;;
  try-restart)
	condrestart 0
	;;
  force-reload)
	condrestart 7
	;;
  status)
	status hdapsd
	exit $?
	;;
  *)
	msg_usage "$0 {start|stop|restart|try-restart|force-reload|status}"
	exit 3
esac

exit $RETVAL
