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 (PG edition; GOTY version; DLSS+Raytracing patch)")

incorrect_prompt = "What the fuck did you just fucking say about me, you little bitch? I'll have you know I graduated top of my class in the Navy Seals, and I've been involved in numerous secret raids on Al-Quaeda, and I have over 300 confirmed kills. I am trained in gorilla warfare and I'm the top sniper in the entire US armed forces. You are nothing to me but just another target. I will wipe you the fuck out with precision the likes of which has never been seen before on this Earth, mark my fucking words. You think you can get away with saying that shit to me over the Internet? Think again, fucker. As we speak I am contacting my secret network of spies across the USA and your IP is being traced right now so you better prepare for the storm, maggot. The storm that wipes out the pathetic little thing you call your life. You're fucking dead, kid. I can be anywhere, anytime, and I can kill you in over seven hundred ways, and that's just with my bare hands. Not only am I extensively trained in unarmed combat, but I have access to the entire arsenal of the United States Marine Corps and I will use it to its full extent to wipe your miserable ass off the face of the continent, you little shit. If only you could have known what unholy retribution your little \"clever\" comment was about to bring down upon you, maybe you would have held your fucking tongue. But you couldn't, you didn't, and now you're paying the price, you goddamn idiot. I will shit fury all over you and you will drown in it. You're fucking dead, kiddo. \n\nSo, y/n?"


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("I am unable to can clean the "+directory+" directory, for I am rootn't ")
    else:
        print("Mr clean "+directory+" ("+str(len(dfiles))+" files)")
        answer = input("[y/n?]: ")
        while not (answer in ["y","n"]):
            print(incorrect_prompt)
            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(incorrect_prompt)
            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")
            continue
        if "=" in arg:
            print("Assuming '"+arg+"' is an environment variable")
            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(incorrect_prompt)
            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(incorrect_prompt)
            answer = input("[y/n?]: ")
        if answer == "y":
            print(str(f)+" delet")
            f.unlink(missing_ok=True)
        else:
            print("skipping")
        continue
