#!/bin/sh
#
# sensors:	Hardware sensors monitoring daemon
#
# Version:	$Revision: 1.14 $
#
# chkconfig:	2345 32 68
# description:	Starts and stops hardware sensors monitoring daemon.
# author:	Filip Kalinski <filon@pld.org.pl>

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

# Get service config
if [ -f /etc/sysconfig/sensors ]; then
	. /etc/sysconfig/sensors
fi

RETVAL=0
# See how we were called.
case "$1" in
  start)
	# Check if the service is already running?
	if [ ! -f /var/lock/subsys/sensors ]; then
		if [ -z "$BUS" ] || [ -z "$CHIP" ]; then
			msg_starting "sensors daemon"; fail
			nls "BUS or CHIP not configured in /etc/sysconfig/sensors" >&2
			exit 6
		fi

		# insert modules
		for i in $BUS; do
			_modprobe single die "i2c-$i"
		done
		for i in $CHIP; do
			_modprobe single die "$i"
		done

		if [ "$SENSORS_SET" = "1" ]; then
			sensors -s
		fi

		msg_starting "sensors daemon"
		daemon sensord \
			$([ -n "$INTERVAL" ] && echo "-i $INTERVAL") \
			$([ -n "$LOG_INTERVAL" ] && echo "-l $LOG_INTERVAL") \
			$([ -n "$RRD_LOGFILE" ] && echo "-r $RRD_LOGFILE") \
			$([ -n "$RRD_INTERVAL" ] && echo "-t $RRD_INTERVAL") \
			-f daemon

		RETVAL=$?
		[ $RETVAL -eq 0 ] && touch /var/lock/subsys/sensors
		touch /var/lock/subsys/sensors
	else
		msg_already_running "sensors daemon"
	fi
	;;
  stop)
	if [ -f /var/lock/subsys/sensors ]; then
		msg_stopping "sensors daemon"
		killproc sensord

		# remove kernel modules
		for i in $CHIP; do
			/sbin/rmmod $i >/dev/null 2>&1
		done
		for i in $BUS; do
			/sbin/rmmod i2c-$BUS >/dev/null 2>&1
		done

		rm -f /var/lock/subsys/sensors
	else
		msg_not_running "sensors daemon"
	fi
	;;
  status)
	status sensors sensord
	RETVAL=$?
	sensors
	;;
  restart|force-reload)
	$0 stop
	$0 start
	exit $?
	;;
  *)
	msg_usage "$0 {start|stop|restart|force-reload|status}"
	exit 3
esac

exit $RETVAL
