#!/usr/bin/env bash
# JA3 + D3DMetal: убрать DXVK/GPTK PE из system32, оставить builtin Gcenx + d3d12/dxgi.so → libd3dshared.
set -euo pipefail
GOG_DIR="$(cd "$(dirname "$0")" && pwd)"
# shellcheck source=config.sh
source "${GOG_DIR}/config.sh"

SYS32="${BOTTLE_DIR}/drive_c/windows/system32"
WINE_PE="${WHISKY_LIBS}/Libraries/Wine/lib/wine/x86_64-windows"
WINE_UNIX="${WHISKY_LIBS}/Libraries/Wine/lib/wine/x86_64-unix"
BAK="${SYS32}/.ja3-d3dmetal-bak"
LS="${BOTTLE_DIR}/drive_c/users/${WINE_USER:-eternal}/AppData/Roaming/Jagged Alliance 3/LocalStorage.lua"

ja3_gptk_available || ja3_bottle_uses_d3dmetal || {
  echo "❌ Нет GPTK/D3DMetal — configure-ja3-d3dmetal.sh"
  exit 1
}

bash "${GOG_DIR}/install-gptk-whisky.sh"

mkdir -p "${BAK}"
# DXVK / GPTK PE в system32 ломают D3DMetal (114688/86016 → «graphics subsystem»).
for dll in d3d12.dll dxgi.dll d3d11.dll d3d10core.dll d3d12core.dll winemetal.dll; do
  if [[ -f "${SYS32}/${dll}" ]]; then
    mv -f "${SYS32}/${dll}" "${BAK}/${dll}" 2>/dev/null || true
  fi
done

# Мост Metal + Gcenx PE (94208/200704). НЕ GPTK PE — __wine_unix_call на Wine 11.
# НЕ копировать в system32 DXVK/GPTK DLL.
mkdir -p "${WINE_UNIX}"
ln -sf ../../external/libd3dshared.dylib "${WINE_UNIX}/d3d12.so"
ln -sf ../../external/libd3dshared.dylib "${WINE_UNIX}/dxgi.so"
# system32: убрать overrides — builtin из Wine lib
for dll in d3d12.dll dxgi.dll d3d11.dll d3d10core.dll d3d12core.dll winemetal.dll; do
  if [[ -f "${SYS32}/${dll}" ]]; then
    mv -f "${SYS32}/${dll}" "${BAK}/${dll}" 2>/dev/null || true
  fi
done

export WINEPREFIX="${BOTTLE_DIR}"
export DYLD_LIBRARY_PATH="${WHISKY_LIBS}/Libraries/Wine/lib/external:${WINE_LIB}:${DYLD_LIBRARY_PATH:-}"
export WINED3DMETAL=1
export WINEDEBUG=-all

"${WINE64}" reg delete 'HKCU\Software\Wine\DllOverrides' /v d3d11 /f >/dev/null 2>&1 || true
"${WINE64}" reg delete 'HKCU\Software\Wine\DllOverrides' /v d3d10core /f >/dev/null 2>&1 || true
"${WINE64}" reg delete 'HKCU\Software\Wine\DllOverrides' /v dxgi /f >/dev/null 2>&1 || true
"${WINE64}" reg delete 'HKCU\Software\Wine\DllOverrides' /v d3d12 /f >/dev/null 2>&1 || true
"${WINE64}" reg delete 'HKCU\Software\Wine\Direct3D' /v renderer /f >/dev/null 2>&1 || true

if [[ -f "${LS}" ]]; then
  perl -i -pe 's/^\t\tGraphicsApi = ".*"/\t\tGraphicsApi = "d3d12"/' "${LS}"
else
  mkdir -p "$(dirname "${LS}")"
  cat >"${LS}" <<'EOF'
return {
	Options = {
		GraphicsApi = "d3d12",
		VideoPreset = "Low",
	},
}
EOF
fi

if [[ -f "${BOTTLE_DIR}/Metadata.plist" ]]; then
  /usr/libexec/PlistBuddy -c 'Set :dxvkConfig:dxvk false' "${BOTTLE_DIR}/Metadata.plist" 2>/dev/null || true
  /usr/libexec/PlistBuddy -c 'Set :performanceConfig:forceD3D11 false' "${BOTTLE_DIR}/Metadata.plist" 2>/dev/null || \
    /usr/libexec/PlistBuddy -c 'Add :performanceConfig:forceD3D11 bool false' "${BOTTLE_DIR}/Metadata.plist" 2>/dev/null || true
  /usr/libexec/PlistBuddy -c 'Set :graphicsConfig:backend d3dMetal' "${BOTTLE_DIR}/Metadata.plist" 2>/dev/null || true
  /usr/libexec/PlistBuddy -c 'Set :metalConfig:sequoiaCompatMode true' "${BOTTLE_DIR}/Metadata.plist" 2>/dev/null || true
fi

touch "${BOTTLE_DIR}/.ja3_d3dmetal_prepared"
# Ярлык для Whisky Run (тот же CWD + D3DMetal env из Metadata)
cat >"${BOTTLE_DIR}/drive_c/GOG Games/Jagged Alliance 3/run-ja3.cmd" <<'EOF'
@echo off
cd /d "C:\GOG Games\Jagged Alliance 3"
start "" "JA3.exe" -unpacked %*
EOF

echo "OK: D3DMetal graphics для ${BOTTLE_NAME}"
echo "   Скрипт: bash ${GOG_DIR}/run-ja3.sh"
echo "   Whisky Run: C:\\GOG Games\\Jagged Alliance 3\\run-ja3.cmd (не JA3.exe напрямую)"
