#!/usr/bin/bash
# Original - https://github.com/Nobara-Project/steamdeck-edition-packages/blob/main/gamescope-session-plus/usr/bin/deckscale
# Copyright (C) 2022-2023 CachyOS team
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

# Refresh our desktop icons
#cp /etc/skel/Desktop/* "$HOME"/Desktop/
#chmod +x "$HOME"/Desktop/*

# We need to wait until the display is available before executing our changes
sleep 5

scale=0
rotation="none"

#Source config file for scaling values
if [[ -f ~/.config/deckscale ]]; then
        source ~/.config/deckscale
fi

#Find enabled eDP device.
for p in /sys/class/drm/*eDP*/enabled; do 
 if [[ $(cat $p) == "enabled" ]]; then
        cardpath=${p%/enabled}
        dp=${cardpath#*/card?-}
        echo "Found first enabled eDP card. Using $cardpath"
        #Break out of loop to only use first card found.
        break
 fi
done

#Check for specific devices
edid=$(edid-decode $cardpath/edid)
# Steam Deck
if [[ ! -z $(grep 'ANX7530 U' <<< "$edid") ]]; then
        if [[ -n "$Deck" ]]; then
                scale=$Deck
        else
                scale=1.0
        fi
        rotation="right"
        echo "Steam Deck display found."
# GPD Win 4 screen
elif [[ ! -z $(grep 'G1618-04' <<< "$edid") ]]; then
        if [[ -n "$GPDWinFour" ]]; then
                scale=$GPDWinFour
        else
                scale=1.5
        fi
        echo "GPD Win 4 display found"
# Lenovo Legion Go screen
elif [[ ! -z $(grep 'Go Display' <<< "$edid") ]]; then
        if [[ -n "$LegionGO" ]]; then
                scale=$LegionGO
        else
                scale=2.0
        fi
        rotation="left"
        echo "Lenovo Legion Go display found."
# ROG Ally
elif [[ ! -z $(grep 'TL070FVXS01-0' <<< "$edid") ]]; then
                if [[ -n "$Ally" ]]; then
                scale=$Ally
        else
                scale=1.25
        fi
        echo "ROG Ally display found"
else
        echo "No known display found."
fi

# Run rotate command if a rotation was found
if [[ $rotation != "none" ]]; then
        /usr/bin/kscreen-doctor output.$dp.rotation.$rotation
        echo "$rotation rotation applied."
else
        echo "No rotation applied."
fi
# Run scale command if a scale was found
if (( $(echo "$scale 0" | awk '{print ($1 != $2)}') )); then
        /usr/bin/kscreen-doctor output.$dp.scale.$scale
        echo "$scale scale factor applied."
else 
        echo "No scaling applied."
fi
