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

# nomarchy-docs-keybindings
#
# Regenerates docs/KEYBINDINGS.md from the Hyprland binding files. Run from the
# repo root or anywhere — paths are resolved relative to this script.
#
#   nomarchy-docs-keybindings                       # write to stdout
#   nomarchy-docs-keybindings --out docs/KEYBINDINGS.md
#
# Source files in render order. Each entry is "<repo-relative path>|<title>".

repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"

sources=(
  "core/home/config/nomarchy/default/hypr/bindings/utilities.conf|Utilities"
  "core/home/config/nomarchy/default/hypr/bindings/tiling-v2.conf|Tiling"
  "core/home/config/nomarchy/default/hypr/bindings/clipboard.conf|Clipboard"
  "core/home/config/nomarchy/default/hypr/bindings/media.conf|Media keys"
  "features/desktop/hyprland/config/bindings.conf|Apps & web shortcuts"
)

prettify_key() {
  case "$1" in
    code:10) echo "1" ;; code:11) echo "2" ;; code:12) echo "3" ;;
    code:13) echo "4" ;; code:14) echo "5" ;; code:15) echo "6" ;;
    code:16) echo "7" ;; code:17) echo "8" ;; code:18) echo "9" ;;
    code:19) echo "0" ;;
    XF86AudioRaiseVolume)   echo "Volume Up" ;;
    XF86AudioLowerVolume)   echo "Volume Down" ;;
    XF86AudioMute)          echo "Mute" ;;
    XF86AudioMicMute)       echo "Mic Mute" ;;
    XF86AudioPlay)          echo "Play/Pause" ;;
    XF86AudioStop)          echo "Stop" ;;
    XF86AudioNext)          echo "Next Track" ;;
    XF86AudioPrev)          echo "Previous Track" ;;
    XF86MonBrightnessUp)    echo "Brightness Up" ;;
    XF86MonBrightnessDown)  echo "Brightness Down" ;;
    XF86KbdBrightnessUp)    echo "Kbd Brightness Up" ;;
    XF86KbdBrightnessDown)  echo "Kbd Brightness Down" ;;
    XF86KbdLightOnOff)      echo "Kbd Backlight" ;;
    *) echo "$1" ;;
  esac
}

trim() { sed -E 's/^[[:space:]]+//; s/[[:space:]]+$//'; }

render_section() {
  local file="$1" title="$2"
  [[ ! -f "$repo_root/$file" ]] && return
  local rows
  rows=$(grep -E '^[[:space:]]*bind[a-z]*[[:space:]]*=' "$repo_root/$file" || true)
  [[ -z "$rows" ]] && return

  local body=""
  while IFS= read -r line; do
    # Strip the "bindXXX =" prefix.
    local rhs="${line#*=}"
    local mods key desc
    IFS=',' read -r mods key desc _ <<<"$rhs"
    mods=$(printf '%s' "${mods:-}" | trim)
    key=$(printf '%s'  "${key:-}"  | trim)
    desc=$(printf '%s' "${desc:-}" | trim)
    [[ -z "$desc" ]] && continue          # skip non-descriptive bindings
    [[ -z "$mods" ]] && mods="—"
    key=$(prettify_key "$key")
    body+=$(printf '| %s | %s | %s |\n' "$mods" "$key" "$desc")
    body+=$'\n'
  done <<<"$rows"

  [[ -z "$body" ]] && return

  printf '\n## %s\n\n' "$title"
  printf '_Source: `%s`_\n\n' "$file"
  printf '| Modifiers | Key | Action |\n'
  printf '| --- | --- | --- |\n'
  printf '%s' "$body"
}

main() {
  cat <<'HEADER'
# Nomarchy Keybindings

Auto-generated from the Hyprland binding files. **Do not edit by hand.**
Re-run the generator after changing any `bindings/*.conf`:

```bash
./bin/utils/nomarchy-docs-keybindings --out docs/KEYBINDINGS.md
```

`SUPER` is the Meta / Win key. `code:NN` keys (X11 digit keycodes) are
shown as the digit they correspond to. Media keys (`XF86Audio*`,
`XF86MonBrightness*`, …) are prettified.
HEADER
  for entry in "${sources[@]}"; do
    render_section "${entry%|*}" "${entry#*|}"
  done
}

out=""
if [[ "${1:-}" == "--out" ]]; then
  out="${2:?--out needs a path}"; shift 2
fi
if [[ -n "$out" ]]; then
  main >"$out"
else
  main
fi
