#!/bin/bash

# AZERTY Global - Linux Uninstaller
# Removes only the AZERTY Global XKB registration and Compose fragment.

set -e

if [ "$EUID" -ne 0 ]; then
  echo "Please run as root (sudo ./uninstall.sh)"
  exit 1
fi

LAYOUT_NAME="azerty_global"
SYMBOLS_DEST="/usr/share/X11/xkb/symbols/$LAYOUT_NAME"

timestamp() {
    date +%Y%m%d_%H%M%S
}

backup_file() {
    local file="$1"
    local backup="${file}.backup.$(timestamp)"
    cp "$file" "$backup"
    echo "✓ Backed up $file to $backup"
}

remove_layout_from_xml() {
    local xml_file="$1"

    if [ ! -f "$xml_file" ]; then
        return
    fi

    if ! grep -q "<name>${LAYOUT_NAME}</name>" "$xml_file"; then
        echo "✓ Layout not registered in $xml_file"
        return
    fi

    local tmp_file
    tmp_file="$(mktemp)"

    if awk -v layout_name="$LAYOUT_NAME" '
        BEGIN {
            in_layout = 0
            remove_layout = 0
            buffer = ""
        }
        /^[[:space:]]*<layout>[[:space:]]*$/ {
            in_layout = 1
            remove_layout = 0
            buffer = $0 ORS
            next
        }
        in_layout == 1 {
            buffer = buffer $0 ORS
            if ($0 ~ "<name>" layout_name "</name>") {
                remove_layout = 1
            }
            if ($0 ~ /<\/layout>/) {
                if (remove_layout == 0) {
                    printf "%s", buffer
                }
                in_layout = 0
                remove_layout = 0
                buffer = ""
            }
            next
        }
        { print }
        END {
            if (in_layout == 1 && remove_layout == 0) {
                printf "%s", buffer
            }
        }
    ' "$xml_file" > "$tmp_file"; then
        backup_file "$xml_file"
        cp "$tmp_file" "$xml_file"
        rm -f "$tmp_file"
        echo "✓ Removed AZERTY Global from $xml_file"
    else
        rm -f "$tmp_file"
        echo "✗ Could not update $xml_file" >&2
        exit 1
    fi
}

remove_compose_rules() {
    local real_user
    local user_home
    local user_group
    local fragment_dest
    local compose_dest
    local include_line
    local tmp_file

    real_user=${SUDO_USER:-$USER}
    user_home=$(getent passwd "$real_user" | cut -d: -f6)
    user_group=$(id -gn "$real_user" 2>/dev/null || true)

    if [ -z "$user_group" ]; then
        user_group="$real_user"
    fi

    if [ -z "$user_home" ] || [ ! -d "$user_home" ]; then
        echo "⚠ Could not detect real user home to remove Compose rules"
        return
    fi

    fragment_dest="$user_home/.XCompose_azerty_global"
    compose_dest="$user_home/.XCompose"
    include_line='include "%H/.XCompose_azerty_global"'

    if [ -f "$compose_dest" ] && grep -Fxq "$include_line" "$compose_dest"; then
        backup_file "$compose_dest"
        tmp_file="$(mktemp)"
        grep -Fxv "$include_line" "$compose_dest" > "$tmp_file"
        cp "$tmp_file" "$compose_dest"
        rm -f "$tmp_file"
        chown "$real_user:$user_group" "$compose_dest"
        echo "✓ Removed AZERTY Global include from ~/.XCompose"
    else
        echo "✓ ~/.XCompose does not include AZERTY Global"
    fi

    if [ -f "$fragment_dest" ]; then
        rm -f "$fragment_dest"
        echo "✓ Removed ~/.XCompose_azerty_global"
    else
        echo "✓ AZERTY Global Compose fragment already absent"
    fi
}

echo "Uninstalling AZERTY Global..."

if [ -f "$SYMBOLS_DEST" ]; then
    rm -f "$SYMBOLS_DEST"
    echo "✓ Removed symbols file $SYMBOLS_DEST"
else
    echo "✓ Symbols file already absent"
fi

remove_layout_from_xml "/usr/share/X11/xkb/rules/evdev.xml"
remove_layout_from_xml "/usr/share/X11/xkb/rules/base.xml"
remove_compose_rules

echo ""
echo "Success! Please restart your session (logout/login)."
