#!/bin/awk -f

BEGINFILE {
    printf "# DWM Keybinds\n"
    printf "|Keybind|Function|\n"
    printf "|-|-|\n"
}

/^#define MODKEY/ {
    modkey = $3
}

/START KEYBINDS/ {
    in_keybinds = 1
    next
}

/END KEYBINDS/ {
    in_keybinds = 0
    next
}

in_keybinds {
    if (/^[[:space:]]*$/ || /^[[:space:]]*\/(\/|\*)/ || /^[[:space:]]*TAGKEYS\(/) {
        next
    }

    if (match($0, /\{[[:space:]]*([^,]+)/, modifier_matches)) {
        modifier = modifier_matches[1]
    } else {
        modifier = "0"
    }

    gsub(/MODKEY/, modkey, modifier)
    gsub(/\|/, "+", modifier)
    gsub(/Mod1Mask/, "Alt", modifier)
    gsub(/Mod4Mask/, "Super", modifier)
    gsub(/ControlMask/, "Control", modifier)
    gsub(/ShiftMask/, "Shift", modifier)

    match($0, /[[:space:]]*XK_([^,]+)/, key_matches)
    key = key_matches[1]
    key = toupper(substr(key,1,1)) substr(key,2)

    match($0, /,[[:space:]]*\/\/[[:space:]]*(.+)$/, comment_matches)
    comment = comment_matches[1]
    comment = toupper(substr(comment,1,1)) substr(comment,2)

    printf "|"
    if (modifier != "0") {
        printf "%s + ", modifier
    }
    printf "%s|%s|\n", key, comment
}