#!/usr/bin/env bash
# Удалить клиент Steam из GOG-bottle (если попал). JA3/GOG — без Steam.app.
# Не трогает steam_api64.dll в папке игры (GOG Galaxy wrapper, не Steam client).
set -euo pipefail
GOG_DIR="$(cd "$(dirname "$0")" && pwd)"
# shellcheck source=config.sh
source "${GOG_DIR}/config.sh"

STEAM_DIRS=(
  "${BOTTLE_DIR}/drive_c/Program Files (x86)/Steam"
  "${BOTTLE_DIR}/drive_c/Program Files/Steam"
  "${BOTTLE_DIR}/drive_c/Steam"
)

main() {
  echo "=== Purge Steam из bottle ${BOTTLE_NAME} ==="

  pkill -9 -f "${BOTTLE_ID}.*[Ss]team" 2>/dev/null || true
  pkill -9 -f "${BOTTLE_DIR}.*steam\.exe" 2>/dev/null || true

  local d removed=0
  for d in "${STEAM_DIRS[@]}"; do
    if [[ -d "${d}" ]]; then
      echo "  удаляю: ${d}"
      rm -rf "${d}"
      removed=1
    fi
  done

  find "${BOTTLE_DIR}/drive_c" -maxdepth 4 \( -iname 'steam.exe' -o -iname 'steamservice.exe' \) 2>/dev/null | while read -r f; do
    echo "  удаляю: ${f}"
    rm -f "${f}"
    removed=1
  done

  export WINEPREFIX="${BOTTLE_DIR}"
  export DYLD_LIBRARY_PATH="${WINE_LIB}:${DYLD_LIBRARY_PATH:-}"
  if [[ -x "${WINE64}" ]]; then
  "${WINE64}" reg delete 'HKCU\Software\Valve' /f >/dev/null 2>&1 || true
  "${WINE64}" reg delete 'HKLM\Software\Valve' /f >/dev/null 2>&1 || true
  fi

  if [[ "${removed}" -eq 0 ]]; then
    echo "OK: каталога Steam в bottle не было."
  else
    echo "OK: Steam client удалён из bottle."
  fi
  echo ""
  echo "Примечание: JA3.exe использует steam_api64.dll + Galaxy.dll (GOG), это не Steam.app."
}

main "$@"
