#!/bin/bash
me="colorizer-menus -s"
. $HOME/.config/mabox/mabox.conf
case "$jgmenu_theme" in
    obtheme)
    OBTHEME=$(awk '/<theme>/ {while (getline n) {if (match(n, /<name>/))
              {l=n; exit}}} END {split(l, a, "[<>]"); print a[3]}' "$HOME/.config/openbox/rc.xml");
    #echo "$OBTHEME"
    THEMERC="$HOME/.config/mabox/jgobthemes/${OBTHEME}.colorrc"
    jgmenu_theme="$OBTHEME";; #only to show real theme name in menu
    *)
    THEMERC="$HOME/.config/mabox/jgobthemes/${jgmenu_theme}.colorrc";;
esac

GRADIENT_FILE="$HOME"/.config/colorizer/gradients_menu.inc

if [ ! -f "$GRADIENT_FILE" ]; then
cp /usr/share/mabox-colorizer/gradients_menu.inc "$GRADIENT_FILE"
fi

# Backup original theme file
if [ ! -f "${THEMERC}.bak" ]; then
    cp ${THEMERC} ${THEMERC}.bak
fi
case "$LANG" in
    pl*)
    MENU_THEME="Motyw menu"
    GENERATEFROMWP="<i>Generuj z kolorów tapety...</i>"
    LIGHTBG="Jasne tło"
    DARKBG="Ciemne tło"
    COLOR_MENU_BG="Tło menu"
    COLOR_MENU_BG_TO="Tło menu 2 (gradient)"
    COLOR_MENU_BORDER="Obramowanie menu"
    GRADIENT_POS="Kierunek gradientu"
    NONE="brak gradientu (none)"
    TOP="od góry (top)"
    RIGHT="od prawej (right)"
    BOTTOM="z dołu (bottom)"
    LEFT="od lewej (left)"
    TOP_LEFT="od góry z lewej (top_left)"
    TOP_RIGHT="od góry z prawej (top_right)"
    BOTTOM_LEFT="z dołu od lewej (bottom_left)"
    BOTTOM_RIGHT="z dołu od prawej (bottom_right)"
    PRE_GRADIENTS="Gotowe gradienty"
    GRADIENT_REVERSE="Odwróć kolory Gradientu"
    ITEM="pozycja menu (element)"
    COLOR_NORM_FG="Kolor tekstu"
    COLOR_SEL_BG="Tło aktywnej pozycji"
    COLOR_SEL_FG="Tekst aktywnej pozycji"
    COLOR_SEL_BORDER="Obramowanie akt. poz."
    ITEM_BORDER="Obramowanie (px)"
    ITEM_RADIUS="Zaokrąglenie (px)"
    COLOR_NORM_BG="Tło"
    SEPARATOR_TITLE="separator/nagłówek"
    COLOR_TITLE_BG="Tło nagłówka"
    COLOR_TITLE_FG="Kolor tekstu nagłówka"
    COLOR_TITLE_BORDER="Kolor obramowania"
    SEP_HALIGN="Wyrównanie nagłówka"
    H_LEFT="do lewej"
    H_CENTER="po środku"
    H_RIGHT="do prawej"
    COLOR_SEP_FG="Kolor separatora (linii)"
    MORE_MENU_SETTINGS="więcej ustawień menu"
    GLOBAL_MENU_SETTINGS="Globalne Ustawienia Menu"
    RESET="Resetuj motyw"
    TO_DEFAULT=""
    EDIT="Edytuj plik "
    FILE=""
    SET_RANDOM="Ustaw losowy motyw menu"
    ;;
    *)
    MENU_THEME="Menu theme"
    GENERATEFROMWP="<i>Generate from wallpaper colors...</i>"
    LIGHTBG="Light Background"
    DARKBG="Dark Background"
    COLOR_MENU_BG="Background"
    COLOR_MENU_BG_TO="Background to (gradient)"
    COLOR_MENU_BORDER="Menu Border"
    GRADIENT_POS="Gradient direction"
    NONE="none"
    TOP="top"
    RIGHT="right"
    BOTTOM="bottom"
    LEFT="left"
    TOP_LEFT="top_left"
    TOP_RIGHT="top_right"
    BOTTOM_LEFT="bottom_left"
    BOTTOM_RIGHT="bottom_right"
    PRE_GRADIENTS="Predefined gradients"
    GRADIENT_REVERSE="Reverse Gradient"
    ITEM="item"
    COLOR_NORM_FG="Text Color"
    COLOR_SEL_BG="Selected Item Background"
    COLOR_SEL_FG="Selected Item Text"
    COLOR_SEL_BORDER="Selected Item Border"
    ITEM_BORDER="Item Border (px)"
    ITEM_RADIUS="Item Radius (px)"
    COLOR_NORM_BG="Item Background"
    SEPARATOR_TITLE="separator/title"
    COLOR_TITLE_BG="Title Background"
    COLOR_TITLE_FG="Title Foreground"
    COLOR_TITLE_BORDER="Title Border color "
    SEP_HALIGN="Title alignment"
    H_LEFT="left"
    H_CENTER="center"
    H_RIGHT="right"
    COLOR_SEP_FG="Separator (line) color"
    MORE_MENU_SETTINGS="more menu settings"
    GLOBAL_MENU_SETTINGS="Global Menu Settings"
    RESET="Reset"
    TO_DEFAULT="to default"
    EDIT="Edit"
    FILE="file"
    SET_RANDOM="Set random menu theme"
    ;;
