#!/usr/bin/env bash
# GOG Galaxy в GOG-bottle без Wine-Inno (gdi32/botva2). Как install-gog-galaxy.sh для HoMM.
set -euo pipefail
GOG_DIR="$(cd "$(dirname "$0")" && pwd)"
# shellcheck source=config.sh
source "${GOG_DIR}/config.sh"

GAMES_DIR="$(cd "${GOG_DIR}/.." && pwd)"
GOG_SETUP="${GAMES_CACHE_DIR:-${GAMES_DIR}/cache}/setup_galaxy.exe"
EXTRACT_DIR="${GAMES_CACHE_DIR:-${GAMES_DIR}/cache}/gog-galaxy-extract-gog-bottle"
GOG_PF="${BOTTLE_DIR}/drive_c/Program Files (x86)/GOG Galaxy"
GOG_PD="${BOTTLE_DIR}/drive_c/ProgramData/GOG.com/Galaxy"
MARKER="${BOTTLE_DIR}/.gog_galaxy_extract_installed"

gog_galaxy_exe() {
  if [[ -f "${GOG_PF}/GalaxyClient.exe" ]]; then
    echo "${GOG_PF}/GalaxyClient.exe"
    return 0
  fi
  local found
  found="$(find "${BOTTLE_DIR}/drive_c/Program Files"* -maxdepth 3 -name 'GalaxyClient.exe' 2>/dev/null | head -1)"
  [[ -n "${found}" ]] && echo "${found}" && return 0
  return 1
}

homm_gog_setup_url() {
  if [[ -n "${GOG_SETUP_URL:-}" ]]; then
    echo "${GOG_SETUP_URL}"
    return 0
  fi
  local json url
  json="$(curl -fsSL "https://remote-config.gog.com/components/webinstaller?component_version=2.0.0")"
  url="$(python3 -c 'import json,sys; d=json.load(sys.stdin); print(d["content"]["windows"]["downloadLink"])' <<<"${json}" 2>/dev/null)" || true
  if [[ -z "${url}" ]]; then
    url="https://content-system.gog.com/open_link/download?path=/open/galaxy/client/setup_galaxy_2.0.100.1.exe"
  fi
  echo "${url}"
}

main() {
  gog_wine_check
  gog_disk_guard 3 || exit 1

  if gog_galaxy_exe >/dev/null 2>&1; then
    echo "GOG Galaxy уже в bottle: $(gog_galaxy_exe)"
    exit 0
  fi

  if [[ ! -f "${GOG_SETUP}" ]]; then
    mkdir -p "$(dirname "${GOG_SETUP}")"
    dl_url="$(homm_gog_setup_url)"
    echo "Скачиваю setup_galaxy.exe (~290 MB)…"
    curl -L --fail --retry 3 -C - -o "${GOG_SETUP}" "${dl_url}"
  fi

  if ! command -v innoextract >/dev/null 2>&1; then
    echo "Нужен innoextract: brew install innoextract"
    exit 1
  fi

  echo "Распаковка GOG Galaxy (innoextract) → ${BOTTLE_NAME}…"
  rm -rf "${EXTRACT_DIR}"
  mkdir -p "${EXTRACT_DIR}"
  innoextract -e -d "${EXTRACT_DIR}" "${GOG_SETUP}"

  mkdir -p "${GOG_PF}" "${GOG_PD}"
  cp -R "${EXTRACT_DIR}/app/." "${GOG_PF}/"
  local pd_src
  pd_src="$(find "${EXTRACT_DIR}" -type d -path '*/GOG.com/Galaxy' 2>/dev/null | head -1)"
  if [[ -n "${pd_src}" ]]; then
    cp -R "${pd_src}/." "${GOG_PD}/"
  fi

  if ! gog_galaxy_exe >/dev/null 2>&1; then
    echo "❌ GalaxyClient.exe не найден после распаковки"
    exit 1
  fi

  echo "VC++ redist (из setup_galaxy)…"
  local vc_x86="${EXTRACT_DIR}/tmp/VC_redist.x86.exe"
  local vc_x64="${EXTRACT_DIR}/tmp/VC_redist.x64.exe"
  if [[ -f "${vc_x86}" ]]; then
    export_wine_env_gog
    cp -f "${vc_x86}" "${BOTTLE_DIR}/drive_c/VC_redist.x86.exe"
    cp -f "${vc_x64}" "${BOTTLE_DIR}/drive_c/VC_redist.x64.exe"
    "${WINE64}" C:\\VC_redist.x86.exe /install /quiet /norestart
    "${WINE64}" C:\\VC_redist.x64.exe /install /quiet /norestart
  fi

  local fonts_dst="${BOTTLE_DIR}/drive_c/windows/Fonts"
  mkdir -p "${fonts_dst}"
  cp -f "${EXTRACT_DIR}"/fonts/*.ttf "${fonts_dst}/" 2>/dev/null || true

  bash "${GOG_DIR}/register-galaxy-registry.sh"

  date -u +%Y-%m-%dT%H:%M:%SZ >"${MARKER}"
  echo "OK: $(gog_galaxy_exe)"
}

main "$@"
