#!/bin/bash

ENABLED=yes
[ "$ENABLED" != "yes" ] && exit 0
DEBUGMODE=no

STEAMNODPMS=yes

. /usr/lib/ublinux/functions
. /usr/lib/ublinux/os-config
debug_mode "$0" "$@"

#. $HARDCONF
#echo "$XDRIVER" | grep -q "nvidia" || rmmod nvidia

[ -f ${SYSCONF}/config ] && . ${SYSCONF}/config
[ -f ${SYSCONF}/desktop ] && . ${SYSCONF}/desktop
[ -f ${SYSCONF}/keyboard ] && . ${SYSCONF}/keyboard

[[ ${DISPLAYMANAGER} == "none" ]] && exit

#[ ! -z "$VGAID" ] && grep -q $VGAID /usr/share/ublinux/hwdata/deleteconf && rm -f /etc/X11/xorg.conf
[ "$DELETE_XORG_CONF" = "yes" ] && rm -f /etc/X11/xorg.conf

grep -q xres= /proc/cmdline && XORG_RES=$(cat /proc/cmdline | awk -F xres= '{print $2}' | awk '{print $1}')
if [ "$SETXORGRES" != "no" ] ;then
  if ! [ -z "$XORG_RES" ] ;then 
     XCF=/etc/X11/xorg.conf
     grep -qi modes $XCF 2>/dev/null || XCF=/etc/X11/xorg.conf.d/00-modes.conf
     if grep -qi modes $XCF 2>/dev/null ;then
        sed -i 's|^[[:space:]]*Modes.*|        Modes "'$XORG_RES'"|' $XCF
     else
        cat >$XCF <<EOF
Section "Screen"
   Identifier "Screen"
   SubSection "Display"
        Modes "$XORG_RES"
   EndSubSection
EndSection
EOF
     fi
  fi
fi

echo $DPMS | egrep -qi 'no|off|false' && DPMS=no
[ "$STEAMNODPMS" = "yes" -a "$DESKTOP" = "Steam" ] && DPMS=no

if [ "$DPMS" = "no" ] ;then
     sed -i s/'.*Option .DPMS..*'/'    Option "DPMS" "false"'/ /etc/X11/xorg.conf
fi

if [ "$MKKBDFXORG" != "no" ] ;then
  if ! grep -sqi XkbOptions /etc/X11/xorg.conf ;then
     cat > /etc/X11/xorg.conf.d/00-keyboard.conf <<EOF
Section "InputClass"
        Identifier "system-keyboard"
        MatchIsKeyboard "on"
        Option "XkbLayout" "$XkbLayout"
        Option "XkbModel" "$XkbModel"
        Option "XkbVariant" "$KEYBOARD"
        Option "XkbOptions" "$XkbOptions"
EndSection
EOF
  fi
fi

# Блок простого MultiSeat
I=1
SIMPLEMULTISEAT="SIMPLEMULTISEAT${I}"
while [[ ${!SIMPLEMULTISEAT} ]]; do
    MULTISEATCOMMAND="$(echo ${!SIMPLEMULTISEAT} | cut -d " " -f1)"
    if [[ "${MULTISEATCOMMAND}" == "attach" ]]; then
#	SEATN="$(echo ${!SIMPLEMULTISEAT} | cut -d " " -f2)"
#        DEVICE="$(echo ${!SIMPLEMULTISEAT} | cut -d "" -f3)"
#	loginctl "${MULTISEATCOMMAND}" "${SEATN}" "${DEVICE}"
	loginctl "${!SIMPLEMULTISEAT}"
    fi
    SIMPLEMULTISEAT="SIMPLEMULTISEAT$((++I))"
done

