#!/usr/bin/env bash

## Copyright (C) 2020-2023 Aditya Shakya <adi1090x@gmail.com>
##
## Openbox Pipemenu for displaying keybinds

readonly KBSCRIPTDIR="/usr/share/debiancraft/openbox/pipemenus"
readonly KBSCRIPT="$KBSCRIPTDIR/ac-kb"
readonly CFG_DIR="$HOME/.config/openbox"
readonly RCPATH="$CFG_DIR/rc.xml"
readonly KBTEXT="$CFG_DIR/kbinds.txt"
readonly HELP='\tal-kb-pipemenu an Openbox Pipe Menu for displaying keybinds
\tIt should normally be called from an openbox menu.
\nOptions (usually passed from the menu):
\t-h --help   show this message
\t--keybinds  display keybindings defined in rc.xml as a menu
\t(The keybinds in the menu are clickable, except for the Openbox
\tcommands, which just exit the menu.)
\t--rcedit    backup and edit rc.xml
\nIf al-keyboard-pipemenu is called with no options (the usual method),
it will output an xml openbox pipemenu for displaying keybindings.
\tNOTE: Some of the functionality of this pipemenu depends on the script
/usr/bin/al-kb which is provided by the package "archlabs-utilities"'

case "$@" in
    -h|--help) echo -e "$HELP" ; exit 0
esac

# Variables and functions
MENUS_LIBDIR='/usr/share/debiancraft/openbox/menulib'
if ! . "$MENUS_LIBDIR/debiancraft.cfg" 2> /dev/null; then
	echo "Error: Failed to locate debiancraft.cfg in $MENUS_LIBDIR" >&2
	exit 1
fi

if ! type "$KBSCRIPT" &>/dev/null; then
    menuStart
    menuSeparator "$KBSCRIPT is not available. Try installing archlabs-pipemenus."
    menuEnd
    exit 1
fi

if [[ $1 = '--rcedit' ]]; then
    NOW=$(date +"%Y%m%d-%H%M")
    RCBKP="$RCPATH.$NOW"
    cp "$RCPATH" "$RCBKP"
    if hash exo-open &>/dev/null; then
        exo-open "$RCPATH"
    else
        geany "$RCPATH"
    fi
    exit 0
fi

if [[ $1 = '--keybinds' ]]; then
    "$KBSCRIPT" > /dev/null
    menuStart
    menuSeparator "Openbox window commands"
    while read -r a b c; do
        if [[ "$a" = "o" ]]; then
            curItem=$(printf "%-20s %s" "$b" "$c")
            menuItem "$curItem" "echo >/dev/null 2>&1"
        fi
    done < "$KBTEXT"

    menuSeparator
    menuSeparator "Run commands"
    while read -r a b c; do
        if [[ "$a" = "x" ]]; then
            curItem=$(printf "%-20s %s" "$b" "$c")
            menuItem "$curItem" "$c"
        fi
    done < "$KBTEXT"
    menuEnd
    exit 0
fi

menuStart
    menuItem "Backup and Edit rc.xml" "$KBSCRIPTDIR/ac-kb-pipemenu --rcedit"
    menuSeparator
    menuItem "Display Keybinds in window" "$KBSCRIPTDIR/ac-kb --gui"
    echo '        <menu execute="/usr/share/debiancraft/openbox/pipemenus/ac-kb-pipemenu --keybinds" id="Display keybinds" label="Display Keybinds in menu"/>'
menuEnd

exit 0
