#/bin/sh
#
# Simple program for controlling the CPU turbo state in pstate
# governed Intel CPUs. Call program to switch or specify on or off.

TURBO_CTRLFN="/sys/devices/system/cpu/intel_pstate/no_turbo"

if [ "$1" == "-?" ]; then
	echo "Usage:"
	echo -e "\t$0 [on|off][-?]"
	exit 0
fi

if [ -f $TURBO_CTRLFN ]; then
	TURBO_STATUS=$(cat $TURBO_CTRLFN)

    if [ ! -w $TURBO_CTRLFN ]; then
        echo "Insufficient permissions to control the CPU."
        exit 1
    fi
else
	echo "Couldn't find a Intel pstate turbo switch in this system."
	exit 1
fi

if [ "$1" == "on" ]; then
	if [ $TURBO_STATUS -eq 0 ]; then
		echo "CPU turbo already enabled."
		exit 0
	fi
elif [ "$1" == "off" ]; then
	if [ $TURBO_STATUS -eq 1 ]; then
		echo "CPU turbo already disabled."
		exit 0
	fi
fi

if [ $TURBO_STATUS -eq 1 ]; then
	echo 0 > $TURBO_CTRLFN
	echo "Enabled CPU turbo."
	exit 0
elif [ $TURBO_STATUS -eq 0 ]; then
	echo 1 > $TURBO_CTRLFN
	echo "Disabled CPU turbo."
	exit 0
fi

exit 1