#!/usr/bin/bash
#
# Author: Dmitry Razumov <asmeron@ublinux.com>
# Copyright (c) 2021-2025 UBLinux <support@ublinux.com>
#
# SPDX-License-Identifier: AGPL-3.0-or-later
#
# Used as a source

ENABLED=yes
if [[ ${ENABLED} == "yes" ]]; then
    DEFAULT_CONF=$(< /usr/lib/ublinux/default)
    [[ ${DEFAULT_CONF} =~ (^|$'\n')[[:blank:]]*SYSCONF=([^$|$'\n']+)[[:blank:]]*($|$'\n') ]] && SYSCONF=${BASH_REMATCH[2]} || SYSCONF="/etc/sysconf"

    SOURCE=${SYSCONF}/config; [ -f ${SOURCE} ] && . ${SOURCE} 2>/dev/null
    SOURCE=${SYSCONF}/video; [ -f ${SOURCE} ] && . ${SOURCE} 2>/dev/null
    SOURCE=${SYSCONF}/desktop; [ -f ${SOURCE} ] && . ${SOURCE} 2>/dev/null

##
##XFCE x11
## SESSION_MANAGER=local/ubpc-1:@/tmp/.ICE-unix/12234,unix/ubpc-1:/tmp/.ICE-unix/12234
## DESKTOP_SESSION=xfce
## DESKTOP_SESSION=gnome-flashback-metacity
## XDG_MENU_PREFIX=gnome-
## XDG_SEAT=seat0
## XDG_SESSION_DESKTOP=xfce
## XDG_SESSION_DESKTOP=gnome-flashback-metacity
## XDG_SESSION_TYPE=x11
## XDG_CURRENT_DESKTOP=XFCE
## XDG_CURRENT_DESKTOP=GNOME-Flashback:GNOME
## XDG_VTNR=2
## XDG_SESSION_ID=28
## XAUTHORITY=/run/user/1000/gdm/Xauthority
## DISPLAY=:0
##Gnome wayland
## SESSION_MANAGER=local/ubpc-1:@/tmp/.ICE-unix/12234,unix/ubpc-1:/tmp/.ICE-unix/12234
## DESKTOP_SESSION=gnome
## XDG_MENU_PREFIX=gnome-
## XDG_SEAT=seat0
## XDG_SESSION_DESKTOP=gnome
## XDG_SESSION_TYPE=wayland
## XDG_CURRENT_DESKTOP=GNOME
## XDG_VTNR=2
## XDG_SESSION_ID=8
##TTY
## XDG_SESSION_TYPE=tty
## TERM=xterm-256color
## XDG_SESSION_ID=24
##

    if [[ ${XDG_SESSION_TYPE} == "x11" && -n ${DISPLAY} ]]; then
    ## Parametrs:
    ## cvt , gtf , reduced|r , auto , preferred , off , primary , mode:1920x1080
    ## lo|LeftOf:{name} , ro|RightOf:{name} , ab|Above:{name} , be|Below:{name} , rotate:normal|left|right|invert
        [[ $(declare -p XRANDR_MONITOR 2>/dev/null) =~ ^"declare -A" ]] || declare -gA XRANDR_MONITOR
        [[ ${#XRANDR_MONITOR[@]} -ne 0 ]] && for VIDEO_PORT in "${!XRANDR_MONITOR[@]}"; do
            SOFT_MODELINE="gtf"
            SOFT_MODELINE_R=
            while read VIDEO_PORT_PARAM; do
                MODELINE=
                if grep -Eiq "^[0-9]*x[0-9]*$|^[0-9]*x[0-9]*x[0-9]*$" <<< "${VIDEO_PORT_PARAM}"; then
                    if [[ -n ${SOFT_MODELINE_R} ]]; then
                        MODELINE=$(cvt -r $(sed 's/\([0-9]* [0-9]*\) .*/\1/' <<< ${VIDEO_PORT_PARAM//x/ }) | sed '/^\s*$/d ; /^\s*#/d ; s/\s*Modeline //')
                        SOFT_MODELINE_R=
                    elif [[ ${SOFT_MODELINE} == "cvt" ]]; then
                        MODELINE=$(cvt ${VIDEO_PORT_PARAM//x/ } | sed '/^\s*$/d ; /^\s*#/d ; s/\s*Modeline //')
                    else
                        if grep -Eiq "^[0-9]*x[0-9]*$" <<< "${VIDEO_PORT_PARAM}"; then
                            MODELINE=$(gtf ${VIDEO_PORT_PARAM//x/ } 60 | sed '/^\s*$/d ; /^\s*#/d ; s/\s*Modeline //')
                        else
                            MODELINE=$(gtf ${VIDEO_PORT_PARAM//x/ } | sed '/^\s*$/d ; /^\s*#/d ; s/\s*Modeline //')
                        fi
                    fi
                elif grep -Eiq "^cvt$" <<< "${VIDEO_PORT_PARAM}"; then
                    SOFT_MODELINE="cvt"
                elif grep -Eiq "^gtf$" <<< "${VIDEO_PORT_PARAM}"; then
                    SOFT_MODELINE="gtf"
                elif grep -Eiq "^reduced|r$" <<< "${VIDEO_PORT_PARAM}"; then
                    SOFT_MODELINE_R="reduced"
                elif grep -Eiq "^auto$" <<< "${VIDEO_PORT_PARAM}"; then
                    xrandr --output ${VIDEO_PORT^^} --auto
                elif grep -Eiq "^preferred$" <<< "${VIDEO_PORT_PARAM}"; then
                    xrandr --output ${VIDEO_PORT^^} --preferred
                elif grep -Eiq "^off$" <<< "${VIDEO_PORT_PARAM}"; then
                    xrandr --output ${VIDEO_PORT^^} --off
                elif grep -Eiq "^primary$" <<< "${VIDEO_PORT_PARAM}"; then
                    xrandr --output ${VIDEO_PORT^^} --primary
                elif grep -Eiq "^mode:[0-9x_]*$" <<< "${VIDEO_PORT_PARAM}"; then
                    xrandr --output ${VIDEO_PORT^^} --mode $(cut -d: -f2 <<< ${VIDEO_PORT_PARAM})
                elif grep -Eiq "^mode:[0-9x_]*:[0-9]*$" <<< "${VIDEO_PORT_PARAM}"; then
                    xrandr --output ${VIDEO_PORT^^} --mode $(cut -d: -f2 <<< ${VIDEO_PORT_PARAM}) --rate $(cut -d: -f3 <<< ${VIDEO_PORT_PARAM})
                elif grep -Eiq "^lo:[[:alnum:]]*$|^LeftOf:[[:alnum:]]*$" <<< "${VIDEO_PORT_PARAM}"; then
                    xrandr --output ${VIDEO_PORT^^} --left-of $(cut -d: -f2 <<< ${VIDEO_PORT_PARAM^^})
                elif grep -Eiq "^ro:[[:alnum:]]*$|^RightOf:[[:alnum:]]*$" <<< "${VIDEO_PORT_PARAM}"; then
                    xrandr --output ${VIDEO_PORT^^} --right-of $(cut -d: -f2 <<< ${VIDEO_PORT_PARAM^^})
                elif grep -Eiq "^ab:[[:alnum:]]*$|^Above:[[:alnum:]]*$" <<< "${VIDEO_PORT_PARAM}"; then
                    xrandr --output ${VIDEO_PORT^^} --above $(cut -d: -f2 <<< ${VIDEO_PORT_PARAM^^})
                elif grep -Eiq "^be:[[:alnum:]]*$|^Below:[[:alnum:]]*$" <<< "${VIDEO_PORT_PARAM}"; then
                    xrandr --output ${VIDEO_PORT^^} --below $(cut -d: -f2 <<< ${VIDEO_PORT_PARAM^^})
                elif grep -Eiq "^Rotate:[[:alpha:]]*$" <<< "${VIDEO_PORT_PARAM}"; then
                    xrandr --output ${VIDEO_PORT^^} --rotate $(cut -d: -f2 <<< ${VIDEO_PORT_PARAM^^})
                fi
                if [[ -n ${MODELINE} ]]; then
                    xrandr --newmode ${MODELINE}
                    xrandr --addmode ${VIDEO_PORT^^} $(cut -d" " -f1 <<< ${MODELINE})
                    xrandr --output ${VIDEO_PORT^^} --mode $(cut -d" " -f1 <<< ${MODELINE})
                fi
            done <  <(tr ',' '\n' <<< "${XRANDR_MONITOR[${VIDEO_PORT}]}")
        done
    fi
fi
