#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Unified entrypoint for running client scripts.

Usage:
    python run_client.py get_joint_state_client
    python run_client.py movej_client
"""

import os
import sys
import subprocess

PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
GENERATED_DIR = os.path.join(PROJECT_ROOT, "generated")
CLIENTS_DIR = os.path.join(PROJECT_ROOT, "clients")

if len(sys.argv) < 2:
    print("Please provide a client script name, e.g. get_joint_state_client")
    sys.exit(1)

client_name = sys.argv[1]
client_script = os.path.join(CLIENTS_DIR, f"{client_name}.py")

if not os.path.isfile(client_script):
    print(f"Client script not found: {client_script}")
    sys.exit(1)

module_name = f"clients.{client_name}"
env = os.environ.copy()
extra_paths = [PROJECT_ROOT, GENERATED_DIR]
existing = env.get("PYTHONPATH", "")
env["PYTHONPATH"] = os.pathsep.join(extra_paths + ([existing] if existing else []))

subprocess.run(
    [sys.executable, "-m", module_name] + sys.argv[2:],
    cwd=PROJECT_ROOT,
    env=env,
)
