#!/usr/bin/env python3
"""Make a private Horizon Web resource archive from a prepared Horizon 26.3 installation."""

import argparse
import tarfile
from pathlib import Path


def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("resources", type=Path, help="Horizon 的 .local/horizon-client/26.3 目录")
    parser.add_argument("-o", "--output", type=Path, default=Path("horizon-web-resources.tar.gz"))
    args = parser.parse_args()
    root = args.resources.resolve()
    marker = root / "horizon-resources.json"
    if root.name != "26.3" or not marker.is_file() or not (root / "registries.json").is_file():
        parser.error("请指定 Horizon 26.3 已准备好的资源目录")
    if args.output.resolve().is_relative_to(root):
        parser.error("请将资源包保存到资源目录之外")
    files = [p for p in sorted(root.rglob("*")) if p != marker and p.is_file() and not p.is_symlink()]
    with tarfile.open(args.output, "w:gz", format=tarfile.GNU_FORMAT, compresslevel=3) as archive:
        archive.add(marker, arcname=marker.name, recursive=False)
        for path in files:
            archive.add(path, arcname=path.relative_to(root).as_posix(), recursive=False)
    print(args.output.resolve())


if __name__ == "__main__":
    main()
