
#!/usr/bin/env bash

STATUS_FILE="$XDG_RUNTIME_DIR/touchpad.status"

# Detect touchpad name dynamically
TOUCHPAD_NAME=$(hyprctl devices | awk '/type: touchpad/{getline; print $2; exit}')
TOUCHPAD_KEYWORD="device:${TOUCHPAD_NAME}:enabled"

# Functions
enable_touchpad() {
    printf "true" >"$STATUS_FILE"
    notify-send -u normal "Touchpad Enabled"
    hyprctl keyword "$TOUCHPAD_KEYWORD" "true" -r
}

disable_touchpad() {
    printf "false" >"$STATUS_FILE"
    notify-send -u normal "Touchpad Disabled"
    hyprctl keyword "$TOUCHPAD_KEYWORD" "false" -r
}

# Get current state from Hyprland (1 = enabled, 0 = disabled)
CURRENT_STATE=$(hyprctl getoption "$TOUCHPAD_KEYWORD" | grep "int:" | awk '{print $2}')

# Initialize status file if missing
if ! [ -f "$STATUS_FILE" ]; then
    if [ "$CURRENT_STATE" -eq 1 ]; then
        printf "true" >"$STATUS_FILE"
    else
        printf "false" >"$STATUS_FILE"
    fi
fi

# Toggle based on status file
if [ "$(cat "$STATUS_FILE")" = "true" ]; then
    disable_touchpad
else
    enable_touchpad
fi
