#!/usr/bin/bash
# Script returns true if a card requiring sdl workaround is detected

# Regexes for GPUs requiring the SDL backend workaround
# Can be used in regexes as $POLARIS_RE|$VEGA_RE etc for readability
# Needed output for a regex can gathered from "lspci -nnk | grep -P "(VGA compatible|3D) controller" -A1"
# The regex format is Perl REGEX
POLARIS_RE="Ellesmere|Lexa|Baffin|Polaris\s?\d{2}|RX\s?[4-5]\d{2}[^\d]"

# Used to combine all regex into 1 var ex: FINALE_RE="$POLARIS_RE|$VEGA_RE" for readability if we need to add more card models
FINAL_RE="$POLARIS_RE"

# Get the users homedirs so we can check for export-gpu configs
HOMES=$(grep "/home" /etc/passwd | awk -F ":" '{ print $6 }')

# Find all GPUs using lspci (and include the Subsystem in the output)
VGA_CONTROLLERS=$(lspci -nnk | grep -P "(VGA compatible|3D) controller" -A1)

# Check inside users home if gamescope is configured to use a specific GPU
# This at least does not break gamescope on systems where users have re-enabled the login screen
# or use a different card for gamescope.
while IFS= read -r USERDIR
do
    # If a vulkan device config is present
    if [ -f "$USERDIR/.config/environment.d/00-vulkan-device.conf" ]; then
        # Check if a vulkan device is configured
        VULKAN_GPU=$(grep -P "^VULKAN_ADAPTER=" "$USERDIR/.config/environment.d/00-vulkan-device.conf" | sed 's/VULKAN_ADAPTER=//')

        # Write info to journal
        echo "INFO: VULKAN_ADAPTER is set to $VULKAN_GPU by env config." | systemd-cat -t gamescope-sdl-workaround -p info

        # If a Vulkan Device is configured
        if [ -n "$VULKAN_GPU" ]; then
            # Get the current Vulkan Device
            CUR_VULKAN_ADAPTER=$(lspci -nnk -d "$VULKAN_GPU" | head -n 2)

            # Check if output is not empty
            if [ -n "$CUR_VULKAN_ADAPTER" ]; then
                # Replace list of VGA controllers with $CUR_VULKAN_ADAPTER and it's Subsystem
                VGA_CONTROLLERS="$CUR_VULKAN_ADAPTER"
            fi
        fi
    fi
done <<< "$HOMES"

# Process the VGA controllers list to see if we match any cards that need the sdl workaround
while IFS= read -r GPU
do
    # We will utilize grep and its Perl regex engine for this, the bash regex engine is too basic
    # If we match with a card that requires sdl workaround
    if echo "$GPU" | grep -P "($FINAL_RE)" > /dev/null; then
        # Write info to journal
        echo "INFO: $GPU detected" | systemd-cat -t gamescope-sdl-workaround -p info
        echo "INFO: SDL backend workaround will be applied." | systemd-cat -t gamescope-sdl-workaround -p info

        # This gpu requires sdl workaround, exit 0 (true)
        exit 0
    fi
done <<< "$VGA_CONTROLLERS"

# Write info to journalctl
echo "INFO: SDL backend workaround not required for this system" | systemd-cat -t gamescope-sdl-workaround -p info
echo 'INFO: If SDL backend workaround IS required for your card. Provide your "lspci -nnk | grep -P '"'"'(VGA compatible|3D) controller'"'"' -A1" to the developers.' | systemd-cat -t gamescope-sdl-workaround -p info

# This GPU does not require sdl workaround, exit 1 (false)
exit 1
