#!/opt/anaconda1anaconda2anaconda3/bin/python
# encoding: utf-8

from landmarkerio.serverconfigs import serve_from_cache, serve_with_cherrypy
from landmarkerio import TEMPLATE_DINAME
from landmarkerio.landmark import SeparateDirFileLmAdapter
from landmarkerio.utils import parse_username_and_password_file

if __name__ == "__main__":
    from argparse import 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",
                        help="The prebuilt cache folder as generated "
                             "by lmiocache")
    parser.add_argument("landmarks",
                        help="The directory where landmarks should be served "
                             "from")
    parser.add_argument("-t", "--templates",
                        help="The directory containing the template files. "
                             "If None provided taken as "
                             "'~/{}'".format(TEMPLATE_DINAME))
    parser.add_argument("-c", "--collections",
                        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.")
    parser.add_argument('--upgrade-templates', action='store_true',
                        help="Rewrite old .txt template to the new .yml "
                             "format and delete old .txt files from the "
                             "template directory (consider backing up the"
                             "directory first).")
    ns = parser.parse_args()
    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,
                           upgrade_templates=ns.upgrade_templates,
                           collection_dir=ns.collections, dev=ns.dev,
                           username=username, password=password)
    if ns.port is None:
        port = 5000
    else:
        port = int(ns.port)
    serve_with_cherrypy(app, port=port, public=ns.public)
