#!/bin/bash
# calculate screen parameters and grid row for
# partition icons based on current DPI scaling
# 260101 radky

[ ! -e ~/.config/autostart/zzzfm.desktop ] && exit # zzzfm desktop is disabled

# calculate screen parameters
xpix=$(wlr-randr | grep current | grep -oP '\d+x\d+' | cut -d 'x' -f1)
ypix=$(wlr-randr | grep current | cut -d'x' -f2 | cut -d' ' -f1)
xicons=$(($xpix / 112))
yicons=$(($ypix / 108))
echo "$xicons" > ~/.config/zzzfm/col-max
echo "$yicons" > ~/.config/zzzfm/row-max

# calculate screen grid row for partition icons                         Recommended DPI Scale
if [ $(echo "$ypix >= 2160" | bc) -eq 1 ]; then      # UHD (4K):        1-5 to 2.0
	a=1 b=3 c=4 d=5 e=7 f=8 g=8 h=9 i=10 j=10
elif [ $(echo "$ypix >= 1600" | bc) -eq 1 ]; then    # WQXGA (2.5K):    1.5 to 1.6
	a=1 b=2 c=3 d=4 e=4 f=5 g=6 h=6 i=7 j=7
elif [ $(echo "$ypix >= 1440" | bc) -eq 1 ]; then    # WQHD (2K):       1.1 to 1.5
	a=1 b=2 c=3 d=4 e=4 f=5 g=6 h=6 i=7 j=7
elif [ $(echo "$ypix >= 1200" | bc) -eq 1 ]; then    # UXGA/WQXGA:      1.0 to 1.3
	a=1 b=2 c=3 d=3 e=4 f=4 g=5 h=5 i=5 j=6
elif [ $(echo "$ypix >= 1080" | bc) -eq 1 ]; then    # FHD:             1.0 to 1.2
	a=1 b=2 c=3 d=3 e=4 f=4 g=5 h=5 i=5 j=6
else # ypix < 1080                                   # Low-Res:         1.0 (scaling not recommended)
	a=1 b=2 c=3 d=3 e=4 f=4 g=6 h=6 i=7 j=7
fi
MAXROW=$(cat ~/.config/zzzfm/row-max)
ROW=$MAXROW
PROFILE=$(ls -t ~/.config/kanshi/*.profile | tr ' ' 'n\\' | head -n1)
SCALE=$(grep -oP 'scale \K\d+\.\d+' $PROFILE | grep -v 'disable' | tail -n1)
if	[ -n "$SCALE" ]; then
	[ $(echo "$SCALE >= 1.1" | bc) -eq 1 ] && ROW=$(($MAXROW - $a))
	[ $(echo "$SCALE >= 1.2" | bc) -eq 1 ] && ROW=$(($MAXROW - $b))
	[ $(echo "$SCALE >= 1.3" | bc) -eq 1 ] && ROW=$(($MAXROW - $c))
	[ $(echo "$SCALE >= 1.4" | bc) -eq 1 ] && ROW=$(($MAXROW - $d))
	[ $(echo "$SCALE >= 1.5" | bc) -eq 1 ] && ROW=$(($MAXROW - $e))
	[ $(echo "$SCALE >= 1.6" | bc) -eq 1 ] && ROW=$(($MAXROW - $f))
	[ $(echo "$SCALE >= 1.7" | bc) -eq 1 ] && ROW=$(($MAXROW - $g))
	[ $(echo "$SCALE >= 1.8" | bc) -eq 1 ] && ROW=$(($MAXROW - $h))
	[ $(echo "$SCALE >= 1.9" | bc) -eq 1 ] && ROW=$(($MAXROW - $i))
	[ $(echo "$SCALE >= 2.0" | bc) -eq 1 ] && ROW=$(($MAXROW - $j))
	echo "$ROW" > ~/.config/zzzfm/row-calc
fi
