# Finish scheme B after reboot: LSPosed scope + verify spoof + clear MariBank
param([switch]$Reboot)
$ErrorActionPreference = "Stop"
$ProjectRoot = Split-Path -Parent $PSScriptRoot
$adb = "C:\Users\Administrator\AppData\Local\Android\Sdk\platform-tools\adb.exe"
$Pkg = "sg.com.maribankmobile.digitalbank"
$WaitSeconds = 60

Write-Host "Checking adb ($WaitSeconds s timeout)..." -ForegroundColor Cyan
$deadline = (Get-Date).AddSeconds($WaitSeconds)
$ready = $false
while ((Get-Date) -lt $deadline) {
    $lines = & $adb devices 2>&1
    if ($lines -match "1C081FDF600K5Q\s+device") {
        $ready = $true
        break
    }
    if ($lines -match "\tunauthorized") {
        Write-Host "Device connected but UNAUTHORIZED — unlock phone and tap Allow USB debugging." -ForegroundColor Red
    }
    Start-Sleep -Seconds 2
}
if (-not $ready) {
    Write-Host @"

No adb device found after ${WaitSeconds}s.

On Pixel 6:
  1. USB cable connected (data port, not charge-only)
  2. Settings -> Developer options -> USB debugging ON
  3. USB mode: File transfer / PTP
  4. Unlock screen -> tap Allow on RSA prompt
  5. Re-run: .\scripts\maribank-scheme-b-finish.ps1

If USB debugging was turned off earlier, you must enable it on the phone first.

"@ -ForegroundColor Yellow
    exit 1
}
Start-Sleep -Seconds 2

Write-Host "`n=== Modules ===" -ForegroundColor Cyan
& $adb shell su -c "ls /data/adb/modules/"
& $adb shell su -c "magisk --denylist status"

Write-Host "`n=== Identity ===" -ForegroundColor Cyan
& $adb shell su -c "getprop ro.serialno; getprop ro.boot.serialno; settings get secure android_id"
& $adb shell su -c "cat /data/adb/modules/maribank_device_spoof/serial.txt 2>/dev/null; cat /data/adb/modules/maribank_device_spoof/android_id.txt 2>/dev/null"

Write-Host "`n=== LSPosed scope (MariBank SG) ===" -ForegroundColor Cyan
$apkPath = (& $adb shell pm path com.miraclegarden.smsmessage.xposed 2>$null) -replace '^package:', ''
$apkPath = $apkPath.Trim()
if (-not $apkPath) {
    Write-Host "Xposed module not installed" -ForegroundColor Red
    exit 1
}
& $adb shell su -c "cp /data/adb/lspd/config/modules_config.db /sdcard/Download/modules_config.db; chmod 644 /sdcard/Download/modules_config.db"
$db = Join-Path $env:TEMP "modules_config_finish.db"
& $adb pull /sdcard/Download/modules_config.db $db | Out-Null
python "$ProjectRoot\scripts\configure-lsposed.py" $db $apkPath
& $adb push $db /sdcard/Download/modules_config.db | Out-Null
& $adb shell su -c "cp /sdcard/Download/modules_config.db /data/adb/lspd/config/modules_config.db; rm -f /data/adb/lspd/config/modules_config.db-wal /data/adb/lspd/config/modules_config.db-shm; chmod 660 /data/adb/lspd/config/modules_config.db"

Write-Host "`n=== Disable USB debug + clear MariBank ===" -ForegroundColor Cyan
& $adb shell su -c "settings put global adb_enabled 0; settings put global development_settings_enabled 0"
& $adb shell su -c "pm clear $Pkg"

Write-Host "`n=== Done ===" -ForegroundColor Green
Write-Host "Open MariBank -> Sign up -> enter phone -> Next"
Write-Host "Log: adb logcat -d | Select-String 'MariBankEncrypt|MariBankAttest|3100012|deviceFingerprint'"

if ($Reboot) {
    Write-Host "Rebooting..." -ForegroundColor Yellow
    & $adb reboot
}
