#!/bin/sh
#
# cpufreqd		cpufreq daemon
#
# Author:	Daisuke SUZUKI <daisuke@linux.or.jp>
# chkconfig: 2345 27 73
# description: cpufreqd is used for monitor apm status and change cpu frequency.
# processname: cpufreqd

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

#

SERVER=/usr/sbin/cpufreqd
LOCKD=/var/lock/subsys
PROGNAME=cpufreqd
prog="CPU Frequency daemon"

if !([ -f /proc/cpufreq ] || [ -d /sys/devices/system/cpu/cpu0/cpufreq ]); then
	echo "$prog: no cpufreq interface found. "
	exit 0
fi

if !([ -d /proc/pmu ] || [ -d /proc/acpi ] || [ -f /proc/apm ]); then
	echo "$prog: no supported power management interface found. "
	exit 0
fi

[ -f $SERVER ] || exit 0

start() {
	if [ -d /proc/pmu ]; then
            cp -f /etc/cpufreqd.conf /etc/cpufreqd.conf.orig
            sed 's/^pm_type=.*/pm_type=pmu/' /etc/cpufreqd.conf.orig > /etc/cpufreqd.conf
	elif [ -f /proc/apm ]; then
            cp -f /etc/cpufreqd.conf /etc/cpufreqd.conf.orig
            sed 's/^pm_type=.*/pm_type=apm/' /etc/cpufreqd.conf.orig > /etc/cpufreqd.conf
	elif [ -d /proc/acpi ]; then
            cp -f /etc/cpufreqd.conf /etc/cpufreqd.conf.orig
            sed 's/^pm_type=.*/pm_type=acpi/' /etc/cpufreqd.conf.orig > /etc/cpufreqd.conf
	else
	    exit 0
	fi

	echo -n $"Starting $prog: "
	daemon $SERVER
	echo
	if [ -d $LOCKD ]; then
	    touch $LOCKD/$PROGNAME
	fi
}

stop() {
	echo -n $"Shutting down $prog: "
	killproc $SERVER
	echo
	if [ -d $LOCKD ]; then\
	    rm -f $LOCKD/$PROGNAME
	fi
}

restart() {
	stop
	start
}

case "$1" in
  start)
  	start
	;;
  stop)
  	stop
	;;
  restart)
	restart
	;;
  condrestart)
	if [ -f $LOCKD/$PROGNAME ]; then
		restart ||:
	fi
	;;
  *)
	echo "Usage: cpufreqd {start|stop|restart|condrestart}"
	exit 1
esac

exit 0
