# MariBank SG 一键：编译 → 安装模块 → 换 ID → 清数据 → 启动 SG
# 用法:
#   .\scripts\maribank-sg-all-in.ps1
#   .\scripts\maribank-sg-all-in.ps1 -DisableUsbDebug   # 测前关 USB 调试（会断 adb）
#   .\scripts\maribank-sg-all-in.ps1 -SkipBuild         # 仅换 ID + 启动
param(
    [switch]$SkipBuild,
    [switch]$DisableUsbDebug,
    [switch]$KeepAdb,
    [switch]$ColdStart
)

$ErrorActionPreference = "Stop"
$ProjectRoot = Split-Path -Parent $PSScriptRoot

Write-Host "=== MariBank SG All-In ===" -ForegroundColor Cyan

if (-not $SkipBuild) {
    Write-Host "`n[1/5] Build debug APKs..." -ForegroundColor Cyan
    & "$ProjectRoot\scripts\build-debug.ps1"
    if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }

    Write-Host "`n[2/5] Install APKs..." -ForegroundColor Cyan
    & "$ProjectRoot\scripts\install-debug.ps1"
    if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
} else {
    Write-Host "`n[1-2/5] Skip build/install (-SkipBuild)" -ForegroundColor Yellow
}

Write-Host "`n[3/5] New device identity + clear MariBank SG..." -ForegroundColor Cyan
& "$ProjectRoot\scripts\maribank-spoof-device.ps1" -NewIdentity -ClearMariBank
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }

Write-Host "`n[4/5] LSPosed 必做（PC 无法代劳）" -ForegroundColor Yellow
Write-Host "  1. LSPosed -> 模块 -> notiMessage 启用"
Write-Host "  2. 作用域勾选: sg.com.maribankmobile.digitalbank"
Write-Host "  3. 对该包: 重新优化 -> 强行停止 -> 启动（等价软重启）"
Write-Host "  4. Shamiko DenyList 含 SG 全部进程，Enforce=OFF"
Write-Host ""
Read-Host "完成 LSPosed 软重启后按 Enter 继续" | Out-Null

$launchArgs = @("-StopPh")
if ($ColdStart) { $launchArgs += "-ColdStart" }

Write-Host "`n[5/5] Launch MariBank SG..." -ForegroundColor Cyan
& "$ProjectRoot\scripts\launch-maribank-sg.ps1" @launchArgs
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }

if ($DisableUsbDebug -and -not $KeepAdb) {
    Write-Host "`n=== Disable USB debugging (SG stricter) ===" -ForegroundColor Cyan
    & "$ProjectRoot\scripts\maribank-sg-register.ps1" -DisableUsbDebug
} else {
    Write-Host "`n=== Ready to test ===" -ForegroundColor Green
    Write-Host "Phone: Sign up -> +65 -> Next"
    Write-Host "Log:   .\scripts\maribank-sg-register.ps1 -CaptureLog -KeepAdb"
    Write-Host "       .\scripts\logcat-maribank.ps1"
    Write-Host "Strict test (no adb): .\scripts\maribank-sg-all-in.ps1 -SkipBuild -DisableUsbDebug"
}
