#!/bin/sh

get_next()
{
case "$1" in
    normal)
	current_state="left"
    ;;
    left)
	current_state="inverted"
    ;;
    inverted)
	current_state="right"
    ;;
    right)
	current_state="normal"
    ;;
    *)
	current_state="normal"
    ;;
esac
}

update_rgba()
{
case "$1" in 
    rgb)
    	gconftool-2 --set "/desktop/gnome/font_rendering/rgba_order" --type "string" "rgb" 
	echo "Xft.rgba:rgb" | xrdb -merge
    ;;
    bgr)
    	gconftool-2 --set "/desktop/gnome/font_rendering/rgba_order" --type "string" "bgr" 
	echo "Xft.rgba:bgr" | xrdb -merge
    ;;
    vrgb)
    	gconftool-2 --set "/desktop/gnome/font_rendering/rgba_order" --type "string" "vrgb" 
	echo "Xft.rgba:vrgb" | xrdb -merge
    ;;
    vbgr)
    	gconftool-2 --set "/desktop/gnome/font_rendering/rgba_order" --type "string" "vbgr" 
	echo "Xft.rgba:vbgr" | xrdb -merge
    ;;
    *)
    	gconftool-2 --set "/desktop/gnome/font_rendering/rgba_order" --type "string" "rgb" 
	echo "Xft.rgba:rgb" | xrdb -merge
    ;;
esac
# xrdb -q | grep Xft
}

current_state=`echo $1 | egrep -o "(normal|left|inverted|right)"`

#if no parameters rotate in cycle
if [ -z $1 ] ; then
 get_next `xrandr --verbose | grep 'LCD' | egrep -o  '\) (normal|left|inverted|right) \(' | egrep -o "(normal|left|inverted|right)"` 
fi

#real=`ls -l --color=never /dev/input/wacom | grep -o "[[:alpha:][:alnum:]]*$"`
#device="/dev/input/$real"
#device="stylus"

case "$current_state" in 
    left)
	#echo "to left"
	xrandr --orientation left
	xsetwacom set stylus rotate CCW
	xsetwacom set touch rotate CCW 
	#update_rgba "vrgb"
    ;;
    inverted)
	#echo "inverted"
	xrandr --orientation inverted
	xsetwacom set stylus rotate HALF 
	xsetwacom set touch rotate HALF
	update_rgba "bgr"
    ;;
    right)
	#echo "right"
	xrandr --orientation right
	xsetwacom set stylus rotate  CW
	xsetwacom set touch rotate CW  
	#update_rgba "vbgr"
    ;;
    normal)
	#echo "normal"
	xrandr --orientation normal
	xsetwacom set stylus rotate NONE
	xsetwacom set touch rotate NONE
	update_rgba "rgb"
    ;;
esac
