################################################################################
#      This file is part of OpenELEC - http://www.openelec.tv
#      Copyright (C) 2009-2012 Stephan Raue (stephan@openelec.tv)
#
#  This Program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2, or (at your option)
#  any later version.
#
#  This Program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with OpenELEC.tv; see the file COPYING.  If not, write to
#  the Free Software Foundation, 51 Franklin Street, Suite 500, Boston, MA 02110, USA.
#  http://www.gnu.org/copyleft/gpl.html
################################################################################

# starting XBMC
#
# runlevels: openelec

LIRCDEV="/var/run/lirc/lircd"
[ -e /var/run/lirc/lircd.irtrans ] && LIRCDEV="/var/run/lirc/lircd.irtrans"
XBMC_ARGS="--standalone -fs --lircdev $LIRCDEV"

progress "starting XBMC"

# hack for Boxee Remote
if grep -q "Vendor=0471 Product=20d9" /proc/bus/input/devices; then
  export SDL_MOUSE_RELATIVE=0
fi

# hack: make addon-bins executable
  chmod +x /storage/.xbmc/addons/*/bin/* > /dev/null 2>&1

# starting autostart script (will be removed later again, dont use it!!!)
  AUTOSTART="/storage/.config/autostart.sh"
  if [ -f $AUTOSTART ]; then
    echo "!!! AUTOSTART script detected !!!" >> /var/log/messages
    cat "$AUTOSTART" >> /var/log/messages
    echo "!!! -End of autostart script- !!!" >> /var/log/messages

    sh $AUTOSTART
  fi

# starting autoupdate
  [ -f /usr/bin/autoupdate ] && /usr/bin/autoupdate &

# waiting for Xorg to start
  wait_for_xorg

# set cpu's to 'conservative'
  ( usleep 15000000
    progress "set cpu's to 'ondemand'"
    cpupower frequency-set -g ondemand > /dev/null 2>&1
  )&

# prevent restrating XBMC at reboot or shutdown
  LOCKDIR="/var/lock/"
  LOCKFILE="xbmc.disabled"
  [ -f "$LOCKDIR/$LOCKFILE" ] && rm -f "$LOCKDIR/$LOCKFILE" &

# starting XBMC
  usleep $XBMC_STARTDELAY
  while true; do

    while [ -f "$LOCKDIR/$LOCKFILE" ]; do
      usleep 250000
    done

    DISPLAY=:0.0 /usr/lib/xbmc/xbmc.bin $XBMC_ARGS > /dev/null 2>&1
    RET=$?

    case "$RET" in
      0)
        if [ ! $(pidof console-kit-daemon) ]; then
          touch "$LOCKDIR/$LOCKFILE"
          poweroff -f
        fi
        ;;
      64)
        if [ ! $(pidof console-kit-daemon) ]; then
          touch "$LOCKDIR/$LOCKFILE"
          poweroff -f
        fi
        ;;
      66)
        if [ ! $(pidof console-kit-daemon) ]; then
          touch "$LOCKDIR/$LOCKFILE"
          reboot
        fi
        ;;
      255)
        echo "Abnormal Exit. Exited with code $RET"
        echo "is Xorg running? check /var/log/Xorg.log"
        ;;
      *)
        echo "Abnormal Exit. Exited with code $RET"
        ;;
    esac

    usleep 250000
  done
