#!/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., 51 Franklin Street, Fifth Floor, Boston,
#    MA 02110-1301 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 this file is a shell script that simply defines variables:
# HWMON_MODULES for hardware monitoring driver modules, and optionally
# BUS_MODULES for any required bus driver module (for example for I2C or SPI).


PSENSORS=/usr/bin/sensors
FANCONFIG=/etc/fancontrol
PFAN=/usr/sbin/fancontrol

if [ ! -x $PSENSORS ]; then
	PSENSORS=/usr/bin/sensors
fi

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

# This functions checks if sensor support is compiled into the kernel, if
# sensors are configured, and loads the config file
check_sensors() {
	CONFIG=/etc/sysconfig/lm_sensors
	if ! [ -r "$CONFIG" ] || ! grep '^HWMON_MODULES' $CONFIG >/dev/null 2>&1; then
		echo -n "$1 $prog: not configured, run sensors-detect"
		echo_warning
		echo
		exit 6
	fi

	# Load config file
	. "$CONFIG"
}

start() {
	echo -n "Starting up sensors"

	check_sensors "Starting"

	for module in $BUS_MODULES $HWMON_MODULES ; do
		/sbin/modprobe $module >/dev/null 2>&1
		rc_status
	done
	rc_status

	$PSENSORS -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/lm_sensors
}

stop() {
	echo -n "Shutting down sensors"

	check_sensors "Stopping"

	# 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 module in $HWMON_MODULES $BUS_MODULES ; do
		/sbin/modprobe -r $module >/dev/null 2>&1
		rc_status
	done

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

dostatus() {
	$PSENSORS
	rc_status
}

restart() {
	stop
	start
	rc_status
}

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

# Reset the 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: $0 {start|stop|status|restart|reload|condrestart}"
	exit 3
esac

rc_exit
