# shellcheck shell=bash
# Шрифты для GOG web-installer (.NET). Только macOS + corefonts — без Steam.

gog_wine_install_fonts_mac() {
  local fonts="${BOTTLE_DIR}/drive_c/windows/Fonts"
  mkdir -p "${fonts}"
  for f in /System/Library/Fonts/Supplemental/{Arial,Tahoma,Verdana,"Times New Roman","Courier New","Segoe UI"}.ttf \
    /System/Library/Fonts/Supplemental/Arial*.ttf \
    /Library/Fonts/Arial*.ttf; do
    [[ -f "${f}" ]] && cp -n "${f}" "${fonts}/" 2>/dev/null || true
  done
  # .NET GenericSansSerif → Microsoft Sans Serif / Arial
  cp -n "${fonts}/Arial.ttf" "${fonts}/sans-serif.ttf" 2>/dev/null || true
  cp -n "${fonts}/Arial.ttf" "${fonts}/arial.ttf" 2>/dev/null || true
  cp -n "${fonts}/Arial.ttf" "${fonts}/micross.ttf" 2>/dev/null || true
  cp -n "${fonts}/Arial.ttf" "${fonts}/Microsoft Sans Serif.ttf" 2>/dev/null || true
  cp -n "${fonts}/Tahoma.ttf" "${fonts}/tahoma.ttf" 2>/dev/null || true
}

gog_wine_register_font_file() {
  local reg_name="$1"
  local ttf_file="$2"
  export WINEPREFIX="${BOTTLE_DIR}"
  "${WINE64}" reg add 'HKLM\Software\Microsoft\Windows NT\CurrentVersion\Fonts' \
    /v "${reg_name} (TrueType)" /t REG_SZ /d "${ttf_file}" /f >/dev/null 2>&1 || true
}

gog_wine_register_all_fonts_dir() {
  local fonts="${BOTTLE_DIR}/drive_c/windows/Fonts"
  local f base reg
  for f in "${fonts}"/*; do
    [[ -f "${f}" ]] || continue
    [[ "${f}" == *.ttf || "${f}" == *.TTF ]] || continue
    base="$(basename "${f}")"
    reg="${base%.*}"
    gog_wine_register_font_file "${reg}" "${base}"
  done
}

gog_wine_wineboot_fonts() {
  export WINEPREFIX="${BOTTLE_DIR}"
  export DYLD_LIBRARY_PATH="${WINE_LIB}:${DYLD_LIBRARY_PATH:-}"
  "${WINE64}" wineboot -u 2>/dev/null || true
}

gog_wine_register_fonts_registry() {
  export WINEPREFIX="${BOTTLE_DIR}"
  local fonts="${BOTTLE_DIR}/drive_c/windows/Fonts"

  gog_wine_register_font_file 'Arial' 'arial.ttf'
  gog_wine_register_font_file 'Tahoma' 'tahoma.ttf'
  gog_wine_register_font_file 'Verdana' 'verdana.ttf'
  gog_wine_register_font_file 'Times New Roman' 'Times New Roman.ttf'
  gog_wine_register_font_file 'Courier New' 'Courier New.ttf'
  gog_wine_register_font_file 'Microsoft Sans Serif' 'micross.ttf'

  local sub='HKLM\Software\Microsoft\Windows NT\CurrentVersion\FontSubstitutes'
  "${WINE64}" reg add "${sub}" /v 'MS Shell Dlg' /t REG_SZ /d 'Tahoma' /f >/dev/null 2>&1 || true
  "${WINE64}" reg add "${sub}" /v 'MS Shell Dlg 2' /t REG_SZ /d 'Tahoma' /f >/dev/null 2>&1 || true
  "${WINE64}" reg add "${sub}" /v 'MS Sans Serif' /t REG_SZ /d 'Arial' /f >/dev/null 2>&1 || true
  "${WINE64}" reg add "${sub}" /v 'Segoe UI' /t REG_SZ /d 'Tahoma' /f >/dev/null 2>&1 || true

  local rep='HKCU\Software\Wine\Fonts\Replacements'
  "${WINE64}" reg add "${rep}" /v '@@MS_SHELLDLG' /t REG_SZ /d 'Tahoma' /f >/dev/null 2>&1 || true
  "${WINE64}" reg add "${rep}" /v 'MS Shell Dlg' /t REG_SZ /d 'Tahoma' /f >/dev/null 2>&1 || true
  "${WINE64}" reg add "${rep}" /v 'Microsoft Sans Serif' /t REG_SZ /d 'Arial' /f >/dev/null 2>&1 || true
  "${WINE64}" reg add "${rep}" /v 'SansSerif' /t REG_SZ /d 'Arial' /f >/dev/null 2>&1 || true
  "${WINE64}" reg add "${sub}" /v 'SansSerif' /t REG_SZ /d 'Arial' /f >/dev/null 2>&1 || true

  gog_wine_register_all_fonts_dir
}

gog_wine_ensure_fonts() {
  local rebuild="${1:-0}"
  export_wine_env_gog
  gog_wine_install_fonts_mac
  gog_wine_register_fonts_registry
  if [[ "${rebuild}" == "1" ]]; then
    echo "wineboot -u (~30–60 с)…"
    gog_wine_wineboot_fonts
  fi
  local n
  n="$(ls "${BOTTLE_DIR}/drive_c/windows/Fonts" 2>/dev/null | wc -l | tr -d ' ')"
  echo "Шрифты (${BOTTLE_NAME}): ${n} файлов"
}
