# shellcheck shell=bash
# Шрифты для HoMM/Steam bottle. GOG: scripts/games/gog/wine-fonts.sh (без Steam).

homm_wine_install_fonts_mac() {
  local fonts="${BOTTLE_DIR}/drive_c/windows/Fonts"
  local steam="${STEAM_DIR}"
  mkdir -p "${fonts}"
  for f in /System/Library/Fonts/Supplemental/{Arial,Tahoma,Verdana,"Times New Roman","Courier New"}.ttf; do
    [[ -f "${f}" ]] && cp -n "${f}" "${fonts}/" 2>/dev/null || true
  done
  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
  [[ -f "${fonts}/liberationsans-regular.ttf" ]] || {
    if command -v winetricks >/dev/null 2>&1 && [[ "${WINE_SETUP_WINETRICKS:-0}" == "1" ]]; then
      :
    fi
  }
  if [[ "${WINE_FONTS_USE_STEAM:-1}" == "1" && -d "${steam}" ]]; then
    cp -n "${steam}/resource/fonts/marlett.ttf" "${fonts}/" 2>/dev/null || true
    cp -n "${steam}/clientui/fonts/"*.ttf "${fonts}/" 2>/dev/null || true
  fi
}

homm_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
}

homm_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%.*}"
    homm_wine_register_font_file "${reg}" "${base}"
  done
}

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

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

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

  [[ -f "${fonts}/liberationsans-regular.ttf" ]] && \
    homm_wine_register_font_file 'Liberation Sans' 'liberationsans-regular.ttf'
  [[ -f "${fonts}/liberationsans-bold.ttf" ]] && \
    homm_wine_register_font_file 'Liberation Sans Bold' 'liberationsans-bold.ttf'

  # Substitutes — .NET GenericSansSerif / MS Shell Dlg
  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
  "${WINE64}" reg add "${sub}" /v 'MessageBox' /t REG_SZ /d 'Tahoma' /f >/dev/null 2>&1 || true

  # Wine-internal mapping (libgdiplus / GenericSansSerif)
  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 'MS Shell Dlg 2' /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 'HKCU\Software\Valve\Steam' /v DWriteEnable /t REG_DWORD /d 0 /f >/dev/null 2>&1 || true
  homm_wine_register_all_fonts_dir
}

homm_wine_ensure_fonts() {
  local rebuild="${1:-0}"
  if [[ ! -x "${WINE64}" ]]; then
    echo "Нет Wine64: ${WINE64}"
    return 1
  fi
  export WINEPREFIX="${BOTTLE_DIR}"
  export DYLD_LIBRARY_PATH="${WINE_LIB}:${DYLD_LIBRARY_PATH:-}"
  homm_wine_install_fonts_mac
  homm_wine_register_fonts_registry
  if [[ "${rebuild}" == "1" ]]; then
    echo "wineboot -u (перечитать шрифты, ~30–60 с)…"
    homm_wine_wineboot_fonts
  fi
  local n
  n="$(ls "${BOTTLE_DIR}/drive_c/windows/Fonts" 2>/dev/null | wc -l | tr -d ' ')"
  echo "Шрифты Wine: ${n} файлов, реестр FontSubstitutes + Wine\\Fonts\\Replacements"
}
