--- /dev/null
+#!/usr/bin/env python3
+# -*- mode: python; coding: utf-8; python-indent-offset: 2; indent-tabs-mode: nil -*-
+
+import sys ,os ,subprocess
+from pathlib import Path
+
+def get_Z_date() -> str:
+ try:
+ res = subprocess.run(["Z" ,"format-%year-%month-%day"] ,capture_output=True ,text=True ,check=True)
+ return res.stdout.strip()
+ except Exception:
+ return "1970-01-01"
+
+def work(project_name: str ,remotes: list[str]) -> int:
+ repo_home = os.environ.get("REPO_HOME")
+ if not repo_home:
+ print("Error: REPO_HOME is not set. A person must run this from within a sourced role environment." ,file=sys.stderr)
+ return 1
+
+ repo_path = Path(repo_home).resolve()
+
+ dest_path = Path(project_name)
+ if not dest_path.is_absolute():
+ dest_path = (repo_path.parent / project_name).resolve()
+
+ if dest_path.exists():
+ print(f"Error: Destination {dest_path} already exists." ,file=sys.stderr)
+ return 1
+
+ print(f"Creating new project at: {dest_path}")
+ dest_path.mkdir(parents=True)
+
+ archive_cmd = "git archive HEAD | tar -x -C " + str(dest_path)
+ res = subprocess.run(archive_cmd ,shell=True ,cwd=str(repo_path))
+ if res.returncode != 0:
+ print("Error extracting archive." ,file=sys.stderr)
+ return 1
+
+ opus_src = dest_path / "0pus_Harmony"
+ opus_dst = dest_path / f"0pus_{dest_path.name}"
+ if opus_src.exists():
+ opus_src.rename(opus_dst)
+
+ version_file = dest_path / "shared" / "tool" / "version"
+ date_str = get_Z_date()
+ version_line = f"echo \"{dest_path.name} v0.1 {date_str}\"\n"
+
+ if version_file.exists():
+ with open(version_file ,"a" ,encoding="utf-8") as vf:
+ vf.write(version_line)
+ else:
+ with open(version_file ,"w" ,encoding="utf-8") as vf:
+ vf.write(version_line)
+
+ print("Initializing git repository...")
+ subprocess.run(["git" ,"init" ,"-b" ,"core-developer_branch"] ,cwd=str(dest_path) ,check=True)
+ subprocess.run(["git" ,"add" ,"."] ,cwd=str(dest_path) ,check=True)
+ subprocess.run(["git" ,"commit" ,"-m" ,f"Initial commit of {dest_path.name} from Harmony skeleton"] ,cwd=str(dest_path) ,check=True)
+
+ for idx ,remote_url in enumerate(remotes):
+ remote_name = f"remote_{idx}"
+ if "github.com" in remote_url:
+ remote_name = "github"
+ elif "RT" in remote_url:
+ remote_name = "RT"
+ elif idx == 0:
+ remote_name = "origin"
+
+ print(f"Adding remote '{remote_name}': {remote_url}")
+ subprocess.run(["git" ,"remote" ,"add" ,remote_name ,remote_url] ,cwd=str(dest_path) ,check=True)
+
+ print(f"Project '{dest_path.name}' successfully created.")
+ return 0
+
+def CLI(argv=None) -> int:
+ if argv is None:
+ argv = sys.argv[1:]
+
+ if not argv or "-h" in argv or "--help" in argv:
+ print("Usage: new-project <name> [-remote <url>]...")
+ return 0
+
+ project_name = None
+ remotes = []
+
+ idx = 0
+ while idx < len(argv):
+ arg = argv[idx]
+ if arg == "-remote":
+ if idx + 1 < len(argv):
+ remotes.append(argv[idx+1])
+ idx += 1
+ else:
+ print("Error: -remote requires a URL." ,file=sys.stderr)
+ return 1
+ elif project_name is None:
+ project_name = arg
+ else:
+ print(f"Error: unexpected argument '{arg}'" ,file=sys.stderr)
+ return 1
+ idx += 1
+
+ if not project_name:
+ print("Error: project name is required." ,file=sys.stderr)
+ return 1
+
+ return work(project_name ,remotes)
+
+if __name__ == "__main__":
+ sys.exit(CLI())