#!/usr/bin/env python3
"""SuperV setup helper: registers each feature's keyboard shortcut with your desktop, and diagnostics.

Runs during install, when a feature is turned on or off, or when you change a shortcut. SuperV itself is C.

Features:  clipboard (Super+V, the default), emoji (Super+.), plain_paste (Ctrl+Super+Alt+V),
           color_picker (Super+Shift+C), text_extractor (Super+Shift+T), screenshot (Super+Shift+S)

  superv-setup --hotkey "<Ctrl><Alt>v"                        change the clipboard shortcut
  superv-setup --feature emoji --hotkey "<Ctrl><Alt>e"        change a feature's shortcut
  superv-setup [--feature NAME] --setup-hotkeys "<command>" [--hotkey ACCEL]
  superv-setup [--feature NAME | --all] --remove-hotkeys      put the desktop's shortcuts back
  superv-setup --doctor                                       check that everything is set up

Exit status: 0 done, 2 bad shortcut, 3 this desktop needs the shortcut added by hand (it says how).
"""
import json
import os
import re
import shlex
import shutil
import subprocess
import sys

from gi.repository import Gio, GLib

APP_ID = "io.github.superv.SuperV"
CONFIG_DIR = os.path.join(GLib.get_user_config_dir(), "superv")
MAIN_CONFIG = os.path.join(CONFIG_DIR, "config.ini")
# id: (name in the desktop's shortcut list, superv argument, default shortcut, settings file, its group, slug)
FEATURES = {
    "clipboard":      ("SuperV", "--toggle", "<Super>v", "config.ini", "superv", ""),
    "emoji":          ("SuperV Emoji", "--emoji", "<Super>period", "emoji.ini", "emoji", "emoji"),
    "plain_paste":    ("SuperV Plain Paste", "--plain-paste", "<Primary><Super><Alt>v", "shortcuts.ini", "plain_paste", "plain-paste"),
    "color_picker":   ("SuperV Colour Picker", "--pick-color", "<Shift><Super>c", "shortcuts.ini", "color_picker", "color-picker"),
    "text_extractor": ("SuperV Text Extractor", "--extract-text", "<Shift><Super>t", "shortcuts.ini", "text_extractor", "text-extractor"),
    "screenshot":     ("SuperV Screenshot", "--screenshot", "<Shift><Super>s", "shortcuts.ini", "screenshot", "screenshot"),
}
SWITCH = {"emoji": "emoji_panel"}   # the feature's on/off key in config.ini, when it isn't the feature's id
CIN_PATH = "/org/cinnamon/desktop/keybindings/custom-keybindings/%s/"
SCREENSHOT = "<Shift><Super>s"      # goes to the desktop's own screenshot tool while SuperV's is off (as in SuperV 1.x)
# desktop shortcuts that may already use the key we want: taken (and given back on removal)
CONFLICT_SCHEMAS = [
    "org.gnome.shell.keybindings", "org.gnome.desktop.wm.keybindings",
    "org.gnome.mutter.keybindings", "org.gnome.mutter.wayland.keybindings",
    "org.gnome.settings-daemon.plugins.media-keys",
    "org.cinnamon.desktop.keybindings.wm", "org.cinnamon.desktop.keybindings.media-keys",
    "org.cinnamon.desktop.keybindings",
    "org.freedesktop.ibus.panel.emoji",   # IBus's own emoji typing shortcut
]
MOD_ALIASES = {"control": "ctrl", "primary": "ctrl", "ctl": "ctrl", "mod4": "super",
               "mod1": "alt", "meta": "alt", "win": "super"}
# GTK key names -> how KDE, Sway/i3 and Hyprland write them
KEY_SYMBOLS = {"period": ".", "comma": ",", "semicolon": ";", "colon": ":", "slash": "/", "backslash": "\\",
               "minus": "-", "equal": "=", "grave": "`", "apostrophe": "'", "bracketleft": "[",
               "bracketright": "]", "space": "Space", "page_up": "PgUp", "page_down": "PgDown",
               "prior": "PgUp", "next": "PgDown", "escape": "Esc", "delete": "Del", "insert": "Ins",
               "return": "Return", "backspace": "Backspace", "tab": "Tab", "home": "Home", "end": "End",
               "greater": ">", "less": "<", "question": "?", "exclam": "!", "at": "@", "numbersign": "#",
               "dollar": "$", "percent": "%", "asciicircum": "^", "ampersand": "&", "asterisk": "*",
               "parenleft": "(", "parenright": ")", "underscore": "_", "plus": "+", "braceleft": "{",
               "braceright": "}", "bar": "|", "quotedbl": '"', "asciitilde": "~"}


