#!/usr/bin/env python3
import subprocess
import sys
import urllib.parse


def main(sysargs=sys.argv[:]):
    cb_uri = subprocess.check_output(["cb", "uri", *sysargs[1:]], text=True)

    parsed_cb_uri = urllib.parse.urlsplit(cb_uri)
    tailscale_cb_uri = parsed_cb_uri._replace(
        netloc=str(parsed_cb_uri.username)
        + ":"
        + str(parsed_cb_uri.password)
        + "@"
        + str(parsed_cb_uri.hostname).split(".")[1]
        + ":"
        + str(parsed_cb_uri.port)
    )

    print(tailscale_cb_uri.geturl())

    return 0


if __name__ == "__main__":
    sys.exit(main())
