#!/usr/bin/env python
import os
import sys
from contextlib import chdir
from subprocess import run


def main():
    with chdir(f'{os.path.dirname(__file__)}/../server'):
        wbsng_ref = readfile('wbsng-ref')

        wbsng_dir = 'wbsng'
        if os.path.exists(wbsng_dir):
            sys.exit(f"'{wbsng_dir}' already exists")

        os.mkdir(wbsng_dir)
        with chdir(wbsng_dir):
            run(['git', 'clone', 'git@github.com:dangoodman/wbsng.git', '.'], check=True)
            run(['git', 'checkout', '-q', wbsng_ref], check=True)


def readfile(path: str) -> str:
    with open(path) as f:
        return f.read()


main()