def backup_path(feature):
    """Where a feature keeps what it changed on the desktop (names as in SuperV 1.x / 2.0, so old installs undo cleanly)."""
    return os.path.join(CONFIG_DIR, "hotkey-backup.json" if feature == "clipboard" else "%s-hotkey-backup.json" % feature)


def select(feature):
    """Point the module-level settings below at one feature."""
    global FEATURE, APP_NAME, ARG, DEFAULT, CONFIG, GROUP, BACKUP, GNOME_PATH, KDE_DESKTOP
    FEATURE = feature
    APP_NAME, ARG, DEFAULT, conf, GROUP, slug = FEATURES[feature]
    CONFIG = os.path.join(CONFIG_DIR, conf)
    BACKUP = backup_path(feature)
    GNOME_PATH = "/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/superv%s/" % ("-" + slug if slug else "")
    KDE_DESKTOP = "superv-%s.desktop" % (slug or "toggle")


select("clipboard")


def settings(schema_id, path=None):
    src = Gio.SettingsSchemaSource.get_default()
    schema = src.lookup(schema_id, True) if src else None
    if not schema or (path is None and schema.get_path() is None):
        return None
    return Gio.Settings.new_full(schema, None, path)


def has_key(s, key):
    return s is not None and s.props.settings_schema.has_key(key)


def normalize(accel):
    """'<Primary><Super>V' and '<Super><Control>v' -> same tuple, for conflict checks."""
    mods = frozenset(MOD_ALIASES.get(m.lower(), m.lower()) for m in re.findall(r"<([^>]+)>", accel))
    return mods, re.sub(r"<[^>]+>", "", accel).strip().lower()


def to_mate(accel):
    return accel.replace("<Super>", "<Mod4>")


def load_backup(path=None):
    try:
        with open(path or BACKUP) as f:
            data = json.load(f)
    except Exception:
        data = {}
    data.setdefault("took", {})   # bindings we removed from the desktop's own shortcuts: {schema: {key: [bindings]}}
    data.setdefault("was", {})    # ...and the whole list as it was, to put them back in the same place
    data.setdefault("gave", {})   # bindings we added to them
    return data


def save_backup(data, path=None):
    os.makedirs(CONFIG_DIR, mode=0o700, exist_ok=True)
    fd = os.open(path or BACKUP, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600)
    with os.fdopen(fd, "w") as f:
        json.dump(data, f, indent=2)


def strv(schema_id, key):
    s = settings(schema_id)
    if has_key(s, key) and s.get_value(key).get_type_string() == "as":
        return s, s.get_strv(key)
    return None, None


def give_back(backup):
    """Undo "took": add the bindings we removed, unless they're there again. Only our own change is undone,
    so anything you changed on that key since stays."""
    for schema_id, keys in backup.pop("freed", {}).items():   # SuperV 2.0 and older kept whole lists
        for key, value in keys.items():
            s, _ = strv(schema_id, key)
            if s:
                s.set_strv(key, value)
    for schema_id, keys in backup["took"].items():
        for key, bindings in keys.items():
            s, cur = strv(schema_id, key)
            if s:
                was = backup["was"].get(schema_id, {}).get(key)
                have = [normalize(b) for b in cur]
                add = [b for b in bindings if normalize(b) not in have]
                if was and [b for b in was if b not in bindings] == cur:
                    s.set_strv(key, was)          # untouched since: exactly as it was, same order
                elif add:
                    s.set_strv(key, cur + add)
    backup["took"], backup["was"] = {}, {}


def take_away(backup):
    """Undo "gave": remove the bindings we added."""
    for schema_id, keys in backup.pop("added", {}).items():   # SuperV 2.0 and older kept the list as it was before
        for key, value in keys.items():
            s, _ = strv(schema_id, key)
            if s:
                s.set_strv(key, value)
    for schema_id, keys in backup["gave"].items():
        for key, bindings in keys.items():
            s, cur = strv(schema_id, key)
            if s:
                ours = [normalize(b) for b in bindings]
                s.set_strv(key, [b for b in cur if normalize(b) not in ours])
    backup["gave"] = {}


