#!/usr/bin/env bash
set -Eeuo pipefail

key=""
trigger_mode="press"
release_modifiers=""

while [ "$#" -gt 0 ]; do
    case "$1" in
        --key)
            shift
            key="${1:-}"
            [ -n "$key" ] || {
                echo "missing --key value" >&2
                exit 2
            }
            ;;
        --immediate)
            trigger_mode="press"
            ;;
        --trigger-on-release)
            trigger_mode="release"
            ;;
        --request-permission)
            exit 0
            ;;
        --release-modifiers)
            shift
            release_modifiers="${1:-}"
            ;;
        *)
            if [ -z "$key" ] && [[ "$1" != -* ]]; then
                key="$1"
            else
                echo "unknown bare modifier monitor argument '$1'" >&2
                exit 2
            fi
            ;;
    esac
    shift
done

normalize_key() {
    printf '%s' "$1" | tr '[:upper:]' '[:lower:]' | tr -d ' _-'
}

select_keys() {
    case "$(normalize_key "$1")" in
        doubleshift|shift+shift|leftshift+rightshift)
            printf '%s\t%s\n' "Shift_L Shift_R" "50 62"
            ;;
        doublealt|doubleoption|alt+alt|option+option|leftalt+rightalt|leftoption+rightoption)
            printf '%s\t%s\n' "Alt_L Alt_R Meta_L Meta_R ISO_Level3_Shift" "64 108"
            ;;
        doublesuper|doublecommand|doublemeta|super+super|command+command|meta+meta|leftsuper+rightsuper|leftcommand+rightcommand|leftmeta+rightmeta)
            printf '%s\t%s\n' "Super_L Super_R Meta_L Meta_R Hyper_L Hyper_R" "133 134"
            ;;
        doublecontrol|doublectrl|control+control|ctrl+ctrl|leftcontrol+rightcontrol|leftctrl+rightctrl)
            printf '%s\t%s\n' "Control_L Control_R" "37 105"
            ;;
        *)
            echo "unsupported bare modifier hotkey '$1'" >&2
            exit 2
            ;;
    esac
}

find_keycodes() {
    local symbols="$1"
    local fallbacks="$2"
    local symbol
    local code
    local codes=""

    for symbol in $symbols; do
        code="$(
            awk -v symbol="$symbol" '
                $1 == "keycode" {
                    for (i = 4; i <= NF; i++) {
                        if ($i == symbol) {
                            print $2
                            exit
                        }
                    }
                }
            ' <<<"${xmodmap_output:-}"
        )"
        if [ -n "$code" ]; then
            codes="$codes $code"
        fi
    done

    for code in $fallbacks; do
        codes="$codes $code"
    done

    printf '%s\n' "$codes" | tr ' ' '\n' | awk 'NF && !seen[$1]++' | tr '\n' ' '
}

if [ -n "$release_modifiers" ]; then
    echo "up"
    exit 0
fi

[ -n "$key" ] || {
    echo "missing --key" >&2
    exit 2
}

if [ -z "${DISPLAY:-}" ] || ! command -v xinput >/dev/null 2>&1; then
    echo "permission-denied"
    exit 1
fi

xmodmap_output="$(xmodmap -pke 2>/dev/null || true)"
IFS=$'\t' read -r key_symbols fallback_codes < <(select_keys "$key")
key_codes="$(find_keycodes "$key_symbols" "$fallback_codes")"
double_tap_ms="${CODEX_BARE_MODIFIER_DOUBLE_TAP_MS:-500}"

if [ -z "$key_codes" ]; then
    echo "modifier key did not resolve to any keycodes" >&2
    exit 1
fi

is_target_keycode() {
    local candidate="$1"
    local code

    for code in $key_codes; do
        [ "$candidate" = "$code" ] && return 0
    done
    return 1
}

now_ms() {
    date +%s%3N
}

last_tap_ms=0
last_tap_code=""
active=0

parent_pid="$PPID"
monitor_pid=""
event_fd=""
cleanup_started=0

cleanup() {
    [ "$cleanup_started" -eq 0 ] || return
    cleanup_started=1

    [ -n "$monitor_pid" ] || return
    kill "$monitor_pid" >/dev/null 2>&1 || true
    sleep 0.05
    kill -KILL "$monitor_pid" >/dev/null 2>&1 || true
    wait "$monitor_pid" >/dev/null 2>&1 || true
}
trap cleanup EXIT
trap 'exit 143' INT TERM

if command -v stdbuf >/dev/null 2>&1; then
    coproc APPSHOT_XINPUT { exec stdbuf -oL xinput test-xi2 --root 2>/dev/null; }
else
    coproc APPSHOT_XINPUT { exec xinput test-xi2 --root 2>/dev/null; }
fi
monitor_pid="$APPSHOT_XINPUT_PID"
event_fd="${APPSHOT_XINPUT[0]}"

sleep 0.1
if ! kill -0 "$monitor_pid" >/dev/null 2>&1; then
    monitor_status=0
    wait "$monitor_pid" || monitor_status=$?
    monitor_pid=""
    [ "$monitor_status" -ne 0 ] || monitor_status=1
    echo "permission-denied"
    exit "$monitor_status"
fi

echo "ready"

pending=""
while kill -0 "$parent_pid" >/dev/null 2>&1 && kill -0 "$monitor_pid" >/dev/null 2>&1; do
    if ! IFS= read -r -t 1 -u "$event_fd" line; then
        continue
    fi

    case "$line" in
        *"(RawKeyPress)")
            pending="press"
            continue
            ;;
        *"(RawKeyRelease)")
            pending="release"
            continue
            ;;
        EVENT\ type*)
            pending=""
            continue
            ;;
    esac

    [ -n "$pending" ] || continue
    set -- $line
    [ "${1:-}" = "detail:" ] || continue
    code="${2:-}"
    action="$pending"
    pending=""
    code="${code//[[:space:]]/}"
    [ -n "$code" ] || continue

    if ! is_target_keycode "$code"; then
        if [ "$action" = "press" ]; then
            last_tap_ms=0
            last_tap_code=""
        fi
        continue
    fi

    if [ "$action" = "press" ]; then
        current_ms="$(now_ms)"
        elapsed_ms=$((current_ms - last_tap_ms))
        if [ "$last_tap_ms" -gt 0 ] && [ "$elapsed_ms" -le "$double_tap_ms" ] && [ "$code" != "$last_tap_code" ]; then
            active=1
            last_tap_ms=0
            last_tap_code=""
            if [ "$trigger_mode" = "press" ]; then
                echo "down"
            fi
        else
            last_tap_ms="$current_ms"
            last_tap_code="$code"
        fi
        continue
    fi

    if [ "$action" = "release" ] && [ "$active" -eq 1 ]; then
        if [ "$trigger_mode" = "release" ]; then
            echo "down"
        fi
        echo "up"
        active=0
    fi
done
