#!/bin/env python

import os
import sys
import argparse

import pyrox.log as log
import pyrox.server as server
import pyrox.server.config as config


args_parser = argparse.ArgumentParser(
    prog='proxy',
    description='Pyrox, the fast Python HTTP middleware server.')

args_parser.add_argument(
    '-c',
    nargs='?',
    dest='cfg_location',
    default=None,
    help="""
        Sets the configuration file to load on startup. If unset this
        option defaults to /etc/pyrox/pyrox.conf""")

args_parser.add_argument(
    '-p',
    nargs='?',
    dest='plugin_paths',
    default=None,
    help=('"{}" character separated string of paths to '
          'import from when loading plugins.'.format(os.sep)))

args_parser.add_argument(
    'start',
    default=False,
    help='Starts the daemon.')


def start(args):
    cfg = config.load_pyrox_config(args.cfg_location)
    log.get_log_manager().configure(cfg)
    server.start_pyrox(cfg)


if len(sys.argv) > 1:
    args = args_parser.parse_args()

    if args.start:
        start(args)
else:
    args_parser.print_help()
