#!/bin/bash
# View Android logcat output filtered for the app.
#
# Usage: ./scripts/logcat.sh [debug|release]   (default: debug)
#
# The debug build has applicationIdSuffix ".debug" so it can be installed
# alongside a release build; pick the package to follow accordingly.

set -e

BUILD_TYPE="${1:-debug}"

if [ "$BUILD_TYPE" = "release" ]; then
    APP_PACKAGE="com.dtourolle.jellytau"
else
    APP_PACKAGE="com.dtourolle.jellytau.debug"
fi

echo "📱 Showing logcat for $APP_PACKAGE"
echo "Press Ctrl+C to stop"
echo ""

# Prefer PID-scoped output when the app is running — it drops the noise that a
# text grep can't. Fall back to the old keyword filter when it isn't (so you can
# start the script first and then launch the app).
PID="$(adb shell pidof "$APP_PACKAGE" 2>/dev/null | tr -d '\r\n' | awk '{print $1}')"

if [ -n "$PID" ]; then
    echo "   (attached to pid $PID)"
    adb logcat --pid="$PID"
else
    echo "   (app not running — falling back to keyword filter)"
    adb logcat | grep -i "$APP_PACKAGE\|jellytau\|tauri\|rust"
fi
