#!/usr/bin/env python3
# This file is placed in the Public Domain.


import os
import readline
import sys
import termios
import time
import traceback


sys.path.insert(0, os.getcwd())


from genocide.default import Default
from genocide.message import Message
from genocide.objects import update
from genocide.handler import Handler, command, parse_cli
from genocide.modules import cmd, flt, irc, log, mdl, req, rss, tdo, thr
from genocide.storage import Storage
from genocide.threads import launch
from genocide.utility import wait


Storage.workdir = os.path.expanduser("~/.genocide")


class CLI(Handler):

    def announce(self, txt):
        pass

    def raw(self, txt):
        print(txt)
        sys.stdout.flush()


class Console(CLI):

    def handle(self, event):
        CLI.handle(self, event)
        event.wait()

    def poll(self):
        event = Message()
        event.txt = input("> ")
        event.orig = repr(self)
        return event


def waiter():
    got = []
    for ex in Handler.errors:
        traceback.print_exception(type(ex), ex, ex.__traceback__)
        got.append(ex)
    for exc in got:
        Handler.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():
    evt = parse_cli(" ".join(sys.argv[1:]))
    cli = CLI()
    cli.scan(cmd)
    cli.scan(log)
    cli.scan(mdl)
    cli.scan(tdo)
    if evt.txt:
        cli.scan(irc)
        cli.scan(rss)
        return command(cli, evt.otxt)
    elif "c" in evt.opts:
        date = time.ctime(time.time()).replace("  ", " ")
        print(f"GENOCIDE started {date}")
        bot = irc.init()
        bot.clone(cli) 
        rss.init()
        mdl.init()
        csl = Console()
        csl.clone(cli)
        csl.scan(irc)
        csl.scan(rss)
        csl.scan(flt)
        csl.scan(thr)
        csl.start()
        wait(waiter)


wrap(main)
