#! /bin/sh
#
# Script to change do system side things for studio-controls
#
#
GOVERNOR="ondemand"
NO_TURBO="0"

set_governors() {
	# do this here in case we hit exit 0 before the end
	echo "NO_TURBO=$NO_TURBO\n" > /etc/default/studio
	echo "GOVERNOR=${GOVERNOR}\n" >> /etc/default/studio
	AVAILABLE="/sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors"
	[ -f $AVAILABLE ] || exit 0
	read governors < $AVAILABLE
	case "$GOVERNOR" in
		performance)
			case $governors in
				*performance*)
					GOVERNOR="performance"
					;;
				*)
					exit 0
					;;
			esac
			;;
		*)
			case $governors in
				*ondemand*)
					GOVERNOR="ondemand"
					;;
				*powersave*)
					GOVERNOR="powersave"
					;;
				*)
					exit 0
					;;
			esac
			;;
	esac

	for CPUFREQ in /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor
		do
			[ -f $CPUFREQ ] || continue
			echo -n $GOVERNOR > $CPUFREQ
		done
	#reswrite just in case we corrected something
	echo "NO_TURBO=$NO_TURBO\n" > /etc/default/studio
	echo "GOVERNOR=${GOVERNOR}\n" >> /etc/default/studio

}

# any setting in this file will override the above defaults.
# studio-controls will normally write the defaults file
if [ -f /etc/default/studio ] ; then
	. /etc/default/studio
fi

case "$1" in
	performance)
		GOVERNOR="performance"
		set_governors
		;;

	ondemand)
		GOVERNOR="ondemand"
		set_governors
		;;

	powersave)
		GOVERNOR="powersave"
		set_governors
		;;

	fix)
		if [ -f /etc/security/limits.d/audio.conf.disabled ]; then
			mv /etc/security/limits.d/audio.conf.disabled /etc/security/limits.d/audio.conf
		elif ! [ -f /etc/security/limits.d/audio.conf ]; then
			cp /usr/share/studio-controls/audio.conf /etc/security/limits.d/audio.conf
		fi
		if [ ${PKEXEC_UID} -gt 999 ]; then
			/usr/sbin/usermod `id -nu ${PKEXEC_UID}` -aG audio
		fi
		;;

	boost)
		if [ -f /sys/devices/system/cpu/intel_pstate/no_turbo ]; then
			echo "NO_TURBO=0\n" > /etc/default/studio
			echo "GOVERNOR=${GOVERNOR}" >> /etc/default/studio
			echo "0" | tee /sys/devices/system/cpu/intel_pstate/no_turbo
		fi
		;;

	noboost)
		if [ -f /sys/devices/system/cpu/intel_pstate/no_turbo ]; then
			echo "NO_TURBO=1\n" > /etc/default/studio
			echo "GOVERNOR=${GOVERNOR}" >> /etc/default/studio
			echo "1" | tee /sys/devices/system/cpu/intel_pstate/no_turbo
		fi
		;;


	nosys)
		/etc/init.d/cron stop
		;;

	sys)
		/etc/init.d/cron start
		;;

	ffado)
		if ! [ -f /etc/modprobe.d/blacklist-studio.conf ]; then
			cp /usr/share/studio-controls/blacklist-studio.conf /etc/modprobe.d/blacklist-studio.conf
		fi
		;;

	alsa)
		if [ -f /etc/modprobe.d/blacklist-studio.conf ]; then
			rm /etc/modprobe.d/blacklist-studio.conf
		fi
		;;

	*)
		exit 0
		;;

	esac
