#!/bin/bash
# PlayTab Uninstall (macOS / Linux)
# Removes device owner and uninstalls PlayTab from the tablet.

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"

echo ""
echo "  ============================================"
echo "   PlayTab Uninstall"
echo "  ============================================"
echo ""
echo "  Before running this script, you MUST first"
echo "  allow uninstall from within the app:"
echo ""
echo "    1. Open PlayTab on the tablet"
echo "    2. Go to Admin Panel (PIN: 1234)"
echo "    3. Scroll to Kiosk Mode section"
echo "    4. Tap \"Enter Maintenance Mode\""
echo "    5. Tap \"Allow Uninstall\""
echo ""
echo "  Also make sure:"
echo "    - Tablet is connected via USB"
echo "    - USB debugging is ON"
echo ""
read -p "  Press Enter when ready..."

# Find adb
find_adb() {
    if command -v adb &>/dev/null; then
        ADB="adb"
        return 0
    elif [ -f "$SCRIPT_DIR/platform-tools/adb" ]; then
        ADB="$SCRIPT_DIR/platform-tools/adb"
        return 0
    fi
    return 1
}

# Download platform-tools if adb not found
if ! find_adb; then
    echo ""
    echo "  ADB not found. Downloading Android Platform Tools..."
    echo ""

    case "$(uname -s)" in
        Darwin*) PLATFORM="darwin" ;;
        Linux*)  PLATFORM="linux" ;;
        *)
            echo "  ERROR: Unsupported OS. Please install ADB manually."
            echo "  https://developer.android.com/tools/releases/platform-tools"
            exit 1
            ;;
    esac

    URL="https://dl.google.com/android/repository/platform-tools-latest-${PLATFORM}.zip"
    ZIP_PATH="$SCRIPT_DIR/platform-tools.zip"

    if command -v curl &>/dev/null; then
        curl -L -o "$ZIP_PATH" "$URL" 2>&1 | tail -1
    elif command -v wget &>/dev/null; then
        wget -O "$ZIP_PATH" "$URL" 2>&1 | tail -1
    else
        echo "  ERROR: Neither curl nor wget found. Cannot download."
        echo "  Install ADB manually: brew install android-platform-tools"
        exit 1
    fi

    if [ ! -f "$ZIP_PATH" ]; then
        echo "  ERROR: Download failed."
        exit 1
    fi

    echo "  Extracting..."
    unzip -qo "$ZIP_PATH" -d "$SCRIPT_DIR"
    rm -f "$ZIP_PATH"

    if ! find_adb; then
        echo "  ERROR: ADB still not found after download."
        exit 1
    fi

    echo "  ADB installed to $SCRIPT_DIR/platform-tools/"
    echo ""
fi

echo ""
echo "  Waiting for device..."
$ADB wait-for-device

MODEL=$($ADB shell getprop ro.product.model | tr -d '\r')
echo ""
echo "  Device found! ($MODEL)"

# Check if PlayTab is installed
if ! $ADB shell pm list packages | grep -q "com.symflo.playtab"; then
    echo ""
    echo "  PlayTab is not installed on this device. Nothing to do."
    echo ""
    exit 0
fi

# Check if device owner is still active
if $ADB shell dumpsys device_policy 2>/dev/null | grep -q "com.symflo.playtab"; then
    echo ""
    echo "  ERROR: Device owner is still active."
    echo ""
    echo "  You must tap \"Allow Uninstall\" in the app first."
    echo "  See the steps above, then run this script again."
    echo ""
    exit 1
fi

echo ""
echo "  Device owner already cleared. Uninstalling..."
if ! $ADB uninstall com.symflo.playtab >/dev/null 2>&1; then
    echo ""
    echo "  ERROR: Uninstall failed. Try uninstalling from Settings > Apps."
    echo ""
    exit 1
fi

echo ""
echo "  ============================================"
echo "   PlayTab has been uninstalled."
echo "  ============================================"
echo ""
echo "  The tablet is back to normal."
echo ""
