#!/usr/bin/env bash
# Steam в bottle без GUI-мастера (распаковка SteamSetup.exe).
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
# shellcheck source=config.sh
source "${SCRIPT_DIR}/config.sh"

if [[ ! -f "${STEAM_SETUP}" ]]; then
  echo "Скачиваю SteamSetup.exe..."
  mkdir -p "${GAMES_CACHE_DIR}"
  curl -L --fail -o "${STEAM_SETUP}" \
    "https://cdn.fastly.steamstatic.com/client/installer/SteamSetup.exe"
fi

command -v 7z >/dev/null || { echo "Нужен: brew install p7zip"; exit 1; }

pkill -f wineserver 2>/dev/null || true
sleep 1

EXTRACT="/tmp/steam-extract-$$"
rm -rf "${EXTRACT}"
mkdir -p "${EXTRACT}"
7z x -o"${EXTRACT}" "${STEAM_SETUP}" -y >/dev/null

rm -rf "${STEAM_DIR}"
mkdir -p "${STEAM_DIR}"
cp -R "${EXTRACT}/Steam.exe" "${EXTRACT}/uninstall.exe" \
  "${EXTRACT}/bin" "${EXTRACT}/public" "${STEAM_DIR}/"
rm -rf "${EXTRACT}"

echo "OK: Steam скопирован в bottle. Запуск: bash ${SCRIPT_DIR}/start.sh"
