#/bin/sh
#set -x


#define U-Boot settings
#IPU_ROTATE_NONE = 0,
#IPU_ROTATE_VERT_FLIP = 1,
#IPU_ROTATE_HORIZ_FLIP = 2,
#IPU_ROTATE_180 = 3,
#IPU_ROTATE_90_RIGHT = 4,
#IPU_ROTATE_90_RIGHT_VFLIP = 5,
#IPU_ROTATE_90_RIGHT_HFLIP = 6,
#IPU_ROTATE_90_LEFT = 7,


SKIPXRESTART="false"
SKIPREBOOT="false"
OVERRIDE=""
OVERRIDEVALUE=""
OVERRIDELOCATION="/etc/raven/rotation"

CheckOverride () {
	if [ -f ${OVERRIDELOCATION} ]; then
		OVERRIDE="set" #todo:: i could just source this file
	fi
}

CheckXRotation () {
	#check if its a symlink and the correct one at that
	if [ -L /etc/X11/xorg.conf ]; then  
		XLINK=`readlink -f /etc/X11/xorg.conf`
		if [ "$XLINK" = "/etc/X11/xorg.conf.$1" ]; then
			echo "Already at requested rotation"
			exit 0
		fi
	else #if its not a symlink this is a first run, just make the link and leave
		UpdateXorg $1
		if [ "${SKIPXRESTART}" = "false" ]; then
			/etc/init.d/xserver-nodm restart
		fi
		exit 0
	fi
	
	if [ "${SKIPXRESTART}" = "false" ]; then
		/etc/init.d/xserver-nodm stop #kill X while setting up the new parameters
	fi
}

UpdateXorg () {
	rm /etc/X11/xorg.conf
	ln -s /etc/X11/xorg.conf.$1 /etc/X11/xorg.conf
	sync
}

CheckUBoot () {
	CURRENT=`fw_printenv rotation | grep $1`
	if [ $? -ne 0 ]; then
		echo "Setting U-boot rotation to $1"
		fw_setenv rotation $1
		#/usr/sbin/mmarotate --setfb $1
		if [ "${SKIPREBOOT}" = "false" ]; then
			reboot
		fi
	elif [ "$CURRENT" = "rotation=$1" ]; then
		echo "U-boot set to correct rotation"
	else
		echo "Setting U-boot rotation to $1"
		fw_setenv rotation $1
		#/usr/sbin/mmarotate --setfb $1
		if [ "${SKIPREBOOT}" = "false" ]; then
			reboot
		fi
	fi
}

ParseParam () {
	if [ "$1" = "init" ]; then
		SKIPXRESTART="true"
		return
	fi
	if [ "$1" = "stop" ]; then
		SKIPXRESTART="true"
		SKIPREBOOT="true"
		return
	fi
	
	if [ "$1" = "1" ]; then
		OVERRIDE="enable"
		echo "OVERRIDE=set" > ${OVERRIDELOCATION}
		return
	elif [ "$1" = "0" ]; then
		OVERRIDE="disable"
		rm -f ${OVERRIDELOCATION}
		return
	fi
}

CheckOverride

if [ "$#" -eq "2" ]; then
	ParseParam $2
fi

if [ "$#" -eq "3" ]; then
	ParseParam $2
	ParseParam $3
fi

if [ "${OVERRIDE}" = "set" ]; then
	echo "Rotation Override Set, Ignoring request"
	exit 0
fi

case "$1" in
  0)
	echo "Setting Rotation $1 degrees"
	sqlite3 /usr/bin/raven/databases/my.db.sqlite "update ScreenOrientation set Orientation = 3 where rowid=1" > /dev/null 2>&1
	sqlite3 /usr/bin/raven/databases/demo.db.sqlite "update ScreenOrientation set Orientation = 3 where rowid=1" > /dev/null 2>&1
	CheckXRotation $1
	UpdateXorg $1
	CheckUBoot 0
    ;;
  180)
	echo "Setting Rotation $1 degrees"
	sqlite3 /usr/bin/raven/databases/my.db.sqlite "update ScreenOrientation set Orientation = 3 where rowid=1" > /dev/null 2>&1
	sqlite3 /usr/bin/raven/databases/demo.db.sqlite "update ScreenOrientation set Orientation = 3 where rowid=1" > /dev/null 2>&1
	CheckXRotation $1
	UpdateXorg $1
	CheckUBoot 3
    ;; 
  90)
	echo "Setting Rotation $1 degrees"
	sqlite3 /usr/bin/raven/databases/my.db.sqlite "update ScreenOrientation set Orientation = 4 where rowid=1" > /dev/null 2>&1
	sqlite3 /usr/bin/raven/databases/demo.db.sqlite "update ScreenOrientation set Orientation = 4 where rowid=1" > /dev/null 2>&1
	CheckXRotation $1
	UpdateXorg $1
	CheckUBoot 4
    ;;
  270)
	echo "Setting Rotation $1 degrees"
	sqlite3 /usr/bin/raven/databases/my.db.sqlite "update ScreenOrientation set Orientation = 4 where rowid=1" > /dev/null 2>&1
	sqlite3 /usr/bin/raven/databases/demo.db.sqlite "update ScreenOrientation set Orientation = 4 where rowid=1" > /dev/null 2>&1
	CheckXRotation $1
	UpdateXorg $1
	CheckUBoot 7
    ;;
  *)
    echo "Usage: setrotation {0|90|180|270 [init|stop] [override=1|0]}"
	echo "\t override = 1 or 0 to force orentation permanently}"
    exit 1
    ;;
esac

if [ "${SKIPXRESTART}" = "false" ]; then
	/etc/init.d/xserver-nodm restart
fi

exit 0
