#! /usr/bin/env Python3

import os 

class create_unit_service():
    def __init__(self, UnitName = str,systemd_sysfolder = "/etc/systemd/system", execstart = str, systemd_type = "simple") -> None:
        self.UnitName = UnitName
        self.systemd_systemfolder = systemd_sysfolder
        self.execstart = execstart
        if systemd_type == "simple" or systemd_type == "oneshot":
            self.systemd_type = systemd_type
        else:
            print("Kein richtiger Typ")
            raise "systemd_type ist nicht richtig"
    
    def create_unit(self):
        UnitFile = self.systemd_systemfolder + os.sep + self.UnitName + ".service"
        if os.path.exists(self.systemd_systemfolder):
            if os.path.exists(UnitFile):
                print("Gibt es bereits")
            else:
                print("Wird angelegt !!")
                with open(UnitFile, "w") as ufile:
                    print("[Unit]",
                          "[Service]",
                          "Type=oneshot",
                          "PrivateTmp=yes",
                          "DynamicUser=yes",
                          "ExecStart={my_execStart}".format(my_execStart=self.execstart), file=ufile, sep="\n")
                os.popen("systemctl daemon-reload")

    def delete_unit(self):
        pass

if __name__ == "__main__":
    new_unit = create_unit_service(UnitName="TestUnit",
                                   execstart="""/usr/bin/echo "Ich bin eine TestUnit %p" """,
                                   systemd_type="oneshot")
    new_unit.create_unit()