#
# This script is indented to be run after every changes. Therefore below commands
# should be idempotent.
#

import os
import platform
import sys
import shutil
from typing import List


if platform.system().lower() != "windows":
    print("Are you okay?")
    sys.exit(1)


def home(path: str | List) -> str:
    if isinstance(path, list):
        return os.path.join(os.environ["USERPROFILE"], *path)

    return os.path.join(os.environ["USERPROFILE"], path)


def files(path: str | List) -> str:
    if isinstance(path, list):
        return os.path.join("files", *path)

    return os.path.join("files", path)


def vim():
    shutil.copyfile(files(".vimrc"), home(".vimrc"))


def git():
    shutil.copyfile(files(".gitconfig"), home(".gitconfig"))
    shutil.copyfile(files(".gitignore"), home(".gitignore"))


def bin():
    shutil.copytree(files("bin"), home("Bin"), dirs_exist_ok=True)

    with open(
        home(
            [
                "AppData",
                "Roaming",
                "Microsoft",
                "Windows",
                "Start Menu",
                "Programs",
                "Startup",
                "Maxime.bat",
            ]
        ),
        "w",
    ) as fd:
        fd.write(
            f"""SETX /M PATH "%PATH%;{home("Bin")}"
"""
        )


def main():
    vim()
    git()
    bin()


if __name__ == "__main__":
    main()

@@ -138,18 +138,21 @@
 import s
-ys
+hutil
 %0Aimport 
@@ -152,21 +152,32 @@
 import s
-hutil
+ys%0Aimport winreg
 %0Afrom ty
@@ -191,16 +191,20 @@
 port
+ Any,
  List%0A%0A
-%0A
 if p
@@ -283,24 +283,811 @@
 s.exit(1)%0A%0A%0A
+class RegistryHKEY:%0A    def __init__(self, path) -%3E None:%0A        self.path = path%0A%0A    def __enter__(self):%0A        self.reg = winreg.ConnectRegistry(None, winreg.HKEY_CURRENT_USER)%0A        self.key = winreg.OpenKey(self.reg, self.path, 0, winreg.KEY_ALL_ACCESS)%0A        return self%0A%0A    def __exit__(self, exc_type, exc_value, exc_traceback):%0A        winreg.CloseKey(self.key)%0A        winreg.CloseKey(self.reg)%0A%0A    def get(self, name: str) -%3E Any:%0A        value, _ = winreg.QueryValueEx(self.key, name)%0A        return value%0A%0A    def set(self, name: str, value: str):%0A        winreg.SetValueEx(self.key, name, 0, winreg.REG_EXPAND_SZ, value)%0A%0A    def append(self, name: str, value: str):%0A        value = self.get(self.key, name) + %22;%22 + value%0A        self.set(self.key, name, value)%0A%0A%0A
 def home(pat
@@ -1708,400 +1708,227 @@
 ith 
-open(%0A        home(%0A            %5B%0A                %22AppData%22,%0A                %22Roaming%22,%0A                %22Microsoft%22,%0A                %22Windows%22,%0A                %22Start Menu%22,%0A                %22Programs%22,%0A                %22Startup%22,%0A                %22Maxime.bat%22,%0A            %5D%0A        ),%0A        %22w%22,%0A    ) as fd:%0A        fd.write(%0A            f%22%22%22SETX /M PATH %22%25PATH%25;%7Bhome(%22Bin%22)%7D%22%0A%22%22%22%0A        
+RegistryHKEY(%22Environment%22) as env:%0A        if home(%22Bin%22) not in env.get(%22Path%22):%0A            env.append(%22Path%22, home(%22Bin%22))%0A%0A        if home(%22Apps%22) not in env.get(%22Path%22):%0A            env.append(%22Path%22, home(%22Apps%22)
 )%0A%0A%0A

update $PATH