esac

getvalues () {
    read MBG MBGA<<< "$(grep 'color_menu_bg ' ${THEMERC} | cut -d'=' -f2)"
    read MBGT MBGTA<<< "$(grep 'color_menu_bg_to' ${THEMERC} | cut -d'=' -f2)"
    read MBORDER MBORDERA<<< "$(grep 'color_menu_border' ${THEMERC} | cut -d'=' -f2)"
    read GRADIENT<<< "$(grep 'menu_gradient_pos' ${THEMERC} | cut -d'=' -f2)"
    if [ "$GRADIENT" == "" ]; then
    mb-setvar menu_gradient_pos=none ${THEMERC}
    read GRADIENT<<< "$(grep 'menu_gradient_pos' ${THEMERC} | cut -d'=' -f2)"
    fi
    read SEPHALIGN<<< "$(grep 'sep_halign' ${THEMERC} | cut -d'=' -f2)"
    read SEPFG SEPFGA<<< "$(grep 'color_sep_fg' ${THEMERC} | cut -d'=' -f2)"
    read TBG TBGA<<< "$(grep 'color_title_bg' ${THEMERC} | cut -d'=' -f2)"
    read TFG TFGA<<< "$(grep 'color_title_fg' ${THEMERC} | cut -d'=' -f2)"
    read TBORDER TBORDERA<<< "$(grep 'color_title_border' ${THEMERC} | cut -d'=' -f2)"
    read NORMBG NORMBGA<<< "$(grep color_norm_bg ${THEMERC} | cut -d'=' -f2)"
    read NFG NFGA<<< "$(grep color_norm_fg ${THEMERC} | cut -d'=' -f2)"
    read SBG SBGA<<< "$(grep 'color_sel_bg' ${THEMERC} | cut -d'=' -f2)"
    read SFG SFGA<<< "$(grep 'color_sel_fg' ${THEMERC} | cut -d'=' -f2)"
    read SBORDER SBORDERA<<< "$(grep 'color_sel_border' ${THEMERC} | cut -d'=' -f2)"
    read IBORDER<<< "$(grep 'item_border' ${THEMERC} | cut -d'=' -f2)"
    read IRADIUS<<< "$(grep 'item_radius' ${THEMERC} | cut -d'=' -f2)"
}
getvalues


out+=("^sep($MENU_THEME: <i>${jgmenu_theme}</i>)")
out+=("^sep($GENERATEFROMWP)")
out+=("$LIGHTBG,w2theme menu light;$me")
out+=("$DARKBG,w2theme menu dark;$me")
out+=("^sep(menu)")
out+=("<tt><small>$(printf '%3s' "$MBGA")</small> <span bgcolor='$MBG' bgalpha='${MBGA/#[0]/1}%'>     </span><span bgcolor='$MBG'> </span></tt>   $COLOR_MENU_BG,^pipe(mbclr '$MBG' jgctl color_menu_bg '${COLOR_MENU_BG}' '$me')")
out+=("<tt><small>$(printf '%3s' "$MBGTA")</small> <span bgcolor='$MBGT' bgalpha='${MBGTA/#[0]/1}%'>     </span><span bgcolor='$MBGT'> </span></tt>   $COLOR_MENU_BG_TO,^pipe(mbclr '$MBGT' jgctl color_menu_bg_to '${COLOR_MENU_BG_TO}' '$me')")
out+=("<tt><small>$(printf '%3s' "$MBORDERA")</small> <span bgcolor='$MBORDER' bgalpha='${MBORDERA/#[0]/1}%'>     </span><span bgcolor='$MBORDER'> </span></tt>   $COLOR_MENU_BORDER,^pipe(mbclr '$MBORDER' jgctl color_menu_border '${COLOR_MENU_BORDER}' '$me')")
out+=("^sep()")
out+=("$GRADIENT_POS [ <b>$GRADIENT</b> ],^checkout(gradientpos)")
out+=("$PRE_GRADIENTS,^pipe(mbgrad menu)")
out+=("   $GRADIENT_REVERSE,jgctl gradient '${MBGT}' '${MBG}';$me")

