"""Finish user-only tool launchers/configuration; no service or model start."""
import json
import os
from pathlib import Path
import shutil

STAGE = Path.home() / ".local/share/contextra-offline-prep/20260928-r01"
ROOT = Path.home() / ".local/opt/contextra-offline/20260928-r01"
if os.geteuid() == 0:
    raise SystemExit("Run as si")
os.umask(0o077)
for target in (ROOT / "opencode.json", ROOT / "agent.py", Path.home() / ".local/bin/ct-agent",
               Path.home() / ".local/bin/ct-gui"):
    if target.exists():
        raise RuntimeError("Existing target requires review: " + target.name)
config = json.loads((STAGE / "windows/opencode.json").read_text())
config["provider"]["icerti-local"]["options"]["baseURL"] = "http://127.0.0.1:18080/v1"
config["instructions"] = [str(ROOT / "OFFLINE_AGENT.ko.md")]
(ROOT / "opencode.json").write_text(json.dumps(config, ensure_ascii=False, indent=2))
shutil.copy2(STAGE / "windows/OFFLINE_AGENT.ko.md", ROOT / "OFFLINE_AGENT.ko.md")
for name in ("agent.py", "gui-session.sh"):
    shutil.copy2(STAGE / name, ROOT / name)
(ROOT / "gui-session.sh").chmod(0o700)
for name in ("ct-agent", "ct-gui"):
    target = Path.home() / ".local/bin" / name
    shutil.copy2(STAGE / name, target)
    target.chmod(0o700)
print(json.dumps(dict(status="USER_LAUNCHERS_READY", daemon_created=False,
                      default_gui=False, default_llm=False, source_activation=False)))
