#!/bin/bash
#
#    colorizer - set of tools for Mabox (auto)theming
#    Copyright (C) 2022-2023 Daniel Napora    <napcok@gmail.com>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

. $HOME/.config/mabox/mabox.conf

me="colorizer-fonts -s"

CONKYDIR="$HOME/.config/conky"
BASECONKY="$CONKYDIR/sysinfo_mbcolor.conkyrc"


# Favorited fonts list
if [ -f "$HOME/.config/mabox/fonts.list" ];then
	mapfile -t favfonts < <( grep -vE "^($|#)" ~/.config/mabox/fonts.list)
else
	mapfile -t favfonts < <( grep -vE "^($|#)" /usr/share/mabox-colorizer/fonts.list)
	cp /usr/share/mabox-colorizer/fonts.list ~/.config/mabox/
fi


case "$LANG" in
    pl*)
    _INCREASE_ALL="Powiększ wszystkie"
    _DECREASE_ALL="Pomniejsz wszystkie"
    _RESET="Resetuj do domyślnych"
    _ALL="Wszystko"
	_OPENBOX_WM="Openbox"
	_TITLEBAR_FONT="Czcionka tytułu"
	_BOLD="Pogrubienie"
	_ITALIC="Kursywa"
	_TEXTALIGN="Wyrównanie"
	_LEFT="Lewo"
	_CENTER="Środek"
	_RIGHT="Prowo"
	_MENUS="Menu"
	_MENU_TITLE="Nagłówek"
	_MENU_ITEM="Element"
	_MENU_TITLE_FONT="Czcionka nagłówka"
	_MENU_ITEM_FONT="Czcionka elementu"
	_FONT_CUSTOM="Wybierz czcionkę i rozmiar..."
	_FONT_FAMILY="rodzina"
	_EDIT_FAVFONTS="Edytuj ulubione czcionki"
	_FONT_SIZE="rozmiar"
	_INCREASE="Powiększ do"
	_DECREASE="Pomniejsz do"
	_CONKY="Conky"
	_CONKYFONT="Czcionka dla Conky"
	_CONKYBASEFONT="Czcionka podstawowa"
	_C_EFFECTS="Efekty"
	_SHADOW="Cień"
	_OUTLINE="Poświata"
	_PANEL="Panel tint2"
	_GTK="GTK"
	_FAVORITE_LBL="Ulubione czcionki"
	_USE="Użyj czcionki"
	_FOR="dla..."
	_COLORIZER_ROOT="<b>Colorizer</b> - menu główne"
    ;;
    *)
	_INCREASE_ALL="Increase all"
    _DECREASE_ALL="Decrease all"
    _RESET="Reset to defaults..."
    _ALL="All"
	_OPENBOX_WM="Openbox"
	_TITLEBAR_FONT="TitleBar Font"
	_BOLD="Bold"
	_ITALIC="Italic"
	_TEXTALIGN="Text align"
	_LEFT="Left"
	_CENTER="Center"
	_RIGHT="Right"
	_MENUS="Menus"
	_MENU_TITLE="Menu Title"
	_MENU_ITEM="Menu Item"
	_MENU_TITLE_FONT="Menu Title Font"
	_MENU_ITEM_FONT="Menu Item Font"
	_FONT_CUSTOM="Select custom font and size..."
	_FONT_FAMILY="font family"
	_EDIT_FAVFONTS="Edit favorite fonts list"
	_FONT_SIZE="font size"
	_INCREASE="Increase to"
	_DECREASE="Decrease to"
	_CONKY="Widgets (Conky)"
	_CONKYFONT="Conky Font"
	_CONKYBASEFONT="Base font"
	_C_EFFECTS="Effects"
	_SHADOW="Shadow"
	_OUTLINE="Outline"
	_PANEL="Panel Tint2"
	_GTK="GTK Font"
	_FAVORITE_LBL="Favorite Fonts"
	_USE="Use"
	_FOR="font for ..."
	_COLORIZER_ROOT="<b>Colorizer</b> rootmenu"
    ;;
esac

