#!/bin/bash
# ACPI script for changing the cpu frequency when the ac adaptor power cord is reconnected or disconnected
# included frequency governor selector
# version 0.1 
# 03 December 2005
# email : duckzlandd@yahoo.com for improvement or comment ; duckz------- 
acpipath=/proc/acpi/ac_adapter/ADP1
freqmax=ondemand # set to maximum governor - performance / ondemand
freqmin=powersave # set to minimum governor - powersave / ondemand / conservative

cat $acpipath/state | grep on-line &> /dev/null

	if [ "$?" = "0" ] 

	then 
		acpi=1 
	else
		acpi=2
	fi

	lsmod | grep $freqmax &> /dev/null

	if [ "$?" = "0" ]
	
	then
		gmax=1
	else
		gmax=2
	fi

	lsmod | grep $freqmin &> /dev/null

	if [ "$?" = "0" ]

	then
		gmin=1
	
	else
		gmin=2
		
	fi


if [ "$acpi" = "1" ] 
	then

	echo "Ac adapter is connected.. running $freqmax frequency governor"
	
		if [ "$gmax" = "1" ] 

		then  
			echo "module cpufreq_$freqmax found"

			/usr/sbin/cpufreq-selector -g $freqmax

			lsmod | grep $freqmin &> /dev/null

				if [ "?" = "0" ] 
				then 	
				modprobe -r cpufreq_$freqmin
				else
				exit $E_NOARGS
				fi

		else

	
			if [ "$gmax" = "2" ]
			then
			echo "module cpufreq_$freqmax not found, loading it first"
			modprobe cpufreq_$freqmax
			/usr/sbin/cpufreq-selector -g $freqmax
			lsmod | grep $freqmin &> /dev/null

				if [ "?" = "0" ]
				then
				modprobe -r cpufreq_$freqmin
				else
				exit $E_NOARGS
				fi	
		fi
fi	
fi

if [ "$acpi" = "2" ]

	then 

	echo "Ac adapter is disconnected.. running $freqmin frequency governor"
		if [ "$gmin" = "1" ] 
			then
			echo "module cpufreq_$freqmin found"
			/usr/sbin/cpufreq-selector -g $freqmin
			lsmod | grep $freqmax &> /dev/null

				if [ "?" = "1" ] 
				then
	 
				modprobe -r cpufreq_$freqmax
	
				else
				exit $E_NOARGS
	 
				fi
	
		else

			if [ "$gmin" = "2" ]
			then
			echo "module cpufreq_$freqmin not found, loading it first"
			modprobe cpufreq_$freqmin
			/usr/sbin/cpufreq-selector -g $freqmin
			lsmod | grep $freqmax


				if [ "$?" = "0" ]
				then
				modprobe -r cpufreq_$freqmax
				else
				exit $E_NOARGS
				fi
			fi
		exit $E_NOARGS
		fi
fi






