#!/usr/bin/env bash
#
# Author: Dmitry Razumov <asmeron@ublinux.com>
# Copyright (c) 2021-2025 UBLinux <support@ublinux.com>
#
# Used as a source

ENABLED=yes
[[ ${ENABLED} == "yes" ]] || { return 0 2>/dev/null && return 0 || exit 0; }

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

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