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


"cli"


import getpass
import os
import readline
import sys


from nixt.command import Commands, command, parse
from nixt.modules import face
from nixt.object  import Default
from nixt.runtime import Client, Event, errors


NAME = Default.__module__.split(".", maxsplit=2)[-2]


class CLI(Client):

    "CLI"

    def raw(self, txt):
        "print text."
        print(txt)


def srv(event):
    "create service file (pipx)."
    if event.args:
        name = event.args[0]
    else:
        name  = getpass.getuser()
    txt = """[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"""
    event.reply(txt % (NAME.upper(), name, name, name, NAME))


def main():
    "main"
    Commands.add(srv)
    cfg = Default()
    parse(cfg, " ".join(sys.argv[1:]))
    cli = CLI()
    evt = Event()
    evt.orig = repr(cli)
    evt.txt = cfg.txt
    command(cli, evt)


if __name__ == "__main__":
    main()
    errors(print)
