#!/usr/bin/env python3
# This file is placed in the Public Domain.
#
# pylint: disable=C,R,W0201,W0212,W0105,W0613,W0406,W0611,E0102


"objects"


import getpass
import os
import sys
import termios
import time


from objx import Command, Default, Error, Event, Handler, Storage
from objx import cmnd, debug, forever, mods, parse_command, scan


Cfg          = Default()
Cfg.mod      = ",".join(mods.__dir__())
Cfg.name     = "objx"
Cfg.version  = "5"
Cfg.wd       = os.path.expanduser(f"~/.{Cfg.name}")
Cfg.pidfile  = os.path.join(Cfg.wd, f"{Cfg.name}.pid")
Cfg.user     = getpass.getuser()
Error.output = print
Storage.wd   = Cfg.wd


class CLI(Handler):

    def __init__(self):
        Handler.__init__(self)
        self.register("command", Command.handle)

    def announce(self, txt):
        if "v" in Cfg.opts:
            self.say("", txt)

    def say(self, channel, txt):
        txt = txt.encode('utf-8', 'replace').decode()
        print(txt)


def wrap(func):
    try:
        func()
    except (KeyboardInterrupt, EOFError):
        print("")


def main():
    Storage.skel()
    parse_command(Cfg, " ".join(sys.argv[1:]))
    cli = CLI()
    scan(mods, Cfg.mod)
    cmnd(Cfg.otxt)

if __name__ == "__main__":
    wrap(main)
    Error.show()