def keyfile(path=None):
    kf = GLib.KeyFile()
    try:
        kf.load_from_file(path or CONFIG, GLib.KeyFileFlags.KEEP_COMMENTS)
    except GLib.Error:
        pass
    return kf


def save_config(kf):
    os.makedirs(CONFIG_DIR, mode=0o700, exist_ok=True)
    kf.save_to_file(CONFIG)
    os.chmod(CONFIG, 0o600)


def save_hotkey_to_config(accel):
    kf = keyfile()
    kf.set_string(GROUP, "hotkey", accel)
    kf.set_boolean(GROUP, "shortcut_configured", True)
    save_config(kf)


def mark_unconfigured():
    """After removing the shortcut, make SuperV set it up again when the feature is on."""
    if os.path.exists(CONFIG):
        kf = keyfile()
        kf.set_boolean(GROUP, "shortcut_configured", False)
        save_config(kf)


def read_hotkey_from_config():
    try:
        return keyfile().get_string(GROUP, "hotkey").strip() or DEFAULT
    except GLib.Error:
        return DEFAULT


def feature_on(feature):
    """Is the feature turned on in SuperV's settings? (the clipboard panel always is; the others are off by default)"""
    if feature == "clipboard":
        return True
    try:
        return keyfile(MAIN_CONFIG).get_boolean("superv", SWITCH.get(feature, feature))
    except GLib.Error:
        return False


def default_command():
    exe = os.path.join(os.path.dirname(os.path.realpath(__file__)), "superv")
    if not os.path.exists(exe):
        exe = shutil.which("superv") or "/usr/bin/superv"
    return shlex.quote(exe) + " " + ARG


def key_symbol(key):
    return KEY_SYMBOLS.get(key.lower(), key.upper() if len(key) == 1 else key[:1].upper() + key[1:])


# ---------------------------------------------------------------- Xfce
XFCE = ["xfconf-query", "-c", "xfce4-keyboard-shortcuts"]


def xfce_get(prop):
    r = subprocess.run(XFCE + ["-p", prop], capture_output=True, text=True)
    return r.stdout.strip() if r.returncode == 0 else None


def xfce_set(prop, command):
    return subprocess.run(XFCE + ["-p", prop, "-n", "-t", "string", "-s", command],
                          stderr=subprocess.DEVNULL).returncode == 0


def xfce_reset(prop):
    subprocess.run(XFCE + ["-p", prop, "-r"], stderr=subprocess.DEVNULL)


# ---------------------------------------------------------------- Super+Shift+S for the desktop's own screenshot tool
def share_screenshot_key():
    """While SuperV's screenshot tool is off, Super+Shift+S opens the desktop's own (and the picture lands in your
    clipboard history), as in SuperV 1.x. Remembered in the clipboard feature's backup, undone on removal."""
    if feature_on("screenshot"):
        return []
    path = backup_path("clipboard")
    backup = load_backup(path)
    out = []
    for schema_id, key, label in (
            ("org.gnome.shell.keybindings", "show-screenshot-ui", "GNOME: Super+Shift+S → screenshot (lands in your clipboard history)"),
            ("org.cinnamon.desktop.keybindings.media-keys", "area-screenshot-clip", "Cinnamon: Super+Shift+S → area screenshot to clipboard")):
        s, cur = strv(schema_id, key)
        if not s:
            continue
        if normalize(SCREENSHOT) not in [normalize(b) for b in cur]:
            given = backup["gave"].setdefault(schema_id, {}).setdefault(key, [])
            if SCREENSHOT not in given:
                given.append(SCREENSHOT)
            s.set_strv(key, cur + [SCREENSHOT])
        out.append(label)
    if "XFCE" in os.environ.get("XDG_CURRENT_DESKTOP", "").upper() and shutil.which("xfconf-query") \
            and shutil.which("xfce4-screenshooter"):
        prop = "/commands/custom/" + SCREENSHOT
        if xfce_get(prop) is None and xfce_set(prop, "xfce4-screenshooter -r -c"):   # never over your own binding
            backup["xfce_shot"] = prop
            out.append("Xfce: Super+Shift+S → area screenshot to clipboard")
    Gio.Settings.sync()
    save_backup(backup, path)
    return out


