#!/usr/bin/env python3
# This file is placed in the Public Domain.
# pylint: disable=C0413,W0212,E0401


"main"


import os
import sys


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


from otpcr.cfg    import Config
from otpcr.cli    import CLI
from otpcr.defer  import errors
from otpcr.disk   import Persist
from otpcr.event  import Event
from otpcr.main   import cmnd
from otpcr.parse  import parse


from otpcr import modules
from otpcr import user


Cfg         = Config()
Cfg.dis     = ""
Cfg.mod     = "cmd,err,mod,thr"
Cfg.opts    = ""
Cfg.name    = "otpcr"
Cfg.wdr     = os.path.expanduser(f"~/.{Cfg.name}")
Cfg.pidfile = os.path.join(Cfg.wdr, f"{Cfg.name}.pid")


Persist.workdir = Cfg.wdr


class Console(CLI):

    "Console"

    def announce(self, txt):
        "disable announce."

    def callback(self, evt):
        "wait for callback."
        CLI.callback(self, evt)
        evt.wait()

    def poll(self):
        "poll console and create event."
        evt = Event()
        evt.txt = input("> ")
        evt.type = "command"
        return evt


def modnames():
    "list all modules."
    return sorted({x for x in dir(modules) + dir(user) if not x.startswith("__")})


def wrapped():
    "wrap main."
    main()
    errors()


def main():
    "main"
    parse(Cfg, " ".join(sys.argv[1:]))
    Cfg.dis = Cfg.sets.dis
    Cfg.mod = ",".join(modnames())
    cmnd(Cfg.otxt, print)


if __name__ == "__main__":
    wrapped()
