#!/usr/bin/env bash
set -euo pipefail
GOG_DIR="$(cd "$(dirname "$0")" && pwd)"
# shellcheck source=config.sh
source "${GOG_DIR}/config.sh"

RCA="${GAMES_DIR}/../temp/gog-rca-$(date +%Y%m%d-%H%M%S)"
LOG="/tmp/gog-web-installer-${BOTTLE_NAME}.log"
mkdir -p "${RCA}"

{
  echo "GOG bottle: ${BOTTLE_NAME} (${BOTTLE_ID})"
  df -h "$(gog_data_volume_path)"
  sysctl vm.swapusage 2>/dev/null || true
  echo ""
  echo "markers:"; ls -la "${BOTTLE_DIR}"/.gog_* 2>/dev/null || true
  echo ""
  du -sh "${BOTTLE_DIR}" "${BOTTLE_DIR}/drive_c/windows/Fonts" 2>/dev/null || true
  echo ""
  echo "log ${LOG}:"
  tail -30 "${LOG}" 2>/dev/null || echo "(нет)"
  echo ""
  echo "winedbg: $(pgrep winedbg 2>/dev/null | wc -l | tr -d ' ')"
  pgrep -lf 'GOG_Galaxy|GalaxyWeb' 2>/dev/null || echo "(нет GOG процессов)"
} | tee "${RCA}/report.txt"

cp "${LOG}" "${RCA}/" 2>/dev/null || true
echo "RCA: ${RCA}"