# ---------------------------------------------------------------- KDE Plasma
KLIPPER = ("plasmashell", "show-on-mouse-pos")            # Klipper's "Show clipboard items at mouse position" (Meta+V)
EMOJIER = "org.kde.plasma.emojier.desktop"                # Plasma's own emoji picker, on Meta+. by default


def kde_tools():
    ver = os.environ.get("KDE_SESSION_VERSION", "")
    write = shutil.which("kwriteconfig6") or shutil.which("kwriteconfig5")
    read = shutil.which("kreadconfig6") or shutil.which("kreadconfig5")
    return (ver or ("6" if write and write.endswith("6") else "5")), write, read


def kde_accel(accel):
    """'<Super>period' -> 'Meta+.' (KDE's format)."""
    mods, key = normalize(accel)
    names = [n for m, n in (("super", "Meta"), ("ctrl", "Ctrl"), ("alt", "Alt"), ("shift", "Shift")) if m in mods]
    return "+".join(names + [key_symbol(key)])


def desktop_exec(command):
    """A shell-quoted command -> the quoting .desktop files understand (double quotes only)."""
    return " ".join('"%s"' % a.replace('"', '\\"') if re.search(r"[\s\"']", a) else a for a in shlex.split(command))


def kde_restart_shortcuts(ver):
    if ver == "6":
        subprocess.run(["systemctl", "--user", "try-restart", "plasma-kglobalaccel.service"],
                       stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
        return True
    return False   # Plasma 5: picked up at next login


def kde_group(ver, name):
    return ["--group", "services", "--group", name] if ver == "6" else ["--group", name]


def kde_setup(command, accel, backup):
    ver, write, read = kde_tools()
    if not write:
        return None
    apps = os.path.join(GLib.get_user_data_dir(), "applications")
    os.makedirs(apps, exist_ok=True)
    with open(os.path.join(apps, KDE_DESKTOP), "w") as f:
        f.write("[Desktop Entry]\nType=Application\nName=%s\nExec=%s\n"
                "Icon=%s\nNoDisplay=true\nX-KDE-GlobalAccel-CommandShortcut=true\n"
                % (APP_NAME if FEATURE != "clipboard" else "SuperV (open/close)", desktop_exec(command), APP_ID))
    key = kde_accel(accel)
    rc = ["--file", "kglobalshortcutsrc"]
    if ver == "6":
        subprocess.run([write] + rc + kde_group(ver, KDE_DESKTOP) + ["--key", "_launch", key])
    else:
        subprocess.run([write] + rc + kde_group(ver, KDE_DESKTOP) + ["--key", "_k_friendly_name", APP_NAME])
        subprocess.run([write] + rc + kde_group(ver, KDE_DESKTOP) + ["--key", "_launch", "%s,none,%s" % (key, APP_NAME)])
    if read and FEATURE == "clipboard" and key == "Meta+V":
        # Klipper (KDE's own clipboard tool) uses Meta+V: free it, remember the old value
        cur = subprocess.run([read] + rc + ["--group", KLIPPER[0], "--key", KLIPPER[1]],
                             capture_output=True, text=True).stdout.strip()
        if cur.split(",")[0] == "Meta+V":
            backup.setdefault("kde_klipper", cur)
            subprocess.run([write] + rc + ["--group", KLIPPER[0], "--key", KLIPPER[1],
                                           "none," + ",".join(cur.split(",")[1:])])
    if read and FEATURE == "emoji":
        # Plasma's emoji picker uses Meta+. : free it, remember the old value
        cur = subprocess.run([read] + rc + kde_group(ver, EMOJIER) + ["--key", "_launch"],
                             capture_output=True, text=True).stdout.strip()
        if cur.split(",")[0] == key or (not cur and key == "Meta+."):   # no entry = still on its default, Meta+.
            backup.setdefault("kde_emojier", cur or "<default>")
            off = ",".join(["none"] + cur.split(",")[1:]) if cur else (
                "none" if ver == "6" else "none,Meta+.,Emoji Selector")
            subprocess.run([write] + rc + kde_group(ver, EMOJIER) + ["--key", "_launch", off])
    live = kde_restart_shortcuts(ver)
    return "KDE Plasma %s: %s → %s%s" % (ver, key, APP_NAME, "" if live else " (log out and back in once)")


def kde_remove(backup):
    ver, write, read = kde_tools()
    if not write:
        return
    rc = ["--file", "kglobalshortcutsrc"]
    for key in ("_launch", "_k_friendly_name"):
        subprocess.run([write] + rc + kde_group(ver, KDE_DESKTOP) + ["--key", key, "--delete"],
                       stderr=subprocess.DEVNULL)
    if backup.get("kde_klipper"):
        subprocess.run([write] + rc + ["--group", KLIPPER[0], "--key", KLIPPER[1], backup["kde_klipper"]])
    old = backup.get("kde_emojier")
    if old == "<default>":   # it had no entry: drop ours so its default applies again
        subprocess.run([write] + rc + kde_group(ver, EMOJIER) + ["--key", "_launch", "--delete"],
                       stderr=subprocess.DEVNULL)
    elif old:
        subprocess.run([write] + rc + kde_group(ver, EMOJIER) + ["--key", "_launch", old])
    try:
        os.remove(os.path.join(GLib.get_user_data_dir(), "applications", KDE_DESKTOP))
    except OSError:
        pass
    kde_restart_shortcuts(ver)


def manual_instructions(command, accel):
    """Desktops that keep shortcuts in their own config files."""
    run = " ".join(shlex.split(command))
    mods, key = normalize(accel)
    if os.environ.get("HYPRLAND_INSTANCE_SIGNATURE"):
        hmods = " ".join(m.upper() for m in ("super", "ctrl", "alt", "shift") if m in mods)
        print("  Hyprland: add to ~/.config/hypr/hyprland.conf:")
        print("    exec-once = superv --daemon")
        print("    bind = %s, %s, exec, %s" % (hmods, key, run))
    elif os.environ.get("SWAYSOCK") or os.environ.get("I3SOCK"):
        wm = "sway" if os.environ.get("SWAYSOCK") else "i3"
        smods = "+".join({"super": "$mod", "ctrl": "Ctrl", "alt": "Mod1", "shift": "Shift"}[m]
                         for m in ("super", "ctrl", "alt", "shift") if m in mods)
        print("  %s: add to ~/.config/%s/config:" % (wm, wm))
        print("    exec %ssuperv --daemon" % ("" if wm == "sway" else "--no-startup-id "))
        print("    bindsym %s+%s exec %s%s" % (smods, key, "" if wm == "sway" else "--no-startup-id ", run))
    elif "COSMIC" in os.environ.get("XDG_CURRENT_DESKTOP", "").upper():
        print("  COSMIC: Settings → Keyboard → Keyboard Shortcuts → Custom shortcuts → Add,")
        print("    command: %s   shortcut: %s" % (run, kde_accel(accel).replace("Meta", "Super")))
    else:
        print("  ! Couldn't find your desktop's shortcut settings.")
        print("    Add a custom keyboard shortcut in your desktop's settings that runs:")
        print("    %s" % run)


def setup(command, accel):
    """Returns True when a desktop took the shortcut, False when you have to add it by hand."""
    out = []
    backup = load_backup()
    # put back whatever we took for a previous shortcut, then take the new one
    give_back(backup)
    want = normalize(accel)
    for schema_id in CONFLICT_SCHEMAS:
        s = settings(schema_id)
        if not s:
            continue
        for key in s.props.settings_schema.list_keys():
            v = s.get_value(key)
            if v.get_type_string() != "as" or (schema_id.startswith("org.cinnamon.desktop.keybindings")
                                                and key == "custom-list"):
                continue
            cur = list(v.unpack())
            gone = [b for b in cur if normalize(b) == want]
            if gone:
                backup["took"].setdefault(schema_id, {})[key] = gone
                backup["was"].setdefault(schema_id, {})[key] = cur
                s.set_strv(key, [b for b in cur if normalize(b) != want])
                out.append("freed %s from %s → %s" % (accel, schema_id.split(".")[-1], key))

    # GNOME / Ubuntu / Budgie / Pantheon
    mk = settings("org.gnome.settings-daemon.plugins.media-keys")
    if has_key(mk, "custom-keybindings"):
        paths = mk.get_strv("custom-keybindings")
        if GNOME_PATH not in paths:
            mk.set_strv("custom-keybindings", paths + [GNOME_PATH])
        kb = settings("org.gnome.settings-daemon.plugins.media-keys.custom-keybinding", GNOME_PATH)
        kb.set_string("name", APP_NAME)
        kb.set_string("command", command)
        kb.set_string("binding", accel)
        out.append("GNOME: %s → %s" % (accel, APP_NAME))

    # Cinnamon (Linux Mint)
    cin = settings("org.cinnamon.desktop.keybindings")
    if has_key(cin, "custom-list"):
        ids = cin.get_strv("custom-list")
        mine = next((i for i in ids if settings("org.cinnamon.desktop.keybindings.custom-keybinding",
                                                 CIN_PATH % i).get_string("name") == APP_NAME), None)
        if mine is None:
            n = 0
            while "custom%d" % n in ids:
                n += 1
            mine = "custom%d" % n
            cin.set_strv("custom-list", ids + [mine])
        kb = settings("org.cinnamon.desktop.keybindings.custom-keybinding", CIN_PATH % mine)
        kb.set_string("name", APP_NAME)
        kb.set_string("command", command)
        if kb.get_value("binding").get_type_string() == "as":
            kb.set_strv("binding", [accel])
        else:
            kb.set_string("binding", accel)
        out.append("Cinnamon: %s → %s" % (accel, APP_NAME))

    # MATE: twelve command slots; an unused one holds "" or " " (MATE's default)
    cmds = settings("org.mate.Marco.keybinding-commands")
    gkb = settings("org.mate.Marco.global-keybindings")
    if cmds and gkb:
        slot = None
        for n in range(1, 13):
            c = cmds.get_string("command-%d" % n)
            if "superv" in c and ARG in c:
                slot = n
                break
            if slot is None and not c.strip():
                slot = n
        if slot:
            cmds.set_string("command-%d" % slot, command)
            gkb.set_string("run-command-%d" % slot, to_mate(accel))
            out.append("MATE: %s → %s" % (accel, APP_NAME))

    # Xfce
    if "XFCE" in os.environ.get("XDG_CURRENT_DESKTOP", "").upper() and shutil.which("xfconf-query"):
        old = backup.get("xfce_prop")
        if old:
            xfce_reset(old)
            if backup.pop("xfce_prev", None):
                xfce_set(old, backup.pop("xfce_prev_cmd", ""))
        prop = "/commands/custom/" + accel
        prev = xfce_get(prop)
        if prev is not None and "superv" not in prev and prev != "xfce4-screenshooter -r -c":
            backup["xfce_prev"], backup["xfce_prev_cmd"] = True, prev   # your own binding on that key: put back on removal
        if xfce_set(prop, command):
            backup["xfce_prop"] = prop
            out.append("Xfce: %s → %s" % (accel, APP_NAME))

    # KDE Plasma
    if "KDE" in os.environ.get("XDG_CURRENT_DESKTOP", "").upper():
        line = kde_setup(command, accel, backup)
        if line:
            out.append(line)

    Gio.Settings.sync()
    save_backup(backup)
    save_hotkey_to_config(accel)
    done = any(("→ " + APP_NAME) in line for line in out)
    if FEATURE == "clipboard":
        out += share_screenshot_key()
    for line in out:
        print("  ✓", line)
    if not done:
        manual_instructions(command, accel)
    return done


def remove():
    backup = load_backup()
    give_back(backup)
    take_away(backup)
    mk = settings("org.gnome.settings-daemon.plugins.media-keys")
    if has_key(mk, "custom-keybindings"):
        mk.set_strv("custom-keybindings", [p for p in mk.get_strv("custom-keybindings") if p != GNOME_PATH])
        kb = settings("org.gnome.settings-daemon.plugins.media-keys.custom-keybinding", GNOME_PATH)
        for k in ("name", "command", "binding"):
            kb.reset(k)
    cin = settings("org.cinnamon.desktop.keybindings")
    if has_key(cin, "custom-list"):
        keep = []
        for cid in cin.get_strv("custom-list"):
            kb = settings("org.cinnamon.desktop.keybindings.custom-keybinding", CIN_PATH % cid)
            if kb.get_string("name") == APP_NAME:
                for k in ("name", "command", "binding"):
                    kb.reset(k)
            else:
                keep.append(cid)
        cin.set_strv("custom-list", keep)
    cmds = settings("org.mate.Marco.keybinding-commands")
    gkb = settings("org.mate.Marco.global-keybindings")
    if cmds and gkb:
        for n in range(1, 13):
            c = cmds.get_string("command-%d" % n)
            if "superv" in c and ARG in c:
                cmds.reset("command-%d" % n)
                gkb.reset("run-command-%d" % n)
    if shutil.which("xfconf-query"):
        if backup.get("xfce_prop"):
            xfce_reset(backup["xfce_prop"])
            if backup.get("xfce_prev"):
                xfce_set(backup["xfce_prop"], backup.get("xfce_prev_cmd", ""))
        if backup.get("xfce_shot"):
            if xfce_get(backup["xfce_shot"]) == "xfce4-screenshooter -r -c":   # still ours
                xfce_reset(backup["xfce_shot"])
    if ("KDE" in os.environ.get("XDG_CURRENT_DESKTOP", "").upper() or backup.get("kde_klipper")
            or backup.get("kde_emojier")
            or os.path.exists(os.path.join(GLib.get_user_data_dir(), "applications", KDE_DESKTOP))):
        kde_remove(backup)
    Gio.Settings.sync()
    mark_unconfigured()
    try:
        os.remove(BACKUP)
    except OSError:
        pass
    if FEATURE == "screenshot":   # SuperV's screenshot tool is off again: the desktop's own gets Super+Shift+S
        share_screenshot_key()
    print("  ✓ %s: desktop shortcuts restored" % APP_NAME)


def mark(state):
    return {True: "\033[38;5;141m✓\033[0m", False: "\033[31m✗\033[0m", None: "·"}[state]


def doctor_shortcut():
    accel = read_hotkey_from_config()
    print("   shortcut (config):", accel)
    kb = settings("org.gnome.settings-daemon.plugins.media-keys.custom-keybinding", GNOME_PATH)
    mk = settings("org.gnome.settings-daemon.plugins.media-keys")
    if kb and has_key(mk, "custom-keybindings") and GNOME_PATH in mk.get_strv("custom-keybindings"):
        print(mark(True), "GNOME shortcut:", kb.get_string("binding"), "→", kb.get_string("command"))
    cin = settings("org.cinnamon.desktop.keybindings")
    if has_key(cin, "custom-list"):
        for cid in cin.get_strv("custom-list"):
            kb = settings("org.cinnamon.desktop.keybindings.custom-keybinding", CIN_PATH % cid)
            if kb.get_string("name") == APP_NAME:
                print(mark(True), "Cinnamon shortcut:", kb.get_value("binding").unpack(), "→",
                      kb.get_string("command"))
    cmds = settings("org.mate.Marco.keybinding-commands")
    gkb = settings("org.mate.Marco.global-keybindings")
    if cmds and gkb:
        for n in range(1, 13):
            c = cmds.get_string("command-%d" % n)
            if "superv" in c and ARG in c:
                print(mark(True), "MATE shortcut:", gkb.get_string("run-command-%d" % n), "→", c)
    return accel


def doctor():
    distro = "?"
    try:
        with open("/etc/os-release") as f:
            distro = dict(l.rstrip().split("=", 1) for l in f if "=" in l).get("PRETTY_NAME", "?").strip('"')
    except OSError:
        pass
    print("System  : %s" % distro)
    print("Session : %s / %s" % (os.environ.get("XDG_SESSION_TYPE", "?"),
                                 os.environ.get("XDG_CURRENT_DESKTOP", "?")))
    bus = Gio.bus_get_sync(Gio.BusType.SESSION, None)
    running = bus.call_sync("org.freedesktop.DBus", "/org/freedesktop/DBus", "org.freedesktop.DBus",
                            "NameHasOwner", GLib.Variant("(s)", (APP_ID,)), GLib.VariantType("(b)"),
                            Gio.DBusCallFlags.NONE, -1, None).unpack()[0]
    print(mark(running), "SuperV running" + ("" if running else " — start it: superv --daemon &"))
    if running:
        pid = bus.call_sync("org.freedesktop.DBus", "/org/freedesktop/DBus", "org.freedesktop.DBus",
                            "GetConnectionUnixProcessID", GLib.Variant("(s)", (APP_ID,)),
                            GLib.VariantType("(u)"), Gio.DBusCallFlags.NONE, -1, None).unpack()[0]
        try:
            with open("/proc/%d/status" % pid) as f:
                st = dict(line.split(":", 1) for line in f if ":" in line)
            print("   memory: %s private (%s incl. shared libraries and fonts)"
                  % (st["RssAnon"].strip(), st["VmRSS"].strip()))
        except OSError:
            pass
    up = os.access("/dev/uinput", os.W_OK)
    x11 = os.environ.get("XDG_SESSION_TYPE") == "x11"
    print(mark(up or x11), "pastes and types into apps:", "yes (uinput)" if up else ("yes (XTest, X11)" if x11 else
          "not yet — open SuperV and click Enable (needs your password once), then log out/in"))
    for feature in FEATURES:
        select(feature)
        print()
        if feature == "clipboard":
            print("Clipboard history")
            accel = doctor_shortcut()
            shell = settings("org.gnome.shell.keybindings")
            if has_key(shell, "toggle-message-tray"):
                tray = shell.get_strv("toggle-message-tray")
                clash = normalize(accel) in [normalize(b) for b in tray]
                print(mark(not clash), "notification list doesn't use %s %s" % (accel, tray))
            continue
        on = feature_on(feature)
        print(mark(True if on else None), "%s:" % APP_NAME.replace("SuperV ", ""),
              "on" if on else "off (turn it on in SuperV's settings: Super+V, then the gear)")
        if not on:
            continue
        if feature == "emoji":
            # a colour emoji font (fontconfig 2.13.1+ knows "color"; older ones: any font with 😀)
            fonts = subprocess.run(["fc-list", ":charset=1f600:color=true", "family"], capture_output=True, text=True).stdout.strip() \
                or subprocess.run(["fc-list", ":charset=1f600", "family"], capture_output=True, text=True).stdout.strip()
            print(mark(bool(fonts)), "emoji font:", fonts.splitlines()[0] if fonts else
                  "none — install Noto Color Emoji (fonts-noto-color-emoji / google-noto-emoji-color-fonts / noto-fonts-emoji)")
        if feature == "text_extractor":
            tess = shutil.which("tesseract")
            print(mark(bool(tess)), "text recognition:", tess or
                  "not installed — press the shortcut once and SuperV offers to install Tesseract")
        doctor_shortcut()


def main(argv):
    if "--doctor" in argv:
        doctor()
        return 0
    if "--all" in argv:
        if "--remove-hotkeys" not in argv:
            print("--all goes with --remove-hotkeys or --doctor", file=sys.stderr)
            return 2
        for f in reversed(list(FEATURES)):   # the features first, the clipboard last: undo in reverse order
            main(["--feature", f, "--remove-hotkeys"])
        return 0
    if "--feature" in argv:
        f = argv[argv.index("--feature") + 1] if argv.index("--feature") + 1 < len(argv) else ""
        if f not in FEATURES:
            print("Unknown feature %r; one of: %s" % (f, ", ".join(FEATURES)), file=sys.stderr)
            return 2
        select(f)
    if "--remove-hotkeys" in argv:
        remove()
    elif "--setup-hotkeys" in argv or "--hotkey" in argv:
        cmd = argv[argv.index("--setup-hotkeys") + 1] if "--setup-hotkeys" in argv else default_command()
        accel = argv[argv.index("--hotkey") + 1] if "--hotkey" in argv else read_hotkey_from_config()
        if not re.fullmatch(r"(<[A-Za-z0-9]+>)+[A-Za-z0-9_]+", accel):
            print("Shortcut should look like <Super>v or <Ctrl><Alt>v", file=sys.stderr)
            return 2
        mods = normalize(accel)[0]
        if not ("super" in mods or {"ctrl", "alt"} <= mods):
            # Ctrl+…, Alt+… are needed by apps (copy, paste, menus)
            print("Use Super or Ctrl+Alt in the shortcut, e.g. <Super>v or <Ctrl><Alt>v", file=sys.stderr)
            return 2
        return 0 if setup(cmd, accel) else 3
    else:
        print(__doc__)
    return 0


if __name__ == "__main__":
    sys.exit(main(sys.argv[1:]))
