# AUTOGENERATED! DO NOT EDIT! File to edit: nbs/02_proxyservers.ipynb (unless otherwise specified).

__all__ = ['get_icon_path', 'switch_path', 'fix_cmd', 'plutoserver', 'codeserver', 'docserver', 'bookserver']

# Cell
# from RemoteConnect.core import setup_vscode
import os
import pathlib

# Cell
def get_icon_path(ico_name):
    return os.path.join(
        os.path.dirname(os.path.abspath(__file__)), 'icons', ico_name
)


# Cell
def switch_path(curdir, doc_cmd=False):
    old_path = pathlib.Path(curdir).resolve()
    if doc_cmd:
        if old_path.parts[-1] == 'docs': return old_path
        else: return old_path/"docs"
    else:
        if old_path.parts[-1] == 'docs':  return old_path.parent
        else: return old_path

# fix_path(todoc=True)
# # print(os.getcwd())
# # fix_path(todoc=False)
# # print(os.getcwd())
# # os.getcwd()
# os.getcwd()
# old_path = os.".."
# old_path.split(os.sep)[-1]

# Cell
def fix_cmd(cmdlist, doc_cmd=False):
    new_path = switch_path(".", doc_cmd)
    os.chdir(new_path)
    return cmdlist

# Cell
def plutoserver():
    return {
        "command": fix_cmd(["julia", "--optimize=0", "-e", "import Pluto; Pluto.run(host=\"0.0.0.0\", port={port}, launch_browser=false, require_secret_for_open_links=false, require_secret_for_access=false)"]),
        "timeout": 60,
        "launcher_entry": {
            "title": "Pluto.jl",
            'icon_path': get_icon_path("pluto.svg")
    },
  }

# Cell
def codeserver():
    """
    Export codeserver as jupyter server proxy
    """
    return {
        "command": fix_cmd(["code-server", "--port", "{port}", "--auth","none", "--disable-telemetry"]),
        "timeout":60,
#         'absolute_url': True,
        "launcher_entry": {
            "title": "codeserver",
             'icon_path': get_icon_path("vscode.svg")
    },
  }

# Cell
def docserver():
    """
    Export server for nbdev local documentation site
    """
    def pathfix_cmd():
        return fix_cmd(["bundle", "exec", "jekyll", "serve","--config", "_config.yml,_config_proxy.yml", "--port", "{port}"], doc_cmd=True)

    return {
        "command": pathfix_cmd,
        "timeout":60,
        'absolute_url': True,
        "launcher_entry": {
            "title": "docserver",
             'icon_path': get_icon_path("documentation.svg")
    },
  }

# Cell
def bookserver():
    """
    Runs a local book server inside jupyter instance
    """
    return {
        "command": fix_cmd(['http-server', "./book/_build/html", "-p", "{port}"]),
        'absolute_url': False,
        "launcher_entry": {
            "title": "bookserver",
             'icon_path': get_icon_path("book.svg")
    },
  }