# coding: utf-8

import argparse
import asyncio
import functools
import logging
import platform
import sys

from pathlib import Path

from .config import load_config
from .dedicated_server import DedicatedServer
from .logging import setup_logging


LOG = logging.getLogger(__name__)


def load_args():
    parser = argparse.ArgumentParser(
        description=(
            "Wrapper of dedicated server of "
            "«IL-2 Sturmovik: Forgotten Battles»"
        ),
        formatter_class=argparse.ArgumentDefaultsHelpFormatter,
    )
    parser.add_argument(
        '-c', '--config',
        dest='config_path',
        type=lambda x: Path(x).resolve(),
        default='airbridge.yml',
        help="path to config file",
    )
    return parser.parse_args()


def on_dedicated_server_stdout(s: str) -> None:
    sys.stdout.write(s)
    sys.stdout.flush()


def on_stdin_ready(handler):
    line = sys.stdin.readline()
    asyncio.ensure_future(handler(line))


def get_loop():
    if platform.system() == 'Windows':
        return asyncio.ProactorEventLoop()

    return asyncio.SelectorEventLoop()


def main():
    args = load_args()
    config = load_config(args.config_path)

    setup_logging(config.logging)

    loop = get_loop()
    asyncio.set_event_loop(loop)

    try:
        ds = DedicatedServer(
            loop=loop,
            exe_path=config.ds.exe_path,
            config_path=config.ds.get('config_path'),
            start_script_path=config.ds.get('start_script_path'),
            stdout_handler=on_dedicated_server_stdout,
        )
    except Exception:
        LOG.fatal("failed to init dedicated server", exc_info=True)
        raise SystemExit(-1)

    ds_task = loop.create_task(ds.run())
    loop.run_until_complete(ds.wait_for_start())
    loop.add_reader(sys.stdin, functools.partial(on_stdin_ready, ds.input))

    try:
        loop.run_until_complete(asyncio.gather(ds_task))
    except KeyboardInterrupt:
        LOG.info("interrupted by user")

@@ -757,24 +757,164 @@
 se_args()%0A%0A%0A
+def get_loop():%0A    if platform.system() == 'Windows':%0A        return asyncio.ProactorEventLoop()%0A%0A    return asyncio.SelectorEventLoop()%0A%0A%0A
 def on_dedic
@@ -1110,140 +1110,548 @@
 def 
-get_loop():%0A    if platform.system() == 'Windows':%0A        return asyncio.ProactorEventLoop()%0A%0A    return asyncio.SelectorEventLoop(
+validate_dedicated_server_config(config) -%3E None:%0A    if not config.console.connection.port:%0A        raise ValueError(%0A            %22server's console is disabled, please configure it to proceed %22%0A            %22(see: https://github.com/IL2HorusTeam/il2fb-ds-config#console-section)%22%0A        )%0A%0A    if not config.device_link.connection.port:%0A        raise ValueError(%0A            %22server's device link is disabled, please configure it to proceed %22%0A            %22(see: https://github.com/IL2HorusTeam/il2fb-ds-config#devicelink-section)%22%0A        
 )%0A%0A%0A
@@ -2228,16 +2228,193 @@
 it(-1)%0A%0A
+    try:%0A        validate_dedicated_server_config(ds.config)%0A    except ValueError:%0A        LOG.fatal(%22server's config is invalid%22, exc_info=True)%0A        raise SystemExit(-1)%0A%0A
     ds_t

Validate server's config before running it