#!/usr/bin/env bash
# JA3 = DirectX 12. На Apple Silicon канон: Whisky + Game Porting Toolkit (D3DMetal), не DXVK.
set -euo pipefail
GOG_DIR="$(cd "$(dirname "$0")" && pwd)"
# shellcheck source=config.sh
source "${GOG_DIR}/config.sh"

META="${BOTTLE_DIR}/Metadata.plist"
[[ -f "${META}" ]] || {
  echo "❌ Нет ${META} — откройте bottle ${BOTTLE_NAME} в Whisky."
  exit 1
}

if ! ja3_gptk_available; then
  if [[ -d "/Applications/Game Porting Toolkit.app" ]]; then
    bash "${GOG_DIR}/install-gptk-whisky.sh"
  else
    echo "❌ Game Porting Toolkit не установлен."
    echo "   brew install --cask gcenx/wine/game-porting-toolkit"
    echo "   bash ${GOG_DIR}/install-gptk-whisky.sh"
    exit 1
  fi
fi

/usr/libexec/PlistBuddy -c 'Set :graphicsConfig:backend d3dMetal' "${META}" 2>/dev/null || \
  /usr/libexec/PlistBuddy -c 'Add :graphicsConfig:backend string d3dMetal' "${META}"
/usr/libexec/PlistBuddy -c 'Set :dxvkConfig:dxvk false' "${META}" 2>/dev/null || true

bash "${GOG_DIR}/force-ja3-d3d11.sh" >/dev/null 2>&1 || true
# С GPTK можно d3d12 — сбросить принудительный d3d11 в LocalStorage при желании:
# perl -i -pe 's/GraphicsApi = "d3d11"/GraphicsApi = "d3d12"/' ...

bash "${GOG_DIR}/prepare-ja3-d3dmetal.sh"

echo "OK: bottle ${BOTTLE_NAME} → Graphics Backend D3DMetal"
echo "   Запуск: bash ${GOG_DIR}/run-ja3.sh"
echo "   Whisky Run: C:\\GOG Games\\Jagged Alliance 3\\run-ja3.cmd (не JA3.exe — иначе graphics/CommonLua)"
