#!/usr/bin/env python3
"""Package the expo export dist/ folder into a PixieSprout OTA zip.

Replicates the server-side fix_and_zip_ota.py behavior: the zip contains the
bundle (metadata.json, app.json, package.json, _expo/static/js/android/*.hbc,
assets/*) at the ZIP ROOT (not nested under dist/), matching what the
expo-ota upload endpoint expects.

Usage: python zip_ota.py <dist_dir> <output_zip>
"""
import os
import sys
import zipfile

def main():
    dist_dir = os.path.abspath(sys.argv[1])
    out_zip = os.path.abspath(sys.argv[2])
    if not os.path.isdir(dist_dir):
        print(f"ERROR: dist dir not found: {dist_dir}", file=sys.stderr)
        return 1

    print(f"Zipping {dist_dir} -> {out_zip}")
    added = 0
    with zipfile.ZipFile(out_zip, "w", zipfile.ZIP_DEFLATED) as zf:
        for root, _dirs, files in os.walk(dist_dir):
            for fn in files:
                full = os.path.join(root, fn)
                # Store relative to dist_dir at zip root
                rel = os.path.relpath(full, dist_dir)
                zf.write(full, rel.replace(os.sep, "/"))
                added += 1
    size = os.path.getsize(out_zip)
    print(f"Done: {added} files, {size:,} bytes -> {out_zip}")
    return 0

if __name__ == "__main__":
    sys.exit(main())
