#!/home/travis/miniconda/envs/_build/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

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")
    ns = parser.parse_args()
    lm_adapter = SeparateDirFileLmAdapter(ns.landmarks)
    app = serve_from_cache(ns.mode, ns.cache, lm_adapter,
                           template_dir=ns.templates,
                           collection_dir=ns.collections, dev=ns.dev)
    if ns.port is None:
        port = 5000
    else:
        port = int(ns.port)
    serve_with_cherrypy(app, port=port, public=ns.public)
