#!/usr/bin/env python3
# This file is placed in the Public Domain.


"service"


import getpass
import os
import pathlib
import pwd
import sys


from nixt.modules import face
from nixt.persist import Workdir
from nixt.runtime import errors, forever, init, wrap


from nixt import NAME


def pidfile(name):
    "write the pid to a file."
    pidfile = os.path.join(Workdir.wdr, f"{name}.pid")
    if os.path.exists(pidfile):
        os.unlink(pidfile)
    path = pathlib.Path(pidfile)
    path.parent.mkdir(parents=True, exist_ok=True)
    with open(pidfile, "w", encoding="utf-8") as fds:
        fds.write(str(os.getpid()))


def privileges(username):
    "drop privileges."
    pwnam = pwd.getpwnam(username)
    os.setgid(pwnam.pw_gid)
    os.setuid(pwnam.pw_uid)


def main():
    "main"
    privileges(getpass.getuser())
    pidfile(NAME)
    init(face)
    forever()


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