#!/usr/bin/env python3
"""Provision exact split-GGUF artifacts to mounted-drive storage (DGR-026).

Reads a split-artifact manifest (`meshnet_node.split_gguf.manifest`),
resumably fetches every split it declares — by default from the split's
manifest `url` over HTTP(S), or from a local directory with `--source-dir`
for splits already staged/mirrored elsewhere — verifies each split's SHA-256
against the manifest, and refuses to write under `/home`.

This tool never invents a quantization, split count, or layer layout: it only
executes whatever the manifest declares. It is opt-in and network-using; the
default test suite never calls it.
"""

from __future__ import annotations

import argparse
import pathlib
import sys

ROOT = pathlib.Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / "packages/node"))

from meshnet_node.split_gguf.manifest import (  # noqa: E402
    SplitArtifactManifestError,
    load_split_artifact_manifest,
)
from meshnet_node.split_gguf.provision import (  # noqa: E402
    SplitProvisionError,
    http_split_fetcher,
    local_directory_fetcher,
    provision_split_artifact,
)


def main(argv: list[str] | None = None) -> int:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--manifest", required=True, type=pathlib.Path, help="split-artifact manifest JSON")
    parser.add_argument("--dest", required=True, type=pathlib.Path, help="mounted-drive destination directory")
    parser.add_argument(
        "--source-dir",
        type=pathlib.Path,
        default=None,
        help="copy splits from this local directory instead of downloading each split's manifest url",
    )
    args = parser.parse_args(argv)

    try:
        manifest = load_split_artifact_manifest(args.manifest)
    except SplitArtifactManifestError as exc:
        print(f"error: {exc}", file=sys.stderr)
        return 2

    if args.source_dir is not None:
        fetch = local_directory_fetcher(args.source_dir)
    else:
        missing_urls = [split.name for split in manifest.splits if not split.url]
        if missing_urls:
            print(
                f"error: manifest splits missing a url and no --source-dir given: {missing_urls}",
                file=sys.stderr,
            )
            return 2
        fetch = http_split_fetcher(lambda split: split.url)

    try:
        result = provision_split_artifact(manifest, args.dest, fetch)
    except SplitProvisionError as exc:
        print(f"error: {exc}", file=sys.stderr)
        return 1

    print(f"provisioned {len(result.verified_splits)} split(s) to {result.dest_dir}")
    return 0


if __name__ == "__main__":
    raise SystemExit(main())