getvalues () {
GTK2RC="$HOME"/.gtkrc-2.0
GTK_FONT=( $(grep "gtk-font-name" ${GTK2RC} | cut -d'"' -f2) )
GTK_FAMILY=${GTK_FONT[@]::${#GTK_FONT[@]}-1}
GTK_SIZE=${GTK_FONT[-1]}

nspace="http://openbox.org/3.4/rc"
obcfg="$HOME/.config/openbox/rc.xml"
    
OBFSIZE=$(xmlstarlet sel -N a="$nspace" -t -v '/a:openbox_config/a:theme/a:font[@place="ActiveWindow"]/a:size'  "$obcfg")
OBFNAME=$(xmlstarlet sel -N a="$nspace" -t -v '/a:openbox_config/a:theme/a:font[@place="ActiveWindow"]/a:name'  "$obcfg")
NDSLIMC=$(xmlstarlet sel -N a="$nspace" -t -v '/a:openbox_config/a:theme/a:titleLayout'  "$cfg")
WEIGHT=$(xmlstarlet sel -N a="$nspace" -t -v '/a:openbox_config/a:theme/a:font[@place="ActiveWindow"]/a:weight'  "$obcfg")
SLANT=$(xmlstarlet sel -N a="$nspace" -t -v '/a:openbox_config/a:theme/a:font[@place="ActiveWindow"]/a:slant'  "$obcfg") 

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")

THEMERC="$HOME/.themes/$OBTHEME/openbox-3/themerc"

if [ -f "$THEMERC" ];then
read TXTJUST <<< "$(grep text.justify ${THEMERC} | cut -d':' -f2)"
else
:
fi

read FONTDEF <<< "$(grep "font .*=.*,$" ${BASECONKY} | cut -d'=' -f2,3 |cut -d"'" -f2)"
C_FONT=${FONTDEF%:*}
C_FONTSIZE=${FONTDEF#*=}

read OUTLINE <<< "$(grep draw_outline ${BASECONKY} | cut -d'=' -f2 | cut -d"," -f1)"
read SHADES <<< "$(grep draw_shades ${BASECONKY} | cut -d'=' -f2 | cut -d"," -f1)"
SHADOWS="no"
[[ "$OUTLINE" == "true" ]] && SHADOWS="outline"
[[ "$SHADES" == "true" ]] && SHADOWS="shadow"
}

    getvalues
out+=("<big>󰧴</big>   $_INCREASE_ALL,fontctl inc_all;$me")
out+=("<big>󰧳</big>   $_DECREASE_ALL,fontctl dec_all;$me")
out+=("^sep()")
out+=("<big>󰑓</big>   $_RESET,^checkout(reset)")
	out2+=("^tag(reset)")
	out2+=("^sep($_RESET)")
	out2+=("$_ALL,fontctl resetall;$me")
	out2+=("^sep()")
	out2+=("$_TITLEBAR_FONT,fontctl resetob;$me")
	out2+=("$_MENUS,fontctl resetmenus;$me")
	out2+=("$_CONKY,fontctl resetconky;$me")
	out2+=("$_GTK,fontctl resetgtk;$me")
out+=("^sep($_OPENBOX_WM)")
out+=("$_TITLEBAR_FONT [ <b><span font_family=\"$OBFNAME\">$OBFNAME $OBFSIZE</span></b> ],^checkout(obtitletext)")
out+=("^sep($_MENUS)")
out+=("$_MENU_TITLE [ <b><span font_family=\"$menu_sep_font_family\">$menu_sep_font_family $menu_sep_font_size</span></b> ],^checkout(mtitlefont)")
out+=("$_MENU_ITEM [ <b><span font_family=\"$menu_font_family\">$menu_font_family $menu_font_size</span></b> ],^checkout(mitemfont)")
out+=("^sep($_GTK)")
out+=("$_GTK [ <b>${GTK_FAMILY} ${GTK_SIZE}</b> ],^checkout(gtkfont)")
out+=("^sep($_CONKY)")
out+=("$_CONKYBASEFONT [ <b><span font_family=\"${C_FONT}\">$C_FONT</span> $C_FONTSIZE</b> ],^checkout(conkyfont)")
#out+=("^sep($_PANEL)")
#out+=("<i>(not implemented yet)</i>")

# FAV FONTS in rootmenu
if [ "${#favfonts[@]}" -gt 0 ];then
	out+=("^sep($_FAVORITE_LBL)")
	i=0
    for fon in "${favfonts[@]}";do
    out+=("<span>        </span><span font_family=\"${fon}\">${fon}</span>,^checkout(favfont$i),$me")
		out2+=("^tag(favfont$i)")
		out2+=("^sep($_USE)")
		out2+=("^sep(<span font_family=\"${fon}\">${fon}</span>)")
		out2+=("^sep($_FOR)")
		out2+=("$_TITLEBAR_FONT,obtctl fontfamily \"${fon}\";$me")
		out2+=("$_MENU_TITLE,mb-setvar \"menu_sep_font_family='${fon}'\";$me")
		out2+=("$_MENU_ITEM,mb-setvar \"menu_font_family='${fon}'\";$me")
		out2+=("$_GTK,fontctl gtk_fontfamily \"${fon}\";$me")
		out2+=("$_CONKY,conkyctl basefont_family_all \"${fon}\";$me")
		i=$((i+1))
    done
    out+=("^sep()")
    out+=("<small>   $_EDIT_FAVFONTS </small>,geany ~/.config/mabox/fonts.list")
fi
	

#SUBMENUS

# OpenBox Title Font
out2+=("^tag(obtitletext)")
out2+=("^sep($_TITLEBAR_FONT)")
out2+=("^sep(<span font_family=\"$OBFNAME\">$OBFNAME $OBFSIZE</span>)")
out2+=("<big>󰛖</big>   $_FONT_CUSTOM,mb-setfont obtitle;$me")
out2+=("^sep($_FONT_SIZE)")
out2+=("<big>󰧴</big>   $_INCREASE <b>$((OBFSIZE+1)) px</b>,fontctl ob_fontsize inc;$me")
out2+=("<big>󰧳</big>   $_DECREASE <b>$((OBFSIZE-1)) px</b>,fontctl ob_fontsize dec;$me")
out2+=("^sep()")
sizes=(16 15 14 13 12 11 10 9 8)
for i in "${sizes[@]}"
do
[[ "$OBFSIZE" == "$i" ]] && out2+=("<big>綠</big>  <b>$i px</b>,obtctl fontsize $i;$me") || out2+=("<big>祿</big>  $i px,fontctl ob_fontsize $i;$me")
done
out2+=("^sep($_FONT_FAMILY)")

if [ "${#favfonts[@]}" -gt 0 ];then
    for fon in "${favfonts[@]}";do
    [[ "${OBFNAME}" == "$fon" ]] && out2+=("<big>綠</big>  <span font_family=\"${fon}\">${fon}</span>,$me") || out2+=("<big>祿</big>  <span font_family=\"${fon}\">${fon}</span>,obtctl fontfamily \"${fon}\";$me")
    done
fi


out2+=("^sep()")
[[ "$WEIGHT" =~ .*"old" ]] && out2+=("<big></big>   <b>$_BOLD</b>,obtctl fontweight Normal;$me") || out2+=("<big></big>   <b>$_BOLD</b>,obtctl fontweight Bold;$me")
[[ "$SLANT" =~ .*"talic" ]] && out2+=("<big></big>   <i>$_ITALIC</i>,obtctl fontslant Normal;$me") || out2+=("<big></big>   <i>$_ITALIC</i>,obtctl fontslant Italic;$me")

#SHADOW
out2+=("^sep($_TEXTALIGN)")
[[ "$TXTJUST" = left ]] && out2+=("<big>綠  </big>  $_LEFT,obtctl textjustify left;$me")||out2+=("<big>祿  </big>  $_LEFT,obtctl textjustify left;$me")
[[ "$TXTJUST" = center ]] && out2+=("<big>綠  </big>     $_CENTER,obtctl textjustify center;$me")||out2+=("<big>祿  </big>     $_CENTER,obtctl textjustify center;$me")
[[ "$TXTJUST" = right ]] && out2+=("<big>綠  </big>         $_RIGHT,obtctl textjustify right;$me")||out2+=("<big>祿  </big>         $_RIGHT,obtctl textjustify right;$me")



# Menu Title Font
out2+=("^tag(mtitlefont)")
out2+=("^sep($_MENU_TITLE_FONT)")
out2+=("^sep(<span font_family=\"$menu_sep_font_family\">$menu_sep_font_family $menu_sep_font_size</span>)")

out2+=("<big>󰛖</big>   $_FONT_CUSTOM,mb-setfont menu_sep;$me")
out2+=("^sep($_FONT_SIZE)")
out2+=("<big>󰧴</big>   $_INCREASE <b>$((menu_sep_font_size+1)) px</b>,mb-setvar menu_sep_font_size=$((menu_sep_font_size+1));$me")
out2+=("<big>󰧳</big>   $_DECREASE <b>$((menu_sep_font_size-1)) px</b>,mb-setvar menu_sep_font_size=$((menu_sep_font_size-1));$me")
out2+=("^sep()")
sizes=(14 13 12 11 10 9 8)
for i in "${sizes[@]}"
do
	[[ "$menu_sep_font_size" == "$i" ]] && out2+=("<big>綠</big>  <b>$i px</b>,$me") || out2+=("<big>祿</big>  $i px,mb-setvar menu_sep_font_size=$i;$me")
done

if [ "${#favfonts[@]}" -gt 0 ];then
    out2+=("^sep($_FONT_FAMILY)")
    for fon in "${favfonts[@]}";do
        [[ "$menu_sep_font_family" == "$fon" ]] && out2+=("<big>綠</big>  <span font_family=\"${fon}\">${fon}</span>,$me") || out2+=("<big>祿</big>  <span font_family=\"${fon}\">${fon}</span>,mb-setvar \"menu_sep_font_family='${fon}'\";$me")
    done
fi
    out2+=("^sep()")
    out2+=("<small>   $_EDIT_FAVFONTS </small>,geany ~/.config/mabox/fonts.list")
        



# Item font submenu
out2+=("^tag(mitemfont)")
out2+=("^sep($_MENU_ITEM_FONT)")
out2+=("^sep(<span font_family=\"$menu_font_family\">$menu_font_family $menu_font_size</span>)")
out2+=("<big>󰛖</big>   $_FONT_CUSTOM,mb-setfont menu_item;$me")
out2+=("^sep($_FONT_SIZE)")
out2+=("<big>󰧴</big>   $_INCREASE <b>$((menu_font_size+1)) px</b>,mb-setvar menu_font_size=$((menu_font_size+1));$me")
out2+=("<big>󰧳</big>   $_DECREASE <b>$((menu_font_size-1)) px</b>,mb-setvar menu_font_size=$((menu_font_size-1));$me")
out2+=("^sep()")
sizes=(14 13 12 11 10 9 8)
for i in "${sizes[@]}"
do
	[[ "$menu_font_size" == "$i" ]] && out2+=("<big>綠</big>  <b>$i px</b>,$me") || out2+=("<big>祿</big>  $i px,mb-setvar menu_font_size=$i;$me")
done
  
if [ "${#favfonts[@]}" -gt 0 ];then
    out2+=("^sep($_FONT_FAMILY)")
    for fon in "${favfonts[@]}";do
    [[ "$menu_font_family" == "$fon" ]] && out2+=("<big>綠</big>  <span font_family=\"${fon}\">${fon}</span>,$me") || out2+=("<big>祿</big>  <span font_family=\"${fon}\">${fon}</span>,mb-setvar \"menu_font_family='${fon}'\";$me")
    done
fi
        out2+=("^sep()")
        out2+=("<small>   $_EDIT_FAVFONTS</small>,geany ~/.config/mabox/fonts.list")

# Conky

out2+=("^tag(conkyfont)")
out2+=("^sep($_CONKYFONT)")
out2+=("^sep($C_FONT $C_FONTSIZE)")
out2+=("<big>󰛖</big>   $_FONT_CUSTOM,mb-setfont conky_all;$me")

out2+=("^sep($_FONT_SIZE)")
out2+=("<big>󰧴</big>   $_INCREASE <b>$((C_FONTSIZE+1)) px</b>,conkyctl basefont_inc_all;$me")
out2+=("<big>󰧳</big>   $_DECREASE <b>$((C_FONTSIZE-1)) px</b>,conkyctl basefont_dec_all;$me")
out2+=("^sep()")
sizes=(14 13 12 11 10 9 8 7 6)
for i in "${sizes[@]}"
	do
	[[ "$C_FONTSIZE" == "$i" ]] && out2+=("<big>綠</big>  <b>$i px</b>,conkyctl basefont_size_all $i;$me") || out2+=("<big>祿</big>  $i px,conkyctl basefont_size_all $i;$me")
done
    
if [ "${#favfonts[@]}" -gt 0 ];then
  out2+=("^sep($_FONT_FAMILY)")
  for fon in "${favfonts[@]}";do
    [[ "$C_FONT" == "$fon" ]] && out2+=("<big>綠</big>  <span font_family=\"${fon}\">${fon}</span>,$me") || out2+=("<big>祿</big>  <span font_family=\"${fon}\">${fon}</span>,conkyctl basefont_family_all \"${fon}\";$me")
  done
fi
out2+=("^sep()")
out2+=("<small>   $_EDIT_FAVFONTS</small>,geany ~/.config/mabox/fonts.list")

out2+=("^sep($_C_EFFCTS)")	
[[ "$SHADOWS" == "shadow" ]] && out2+=("<big>󰄲</big>  $_SHADOW,conkyctl shades_all no;$me") || out2+=("<big>󰄱</big>  $_SHADOW,conkyctl shades_all shadow;$me")
[[ "$SHADOWS" == "outline" ]] && out2+=("<big>󰄲</big>  $_OUTLINE,conkyctl shades_all no;$me") || out2+=("<big>󰄱</big>  $_OUTLINE,conkyctl shades_all outline;$me")  
    


# GTK 2/3 font
out2+=("^tag(gtkfont)")
out2+=("^sep($_GTK)")
out2+=("^sep(<span font_family=\"$menu_font_family\">$menu_font_family $menu_font_size</span>)")
out2+=("<big>󰛖</big>   $_FONT_CUSTOM,mb-setfont gtk;$me")
out2+=("^sep($_FONT_SIZE)")
out2+=("<big>󰧴</big>   $_INCREASE <b>$((GTK_SIZE+1)) px</b>,fontctl gtk_fontsize inc;$me")
out2+=("<big>󰧳</big>   $_DECREASE <b>$((GTK_SIZE-1)) px</b>,fontctl gtk_fontsize dec;$me")
out2+=("^sep()")
sizes=(14 13 12 11 10 9 8)
for i in "${sizes[@]}"
do
	[[ "$GTK_SIZE" == "$i" ]] && out2+=("<big>綠</big>  <b>$i px</b>,$me") || out2+=("<big>祿</big>  $i px,fontctl gtk_fontsize $i;$me")
done
  
if [ "${#favfonts[@]}" -gt 0 ];then
    out2+=("^sep($_FONT_FAMILY)")
    for fon in "${favfonts[@]}";do
    [[ "$GTK_FAMILY" == "$fon" ]] && out2+=("<big>綠</big>  <span font_family=\"${fon}\">${fon}</span>,$me") || out2+=("<big>祿</big>  <span font_family=\"${fon}\">${fon}</span>,fontctl gtk_fontfamily \"${fon}\";$me")
    done
fi
        out2+=("^sep()")
        out2+=("<small>   $_EDIT_FAVFONTS</small>,geany ~/.config/mabox/fonts.list")

### RUN
if [[ "$1" == "-s" ]]; then
    . /usr/share/mb-jgtools/pipemenu-standalone.cfg

    
    CNF_DIR="$HOME/.config/colorizer"
    CNF_FILE="$CNF_DIR/colorizer.conf"
    source "$CNF_FILE"

    out+=("^sep()")
    out+=(" ${arrow_string_left}    $_COLORIZER_ROOT,colorizer -s")
    
    if [[ "$colorizer_size" == "full" ]];then
    eval $(xdotool getdisplaygeometry --shell)
    MENU_HEIGHT_MIN="$HEIGHT"
    MENU_HEIGHT_MAX=$MENU_HEIGHT_MIN
    jgtools_radius=0
    menu_margin_x="0"
    else
    MENU_VALIGN="center"
    menu_margin_x=${submenu_spacing:-0}
    fi
    

    MENU_HALIGN="left"
    jgmenu_icon_size=0
    
    [ $(pidof picom) ] && MENU_RADIUS=$jgtools_radius
    [ -z $jgmenu_use_borders ] && menu_border=0
    JGWIDTH=$((jgtools_padding + 270 + jgtools_padding))
    MENU_PADDING_TOP=$((jgtools_padding + 150))
    
    #WALLPAPER
    read WALLPATH<<< $(grep file "$HOME"/.config/nitrogen/bg-saved.cfg | cut -d'=' -f2)
    THUMBDIR="$HOME/.cache/colorizer/thumbs"
    mkdir -p ${THUMBDIR}
    NAME=${WALLPATH////_}
    if [[ "${NAME}" =~ ^_home_.* ]]; then
        n=${#HOME}
        ((n++))
        NAME=${NAME:${n}}
    fi
    THUMB="${THUMBDIR}/${NAME}.png"
    if [[ ! -f "$THUMB" ]]
        then
        convert ${WALLPATH} -resize 270x150^ -gravity center -extent 270x150 ${THUMB}
    fi
    
    mkconfigfile

[[ "$GR" == "1" ]] && clr=${GR_FROM} || clr=${FG}

cat <<EOF > ${MENU_ITEMS}
@icon,,$((jgtools_padding)),$((jgtools_padding)),270,150,4,left,top,,,${THUMB}
#@icon,,$((jgtools_padding + 4)),$((jgtools_padding + 4)),270,150,4,left,top,,,/usr/share/mabox-colorizer/img/colorizer-conky.png

@rect,,$((jgtools_padding)),$((jgtools_padding + 116)),270,28,0,left,top,#222222 20,#222222 70,
@text,,$((jgtools_padding + 10)),$((jgtools_padding + 106)),100,28,0,left,top,#FFFFFF ,${WINCLR},<span size='41600' font_family='Ubuntu'><big></big></span>
@text,,$((jgtools_padding + 128)),$((jgtools_padding + 116)),150,28,0,left,top,#FFFFFF ,${WINCLR},<span size='10400' font_family='Ubuntu'>Mabox <b>Fonts</b> config</span>


$(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[@]}"