#!/bin/bash

at_exit() {
    [ -n "${TMPFILE}" ] && rm "${TMPFILE}"
}

max() {
    echo $(($1>$2?$1:$2))
}

calculate_dpi() {

    local result
    local default_dpi=96
    local calculated_dpi
    local current_resolution=$(xrandr 2>/dev/null | grep -Eo 'current [1-9][0-9]{1,3} x [1-9][0-9]{1,3}' | grep -Eo '[1-9][0-9]{1,3} x [1-9][0-9]{1,3}')
    local x_resolution=$(echo "$current_resolution" | grep -Eo '^[1-9][0-9]{1,3}')
    local y_resolution=$(echo "$current_resolution" | grep -Eo  '[1-9][0-9]{1,3}$')

    local screen_size=$(xrandr --current 2>/dev/null | grep -Eo '[1-9][0-9]{1,3}mm x [1-9][0-9]{1,3}mm')

    #Check if we have screen_size from xrandr output
    if [ -n "$screen_size" ];then
        local x_size=$(echo "$screen_size" | grep -Eo '^[1-9][0-9]{1,3}')
        local y_size=$(echo "$screen_size" | grep -Eo  '[1-9][0-9]{1,3}mm$' | grep -Eo '^[1-9][0-9]{1,3}')

        local x_dpi=$((($x_resolution * 254) / ($x_size * 10)))
        local y_dpi=$((($y_resolution * 254) / ($y_size * 10)))

        calculated_dpi=$(max $x_dpi $y_dpi)
    else
        #screen_size is empty. Let's assume DPI is 96
        calculated_dpi=$default_dpi
        #echo "DPI calculation failed. Using default value $default_dpi." 1>&2
        #fly-qmsg -s -l 3000 -i warning "DPI calculation failed. Using default value $default_dpi." &
        export LANG=ru_RU.UTF-8
        diag=$(QT_STYLE_OVERRIDE=FlyPlastiqueTablet fly-dialog --title "Диагональ экрана" --spinbox "Диагональ экрана в дюймах" 10 4 100 1)
        calculated_dpi=$(echo "sqrt($x_resolution^2+$y_resolution^2)/$diag" | bc)
    fi

    if [ "$calculated_dpi" -lt "120" ]; then
        #echo "Calculated DPI value is less than 120. Using default value $default_dpi." 1>&2
        result=$default_dpi
    else
        result=$calculated_dpi
    fi

    FMS_DPI=$result
    export FMS_DPI
}

get_dpi() {
    local dpi_file
    local dpi_file_dir

    dpi_file_dir=/etc/X11/fly-dm
    if [ ! -d $dpi_file_dir ]; then mkdir -p $dpi_file_dir; fi

    dpi_file="${dpi_file_dir}/dpi"

    if [ -e $dpi_file ]; then
#        if test `find "$dpi_file" -mmin -1` ; then
#            calculate_dpi
#            echo $FMS_DPI > $dpi_file
#            echo $FMS_DPI
#        else
	    cat $dpi_file
#        fi
    else
	calculate_dpi
	echo $FMS_DPI > $dpi_file
	echo $FMS_DPI
    fi
}

sed_cfg() {
#    log_message "sed -i '${1}' '${2}'"
    sed -i "${1}" "${2}"
}

# check if display is local non-nested, i.e. display name starts with ':'
if [ "${DISPLAY:0:1}" != ":" ]; then exit; fi

# ask dpi if touchscreen only
#fly-wmfunc FLYWM_GET_TOUCHSCREEN > /dev/null
#if [ $? -ne "0" ]; then exit; fi

# ask dpi if no VM
#if [ -e /var/log/Xorg.0.log ]; then
#  grep -e VirtualBox -e vmware -e qxl -e spice /var/log/Xorg.0.log -i > /dev/null
#  if [ $? -eq "0" ]; then exit; fi
#fi
#if [ -e /var/log/fly-dm/Xorg.0.log ]; then
#  grep -e VirtualBox -e vmware -e qxl -e spice /var/log/fly-wm/Xorg.0.log -i > /dev/null
#  if [ $? -eq "0" ]; then exit; fi
#fi
systemd-detect-virt
if [ $? -eq "0" ]; then exit; fi

dpi=$(get_dpi)
xft_dpi="Xft.dpi: $dpi"

if [ "$dpi" -gt "130" ]; then
  #CFG='/etc/X11/Xresources/x11-common'
  #[ -f "${CFG}" ] && sed_cfg '/^Xft\.dpi:/d' "${CFG}"
  #echo "${xft_dpi}" >> "${CFG}"
  echo "${xft_dpi}" | /usr/bin/xrdb -merge -quiet

  if [ "$dpi" -le "160" ]; then
    cursor_size=24
  else
    if [ "$dpi" -le "240" ]; then
      cursor_size=32
    else
      if [ "$dpi" -le "336" ]; then
        cursor_size=48
      else
        cursor_size=64
      fi
    fi
  fi
  echo "Xcursor.size: $cursor_size" | xrdb -merge -quiet
fi