out+=("^sep($ITEM)")
out+=("<tt><small>$(printf '%3s' "$NFGA")</small> <span weight='bold' fgcolor='$NFG' fgalpha='${NFGA/#[0]/1}%'> AbCd </span></tt>   $COLOR_NORM_FG,^pipe(mbclr '$NFG' jgctl color_norm_fg '${COLOR_NORM_FG}' '$me')")
out+=("$ITEM_BORDER [ <b>$IBORDER</b> ],^checkout(itemborder)")
out+=("$ITEM_RADIUS [ <b>$IRADIUS</b> ],^checkout(itemradius)")
out+=("<tt><small>$(printf '%3s' "$SBGA")</small> <span bgcolor='$SBG' bgalpha='${SBGA/#[0]/1}%'>     </span><span bgcolor='$SBG'> </span></tt>   $COLOR_SEL_BG,^pipe(mbclr '$SBG' jgctl color_sel_bg '${COLOR_SEL_BG}' '$me')")
out+=("<tt><small>$(printf '%3s' "$SFGA")</small> <span weight='bold' bgcolor='$SBG' bgalpha='${SBGA/#[0]/1}%' fgcolor='$SFG' fgalpha='${SFGA/#[0]/1}%'> AbCd </span></tt>   $COLOR_SEL_FG,^pipe(mbclr '$SFG' jgctl color_sel_fg '${COLOR_SEL_FG}' '$me')")
out+=("<tt><small>$(printf '%3s' "$SBORDERA")</small> <span bgcolor='$SBORDER' bgalpha='${SBORDERA/#[0]/1}%'>     </span><span bgcolor='$SBORDER'> </span></tt>   $COLOR_SEL_BORDER,^pipe(mbclr '$SBORDER' jgctl color_sel_border '${COLOR_SEL_BORDER}' '$me')")
out+=("^sep()")
[[ ! -z "$NORMBG" ]] && out+=("<tt><small>$(printf '%3s' "$NORMBGA")</small> <span bgcolor='${NORMBG}' bgalpha='${NORMBGA/#[0]/1}%'>     </span><span bgcolor='${NORMBG}'> </span></tt>   $COLOR_NORM_BG,^pipe(mbclr '$NORMBG' jgctl color_norm_bg '${COLOR_NORM_BG}' '$me')")

out+=("^sep($SEPARATOR_TITLE)")
out+=("<tt><small>$(printf '%3s' "$TBGA")</small> <span bgcolor='$TBG' bgalpha='${TBGA}%'>     </span><span bgcolor='$TBG'> </span></tt>   $COLOR_TITLE_BG,^pipe(mbclr '$TBG' jgctl color_title_bg '${COLOR_TITLE_BG}' '$me')")
out+=("<tt><small>$(printf '%3s' "$TFGA")</small> <span weight='bold' bgcolor='$TBG' bgalpha='${TBGA/#[0]/1}%' fgcolor='$TFG' fgalpha='${TFGA}%'> AbCd </span></tt>   $COLOR_TITLE_FG,^pipe(mbclr '$TFG' jgctl color_title_fg '${COLOR_TITLE_FG}' '$me')")
out+=("<tt><small>$(printf '%3s' "$TBORDERA")</small> <span bgcolor='$TBORDER' bgalpha='${TBORDERA/#[0]/1}%'>     </span><span bgcolor='$TBORDER'> </span></tt>   $COLOR_TITLE_BORDER,^pipe(mbclr '$TBORDER' jgctl color_title_border '${COLOR_TITLE_BORDER}' '$me')")
out+=("^sep()")
out+=("$SEP_HALIGN [ <b>$SEPHALIGN</b> ],^checkout(sephalign)")
out+=("^sep()")
out+=("<tt><small>$(printf '%3s' "$SEPFGA")</small> <span fgcolor='$SEPFG' fgalpha='${SEPFGA/#[0]/1}%'>------</span></tt>   ${COLOR_SEP_FG},^pipe(mbclr '$SEPFG' jgctl color_sep_fg '${COLOR_SEP_FG}' '$me')")

out2+=("^tag(gradientpos)")
out2+=("^sep($GRADIENT_POS <small>$GRADIENT</small>)")
out2+=("$NONE,jgctl menu_gradient_pos none;$me")
out2+=("^sep()")
out2+=("   $TOP,jgctl menu_gradient_pos top;$me")
out2+=("   $RIGHT,jgctl menu_gradient_pos right;$me")
out2+=("   $BOTTOM,jgctl menu_gradient_pos bottom;$me")
out2+=("   $LEFT,jgctl menu_gradient_pos left;$me")
out2+=("   $TOP_LEFT,jgctl menu_gradient_pos top_left;$me")
out2+=("   $TOP_RIGHT,jgctl menu_gradient_pos top_right;$me")
out2+=("   $BOTTOM_LEFT,jgctl menu_gradient_pos bottom_left;$me")
out2+=("   $BOTTOM_RIGHT,jgctl menu_gradient_pos bottom_right;$me")

