"""Assemble commit-generated launcher bitmaps into the real cached dev-client APK.
Usage: python assemble-icons.py cached.apk checkout unsigned.apk
Run APP_VARIANT=development vp exec expo prebuild --clean --platform android --no-install
in checkout/apps/mobile first. Then zipalign and apksigner with the dev debug key.
Only launcher bitmap entries and old signature entries are replaced.
"""
import sys
from pathlib import Path
from zipfile import ZipFile
source, checkout, output = sys.argv[1:]
res = Path(checkout) / 'apps/mobile/android/app/src/main/res'
replacements = {'res/' + f.parent.name + '-v4/' + f.name: f.read_bytes()
                for f in res.glob('mipmap-*/*.webp')}
with ZipFile(source) as src, ZipFile(output, 'w') as dst:
    for entry in src.infolist():
        if entry.filename.startswith('META-INF/'):
            continue
        dst.writestr(entry, replacements.get(entry.filename, src.read(entry)))
print('Assembled', len(replacements), 'commit-generated launcher bitmaps; app code unchanged.')
