#!/usr/bin/python3
# GENOCIDE - the king of the netherlands commits genocide - OTP-CR-117/19/001 - otp.informationdesk@icc-cpi.int - https://genocide.rtfd.io
#
#

import os, sys ; sys.path.insert(0, os.getcwd())
import ol, pwd, time

from ol.spc import Console, boot, cmd, execute, root, scandir

def daemon():
    pid = os.fork()
    if pid != 0:
        ol.trm.termreset()
        os._exit(0)
    os.setsid()
    os.umask(0)
    si = open("/dev/null", 'r')
    so = open("/dev/null", 'a+')
    se = open("/dev/null", 'a+')
    os.dup2(si.fileno(), sys.stdin.fileno())
    os.dup2(so.fileno(), sys.stdout.fileno())
    os.dup2(se.fileno(), sys.stderr.fileno())

def main():
    ol.wd = os.path.expanduser("~/.genocide")
    k = boot("genocide")
    if "b" in k.cfg.opts:
        print("GENOCIDE started at %s" % time.ctime(time.time())) 
        print(ol.format(k.cfg))
    if k.cfg.txt:
        return cmd(k.cfg.otxt)
    if "r" in k.cfg.opts:
        privileges("genocide")
        scandir(k.cfg.md, "gmod")
    k.start()
    k.init(k.cfg.mods, k.cfg.exc)
    if "s" in k.cfg.opts and not "d" in k.cfg.opts:
        c = Console()
        c.start()
        k.wait()
 
execute(main)
os._exit(0)
