#!/bin/sh
#
# Load kernel modules needed to enable cpufreq scaling
#
# chkconfig: 345 12 90
# description: Save power when idling

# Do not load RH compatibility interface.
WITHOUT_RC_COMPAT=1

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

LOCKFILE=/var/lock/subsys/cpufreq-simple
RETVAL=0

MODPROBE="modprobe -qb"
MODULE=
EXTRA_MODULES=

SourceIfNotEmpty /etc/sysconfig/cpufreq-simple

CPUFREQ=/sys/devices/system/cpu/cpu0/cpufreq

is_loaded()
{
	[ -f "$CPUFREQ/scaling_governor" -a -f "$CPUFREQ/scaling_available_governors" ] &&
		return 0 || return 1
}

start()
{
	echo -n "Loading cpufreq module: "
	[ -n "$MODULE" ] || MODULE="$(detect-cpufreq-module)"

	if [ -z "$MODULE" ] || ! $MODPROBE "$MODULE"; then
		echo_failure
		echo
		return 1
	fi

	[ -z "$EXTRA_MODULES" ] || $MODPROBE $EXTRA_MODULES

	is_loaded && echo_success || echo_failure
	RETVAL=$?
	echo

	if [ $RETVAL -eq 0 ]; then
		action "Tune up cpufreq: " cpufreq-simple
		RETVAL="$?"
	fi

	return $RETVAL
}

# See how we were called.
case "$1" in
	start)
		start
		;;
	stop)
		# Nothing to do
		;;
	reload)
		start
		;;
	restart)
		start
		;;
	condstop)
		# Nothing to do
		;;
	condrestart)
		# Nothing to do
		;;
	condreload)
		# Nothing to do
		;;
	status)
		is_loaded && echo "cpufreq is enabled" ||
			echo "cpufreq is disabled"
		;;
	*)
		msg_usage "${0##*/} {start|stop|reload|restart|condstop|condrestart|condreload|status}"
		RETVAL=1
esac

exit $RETVAL
