#!/usr/bin/env python3

import os
import subprocess
import time

CLEAR_SCREEN = ["clear"]
DAEMON_RELOAD = ["systemctl", "daemon-reload"]
ENABLE_SERVICE = ["systemctl", "enable", "tuxedo-reboot-into-grub.service"]
REBOOT = ["systemctl", "reboot"]
UPDATE_GRUB = ["grub-mkconfig", "-o", "/boot/grub/grub.cfg", "&>", "/dev/null"]

def run_subprocess(cmd: list[str]) -> None:
    subprocess.run(cmd, check=True, text=None, capture_output=False)


if os.geteuid() == 0:
    os.link("/usr/share/tuxedo-reboot-into-grub/99-tuxedo-reboot-into-grub-tmp.cfg",
            "/etc/default/grub.d/99-tuxedo-reboot-into-grub-tmp.cfg")

    os.link("/usr/share/tuxedo-reboot-into-grub/tuxedo-reboot-into-grub.service",
            "/etc/systemd/system/tuxedo-reboot-into-grub.service")

    run_subprocess(DAEMON_RELOAD)
    run_subprocess(ENABLE_SERVICE)
    run_subprocess(UPDATE_GRUB)

    run_subprocess(CLEAR_SCREEN)

    print("Systems startet in 15 Sekunden neu")
    print("System will restart in 15 seconds")

    time.sleep(15)
    run_subprocess(CLEAR_SCREEN)

    print("System startet JETZT neu")
    print("System will restart NOW")
    time.sleep(2)
    run_subprocess(REBOOT)
else:
   print("This program needs to be run as root user!")
   exit()
