#!/usr/bin/env python3
"""Copy the DPU panel example into a new Plugin source folder."""

import re
import shutil
import sys
from pathlib import Path


ROOT = Path(__file__).resolve().parents[1]
ID_RE = re.compile(r"[a-z0-9]+(?:-[a-z0-9]+)*(?:\.[a-z0-9]+(?:-[a-z0-9]+)*)*\Z")


def main() -> None:
    if len(sys.argv) != 2 or not 3 <= len(sys.argv[1]) <= 128 or not ID_RE.fullmatch(sys.argv[1]):
        raise SystemExit("usage: bin/new PLUGIN_ID (lowercase dot-separated ID)")
    plugin_id = sys.argv[1]
    source = ROOT / "plugins" / "local.file-peek-panel"
    target = ROOT / "plugins" / plugin_id
    if target.exists():
        raise SystemExit(f"Plugin folder already exists: {target}")
    shutil.copytree(source, target, ignore=shutil.ignore_patterns("*.eleplugin", "META-INF"))
    manifest = target / "ele-plugin.toml"
    text = manifest.read_text(encoding="utf-8")
    text = text.replace('id = "local.file-peek-panel"', f'id = "{plugin_id}"', 1)
    text = text.replace('name = "File Peek"', f'name = "{plugin_id}"', 1)
    text = text.replace('version = "0.1.1"', 'version = "0.1.0"', 1)
    text = text.replace('name = "ele"', 'name = "your-publisher"', 1)
    manifest.write_text(text, encoding="utf-8")
    readme = target / "README.md"
    readme.write_text(readme.read_text(encoding="utf-8").replace("local.file-peek-panel", plugin_id), encoding="utf-8")
    print(f"Created {target}")
    print("Review the manifest, icon, license, and browser code before packaging.")


if __name__ == "__main__":
    main()
