#!/bin/bash

# /sys/kernel/debug/vgaswitcheroo/switch -->
#   DIS - immediate change to discrete
#   IGD - immediate change to IGD
#   DDIS - delayed change to discrete
#   DIGD - delayed change to IGD
#   ON - turn on not in use
#   OFF - turn off not in use


function help()
{
	echo "Commands:
--help            print help text
--autoset-xorg    set correct xorg confs and opengl
--discrete        switch to powerful discrete graphics card
--internal        switch to powersaving internal adapter
--discrete-now    switch to powerful discrete graphics card immediately
--internal-now    switch to powersaving internal adapter immediately
--off             power off the unused cards
--on              power on all cards
--gui             show a kdialog to switch the card
"
}


function gui()
{
	answer=`su $user -c "kdialog --title 'Switch graphics adapter' \
		--combobox 'Choose Action:' Discrete Internal Off \
		--geometry 400x100"`
	case $answer in
		Discrete)
			switch_discrete
			;;
		Internal)
			switch_internal
			;;
		Off)
			switch_off
			;;
	esac
}

function kde_logout()
{
	su $user -c "dbus-send --session \
		--dest=org.kde.ksmserver \
		--type=method_call \
		/KSMServer org.kde.KSMServerInterface.logout int32:1 int32:0 int32:0"
}


function set_xorg_conf()
{
	fn="/etc/X11/xorg.conf.$1"
	if [ -f "$fn" ]; then
		cat "$fn" "/etc/X11/xorg.conf.common"  > /etc/X11/xorg.conf
	else
		echo "Could not find /etc/X11/$1"
		exit -1
	fi
}

function setgl()
{
        if [ -z "`eselect opengl list | grep \* | grep $1`" ]
        then
                echo "  Changing opengl interface to $1..."
                eselect opengl set $1 > /dev/null
        fi
}

function vga_switch_cmd()
{
	if [ -e /sys/kernel/debug/vgaswitcheroo/switch ]; then
		echo "$1" > /sys/kernel/debug/vgaswitcheroo/switch
	fi
}

function switch_discrete()
{
	logger "Will switch to discrete graphics..."
	set_xorg_conf radeon

	#Check whether fglrx is machted...
	if [ -z "`cat /etc/X11/xorg.conf | sed "/^\s*#/d" | grep fglrx`" ]; then
		setgl xorg-x11
	else
		setgl ati
	fi
	
	vga_switch_cmd "DDIS"
}


function switch_discrete_now()
{
	logger "Will switch to discrete graphics immediately..."
	set_xorg_conf radeon

	#Check whether fglrx is machted...
	if [ -z "`cat /etc/X11/xorg.conf | sed "/^\s*#/d" | grep fglrx`" ]; then
		setgl xorg-x11
	else
		setgl ati
	fi

	vga_switch_cmd "DIS"
}


function switch_internal()
{
	logger "Will switch to internal graphics..."
	set_xorg_conf intel
	setgl xorg-x11
	vga_switch_cmd "DIGD"
}

function switch_internal_now()
{
	logger "Will switch to internal graphics immediately..."
	set_xorg_conf intel
	setgl xorg-x11
	vga_switch_cmd "IGD"
}


function autoset_xorg()
{
	if [ -n "`lspci | grep VGA | grep Intel`" ]
	then
		set_xorg_conf intel
	else
		set_xorg_conf radeon

		#Check whether fglrx is machted...
		if [ -z "`cat /etc/X11/xorg.conf | sed "/^\s*#/d" | grep fglrx`" ]; then
			setgl xorg-x11
		else
			setgl ati
		fi
	fi
}

function switch_off()
{
	vga_switch_cmd "OFF"
}

function switch_on()
{
	vga_switch_cmd "ON"
}



if [[ $# -lt "1" ]]; then	
	help
	echo "Current state:"
	cat /sys/kernel/debug/vgaswitcheroo/switch
	exit
fi



. /usr/local/bin/getXenv.inc

case "$1" in
	--discrete)
		switch_discrete
		;;
	--internal)
		switch_internal
		;;
	--discrete-now)
		switch_discrete_now
		;;
	--internal-now)
		switch_internal_now
		;;
	--autoset-xorg)
		autoset_xorg
		;;
	--off)
		switch_off
		;;
	--on)
		switch_on
		;;
	--gui)
		gui
		;;
	*)
		help
		;;
esac


