#!/bin/bash

# omarchy:summary=Launch the installed Battle.net client via umu-launcher + GE-Proton.
# omarchy:args=[--with-mangohud]
# omarchy:examples=omarchy launch battlenet | omarchy launch battlenet --with-mangohud

set -e

PREFIX="$HOME/Games/battlenet"
LAUNCHER="$PREFIX/drive_c/Program Files (x86)/Battle.net/Battle.net Launcher.exe"

with_mangohud=0
for arg in "$@"; do
  case "$arg" in
    --with-mangohud) with_mangohud=1 ;;
    -h|--help)
      cat <<'EOF'
Usage: omarchy-launch-battlenet [--with-mangohud]

Options:
  --with-mangohud   Enable the MangoHud FPS overlay for games launched from
                    Battle.net. Toggle perf logging in-game with Shift_L+F2;
                    CSV logs land in ~/mangohud/.
EOF
      exit 0
      ;;
    *)
      echo "Unknown argument: $arg" >&2
      echo "Try: omarchy-launch-battlenet --help" >&2
      exit 1
      ;;
  esac
done

if [[ ! -f $LAUNCHER ]]; then
  echo "Battle.net is not installed. Run omarchy-install-gaming-battlenet first." >&2
  exit 1
fi

# Hyprland leaves XWayland apps unscaled (xwayland:force_zero_scaling), and Wine
# reads its DPI from the prefix registry once at startup, so match it to the
# focused monitor's scale on every launch. Skipped while the prefix is running:
# the running launcher ignores it, and wineserver rewrites user.reg on exit.
prefix_running() {
  local pid prefix
  for pid in $(pgrep -x wineserver); do
    prefix=$(tr '\0' '\n' </proc/"$pid"/environ 2>/dev/null | sed -n 's/^WINEPREFIX=//p')
    [[ -n $prefix && $(realpath "$prefix" 2>/dev/null) == "$(realpath "$PREFIX")" ]] && return 0
  done
  return 1
}

match_dpi_to_monitor_scale() {
  local scale dpi
  scale=$(hyprctl monitors -j 2>/dev/null | jq -r '.[] | select(.focused == true).scale')
  [[ $scale =~ ^[0-9]+(\.[0-9]+)?$ ]] || return 0
  dpi=$(awk -v scale="$scale" 'BEGIN { printf "%d", scale * 96 + 0.5 }')
  sed -i "s/^\"LogPixels\"=dword:[0-9a-f]\{8\}$/\"LogPixels\"=dword:$(printf '%08x' "$dpi")/" "$PREFIX/user.reg"
}

if ! prefix_running; then
  match_dpi_to_monitor_scale
fi

env_args=(
  WINEPREFIX="$PREFIX"
  PROTONPATH=GE-Proton
  GAMEID=umu-battlenet
  PROTON_VERB=run
)
(( with_mangohud )) && env_args+=(MANGOHUD=1)

env "${env_args[@]}" umu-run "$LAUNCHER"
