#!/bin/sh
# Wrapper for the plex-desktop package's /usr/bin/Plex (-> /opt/plex-desktop/Plex.sh).
# Both this dir on $PATH and the .desktop override
# (~/.local/share/applications/tv.plex.PlexDesktop.desktop) launch Plex through
# this script, so all env tweaks live here.
#
# QT_QUICK_CONTROLS_STYLE=org.hyprland.style is set globally in
# hypr/core/env.lua for other Qt/QML apps' theming (hyprland-qt-support), but
# Plex Desktop bundles its own private Qt/QML runtime that doesn't ship that
# style plugin — inheriting the var makes it fail to load its UI and exit
# immediately with "module \"org.hyprland.style\" is not installed" (see
# ~/.local/share/plex/Logs/Plex.log).
unset QT_QUICK_CONTROLS_STYLE
# QT_QPA_PLATFORM: hypr env sets "wayland;xcb"; under Wayland the bundled
# QtWebEngine never runs the web client and the window stays blank.
export QT_QPA_PLATFORM=xcb
# Unset in the session; without it the X11 cursor in Plex is huge.
export XCURSOR_SIZE=24

# HiDPI: Plex runs under XWayland (xcb above) and hypr sets
# xwayland.force_zero_scaling = true, so Hyprland doesn't scale it — and
# hypr/core/env.lua sets QT_SCALE_FACTOR=1 globally, so Qt doesn't either.
# On a scaled monitor (laptop eDP-1 @ 1.5667) the UI comes out tiny. Match Qt's
# scale to the focused monitor at launch instead: laptop gets its scale, the
# desktop's 1x monitors get 1 (unchanged). Falls back to 1 if hyprctl/jq are
# unavailable or return something non-numeric.
scale=$(hyprctl monitors -j 2>/dev/null | jq -r '.[] | select(.focused) | .scale' 2>/dev/null | head -n1)
case "$scale" in
    ''|*[!0-9.]*) scale=1 ;;
esac
export QT_SCALE_FACTOR="$scale"

exec /opt/plex-desktop/Plex.sh "$@"
