#!/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 registry
  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 "%08x", scale * 96 + 0.5 }')

  # Wine reads the DPI from [Control Panel\\Desktop], but a fresh prefix leaves
  # it unset there, so add it after the key's #time line when it's missing. Each
  # launch writes its own temp file, so two quick launches can't share one.
  registry=$(mktemp "$PREFIX/user.reg.XXXXXX")
  chmod --reference="$PREFIX/user.reg" "$registry"
  awk -v value="\"LogPixels\"=dword:$dpi" '
    /^\[/ { desktop = ($0 ~ /^\[Control Panel\\\\Desktop\]/) }
    NR == FNR { if (desktop && /^"LogPixels"=/) has_value = 1; next }
    pending && !/^#time=/ { print value; pending = 0 }
    /^"LogPixels"=dword:[0-9a-f]+$/ { print value; next }
    { print }
    pending { print value; pending = 0 }
    desktop && /^\[/ && !has_value { pending = 1 }
    END { if (pending) print value }
  ' "$PREFIX/user.reg" "$PREFIX/user.reg" >"$registry"
  mv "$registry" "$PREFIX/user.reg"
}

if ! prefix_running; then
  match_dpi_to_monitor_scale
fi

if grep -Eq '"mode"[[:space:]]*:[[:space:]]*"Integrated"' "${OMARCHY_SUPERGFXD_CONF:-/etc/supergfxd.conf}" 2>/dev/null && omarchy-hw-hybrid-gpu; then
  omarchy-notification-send --transient -g "" \
    "$(omarchy-i18n gpu.integratedActive)" \
    "$(omarchy-i18n gpu.switchToDgpuHint)" </dev/null >/dev/null 2>&1 &
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"
