# Pull native libs from connected device (Pixel 6 with MariBank installed)
$ErrorActionPreference = "Stop"
$Root = Split-Path -Parent $PSScriptRoot
$OutDir = Join-Path $Root "extracted\native"
New-Item -ItemType Directory -Force -Path $OutDir | Out-Null

$AdbCandidates = @(
    (Join-Path $Root "..\platform-tools\adb.exe"),
    "$env:LOCALAPPDATA\Android\Sdk\platform-tools\adb.exe",
    "adb"
)
$Adb = $AdbCandidates | Where-Object { $_ -eq "adb" -or (Test-Path $_) } | Select-Object -First 1
if (-not $Adb) { throw "adb not found" }

$Pkg = "ph.seabank.seabank"
$Base = & $Adb shell pm path $Pkg 2>$null
if (-not $Base) { throw "package $Pkg not installed on device" }

$Paths = ($Base -split "`n" | ForEach-Object { $_.Trim() -replace "^package:", "" })
foreach ($ApkPath in $Paths) {
    $Name = Split-Path $ApkPath -Leaf
    $LocalApk = Join-Path $OutDir $Name
    Write-Host "pull $ApkPath -> $LocalApk"
    & $Adb pull $ApkPath $LocalApk | Out-Null
    if ($Name -like "split_config.arm64*") {
        python (Join-Path $Root "scripts\extract_all_so.py")
    }
}

Write-Host "done. SO files in $OutDir"
