#!/usr/bin/env bash

# Description:
#   Select which GPU to use for rendering for Hyprland and Niri.

# AMD -> Nvidia -> Intel
prefer_order=(amd nvidia intel)

# Get vendor and path of each GPU
default_dri_path="$(find /dev/dri/card* 2>/dev/null | head -n 1)"
[[ -z "$default_dri_path" ]] && default_dri_path="/dev/dri/card0"
intel_path=""
nvidia_path=""
amd_path=""

for link in /dev/dri/by-path/*-card; do
  [[ -e "$link" ]] || continue
  card="$(readlink -f "$link")"
  vfile="/sys/class/drm/$(basename "$card")/device/vendor"
  [[ -r "$vfile" ]] || continue
  vendor="$(cat "$vfile")"
  case "$vendor" in
    0x10de) nvidia_path="$card" ;;
    0x8086) intel_path="$card" ;;
    0x1002) amd_path="$card" ;;
  esac
done

# Specify device for brightnessctl
# Only tested on my laptop with Intel iGPU & Nvidia dGPU
BRIGHTNESSCTL_DEVICE="auto"
if [[ -n "$intel_path" ]]; then
  BRIGHTNESSCTL_DEVICE="intel_backlight"
elif [[ -n "$nvidia_path" ]]; then
  BRIGHTNESSCTL_DEVICE="nvidia_0"
fi
export BRIGHTNESSCTL_DEVICE

# AQ_DRM_DEVICES allows multiple entries separated by colon
devices=""
for who in "${prefer_order[@]}"; do
  case "$who" in
    nvidia) [[ -n "$nvidia_path" ]] && devices="${devices:+$devices:}$nvidia_path" ;;
    intel)  [[ -n "$intel_path"  ]] && devices="${devices:+$devices:}$intel_path" ;;
    amd)    [[ -n "$amd_path"    ]] && devices="${devices:+$devices:}$amd_path" ;;
  esac
done
HYPR_AQ_DRM_DEVICES="${devices:-$default_dri_path}"
export HYPR_AQ_DRM_DEVICES

# But niri only supports choosing one preferred render device
primary_device="$default_dri_path"
for who in "${prefer_order[@]}"; do
  case "$who" in
    nvidia) [[ -n "$nvidia_path" ]] && { primary_device="$nvidia_path"; break; } ;;
    intel)  [[ -n "$intel_path"  ]] && { primary_device="$intel_path"; break; } ;;
    amd)    [[ -n "$amd_path"    ]] && { primary_device="$amd_path"; break; } ;;
  esac
done

# Update niri config
for file in "$HOME/.config/niri/config.kdl" "$HOME/.config/niri/config.kdl.template"; do
  [[ -f "$file" ]] || continue

  if grep -qE '^\s*render-drm-device\s+"[^"]+"' "$file"; then
    current="$(grep -E '^\s*render-drm-device\s+"[^"]+"' "$file" | sed -E 's/^\s*render-drm-device\s+"([^"]+)".*/\1/')"
    [[ "$current" == "$primary_device" ]] && continue
    sed -i -E "s|^(\s*render-drm-device\s+)\"[^\"]+\"|\1\"$primary_device\"|" "$file"
  else
    printf '\ndebug {\nrender-drm-device "%s"\n}\n' "$primary_device" >> "$file"
  fi
done
