#!/usr/bin/env python3
# This file is placed in the Public Domain.
# pylint: disable=C,I,R,E0611,E0402


__author__ = "B.H.J. Thate <thatebhj@gmail.com>"
__version__ = 1


import os
import sys
import termios
import traceback


from genocide.clients import Client
from genocide.command import command, scan
from genocide.errored import Errors
from genocide.message import parse
from genocide.objects import update
from genocide.persist import Persist


from genocide.modules import cmd, irc, log, mdl, req, rss, slg, tdo, wsd


Persist.workdir = "/var/lib/genocide/"


scan(cmd)
scan(irc)
scan(log)
scan(mdl)
scan(req)
scan(rss)
scan(slg)
scan(tdo)
scan(wsd)


class CLI(Client):

    def announce(self, txt):
        pass

    def raw(self, txt):
        print(txt)


def waiter():
    got = []
    for ex in Errors.errors:
        traceback.print_exception(type(ex), ex, ex.__traceback__)
        got.append(ex)
    for exc in got:
        Errors.errors.remove(exc)


def wrap(func):
    fds = sys.stdin.fileno()
    gotterm = True
    try:
        old = termios.tcgetattr(fds)
    except termios.error:
        gotterm = False
    try:
        func()
    except (EOFError, KeyboardInterrupt):
        print('')
    finally:
        if gotterm:
            termios.tcsetattr(fds, termios.TCSADRAIN, old)
        waiter()


def main():
    if "INVOCATION_ID" not in os.environ:
        print("not a systemd")
        os._exit(0)
    cfg = parse(' '.join(sys.argv[1:]))
    cli = CLI()
    evt = command(cli, cfg.otxt)
    evt.wait()


if __name__ == "__main__":
    wrap(main)
