#!/usr/bin/env bash
# android_app_uninstall — Desinstala una app Android via adb uninstall.

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

android_app_uninstall() {
    # Parse --serial (consumes it, rest stays in ADB_PICK_REST)
    local serial
    adb_pick_serial "$@" || return 1
    local serial="$ADB_PICK_SERIAL"
    set -- "${ADB_PICK_REST[@]}"

    # Parse --keep-data flag
    local keep_data=0
    local args=()
    while [[ $# -gt 0 ]]; do
        case "$1" in
            --keep-data) keep_data=1; shift ;;
            *) args+=("$1"); shift ;;
        esac
    done
    set -- "${args[@]}"

    local pkg="${1:-}"
    if [[ -z "$pkg" ]]; then
        echo "android_app_uninstall: package obligatorio." >&2
        return 1
    fi

    if (( keep_data )); then
        adb_s "$serial" uninstall -k "$pkg" || return 1
    else
        adb_s "$serial" uninstall "$pkg" || return 1
    fi

    echo "uninstalled $pkg on $serial"
}

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