#!/usr/bin/env python3
# This file is place in the Public Domain.
# pylint: disable=C,W0105


"cli"


import sys


from opd.command import NAME, Commands, Config, scanner, command, parse, wrap
from opd.modules import face
from opd.object  import keys
from opd.runtime import Client, Event, errors


"defines"


cfg = Config()


TEXT = """[Unit]
Description=%s
After=network-online.target

[Service]
Type=simple
User=%s
Group=%s
ExecStart=/home/%s/.local/bin/%ss

[Install]
WantedBy=multi-user.target"""


"classes"


class CLI(Client):

    def __init__(self):
        Client.__init__(self)
        self.register("command", command)

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


"commands"


def cmd(event):
    event.reply(",".join(sorted(keys(Commands.cmds))))


def srv(event):
    import getpass
    name  = getpass.getuser()
    event.reply(TEXT % (NAME.upper(), name, name, name, NAME))


"runtime"


def main():
    Commands.add(srv)
    parse(cfg, " ".join(sys.argv[1:]))
    scanner(face)
    evt = Event()
    evt.txt = cfg.txt
    csl = CLI()
    command(csl, evt)
    evt.wait()


if __name__ == "__main__":
    wrap(main)
    for text in errors():
        print(text)
