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

PATH=/usr/bin:/bin:/usr/sbin:/sbin

# Run script to initialize demo environment if it exists
if [ -f /etc/demo-env.sh ] ; then
  . /etc/demo-env.sh
fi

# Set up QWS mouse if not set
if [ -z $QWS_MOUSE_PROTO ] ; then
  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
fi

# Export default fb if not set
if [ -z $QWS_DISPLAY ]; then
  export QWS_DISPLAY=LinuxFb:/dev/fb0
fi

case "$1"  in
  start)
    # if we started another demo prior, kill it
    killall timesys-theatre timesys-theatre-1080p timesys-theatre-720p >/dev/null 2>&1
    if [ -n "$(pidof fluidlauncher)" ]; then
  	    echo "Already running"
  	    exit
    fi
    rm -f /tmp/stopdemo >/dev/null 2>&1
    echo "Starting demo..."
    while [ ! -e /tmp/stopdemo ]; do rm -f /tmp/rundemo ; cd /usr/bin/launcher/ && ./fluidlauncher -qws ; if [ -e /tmp/rundemo ]; then sh /tmp/rundemo;rm -f /tmp/rundemo;fi;done &
  ;;
  stop)
    touch /tmp/stopdemo
    echo "Stopping demo..."
    killall fluidlauncher >/dev/null 2>&1
  ;;
  *)
    echo "Usage: $0 start|stop"
  ;;
esac
