import subprocess, pathlib, json, sys
root = pathlib.Path(__file__).parent
remote = 'https://gitlab.com/julius54/heredoc-link-proof-44728.git'
def run(args, cwd=None):
    return subprocess.run(args,cwd=cwd,check=True,text=True,capture_output=True).stdout
side, checkout = sys.argv[1:]
workspace=root / side / 'Heredoc proof'
workspace.parent.mkdir(parents=True,exist_ok=True)
if not workspace.exists():
    run(['git','clone',remote,str(workspace)])
    for case in ['quoted','control','comment']:
        branch=side+'-'+case
        run(['git','checkout','-b',branch,'origin/main'],workspace)
        (workspace/'example.txt').write_text(case+'\n')
        run(['git','add','example.txt'],workspace)
        run(['git','-c','user.name=Proof','-c','user.email=proof@example.invalid','commit','-m','Demonstrate '+case],workspace)
        run(['git','push','-u','origin',branch],workspace)
    run(['git','checkout',side+'-quoted'],workspace)
home=str(pathlib.Path(checkout)/'.t3')
cli=['node','apps/server/src/bin.ts']
print(run(cli+['project','add',str(workspace),'--title','Heredoc proof','--base-dir',home],checkout))
(root/(side+'-workspace.json')).write_text(json.dumps({'workspace':str(workspace),'home':home,'checkout':checkout}))
