#!/bin/sh
#
### BEGIN INIT INFO
# Provides:       sensors
# Required-Start: $remote_fs
# Required-Stop:  $remote_fs
# Default-Start:  2 3 5
# Default-Stop:
# Short-Description: Load and configure hardware monitoring drivers
# Description:    sensors is used for monitoring motherboard sensor values.
#                 Config file is /etc/sysconfig/lm_sensors
### END INIT INFO
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

# See also the lm_sensors homepage at:
#     http://www.lm-sensors.org

# It uses a config file /etc/sysconfig/lm_sensors that contains the modules
# to be loaded/unloaded. That file is sourced into this one.

# The format of that file a shell script that simply defines the modules 
# in order as normal shell variables with the special names:
#    MODULE_1, MODULE_2, MODULE_3, etc.

# If sensors isn't supported by the kernel, try loading the module...
[ -e /sys/bus/i2c/devices ] || /sbin/modprobe i2c-dev &>/dev/null

# Don't bother if /proc/sensors still doesn't exist, kernel doesn't have
# support for sensors.
[ -e /sys/bus/i2c/devices ] || exit 0
  
# If sensors was not already running, unload the module...
[ -e /var/lock/subsys/sensors ] || /sbin/modprobe -r i2c-dev &>/dev/null

CONFIG=/etc/sysconfig/lm_sensors
PSENSORS=/usr/bin/sensors
FANCONFIG=/etc/fancontrol
PFAN=/usr/sbin/fancontrol

# Source function library.
. /etc/rc.status

start() {
	echo -n "Starting up sensors"
	unset ${!MODULE_*}
	test -r "$CONFIG" && . "$CONFIG"

	for i in ${!MODULE_*} ; do
		eval module=\$$i
		/sbin/modprobe $module &>/dev/null
		rc_status
	done
	rc_status

	/usr/bin/sensors -s &> /dev/null
	rc_status

	# Start fan control, if configured
	if test -s "$FANCONFIG" -a -x "$PFAN" ; then
	    echo -n ", starting fan control: "
	    /sbin/startproc -q "$PFAN"
	    rc_status
	else
	    echo -n ": "
	fi

	rc_status -v && touch /var/lock/subsys/sensors
}

stop() {
	echo -n "Shutting down sensors"
	unset ${!MODULE_*}
	test -r "$CONFIG" && . "$CONFIG"

	# Stop fan control, if it was started
	if test -s /var/run/fancontrol.pid ; then
	    echo -n ", stopping fan control: "
	    /sbin/killproc -TERM "$PFAN"
	    rc_status
	else
	    echo -n ": "
	fi

	for i in ${!MODULE_*} ; do
		eval module=\$$i
		/sbin/modprobe -r $module &>/dev/null
		rc_status
	done

	rc_status -v && rm -f /var/lock/subsys/sensors
}

dostatus() {
	$PSENSORS
	rc_status
}

restart() {
	stop
	start
	rc_status
}

condrestart() {
	[ -e /var/lock/subsys/lm_sensors ] && restart || :
}

# Reset status of this service
rc_reset

# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  status)
	dostatus
	;;
  restart|reload)
	restart
	;;
  try-restart)
	condrestart
	;;
  *)
	echo "Usage: lm_sensors {start|stop|status|restart|reload|try-restart}"
	exit 1
esac

rc_exit
