#!/usr/bin/env bash
# ==============================================================================
# flash_pmos.sh - Fastboot Deployment Script for Lenovo Tab M7 (TB-7306F)
# postmarketOS Phosh Edition (Tablet Optimized)
# ==============================================================================
set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

echo "========================================================================"
echo " Lenovo Tab M7 (3rd Gen) TB-7306F - postmarketOS Fastboot Deployer"
echo " Target UI: Phosh (Tablet Cleaned - No Cellular Apps)"
echo "========================================================================"

if ! command -v fastboot &>/dev/null; then
    echo "[-] Error: 'fastboot' is not installed or not in PATH."
    echo "    On Linux: sudo apt-get install android-tools-fastboot (or pacman -S android-tools)"
    echo "    On macOS: brew install android-platform-tools"
    echo "    On Windows: ensure platform-tools folder is in your PATH."
    exit 1
fi

echo "[*] Waiting for device in Fastboot mode..."
echo "    If not connected, power off tablet, then hold [Power] + [Vol Down]."
DEVICE_OUTPUT=$(fastboot devices)

if [ -z "${DEVICE_OUTPUT}" ]; then
    echo "[-] No fastboot devices detected."
    echo "    Please plug in tablet via USB while in Fastboot mode and re-run."
    exit 1
fi

echo "[+] Detected device:"
echo "${DEVICE_OUTPUT}"
echo ""

# Detect active slot for A/B partitioning
SLOT=$(fastboot getvar current-slot 2>&1 | grep "current-slot:" | awk '{print $2}' || echo "a")
[ -z "${SLOT}" ] && SLOT="a"
echo "[*] Target Fastboot slot: ${SLOT}"

VBMETA_IMG="${SCRIPT_DIR}/vbmeta_disabled.img"
BOOT_IMG="${SCRIPT_DIR}/boot.img"
if [ -f "${SCRIPT_DIR}/lenovo-tb7306f-root.simg" ]; then
    ROOTFS_IMG="${SCRIPT_DIR}/lenovo-tb7306f-root.simg"
elif [ -f "${SCRIPT_DIR}/lenovo-tb7306f-root.img" ]; then
    ROOTFS_IMG="${SCRIPT_DIR}/lenovo-tb7306f-root.img"
else
    ROOTFS_IMG=""
fi

echo ""
echo "[*] Files to deploy:"
echo "    vbmeta: ${VBMETA_IMG}"
echo "    boot:   ${BOOT_IMG}"
echo "    rootfs: ${ROOTFS_IMG}"
echo ""

read -rp "Proceed with flashing postmarketOS to slot ${SLOT}? [y/N]: " CONFIRM
if [[ ! "${CONFIRM}" =~ ^[Yy]$ ]]; then
    echo "[-] Aborted by user."
    exit 0
fi

echo ""
echo "[1/3] Flashing AVB verification disabled vbmeta..."
if [ -f "${VBMETA_IMG}" ]; then
    fastboot flash "vbmeta_${SLOT}" "${VBMETA_IMG}"
else
    echo "[*] Using fastboot flag to disable verification..."
    fastboot --disable-verity --disable-verification flash "vbmeta_${SLOT}" "${BOOT_IMG}" || true
fi

echo ""
echo "[2/3] Flashing postmarketOS boot image (Android Boot Header v2)..."
fastboot flash "boot_${SLOT}" "${BOOT_IMG}"

if [ -n "${ROOTFS_IMG}" ] && [ -f "${ROOTFS_IMG}" ]; then
    echo ""
    echo "[3/3] Flashing postmarketOS Phosh tablet rootfs to userdata..."
    fastboot flash userdata "${ROOTFS_IMG}"
else
    echo "[!] Notice: Rootfs image not found. Boot image only was flashed."
fi

echo ""
echo "========================================================================"
echo "[+] Flashing completed successfully!"
echo "    Initial login credentials:"
echo "      Username: user"
echo "      Password: user (or 147147)"
echo "      USB Debug IP: 172.16.42.1 (ssh user@172.16.42.1)"
echo "========================================================================"
read -rp "Reboot tablet now? [y/N]: " REBOOT_NOW
if [[ "${REBOOT_NOW}" =~ ^[Yy]$ ]]; then
    echo "[*] Rebooting..."
    fastboot reboot
fi
