#!/bin/sh

# SPDX-License-Identifier: GPL-2.0-or-later
# Copyright (C) 2009-2014 Stephan Raue (stephan@openelec.tv)
# Copyright (C) 2019-present Team LibreELEC (https://libreelec.tv)

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

  # Used to support GeForce 600 Series & newer
  if [ "${1}" = "nvidia" ]; then
    ln -sf /usr/lib/xorg/modules/extensions/libglx_nvidia.so /var/lib/libglx.so
    ln -sf /usr/lib/libnvidia-ml.so.@NVIDIA_VERSION@ /var/lib/libnvidia-ml.so.1
    ln -sf /usr/lib/xorg/modules/drivers/nvidia-main_drv.so /var/lib/nvidia_drv.so
    ln -sf /usr/lib/nvidia/nvidia.ko /var/lib/nvidia.ko
    ln -sf /usr/lib/vdpau/libvdpau_nvidia-main.so.1 /var/lib/libvdpau_nvidia.so.1
    ln -sf /usr/lib/vdpau/libvdpau_nvidia-main.so.1 /var/lib/libvdpau_nvidia.so
    ln -sf /usr/bin/nvidia-main-smi /var/lib/nvidia-smi
    ln -sf /usr/bin/nvidia-main-xconfig /var/lib/nvidia-xconfig
    insmod /var/lib/nvidia.ko
    # GL/GLX (GLVND)
    ln -sf /usr/lib/libGL_glvnd.so.1 /var/lib/libGL.so
    ln -sf /usr/lib/libGLX_glvnd.so.0 /var/lib/libGLX.so
    # indirect rendering
    ln -sf /usr/lib/libGLX_nvidia.so.0 /var/lib/libGLX_indirect.so.0
    XORG_ARGS="${XORG_ARGS} -ignoreABI"
  # Used to support GeForce 500 Series & older
  elif [ "${1}" = "nvidia-legacy" ]; then
    ln -sf /usr/lib/libGL_nvidia-legacy.so.1 /var/lib/libGL.so
    ln -sf /usr/lib/xorg/modules/extensions/libglx_nvidia-legacy.so /var/lib/libglx.so
    ln -sf /usr/lib/libnvidia-ml.so.@NVIDIA_LEGACY_VERSION@ /var/lib/libnvidia-ml.so.1
    ln -sf /usr/lib/xorg/modules/drivers/nvidia-legacy_drv.so /var/lib/nvidia_drv.so
    ln -sf /usr/lib/nvidia-legacy/nvidia.ko /var/lib/nvidia.ko
    ln -sf /usr/lib/vdpau/libvdpau_nvidia-legacy.so.1 /var/lib/libvdpau_nvidia.so.1
    ln -sf /usr/lib/vdpau/libvdpau_nvidia-legacy.so.1 /var/lib/libvdpau_nvidia.so
    ln -sf /usr/bin/nvidia-legacy-smi /var/lib/nvidia-smi
    ln -sf /usr/bin/nvidia-legacy-xconfig /var/lib/nvidia-xconfig
    insmod /var/lib/nvidia.ko
    # GLX (NON-GLVND)
    ln -sf /usr/lib/libGL_nvidia-legacy.so.1 /var/lib/libGLX.so
    # indirect rendering
    ln -sf /usr/lib/libGL_nvidia-legacy.so.1 /var/lib/libGLX_indirect.so.0
    XORG_ARGS="${XORG_ARGS} -ignoreABI"
  # Used for AMD & Intel GPUs supported by MESA 3D
  else
    ln -sf /usr/lib/xorg/modules/extensions/libglx_mesa.so /var/lib/libglx.so
    # GL/GLX (GLVND)
    ln -sf /usr/lib/libGL_glvnd.so.1 /var/lib/libGL.so
    ln -sf /usr/lib/libGLX_glvnd.so.0 /var/lib/libGLX.so
    # indirect rendering
    ln -sf /usr/lib/libGLX_mesa.so.0 /var/lib/libGLX_indirect.so.0
  fi

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

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

