#!/usr/bin/python
#
# This program is under GPLv3 license --
# http://www.gnu.org/licenses/gpl-3.0.html
#
# If you think, that this script is usefull, please send me a postcard to
# adress: 
# Michal Nanasi 
# Azalkova 1 
# 82101 Bratislava 
# Slovakia

import os,time,re,sys;

normal='xrandr --output LVDS --rotate normal'
left='xrandr --output LVDS --rotate left'
right='xrandr --output LVDS --rotate right'

type=0

while True:
    time.sleep(0.1);
    try:
        f=open('/sys/devices/platform/hdaps/position','r')
	line=f.readline()
	m=re.search('.([0-9]*).*',line)
#	print m.group(1)
	trash=int(m.group(1))
	if(trash <400):
	    if type!=-1:
		os.system(right)
		time.sleep(1);
		type=-1
	else:
	    if (trash >590):
		if type != 1:
		    os.system(left)
		    time.sleep(1);
		    type=1
	    else:
		if type!=0:
		    os.system(normal)
		    time.sleep(1);
		    type=0
    except:
	type=47
