#!/bin/bash

PACKAGE_NAME=com.example

function monitor_logs {
    echo "Monitoring logs for PID: $1"
    adb logcat --pid="$1" &
    LOGCAT_PID=$!

    trap 'echo "Received SIGINT, cleaning up..."; kill $LOGCAT_PID; exit 1' SIGINT

    while adb shell "pidof -s $PACKAGE_NAME >/dev/null"; do
        sleep 1
    done

    echo "Process $1 stopped. Killing logcat process..."
    kill $LOGCAT_PID
}

while true; do
    PID=$(adb shell pidof -s $PACKAGE_NAME)

    if [ -n "$PID" ]; then
        monitor_logs "$PID"
    else
        echo "$PACKAGE_NAME is not running. Waiting for process..."
        sleep 1
    fi
done
