import os
import re
from pathlib import Path
#get path
PATHS = os.environ['PATH'].split(":")

#directories to check
DIRS = [
    os.environ["HOME"]+"/.local/share/applications/",
    "/usr/share/applications"
]

print("Welcome to shitty link cleaner PRO v1.3.3.7 patch 666")

isroot = (os.environ["USER"] == "root")
engage = []

for directory in DIRS:
    p = Path(directory)
    dfiles = list(p.glob('./*.desktop'))
    if (not isroot) and p.owner() != os.environ["USER"]:
        print("Unable to engage "+directory+" motherfucker - i'm not root and i refuse to delete anything in another user's dir.")
    else:
        print("Engage "+directory+" motherfucker ("+str(len(dfiles))+" files)")
        answer = input("[y/n?]: ")
        while not (answer in ["y","n"]):
            print("Bitch please speak clearly, y/n?")
            answer = input("[y/n?]: ")
        if answer == "y":
            print("Added "+directory+" to cleaning list.")
            engage = engage + dfiles
        else:
            print("Skipping "+directory)

for f in engage:
    if f.is_dir():
        print(str(f)+" isn't even a file, skipping...")
        continue
    execname = re.findall("\nExec=([^\n]+)\n",f.read_text())
    if not execname:
        print(str(f)+" doesn't seem to have an Exec field. delet?")
        answer = input("[y/n?]: ")
        while not (answer in ["y","n"]):
            print("Bitch please speak clearly, y/n?")
            answer = input("[y/n?]: ")
        if answer == "y":
            print(str(f)+" delet")
            f.unlink(missing_ok=True)
        else:
            print("Skipping...")
        continue
    args = execname[0].split(" ")
    comm = None
    for arg in args:
        if arg == "env":
            print("Assuming 'env' is not the target binary - skipping...")
            continue
        if "=" in arg:
            print("Assuming '"+arg+"' is an environment variable - skipping...")
            continue
        comm = arg
        break
    if not comm:
        print("Weirdly enough, "+str(f)+" has an exec, but no target. delet?")
        answer = input("[y/n?]: ")
        while not (answer in ["y","n"]):
            print("Bitch please speak clearly, y/n?")
            answer = input("[y/n?]: ")
        if answer == "y":
            print(str(f)+" delet")
            f.unlink(missing_ok=True)
        else:
            print("skipping")
        continue
    else:
        print("Testing "+comm+" against various PATHs...")
        if f.exists():
            print(str(f)+" seems to be fine, skipping...")
            continue
        found = False
        for path in PATHS:
            if (path / f).exists():
                print(str(f)+" seems to be fine, skipping...")
                found = True
                break
        if found:
            continue
        print(comm+" doesn't seem to exist. delet?")
        answer = input("[y/n?]: ")
        while not (answer in ["y","n"]):
            print("Bitch please speak clearly, y/n?")
            answer = input("[y/n?]: ")
        if answer == "y":
            print(str(f)+" delet")
            f.unlink(missing_ok=True)
        else:
            print("skipping")
        continue
