#!/usr/bin/env bash
# HoMM / Steam bottle (HoMM-OldenEra): шрифты + сеть Wine для Steam UI.
# GOG web-stub: scripts/games/gog/ — отдельный bottle, без Steam.
# См. docs/games/WINE_MAC_RUNBOOK.md
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
# shellcheck source=config.sh
source "${SCRIPT_DIR}/config.sh"
if [[ -z "${BOTTLE_DIR:-}" ]]; then
  echo "❌ config.sh не задал BOTTLE_DIR (файл пустой или повреждён?)"
  exit 1
fi
if [[ ! -d "${BOTTLE_DIR}/drive_c" ]]; then
  echo "❌ Нет bottle drive_c: ${BOTTLE_DIR}"
  echo "   Откройте Whisky → bottle «${BOTTLE_NAME:-HoMM-OldenEra}» и дождитесь создания префикса."
  exit 1
fi
# shellcheck source=wine-fonts.sh
source "${SCRIPT_DIR}/wine-fonts.sh"

WINE_SETUP_VERSION="3"
WINE_SETUP_MARKER="${BOTTLE_DIR}/.wine_mac_setup_v${WINE_SETUP_VERSION}"

cmd_wine_check() {
  if [[ ! -x "${WINE64}" ]]; then
    echo "Нет WhiskyWine: bash ${SCRIPT_DIR}/install-whiskywine-manual.sh"
    exit 1
  fi
}

cmd_install_fonts_winetricks() {
  [[ "${WINE_SETUP_WINETRICKS:-0}" == "1" ]] || return 0
  if ! command -v winetricks >/dev/null 2>&1; then
    echo "winetricks: brew install winetricks, затем WINE_SETUP_WINETRICKS=1"
    return 0
  fi
  export WINEPREFIX="${BOTTLE_DIR}"
  export WINE="${WINE64}"
  export DYLD_LIBRARY_PATH="${WINE_LIB}:${DYLD_LIBRARY_PATH:-}"
  export WINEDEBUG=-all
  echo "winetricks liberation (до 3 мин)…"
  if command -v gtimeout >/dev/null 2>&1; then
    gtimeout 180 winetricks -q liberation 2>&1 | tail -5 || true
  elif command -v timeout >/dev/null 2>&1; then
    timeout 180 winetricks -q liberation 2>&1 | tail -5 || true
  else
    winetricks -q liberation 2>&1 | tail -5 || true
  fi
  homm_wine_register_fonts_registry
}

cmd_wine_network() {
  export WINEPREFIX="${BOTTLE_DIR}"
  "${WINE64}" reg add 'HKCU\Software\Wine\Network' /v DnsIPv6Enabled /t REG_DWORD /d 0 /f >/dev/null 2>&1 || true
  "${WINE64}" reg add 'HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings' \
    /v DisableIPv6 /t REG_DWORD /d 1 /f >/dev/null 2>&1 || true
  "${WINE64}" reg add 'HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings' \
    /v ProxyEnable /t REG_DWORD /d 0 /f >/dev/null 2>&1 || true
  echo "Wine: DnsIPv6Enabled=0, ProxyEnable=0"
}

main() {
  local force=0 fonts_only=0
  for arg in "$@"; do
    case "${arg}" in
      --force) force=1 ;;
      --fonts-only) fonts_only=1 ;;
    esac
  done

  cmd_wine_check

  if [[ "${fonts_only}" -eq 1 ]]; then
    homm_wine_ensure_fonts 1
    exit 0
  fi

  if [[ -f "${WINE_SETUP_MARKER}" && "${force}" -eq 0 ]]; then
    echo "Bottle подготовлен: ${WINE_SETUP_MARKER}"
    echo "Обновить только шрифты: bash ${SCRIPT_DIR}/setup-wine-bottle.sh --fonts-only"
    echo "Полный повтор: bash ${SCRIPT_DIR}/setup-wine-bottle.sh --force"
    exit 0
  fi

  echo "Подготовка bottle «${BOTTLE_NAME}» (v${WINE_SETUP_VERSION})…"
  homm_wine_ensure_fonts 1
  cmd_install_fonts_winetricks
  homm_wine_ensure_fonts 0
  cmd_wine_network
  date -u +"%Y-%m-%dT%H:%M:%SZ" >"${WINE_SETUP_MARKER}"
  echo ""
  echo "OK. Steam/HoMM: bash ${SCRIPT_DIR}/start.sh steam-ui"
  echo "GOG (другой bottle): bash ${SCRIPT_DIR}/gog/README.md"
}

main "$@"
