#!/usr/bin/env bash
# JA3 (GOG) через CrossOver + D3DMetal — D3D12 на Apple Silicon.
set -euo pipefail
GOG_DIR="$(cd "$(dirname "$0")" && pwd)"
# shellcheck source=config.sh
source "${GOG_DIR}/config.sh"
# shellcheck source=gog-process.sh
source "${GOG_DIR}/gog-process.sh"

CX="${HOME}/Applications/CrossOver.app/Contents/SharedSupport/CrossOver"
[[ -d "/Applications/CrossOver.app" ]] && CX="/Applications/CrossOver.app/Contents/SharedSupport/CrossOver"
CX_BOTTLE="JA3-GOG"
CX_DIR="${HOME}/Library/Application Support/CrossOver/Bottles/${CX_BOTTLE}"

[[ -d "${CX}" ]] || {
  echo "❌ CrossOver не установлен: brew install --cask crossover"
  exit 1
}

if [[ ! -f "${CX_DIR}/drive_c/GOG Games/Jagged Alliance 3/JA3.exe" ]]; then
  bash "${GOG_DIR}/setup-ja3-crossover.sh"
fi

bash "${GOG_DIR}/emergency-stop.sh" >/dev/null 2>&1 || gog_kill_all
sleep 2

gog_swap_guard_ja3_launch "${JA3_SWAP_SOFT_MB:-1200}" "${JA3_SWAP_HARD_MB:-4096}" || exit 1

echo "Запуск JA3 (CrossOver D3DMetal; без Steam.app)…"
echo "Cmd+Tab → JA3. В графике: отключить heat haze."
echo "(лог «Steam … GOG» = GOG API-шим, не Steam client)"

# start /d — CWD с CommonLua/ (обязательно; --cx-app без /d → autorun.lua not found)
exec "${CX}/bin/wine" --bottle "${CX_BOTTLE}" start /d "C:\\GOG Games\\Jagged Alliance 3" \
  "C:\\GOG Games\\Jagged Alliance 3\\JA3.exe" ${JA3_EXTRA_ARGS:--unpacked}