out2+=("^tag(sephalign)")
out2+=("^sep($SEP_HALIGN)")
out2+=("   $H_LEFT,jgctl sep_halign left;$me")
out2+=("   $H_CENTER,jgctl sep_halign center;$me")
out2+=("   $H_RIGHT,jgctl sep_halign right;$me")
out2+=("^tag(itemborder)")
out2+=("^sep($ITEM_BORDER)")
out2+=("0,jgctl item_border 0;$me")
out2+=("^sep()")
out2+=("1,jgctl item_border 1;$me")
out2+=("2,jgctl item_border 2;$me")
out2+=("^tag(itemradius)")
out2+=("^sep($ITEM_RADIUS)")
out2+=("0,jgctl item_radius 0;$me")
out2+=("^sep()")
out2+=("1,jgctl item_radius 1;$me")
out2+=("2,jgctl item_radius 2;$me")
out2+=("3,jgctl item_radius 3;$me")
out2+=("4,jgctl item_radius 4;$me")
out2+=("5,jgctl item_radius 5;$me")
out2+=("6,jgctl item_radius 6;$me")



out+=("^sep($MORE_MENU_SETTINGS)")
out+=("   $GLOBAL_MENU_SETTINGS,jgmenusettings-pipe -c")
#out+=("$GLOBAL_MENU_SETTINGS,^pipe(jgmenusettings-pipe -r)")

out+=("^sep()")
if [ -f "${THEMERC}.bak" ]; then
    out+=("   $RESET <i>${jgmenu_theme}</i> $TO_DEFAULT,jgctl reset;$me")
fi
out+=("   $EDIT <i>${jgmenu_theme}</i> $FILE,xdg-open $THEMERC")
### We don't need many menu themes anymore
#out+=("   $SET_RANDOM,mb-setvar jgmenu_theme=$(basename -s .colorrc $(ls ~/.config/mabox/jgobthemes/*.colorrc | shuf -n 1));$me")

### RUN
if [[ "$1" == "-s" ]]; then
    . /usr/share/mb-jgtools/pipemenu-standalone.cfg
    . $HOME/.config/mabox/mabox.conf
    
    CNF_DIR="$HOME/.config/colorizer"
    CNF_FILE="$CNF_DIR/colorizer.conf"
    source "$CNF_FILE"

    out+=("^sep()")
    out+=(" ⮜⮜⮜    Colorizer,colorizer -s")

    if [[ "$colorizer_size" == "full" ]];then
    eval $(xdotool getdisplaygeometry --shell)
    MENU_HEIGHT_MIN="$HEIGHT"
    MENU_HEIGHT_MAX=$MENU_HEIGHT_MIN
    MENU_RADIUS="0"
    menu_margin_x="0"
    else
    MENU_VALIGN="center"
    menu_margin_x=${submenu_spacing:-0}
    fi
     
    MENU_PADDING_TOP=${jgtools_padding:-0}
    MENU_HALIGN="left"
    jgmenu_icon_size=0
    JGWIDTH=40

    [ $(pidof picom) ] && MENU_RADIUS=$jgtools_radius
    [ -z $jgmenu_use_borders ] && menu_border=0
JGWIDTH=$((jgtools_padding + 278 + jgtools_padding))
    MENU_PADDING_TOP=$((jgtools_padding + 158))
    #WALLPAPER
    read WALLPATH<<< $(grep file "$HOME"/.config/nitrogen/bg-saved.cfg | cut -d'=' -f2)
    WALLPAPER=${WALLPATH##*/}
    THUMBDIR="$HOME/.config/mabox/wallthumbs"
    mkdir -p ${THUMBDIR}
    THUMB="${THUMBDIR}/${WALLPAPER}.png"
    if [[ ! -f "$THUMB" ]]
        then
    convert ${WALLPATH} -resize 270x150^ -gravity center -extent 270x150 ${THUMB}
    fi
    
    mkconfigfile

cat <<EOF > ${MENU_ITEMS}
@icon,,$((jgtools_padding + 4)),$((jgtools_padding + 4)),270,150,4,left,top,,,${THUMB}
$(printf '%s\n' "${out[@]}")
$(printf '%s\n' "${out2[@]}")
EOF

    jgmenu --config-file=${CONFIG_FILE} --csv-file=${MENU_ITEMS} 2>/dev/null
    exit 0
fi
printf '%s\n' "${out[@]}"
printf '%s\n' "${out2[@]}"