#!/bin/sh

################################################################################
#      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; 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
################################################################################

. /etc/profile

if [ "$RUNLEVEL" = openelec ]; then

  logger -t Xorg "### starting Xorg with driver ${xorg_driver} ###"

  ##############################################################################
  # setup xorg.conf paths
  ##############################################################################

  logger -t Xorg "### setup xorg.conf paths ###"

  XORG_CONF_USER="/storage/.config/xorg.conf"
  XORG_CONF_USER_DRV="/storage/.config/xorg-${xorg_driver}.conf"
  XORG_CONF_DEFAULT="/etc/X11/xorg.conf"
  XORG_CONF_DEFAULT_DRV="/etc/X11/xorg-${xorg_driver}.conf"

  ##############################################################################
  # creating start options
  ##############################################################################

  logger -t Xorg "### creating start options ###"

  XORG_ARGS="-s 0 -noreset -allowMouseOpenFail"

  if [ "$DEBUG" = yes ]; then
    XORG_ARGS="$XORG_ARGS -logverbose 6 -verbose 6"
  fi

  # load user defined xorg.conf, if exist
  if [ -f "$XORG_CONF_USER" ]; then
    XORG_ARGS="$XORG_ARGS -config $XORG_CONF_USER"
  elif [ -f "$XORG_CONF_USER_DRV" ]; then
    XORG_ARGS="$XORG_ARGS -config $XORG_CONF_USER_DRV"
  elif [ -f "$XORG_CONF_DEFAULT" ]; then
    XORG_ARGS="$XORG_ARGS -config $XORG_CONF_DEFAULT"
  elif [ -f "$XORG_CONF_DEFAULT_DRV" ]; then
    XORG_ARGS="$XORG_ARGS -config $XORG_CONF_DEFAULT_DRV"
  fi

  ##############################################################################
  # creating needed directories and symlinks
  ##############################################################################

  logger -t Xorg "### creating needed directories and symlinks ###"

  mkdir -p /var/cache/xkb
  mkdir -p /var/lib
  mkdir -m 1777 -p /tmp/.ICE-unix
  chown root:root /tmp/.ICE-unix

  if [ "${xorg_driver}" = "nvidia" ]; then
    export __GL_SHADER_DISK_CACHE=1
    export __GL_SHADER_DISK_CACHE_PATH="/tmp/GLCache"
    ln -sf /usr/lib/libGL_nvidia.so.1 /var/lib/libGL.so
    ln -sf /usr/lib/xorg/modules/extensions/libglx_nvidia.so /var/lib/libglx.so
    XORG_ARGS="$XORG_ARGS -ignoreABI"
  elif [ "${xorg_driver}" = "fglrx" ]; then
    mkdir -p /var/run
    ln -sf /usr/lib/fglrx /var/run/fglrx
    ln -sf /usr/lib/libGL_fglrx.so.1 /var/lib/libGL.so
    ln -sf /usr/lib/xorg/modules/extensions/libglx_fglrx.so /var/lib/libglx.so
  elif [ "${xorg_driver}" = "fglrx-legacy" ]; then
    mkdir -p /var/run
    ln -sf /usr/lib/fglrx-legacy /var/run/fglrx
    ln -sf /usr/lib/libGL_fglrx.so.1 /var/lib/libGL.so
    ln -sf /usr/lib/xorg/modules/extensions/libglx_fglrx.so /var/lib/libglx.so
  else
    ln -sf /usr/lib/libGL_mesa.so.1 /var/lib/libGL.so
    ln -sf /usr/lib/xorg/modules/extensions/libglx_mesa.so /var/lib/libglx.so
  fi

  ##############################################################################
  # starting Xorg
  ##############################################################################

  logger -t Xorg "### starting Xorg with '$DISPLAY vt01 $XORG_ARGS' ###"

  Xorg $DISPLAY vt01 $XORG_ARGS > /dev/null 2>&1 &

fi

exit 0
