#!/bin/sh
# chkconfig: 2345 98 02
### BEGIN INIT INFO
# Provides:          timesys-theatre
# Required-Start:    $local_fs
# Required-Stop:     
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Should-Start:      fb
# Short-Description: Start timesys-theatre at boot time
# Description:       Enable service provided by timesys-theatre.
# X-Start-Before:    
# X-Stop-After:      
# X-Timesys-Start-Number:  98
# X-Timesys-Stop-Number:  02
### END INIT INFO

PATH=/usr/bin:/bin:/usr/sbin:/sbin
QWS_MOUSE_PROTO=
TSDEV=
MOUSEDEV=

# if button SW2 is pushed dont start the demo
echo 39 >/sys/class/gpio/export
echo in >/sys/class/gpio/gpio39/direction
push=`cat /sys/class/gpio/gpio39/value`
if [ $push -eq 0 ];
then
        echo 39 >/sys/class/gpio/unexport
        exit
fi
echo 39 >/sys/class/gpio/unexport

case "$1"  in
  start)
    echo "Starting Vybrid accelerometer webgl demo..."

# mount and format ram disk for /var/www
if [ -e /var/www ];
then
        rm -rf /var/www
fi
mkdir -p /var/www
mkfs.ext2 /dev/ram0
mount /dev/ram0 /var/www

# copy the web page and images
tar -xf /home/demo-items/accel-webgl-www.tgz -C /var/www
mkdir -p /var/www/cgi-bin
ln -s /usr/bin/accelerometer-cgi /var/www/cgi-bin

# install the MCC module, and make it availaable to "nobody" user
if [ -e /dev/mcc ];
then
        rm -rf /dev/mcc
fi

echo "Starting mcc ko..."
modprobe mcc
sleep 1
chmod a+rw /dev/mcc

#echo "Waiting 30s"
#sleep 30

# boot MQX app
#echo "Starting CM4 ............"
echo "Skipping CM4 start"
#mqxboot /home/demo-items/accelerometer_example_twrvf65gs10_m4.bin 0x3f000000 0x3f000485

#echo "Waiting 30s ............."
#sleep 30 

# get the welcome up
#cd /
#export QWS_MOUSE_PROTO=tslib:/dev/event0
#/webgl_demo_welcome -qws &

# Have Qt detect each mouse device registered on the board
if [ -e /etc/ts.dev ] ; then
    TSDEV=`cat /etc/ts.dev`
	QWS_MOUSE_PROTO="Tslib:$TSDEV"
fi
if [ -e /etc/mouse.dev ] ; then
	MOUSEDEV=`cat /etc/mouse.dev`
	QWS_MOUSE_PROTO="$QWS_MOUSE_PROTO LinuxInput:$MOUSEDEV "
fi
export QWS_MOUSE_PROTO
export QWS_DISPLAY=LinuxFb:/dev/fb0

#export QWS_MOUSE_PROTO=tslib:/dev/event0
#b36968 26.04.2013
echo "Starting OOBE demo..."
cd /home
./gpio_init.sh
oobe -qws &
  ;;
  *)
  ;;
esac
