#!/bin/bash
# mbhelper - command palette like menu, search and run, quick access to
#    all important config tools, edit config files, show keybord shortcuts
#    favorites, online resources and more.
#

case $LANG in
    pl*)
        LNGDIR="pl"
        TYPE_TO_SEARCH="pisz aby wyszukać"
        ME="Paleta komend (to menu)"
        EDIT="Dostosuj..."
        RESET="Przywróć domyślne"
        QUICKCONFIG="Szybki dostęp do plików i katalogów konfiguracyjnych"
        ;;
    es*) 
        LNGDIR="en"
        TYPE_TO_SEARCH="type to search"
        ME="CMD Palette (this menu)"
        EDIT="Edit..."
        RESET="Reset to default"
    ;;
    *)
        LNGDIR="en"
        TYPE_TO_SEARCH="type to search"
        ME="CMD Palette (this menu)"
        EDIT="Edit..."
        RESET="Reset to default"
        QUICKCONFIG="Quick access to config files and directories"
     ;;
esac

[[ -f "$HOME/.config/mabox/mbhelper.csv" ]] && CSVFILE="$HOME/.config/mabox/mbhelper.csv" TODEFAULT="$RESET,mbhelper reset"|| CSVFILE="/usr/share/mabox/lang/${LNGDIR}/.config/mabox/mbhelper.csv" TODEFAULT=""



run() {
### RUN
if [[ $1 = "-s" ]]; then
. /usr/share/mb-jgtools/pipemenu-standalone.cfg
. $HOME/.config/mabox/mabox.conf
MENU_PADDING_TOP=$((jgtools_padding+24))
MENU_HALIGN="center"
MENU_VALIGN="top"
MENU_MARGIN_Y=40
jgmenu_icon_size=0
JGWIDTH=400
MENU_HEIGHT_MAX=600


[ $(pidof picom) ] && MENU_RADIUS=$jgtools_radius
[ -z $jgmenu_use_borders ] && menu_border=0

mkconfigfile
CNFDIRFILE="$HOME/.config/mabox/configdir.csv"

> $CNFDIRFILE
while read i; do
    echo "~/.config${i#*config},xdg-open ${i}" >> $CNFDIRFILE
done < <(tree -aif -I chromium -I "Local Storage" -I IndexedDB -I registry -I icons -I "*cache*" -I "*Cach*" --noreport ~/.config/)

cat <<EOF > ${MENU_ITEMS}
@search,,$((jgtools_padding + 24)),$((jgtools_padding + 4)),292,20,4,left,top,auto,#262626,"""<big></big> <i>$TYPE_TO_SEARCH</i>"""
^sep($ME)
$EDIT,mbhelper edit
${TODEFAULT}
. $CSVFILE
^sep($QUICKCONFIG)
~/.bashrc,xdg-open ~/.bashrc
. $CNFDIRFILE

EOF

jgmenu --config-file=${CONFIG_FILE} --csv-file=${MENU_ITEMS} 2>/dev/null

exit 0

fi
}

edit() {
if [[ -f "$HOME/.config/mabox/mbhelper.csv" ]];then
xdg-open "$HOME/.config/mabox/mbhelper.csv"
else
cp "/usr/share/mabox/lang/${LNGDIR}/.config/mabox/mbhelper.csv" "$HOME/.config/mabox/mbhelper.csv"
xdg-open "$HOME/.config/mabox/mbhelper.csv"
fi
}

reset() {
    rm "$HOME/.config/mabox/mbhelper.csv"
}

case "$1" in
    -e|edit) edit;;
    -r|reset) reset;;
     *) run "$1";;
esac

