#!/home/circleci/miniconda/conda-bld/_h_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_pla/bin/python
from argparse import ArgumentParser, Namespace
from pathlib import Path

from landmarkerio import TEMPLATE_DINAME
from landmarkerio.landmark import SeparateDirFileLmAdapter
from landmarkerio.servers.serve import serve_from_cache
from landmarkerio.utils import parse_username_and_password_file


def build_argparser() -> ArgumentParser:
    parser = ArgumentParser(
        description=r"""
        Serve landmarks and assets for landmarker.io from a prebuilt cache
        directory. See lmiocache for how to create a cache.
        """
    )
    parser.add_argument("mode", help="'image' or 'mesh'")
    parser.add_argument(
        "cache", type=Path, help="The prebuilt cache folder as generated by lmiocache"
    )
    parser.add_argument(
        "landmarks",
        type=Path,
        help="The directory where landmarks should be served from",
    )
    parser.add_argument(
        "-t",
        "--templates",
        type=Path,
        help=f"The directory containing the template files. If None provided "
        f"taken as '~/{TEMPLATE_DINAME}'",
    )
    parser.add_argument(
        "-c",
        "--collections",
        type=Path,
        help="The directory containing the collection files. "
        "If None provided an 'all' collection will be used with all assets present.",
    )
    parser.add_argument(
        "--dev",
        action="store_true",
        help="Listen to all CORS requests. Useful for development on localhost",
    )
    parser.add_argument(
        "--public", action="store_true", help="Listen to public requests (0.0.0.0)."
    )
    parser.add_argument(
        "-p", "--port", help="The port to host the server on. 5000 by default"
    )
    parser.add_argument(
        "-b",
        "--basicauth",
        help="Enable HTTP basic authentication using the "
        "username and password provided in the file "
        "at this path. The file should contain the "
        "username on the first line, the password on the "
        "second, and no other content.",
    )
    return parser


def main(ns: Namespace) -> None:
    lm_adapter = SeparateDirFileLmAdapter(ns.landmarks)
    if ns.basicauth is not None:
        username, password = parse_username_and_password_file(ns.basicauth)
    else:
        username, password = None, None

    app = serve_from_cache(
        ns.mode,
        ns.cache,
        lm_adapter,
        template_dir=ns.templates,
        collection_dir=ns.collections,
        username=username,
        password=password,
    )
    if ns.port is None:
        port = 5000
    else:
        port = int(ns.port)

    app.run(host="0.0.0.0" if ns.public else "localhost", port=port, debug=ns.dev)


if __name__ == "__main__":
    main(build_argparser().parse_args())
