#!/bin/sh
#
# chkconfig: 2345 26 74
# description: sensors is used for monitoring motherboard sensor values.
# config: /etc/sysconfig/sensors
#
#    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/sensors that contains the modules to
# be loaded/unloaded. That file is sourced into this one.

# The format of that file is a shell script which can set the following
# variables to configure lm_sensors:
#    - MODULE_0, MODULE_1, MODULE_2, etc.: name of modules to load (there must
#      be no gaps in the numbering).
#    - SET_LIMITS: if set to "no", disables running "sensors -s" at startup.

# Do not load RH compatibility interface.
WITHOUT_RC_COMPAT=1

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

LOCKFILE=/var/lock/subsys/sensors
CONFIG=/etc/sysconfig/lm_sensors
PSENSORS=/usr/bin/sensors

# Get config.
SourceIfNotEmpty "$CONFIG" &&
	! is_no "$ENABLE" &&
	[ -x "$PSENSORS" ] ||
	exit 0

RETVAL=0

kernel_2_6()
{
	local major
	local minor
	major="`uname -r | cut -d. -f1`"
	minor="`uname -r | cut -d. -f2`"
	[ "$major" -gt 2 ] && return 0
	[ "$major" -eq 2 ] && [ "$minor" -ge 6 ] && return 0
	return 1
}

show_result()
{
	if [ "$1" -eq 0 ]; then
		echo_success
	else
		echo_failure
	fi
	echo
}

start() {
	echo -n "Loading sensor modules: "

	RETVAL=0
	if ! kernel_2_6; then
		echo -n "i2c-proc "
		/sbin/modprobe i2c-proc &>/dev/null
		RETVAL=$?
	fi

	i=0
	modules_missing=
	while [ $RETVAL -eq 0 ]; do
		module="`eval echo '$'MODULE_$i`"
		[ -n "$module" ] || break

		# Ignore modules which do not exist (not all sensor modules
		# have been ported to 2.6)
		if /sbin/modprobe -nq $module; then
			echo -n "$module "
			/sbin/modprobe $module &>/dev/null
			RETVAL=$?
		else
			echo -n "$module(not found) "
			modules_missing=1
		fi

		i=$(($i + 1))
	done

	if [ $RETVAL -eq 0 ]; then
		if [ -n "$modules_missing" ]; then
			echo_passed
		else
			echo_success
		fi
	else
		echo_failure
	fi
	echo

	if [ $RETVAL -eq 0 ]; then
		if ! is_no "$SET_LIMITS"; then
			action "Setting sensor parameters:" $PSENSORS -s
			RETVAL=$?
		fi
	fi

	if [ $RETVAL -eq 0 ]; then
		touch "$LOCKFILE"
	fi
}

unload_modules()
{
	local module
	local result
	module="`eval echo '$'MODULE_$1`"
	[ -n "$module" ] || return 0
	unload_modules $(($1 + 1))
	result=$?
	if /sbin/modprobe -nq $module; then
		echo -n "$module "
		/sbin/modprobe -r $module &>/dev/null || return $?
	fi
	return $result
}

stop() {
	echo -n "Unloading sensor modules: "

	unload_modules 0
	RETVAL=$?

	if ! kernel_2_6; then
		echo -n "i2c-proc "
		/sbin/modprobe -r i2c-proc &>/dev/null || RETVAL=$?
	fi

	if [ $RETVAL -eq 0 ]; then
		rm -f "$LOCKFILE"
	fi

	show_result $RETVAL
}

dostatus() {
	$PSENSORS
	RETVAL=$?
}

restart() {
	stop
	start
	RETVAL=$?
}

condstop() {
	[ -e "$LOCKFILE" ] && stop || :
}

condrestart() {
	[ -e "$LOCKFILE" ] && restart || :
}

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

exit $RETVAL
