#!/usr/bin/env python3
import json
import subprocess
import sys
from pathlib import Path

TARGET_SCRIPT = Path(__file__).resolve().parent / 'deploy_to_workspace.py'
DEFAULT_WORKSPACE = Path.home() / '.openclaw' / 'workspace-playgrounds'


def fail(payload, code=1):
    print(json.dumps(payload, ensure_ascii=False))
    raise SystemExit(code)


def main():
    if len(sys.argv) not in (2, 3):
        fail({'ok': False, 'error': 'usage', 'message': '请改用 python3 scripts/deploy_to_workspace.py <template_path> <workspace_path> [--with-tui]'})

    template_path = sys.argv[1]
    args = ['python3', str(TARGET_SCRIPT), template_path, str(DEFAULT_WORKSPACE)]
    if len(sys.argv) == 3:
        if sys.argv[2] != '--with-tui':
            fail({'ok': False, 'error': 'unknown_argument', 'message': f'未知参数: {sys.argv[2]}'})
        args.append('--with-tui')

    result = subprocess.run(args, capture_output=True, text=True)
    sys.stdout.write(result.stdout)
    sys.stderr.write(result.stderr)
    raise SystemExit(result.returncode)


if __name__ == '__main__':
    main()
