#!/usr/bin/env python

import os

from panoptes.utils.config.server import config_server


if __name__ == '__main__':

    import argparse

    parser = argparse.ArgumentParser(
        description='Start the config server for PANOPTES')
    parser.add_argument('--host', default='127.0.0.1', type=str,
                        help='Host name, defaults to local interface.')
    parser.add_argument('--port', default=6563, type=int, help='Local port, default 6563')
    parser.add_argument('--public', default=False, action='store_true',
                        help='If server should be public, default False. '
                        'Note: inside a docker container set this to True to expose to host.')
    parser.add_argument('--config-file', dest='config_file', type=str,
                        default=os.path.join(os.environ['PANDIR'], 'conf_files', 'pocs.yaml'),
                        help="Config file, default $PANDIR/conf_files/pocs.yaml")
    parser.add_argument('--no-save', default=False, action='store_true',
                        help='Prevent auto saving of any new values.')
    parser.add_argument('--ignore-local', default=False, action='store_true',
                        help='Ignore the local config files, default False. Mostly for testing.')
    parser.add_argument('--debug', default=False, action='store_true', help='Debug')
    args = parser.parse_args()

    # Set public
    if args.public and args.host == '127.0.0.1':
        args.host = '0.0.0.0'

    server_process = config_server(
        host=args.host,
        port=args.port,
        config_file=args.config_file,
        ignore_local=args.ignore_local,
        auto_save=not args.no_save,
        debug=args.debug,
        auto_start=False
    )

    try:
        server_process.start()
    except KeyboardInterrupt:
        server_process.terminate()
