#! /bin/sh
# Init script for powernowd
#
### BEGIN INIT INFO
# Provides:          powernowd
# Required-Start:    $syslog $local_fs $time
# Required-Stop:     $syslog $local_fs
# Default-Start:     2 3 4 5
# Default-Stop:	     0 1 6
# Short-Description: Start powernowd .
### END INIT INFO


PATH=/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/powernowd
NAME=powernowd
DESC=powernowd
OPTIONS="-q"

test -x $DAEMON || exit 0

# modify the file /etc/default/powernowd if you want to add personal options

[ -f /etc/default/$NAME ] && . /etc/default/$NAME

# Get lsb functions
. /lib/lsb/init-functions
. /etc/default/rcS

if [ "x$VERBOSE" = "xno" ]; then
        MODPROBE_OPTIONS="$MODPROBE_OPTIONS -Q"
        export MODPROBE_OPTIONS
fi

set -e

load_modules() {
        #stop the kernel printk'ing at all while we load.
        PRINTK=`cat /proc/sys/kernel/printk`
        [ "$VERBOSE" = no ] && echo "1 1 1 1" > /proc/sys/kernel/printk

        #build a list of current modules so we don't load a module twice
        LIST=`/sbin/lsmod|awk '!/Module/ {print $1}'`
        
        #get list of available modules
        LOC="/lib/modules/`uname -r`/kernel/drivers/cpufreq"
        if [ -d $LOC ]; then
          MODAVAIL=`( find $LOC -type f -name "*.o" -printf "basename %f .o\n"; \
                   find $LOC -type f -name "*.ko" -printf "basename %f .ko\n" ) | /bin/sh`
        else
          MODAVAIL=""
        fi

        
        #echo "Loading cpufreq modules:"
        for mod in $MODAVAIL; do
        #        echo "     $mod"
                echo $LIST| grep -q -w "$mod" || modprobe $mod >/dev/null || /bin/true
        done
        
        #cpufreq is built in on powerpc; just return
        if [ "`uname -m`" = "ppc" ]; then
                echo "$PRINTK" > /proc/sys/kernel/printk
                return 0
        fi


        #new style detection system
        if [ ! "$FREQDRIVER" = "" ]; then 
                modprobe "$FREQDRIVER"
        else
	        . /usr/share/powernowd/cpufreq-detect.sh
                [ ! -z "$MODULE" ] && (modprobe "$MODULE"||modprobe "$MODULE_FALLBACK")
        fi

        if [ "$USE_OLD_DETECT" = "fish" ]; then
        # now lets load the driver
        if [ ! $FREQDRIVER = "" ]; then 
                modprobe $FREQDRIVER||true
        fi
        if [ "`uname -m`" = "x86_64" ]; then 
                modprobe powernow-k8 >/dev/null 2>&1||true
        fi

        if [ ! -f /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor ]; then
                modprobe acpi > /dev/null 2>&1|| true
        fi
        fi
        echo "$PRINTK" > /proc/sys/kernel/printk
}

use_ondemand() {
	if [ "$OPTIONS" != "-q" ]; then
	    return 1
	fi
	status=1  # return error, if no cpu dirs are found
	for x in /sys/devices/system/cpu/cpu[0-9]*/; do
	    if [ ! -d $x ] || [ ! -f $x"cpufreq/scaling_governor" ]; then
	        continue
	    fi
	    echo -n ondemand > $x"cpufreq/scaling_governor"
	    status=$?
	    if [ $status != 0 ]; then
		return $status
	    fi
	    # The default behaviour of powernowd is to ignore nice load:
	    if [ -f $x"cpufreq/ondemand/ignore_nice_load" ]; then
	        echo -n 1 > $x"cpufreq/ondemand/ignore_nice_load"
	    fi
	done
	return $status
}

check_kernel() {
	CPUFREQ=/sys/devices/system/cpu/cpu0/cpufreq

	if [ -f "$CPUFREQ/scaling_governor" ] && \
		[ -f "$CPUFREQ/scaling_available_governors" ] && \
		grep -q userspace "$CPUFREQ/scaling_available_governors"
	then
		return 0
	else
		return 1
	fi
}

start() {
       	log_begin_msg "Starting $DESC... "
	if use_ondemand
	then
	    log_end_msg 0
	    return 0
	fi
	if check_kernel
	then
	#       echo "Starting $DESC: "
		start-stop-daemon --start --quiet --oknodo --exec $DAEMON -- $OPTIONS >/dev/null 2>&1 || {
                    status=$?
                    log_end_msg $status
                    return $status
                }
	else
		log_action_cont_msg "CPU frequency scaling not supported"
	#	echo "required sysfs objects not found!"
	#	echo -e "\tRead /usr/share/doc/powernowd/README.Debian for more information."
	fi
       	log_end_msg 0
	return 0
}

case "$1" in
  start)
        [ "x$DO_MODULES" = "xyes" -a -f /proc/modules ] && load_modules
	[ "x$DO_MODULES" = "xyes" ] || start
	;;
  stop)
	log_begin_msg "Stopping $DESC: "
	start-stop-daemon --stop --quiet --oknodo --exec $DAEMON
	for x in /sys/devices/system/cpu/cpu[0-9]*/; do
	    if [ -f $x"cpufreq/scaling_governor" ]; then
		echo -n performance > $x"cpufreq/scaling_governor";
	    fi
	done
	log_end_msg $?
	;;
  restart|force-reload)
        $0 stop
	sleep 1
	$0 start
	#echo "$NAME."
	;;
  *)
	N=/etc/init.d/$NAME
	log_success_msg "Usage: $N {start|stop|restart|force-reload}" >&2
	exit 1
	;;
esac

exit 0
