#!/bin/bash

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
BACKUP="$SCRIPT_DIR/gnome-shortcuts-backup.dconf"

SCHEMAS=(
    "org.gnome.desktop.wm.keybindings"
    "org.gnome.mutter.keybindings"
    "org.gnome.mutter.wayland.keybindings"
    "org.gnome.shell.keybindings"
    "org.gnome.settings-daemon.plugins.media-keys"
    "org.gnome.desktop.a11y.keyboard"
)

backup_all() {
    echo "Guardando todos los atajos en $BACKUP..."
    > "$BACKUP"
    for schema in "${SCHEMAS[@]}"; do
        echo "# $schema" >> "$BACKUP"
        dconf dump "/$(echo $schema | tr . /)/" >> "$BACKUP"
        echo "" >> "$BACKUP"
    done
}

disable_all() {
    backup_all

    echo "Deshabilitando todos los atajos…"

    for schema in "${SCHEMAS[@]}"; do
        echo "→ Reset: $schema"
        dconf reset -f "/$(echo $schema | tr . /)/"
    done

    echo "→ Deshabilitando overlay-key (Super)…"
    gsettings set org.gnome.mutter overlay-key ''

    echo "→ Forzando todos los atajos a vacío…"
    for schema in "${SCHEMAS[@]}"; do
        for key in $(gsettings list-keys "$schema"); do
            gsettings set "$schema" "$key" "[]"
        done
    done

    echo "Todos los atajos deshabilitados."
}

enable_all() {
    if [ ! -f "$BACKUP" ]; then
        echo "No existe el archivo de backup: $BACKUP"
        exit 1
    fi

    echo "Restaurando atajos desde $BACKUP…"
    dconf load / < "$BACKUP"

    echo "Restaurando overlay-key…"
    gsettings reset org.gnome.mutter.overlay-key

    echo "Atajos restaurados."
}

case "$1" in
  disable)
    disable_all
    ;;
  enable)
    enable_all
    ;;
  *)
    echo "Uso: $0 {disable|enable}"
    exit 1
    ;;
esac
