#!/bin/bash
# ---------------------------------------------------------------------------
# NAME
#
#     ConfigurePower - performs initial power configuration for the SMC
#
# SYNOPSIS
#
#     sudo ConfigurePower
#
# DESCRIPTION
#
#     Performs the initial power configuration
#
# ---------------------------------------------------------------------------

# exit if not run as root
CURUSER=`whoami`

if [ "$CURUSER" != "root" ]
then
	echo "This script must be run as root "
	exit 1
fi

SYSPOWER=/sys/power
VDD1=$SYSPOWER/sr_vdd1_autocomp
VDD2=$SYSPOWER/sr_vdd2_autocomp
CPUFREQ=/sys/devices/system/cpu/cpu0/cpufreq
AVAILGOV=$CPUFREQ/scaling_available_governors
GOV=$CPUFREQ/scaling_governor
base=`basename $0`
govmode=ondemand

# I'm disabling Smart Reflex (VDD1 and VDD2) as a workaround for SWSMAC-148;
# if the kernel is patched to address the problem of spurrious warning messages
# being generated, we can try re-enabling it again

if [ -f $VDD1 ]
then
	echo 0 > $VDD1
else
	echo "$base: $VDD1 does not exist as a regular file"
fi

if [ -f $VDD2 ]
then
	echo 0 > $VDD2
else
	echo "$base: $VDD2 does not exist as a regular file"
fi

if [ -f $AVAILGOV ]
then
	if [ -f $GOV ]
	then
		cnt=`grep -c $govmode $AVAILGOV`

		if [ "$cnt" -ne 0 ]
		then
			echo $govmode > $GOV
		else
			existing=`cat $GOV`
			echo "$base: $govmode is not an available mode, the existing mode ($existing) remains set"
		fi
	else
		echo "$base: $GOV does not exist as a regular file, so the power governor cannot be configured"
	fi
else
	echo "$base: $AVAILGOV does not exist as a regular file, so the power governor cannot be configured"
fi

