# MariBank device spoof — Magisk resetprop + optional module install
# Usage:
#   .\scripts\maribank-spoof-device.ps1              # apply resetprop once via adb su
#   .\scripts\maribank-spoof-device.ps1 -InstallModule # zip & push Magisk module
#   .\scripts\maribank-spoof-device.ps1 -NewIdentity   # regenerate serial/android_id files on device
#   .\scripts\maribank-spoof-device.ps1 -ClearMariBank # pm clear MariBank after spoof
param(
    [switch]$InstallModule,
    [switch]$NewIdentity,
    [switch]$ClearMariBank,
    [string]$DeviceSerial = ""
)

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

$adb = "C:\Users\Administrator\AppData\Local\Android\Sdk\platform-tools\adb.exe"
$ModuleDir = Join-Path $PSScriptRoot "magisk\maribank-device-spoof"
$Pkg = "sg.com.maribankmobile.digitalbank"

if (-not (Test-Path $adb)) {
    Write-Host "adb not found: $adb" -ForegroundColor Red
    exit 1
}

function Invoke-AdbShell($cmd) {
    & $adb shell "su -c '$cmd'" 2>&1
}

function Test-Magisk {
    $m = Invoke-AdbShell "command -v resetprop 2>/dev/null || ls /data/adb/magisk/magisk 2>/dev/null"
    return ($LASTEXITCODE -eq 0 -and "$m" -match "resetprop|magisk")
}

Write-Host "=== MariBank Device Spoof (方案 B: Magisk resetprop) ===" -ForegroundColor Cyan
& $adb devices -l

if (-not (Test-Magisk)) {
    Write-Host "Magisk/resetprop not found on device. Install Magisk first." -ForegroundColor Red
    exit 1
}

if ($InstallModule) {
    $zipPath = Join-Path $env:TEMP "maribank-device-spoof.zip"
    if (Test-Path $zipPath) { Remove-Item $zipPath -Force }
    # Use tar (Windows 10+) for Unix paths; Magisk needs post-fs-data.sh at zip root
    Push-Location $ModuleDir
    tar -a -cf $zipPath module.prop post-fs-data.sh service.sh
    Pop-Location
    Write-Host "Pushing module to /sdcard/Download/ ..."
    & $adb push $zipPath /sdcard/Download/maribank-device-spoof.zip
    Write-Host @"

Module zip pushed. On phone:
  1. Magisk -> Modules -> Install from storage -> maribank-device-spoof.zip
  2. Reboot
  3. Enable Shamiko (see below)

"@ -ForegroundColor Yellow
}

if ($NewIdentity) {
    Write-Host "Removing saved identity (module will regenerate on next boot) ..."
    Invoke-AdbShell "rm -f /data/adb/modules/maribank_device_spoof/serial.txt /data/adb/modules/maribank_device_spoof/android_id.txt"
}

if ($DeviceSerial -eq "") {
    $DeviceSerial = -join ((48..57) + (65..90) + (97..122) | Get-Random -Count 16 | ForEach-Object { [char]$_ })
}
$AndroidId = -join ((48..57) + (97..102) | Get-Random -Count 16 | ForEach-Object { [char]$_ })

Write-Host "Applying one-shot resetprop (serial=$DeviceSerial android_id=$AndroidId) ..."

$props = @(
    "resetprop ro.serialno $DeviceSerial",
    "resetprop ro.boot.serialno $DeviceSerial",
    "resetprop persist.sys.serialno $DeviceSerial",
    "resetprop ro.debuggable 0",
    "resetprop ro.secure 1",
    "resetprop ro.build.tags release-keys",
    "resetprop ro.boot.verifiedbootstate green",
    "resetprop ro.boot.flash.locked 1",
    "resetprop ro.boot.vbmeta.device_state locked",
    "resetprop ro.boot.veritymode enforcing",
    "resetprop init.svc.adbd stopped",
    "resetprop persist.sys.adb_enable 0"
)
foreach ($p in $props) {
    Invoke-AdbShell $p | Out-Null
}
Invoke-AdbShell "settings put secure android_id $AndroidId" | Out-Null

Write-Host "Verify:" -ForegroundColor Green
Invoke-AdbShell "getprop ro.serialno; getprop ro.boot.serialno; settings get secure android_id"

if ($ClearMariBank) {
    Write-Host "Clearing MariBank app data ..."
    Invoke-AdbShell "pm clear $Pkg"
    Write-Host "MariBank data cleared. Cold start Sign up again." -ForegroundColor Green
}

Write-Host @"

--- Shamiko checklist (required for scheme B) ---
  1. Magisk -> Settings -> Configure DenyList -> enable DenyList
  2. DenyList -> add $Pkg (all sub-processes)
  3. Install Shamiko module (Magisk repo / GitHub releases)
  4. Magisk -> Settings -> hide Magisk app (optional)
  5. LSPosed: keep module scoped to MariBank; soft reboot MariBank after spoof
  6. Turn OFF USB debugging before testing register (or rely on Xposed adb bypass)

To install persistent module:
  .\scripts\maribank-spoof-device.ps1 -InstallModule

To force new identity on next boot:
  .\scripts\maribank-spoof-device.ps1 -NewIdentity -InstallModule
  (then reboot)

Log on device: /cache/maribank_device_spoof.log

"@ -ForegroundColor Cyan
