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


"main"


import getpass
import os
import sys


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


from genocide.config  import Config
from genocide.cmds    import Commands
from genocide.persist import Persist, skel
from genocide.errors  import errors, later
from genocide.main    import cmnd, enable, scan
from genocide.parse   import parse
from genocide.utils   import modnames


from genocide import modules


Cfg         = Config()
Cfg.name    = Config.__module__.split(".")[-2]
Cfg.user    = getpass.getuser()
Cfg.mod     = "cmd,skl,req,srv"
Cfg.wdr     = os.path.expanduser(f"~/.{Cfg.name}")
Cfg.pidfile = os.path.join(Cfg.wdr, f"{Cfg.name}.pid")


Persist.workdir = Cfg.wdr


def srv(event):
    "create service file (pipx)."
    if event.args:
        username = event.args[0]
    else:
        username  = getpass.getuser()
    path = os.path.normpath(f"/home/{username}/.local/bin/")
    txt = f"""[Unit]
Description={Cfg.name.upper()}
Requires=network-online.target
After=network-online.target

[Service]
Type=simple
User={username}
Group={username}
ExecStartPre={path}/{Cfg.name} skl
ExecStart={path}/{Cfg.name}d
ExitType=cgroup
KillSignal=SIGKILL
KillMode=control-group
RemainAfterExit=yes
Restart=no

[Install]
WantedBy=default.target"""
    event.reply(txt)


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


def wrap(func):
    "catch exceptions"
    try:
        func()
    except (KeyboardInterrupt, EOFError):
        print("")
    except Exception as exc:
        later(exc)
    errors()


def main():
    "main"
    Commands.add(srv)
    parse(Cfg, " ".join(sys.argv[1:]))
    skel()
    enable(print)
    Cfg.dis = Cfg.sets.dis
    Cfg.mod = ",".join(modnames(modules))
    scan(Cfg.mod, modules)
    cmnd(Cfg.otxt, print)


if __name__ == "__main__":
    wrapped()
