#!/usr/bin/env bash
# JA3 на Mac (Whisky без GPTK): рендер через D3D11 + DXVK, не D3D12 (wine d3d12 → «Failed creating render device»).
set -euo pipefail
GOG_DIR="$(cd "$(dirname "$0")" && pwd)"
# shellcheck source=config.sh
source "${GOG_DIR}/config.sh"

WINE_USER="${WINE_USER:-eternal}"
LS="${BOTTLE_DIR}/drive_c/users/${WINE_USER}/AppData/Roaming/Jagged Alliance 3/LocalStorage.lua"

if [[ ! -f "${LS}" ]]; then
  mkdir -p "$(dirname "${LS}")"
  cat >"${LS}" <<'EOF'
return {
	Options = {
		GraphicsApi = "d3d11",
		VideoPreset = "Low",
	},
}
EOF
  echo "OK: создан ${LS} (GraphicsApi=d3d11)"
  exit 0
fi

if grep -q 'GraphicsApi = "d3d11"' "${LS}"; then
  echo "OK: GraphicsApi уже d3d11"
  exit 0
fi

perl -i -pe '
  if (/^\t\tGraphicsApi = /) {
    $_ = "\t\tGraphicsApi = \"d3d11\",\n";
  }
' "${LS}"
echo "OK: ${LS} → GraphicsApi=d3d11 (было d3d12 или пусто)"
