#!/usr/bin/env bash
# Один раз: GOG-bottle — шрифты + сеть Wine. Без Steam/HoMM.
set -euo pipefail
GOG_DIR="$(cd "$(dirname "$0")" && pwd)"
# shellcheck source=config.sh
source "${GOG_DIR}/config.sh"
# shellcheck source=wine-fonts.sh
source "${GOG_DIR}/wine-fonts.sh"

GOG_SETUP_VERSION="1"
GOG_SETUP_MARKER="${BOTTLE_DIR}/.gog_setup_v${GOG_SETUP_VERSION}"

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

  gog_wine_check

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

  if [[ -f "${GOG_SETUP_MARKER}" && "${force}" -eq 0 ]]; then
    echo "GOG bottle готов: ${GOG_SETUP_MARKER}"
    echo "  Шрифты: bash ${GOG_DIR}/setup-bottle.sh --fonts-only"
    echo "  Заново: bash ${GOG_DIR}/setup-bottle.sh --force"
    exit 0
  fi

  echo "Подготовка GOG bottle «${BOTTLE_NAME}» (v${GOG_SETUP_VERSION})…"
  gog_wine_ensure_fonts 1
  export_wine_env_gog
  "${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"
  date -u +"%Y-%m-%dT%H:%M:%SZ" >"${GOG_SETUP_MARKER}"
  echo ""
  echo "OK. Дальше:"
  echo "  bash ${GOG_DIR}/install-gdiplus.sh    # обязательно для .NET"
  echo "  bash ${GOG_DIR}/install-corefonts.sh"
  echo "  bash ${GOG_DIR}/run-web-installer.sh /path/GOG_Galaxy_*.exe"
}

main "$@"
