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

OUT="${1:-${HOME}/code/local_machine/temp/gog-setup-rca-$(date +%Y%m%d-%H%M%S)}"
mkdir -p "${OUT}"

{
  echo "=== $(date) ==="
  echo "Bottle: ${BOTTLE_NAME} ${BOTTLE_ID}"
  echo ""
  echo "=== disk ==="
  df -h /System/Volumes/Data | tail -1
  echo ""
  echo "=== processes ==="
  pgrep -fl 'Galaxy|GOG_Galaxy' 2>/dev/null || echo "(none)"
  echo ""
  echo "=== GalaxyClient ==="
  find "${BOTTLE_DIR}/drive_c" -name 'GalaxyClient.exe' 2>/dev/null || true
} >"${OUT}/summary.txt"

for f in \
  "/tmp/gog-web-installer-${BOTTLE_NAME}.log" \
  "/tmp/gog-galaxy-setup-${BOTTLE_NAME}.log"; do
  [[ -f "${f}" ]] && cp "${f}" "${OUT}/"
done

find "${BOTTLE_DIR}/drive_c" -name 'Setup Log*.txt' 2>/dev/null | while read -r f; do
  cp "${f}" "${OUT}/$(basename "${f}" | tr ' #' '_')"
done

find "${BOTTLE_DIR}/drive_c/ProgramData/GOG.com" -name '*.log' 2>/dev/null | while read -r f; do
  cp "${f}" "${OUT}/$(basename "${f}")"
done

echo "${OUT}"
