import subprocess
import sys
import os

def get_debug_path():
    """Determine which debug path exists and return it."""
    paths = [
        "/Library/Application Support/com.comodo.osxclient",
        "/Library/Application Support/com.itarian.osxclient"
    ]
    for path in paths:
        if os.path.exists(path):
            return path
    print("Neither com.comodo.osxclient nor com.itarian.osxclient exists.")
    sys.exit(1)

def disable_debug_logging(debug_path):
    """Removes the debug_enabled file if it exists."""
    debug_file = os.path.join(debug_path, "debug_enabled")
    if os.path.exists(debug_file):
        try:
            subprocess.check_call(["sudo", "rm", debug_file])
            print(f"Debug logging disabled successfully. {debug_file} removed.")
        except subprocess.CalledProcessError as e:
            print("Failed to disable debug logging:", e)
            sys.exit(1)
    else:
        print(f"No debug logging file found at {debug_file}.")

def kill_cdmdaemon():
    """Finds and kills the cdmdaemon process."""
    try:
        pid = subprocess.check_output(["pgrep", "-x", "cdmdaemon"]).decode().strip()
        if pid:
            subprocess.check_call(["sudo", "kill", "-9", pid])
            print(f"Successfully killed cdmdaemon (PID: {pid}).")
        else:
            print("cdmdaemon is not running.")
    except subprocess.CalledProcessError:
        print("cdmdaemon process not found.")


debug_path = get_debug_path()
disable_debug_logging(debug_path)
kill_cdmdaemon()
