#!/bin/sh

################################################################################
#      This file is part of OpenELEC - http://www.openelec.tv
#      Copyright (C) 2009-2014 Stephan Raue (stephan@openelec.tv)
#
#  OpenELEC 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 of the License, or
#  (at your option) any later version.
#
#  OpenELEC 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.  If not, see <http://www.gnu.org/licenses/>.
################################################################################

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

mkdir -p /var/lib
mkdir -p /var/run

# HACK. we have xorg-configure@%. so nice race
# can happen on multi-gpu setups
if [ -e /var/run/xorg-configure.done ] ; then
  exit 0
fi

touch /var/run/xorg-configure.done
logger -t Xorg "creating needed directories and symlinks for driver: "$1""

##############################################################################
# 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-$1.conf"
  XORG_CONF_DEFAULT="/etc/X11/xorg.conf"
  XORG_CONF_DEFAULT_DRV="/etc/X11/xorg-$1.conf"

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

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

  XORG_ARGS="-s 0 -noreset -allowMouseOpenFail"

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

  if [ "$1" = "nvidia" ]; then
    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"
  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

################################################################################
# setup xorg-settings.conf
################################################################################

logger -t Xorg "### starting Xorg with '$DISPLAY vt01 $XORG_ARGS' ###"
  mkdir -p /run/openelec
  echo "XORG_ARGS=\"$XORG_ARGS\"" > /run/openelec/xorg-settings.conf
