#!/bin/bash
# Script for scaling display (monitor)

ICON="/usr/share/icons/hicolor/48x48/apps/screen-scaler.png"
EICON="/usr/share/screen-scaler/icons/error.svg"
OK="/usr/share/screen-scaler/icons/ok.svg"
CANCEL="/usr/share/screen-scaler/icons/cancel.svg"
ABOUT="/usr/share/screen-scaler/icons/about.svg"
RESET="/usr/share/screen-scaler/icons/reset.svg"
Conf= "$HOME/.config/screen-scaler"

if ! test -d "$HOME/.config/screen-scaler"; then
mkdir -p "$HOME/.config/screen-scaler"
fi

GET_RES=$(xrandr | grep -w "connected" | awk '{print $1}' | head -n 1)

# If no monitor is detected, exit safely
if [ -z "$GET_RES" ]; then
    yad --window-icon=$EICON --image=$EICON --text="\nNo connected display found!" \
--height=100 --width=375 --center --fixed --button="Close"!$OK
    exit 1
fi

action=$(yad \
--width=500 \
--height=100 \
--image=$ICON \
--center --fixed \
--window-icon=$ICON \
--title='Screen Scaler' \
--entry \
--column="Icon" \
--column="Size" \
--text=$'👇️ Choose a scaling option from the list below.' \
'🖥️ Scale 70%' '🖥️ Scale 80%' '🖥️ Scale 90%' '🖥️ Scale 100%' '🖥️ Scale 110%' '🖥️ Scale 120%' '🖥️ Scale 130%' '🖥️ Scale 140%' '🖥️ Scale 150%' '🖥️ Scale 160%' '🖥️ Scale 170%' \
--button="About"!$ABOUT:3 \
--button="Reset"!$RESET:2 \
--button="Cancel"!$CANCEL:1 \
--button="Ok"!$OK:0)

STATUS=$?

if [ "$STATUS" = "1" ]; then
exit 0
fi
# About dialog
if [ "$STATUS" = "3" ]; then
 /usr/share/screen-scaler/about/"Sceen Scaler"
 screen-scaler
exit 0
fi

# If "Reset" (exit code 2) is pressed
if [ "$STATUS" = "2" ]; then
    xrandr --output "$GET_RES" --scale 1x1; echo "xrandr --output "$GET_RES" --scale 1x1"  > $HOME/.config/screen-scaler/res
    rm -f $HOME/.config/autostart/screen-scaler.desktop
    rm -f $HOME/.config/autostart/screen-scaler
    exit 0
fi

# If "OK" is pressed and an action was selected
if [ -n "${action}" ]; then
    case $action in
        '🖥️ Scale 70%')  xrandr --output "$GET_RES" --scale 0.7x0.7; echo "xrandr --output "$GET_RES" --scale 0.9x0.9"  > $HOME/.config/screen-scaler/res ;;
        '🖥️ Scale 80%')  xrandr --output "$GET_RES" --scale 0.8x0.8; echo "xrandr --output "$GET_RES" --scale 0.9x0.9"  > $HOME/.config/screen-scaler/res ;;
        '🖥️ Scale 90%')  xrandr --output "$GET_RES" --scale 0.9x0.9; echo "xrandr --output "$GET_RES" --scale 0.9x0.9"  > $HOME/.config/screen-scaler/res ;;
        '🖥️ Scale 100%') xrandr --output "$GET_RES" --scale 1x1; echo "xrandr --output "$GET_RES" --scale 1x1"  > $HOME/.config/screen-scaler/res ;;
        '🖥️ Scale 110%') xrandr --output "$GET_RES" --scale 1.1x1.1; echo "xrandr --output "$GET_RES" --scale 1.1x1.1"  > $HOME/.config/screen-scaler/res ;;
        '🖥️ Scale 120%') xrandr --output "$GET_RES" --scale 1.2x1.2; echo "xrandr --output "$GET_RES" --scale 1.2x1.2"  > $HOME/.config/screen-scaler/res ;;
        '🖥️ Scale 130%') xrandr --output "$GET_RES" --scale 1.3x1.3; echo "xrandr --output "$GET_RES" --scale 1.3x1.3"  > $HOME/.config/screen-scaler/res ;;
        '🖥️ Scale 140%') xrandr --output "$GET_RES" --scale 1.4x1.4; echo "xrandr --output "$GET_RES" --scale 1.4x1.4"  > $HOME/.config/screen-scaler/res ;;
        '🖥️ Scale 150%') xrandr --output "$GET_RES" --scale 1.5x1.5; echo "xrandr --output "$GET_RES" --scale 1.5x1.5"  > $HOME/.config/screen-scaler/res ;;
        '🖥️ Scale 160%') xrandr --output "$GET_RES" --scale 1.6x1.6; echo "xrandr --output "$GET_RES" --scale 1.6x1.6"  > $HOME/.config/screen-scaler/res ;;
        '🖥️ Scale 170%') xrandr --output "$GET_RES" --scale 1.7x1.7; echo "xrandr --output "$GET_RES" --scale 1.7x1.7"  > $HOME/.config/screen-scaler/res ;;
    esac
fi
# Save current setup option to boot in saved
if ! test -d $HOME/.config/autostart; then
 mkdir -p $HOME/.config/autostart
fi
ENTRY=`yad --width=475 --center --fixed \
--form --title="Sceen Scaler" \
--field="Save Settings:CHK" \
--text="\nSave current setting with auto starting Screen Scaler?" \
--window-icon=$ICON \
--image="$ICON" \
--button=No!$CANCEL:1 \
--button=Yes!$OK:0`
if [ $? = "1" ]; then
rm -f $HOME/.config/autostart/screen-scaler.desktop
rm -f $HOME/.config/autostart/screen-scaler
exit
 fi
    RET=$?
    OIFS=$IFS # save Internal Field Separator
    IFS="|" # separator is "|" in returned choices
    i=0
    retChoice=()
    for ret in $ENTRY;do
        retChoice[$i]="$ret"
        i=$(($i+1))
    done
IFS=$OIFS # reset IFS back to default
Auto_start=${retChoice[0]} # Autostart
if [[ $Auto_start == TRUE ]]; then
`echo \
'#!/bin/bash
RES=$(cat $HOME/.config/screen-scaler/res)
exec $RES' > $HOME/.config/autostart/screen-scaler`
chmod a+x $HOME/.config/autostart/screen-scaler
`echo \
'[Desktop Entry]
Version=1.0
Type=Application
Terminal=false
Comment=Change desktop screen scaling
Exec=".config/autostart/screen-scaler"
Name=Screen Scaler' | \
awk '{print $0}' > $HOME/.config/autostart/screen-scaler.desktop`
chmod a+x $HOME/.config/autostart/screen-scaler.desktop
fi
 fi


