#!/usr/bin/env bash
set -euo pipefail
OUT=${1:-/tmp/hevy-capture-$(date +%Y%m%d-%H%M%S)}
mkdir -p "$OUT"
echo "[hevy-capture] output: $OUT"
adb devices | tee "$OUT/adb-devices.txt"
adb shell date | tee "$OUT/device-date.txt"
adb shell pm path com.hevy | tee "$OUT/hevy-pm-path.txt" || true
adb shell dumpsys package com.hevy > "$OUT/hevy-package.txt" || true
adb shell dumpsys notification > "$OUT/notifications-before.txt" || true
adb shell dumpsys activity top > "$OUT/activity-top.txt" || true
adb shell dumpsys window > "$OUT/window.txt" || true
# Try run-as extraction; may fail if app is not debuggable. Keep logs.
adb shell 'run-as com.hevy ls -la /data/data/com.hevy 2>&1' | tee "$OUT/run-as-ls.txt" || true
adb shell 'run-as com.hevy ls -la /data/data/com.hevy/databases 2>&1' | tee "$OUT/run-as-databases.txt" || true
adb shell 'run-as com.hevy cp /data/data/com.hevy/databases/RKStorage /sdcard/RKStorage.hevy 2>&1' | tee "$OUT/run-as-copy-rkstorage.txt" || true
adb pull /sdcard/RKStorage.hevy "$OUT/RKStorage.hevy" 2>&1 | tee "$OUT/pull-rkstorage.txt" || true
adb shell screencap -p /sdcard/hevy-screen.png || true
adb pull /sdcard/hevy-screen.png "$OUT/hevy-screen.png" || true
echo "[hevy-capture] done. Start a Hevy workout and rerun during active/rest state."
