#!/usr/bin/env bash
# android_app_launch — Launch an Android app via adb am start or monkey LAUNCHER intent.
# Usage: android_app_launch [--serial <S>] <package> [<activity>]
set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/adb_wsl.sh"

android_app_launch() {
    adb_pick_serial "$@" || exit 3
    local serial="$ADB_PICK_SERIAL"
    set -- "${ADB_PICK_REST[@]}"

    local pkg="${1:-}"
    local activity="${2:-}"

    if [[ -z "$pkg" ]]; then
        echo "android_app_launch: package is required." >&2
        return 1
    fi

    if [[ -n "$activity" ]]; then
        adb_s "$serial" shell am start -n "$pkg/$activity"
    else
        adb_s "$serial" shell monkey -p "$pkg" -c android.intent.category.LAUNCHER 1
    fi

    echo "launched $pkg on $serial"
}

if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
    android_app_launch "$@"
fi
