#!/bin/bash

# mb-setfont
get_obfont () {
    nspace="http://openbox.org/3.4/rc"
    cfg="$HOME/.config/openbox/rc.xml"
    
    NAME=$(xmlstarlet sel -N a="$nspace" -t -v '/a:openbox_config/a:theme/a:font[@place="ActiveWindow"]/a:name'  "$cfg")
    SIZE=$(xmlstarlet sel -N a="$nspace" -t -v '/a:openbox_config/a:theme/a:font[@place="ActiveWindow"]/a:size'  "$cfg")
    WEIGHT=$(xmlstarlet sel -N a="$nspace" -t -v '/a:openbox_config/a:theme/a:font[@place="ActiveWindow"]/a:weight'  "$cfg")
    SLANT=$(xmlstarlet sel -N a="$nspace" -t -v '/a:openbox_config/a:theme/a:font[@place="ActiveWindow"]/a:slant'  "$cfg")
    
    #notify-send.sh "OB ActiveWindow Font" "$NAME $WEIGHT $SLANT $SIZE"
    CURRENT_FONT="$NAME $WEIGHT $SLANT $SIZE"
    
}
get_gtkfont() {
	GTK2RC="$HOME"/.gtkrc-2.0
	GTK3RC="$HOME"/.config/gtk-3.0/settings.ini

	GTK_FONT=( $(grep "gtk-font-name" ${GTK3RC} | cut -d'=' -f2) )
	GTK_FAMILY=${GTK_FONT[@]::${#GTK_FONT[@]}-1}
	GTK_SIZE=${GTK_FONT[-1]}
	CURRENT_FONT="${GTK_FAMILY} ${GTK_SIZE}"
}
get_menu_item_font () {
    . ~/.config/mabox/mabox.conf
    CURRENT_FONT="${menu_font_family} ${menu_font_size}"
}
get_menu_sep_font () {
       . ~/.config/mabox/mabox.conf
    CURRENT_FONT="${menu_sep_font_family:-Ubuntu} ${menu_sep_font_size:-10}" 
}

selectfont () {
    case "$LANG" in
        pl*)
        SELECTFONT="Wybierz czcionkę i jej rozmiar"
        CANCEL="Anuluj"
        SELECT="Wybierz"
        ;;
        *)
        SELECTFONT="Select A Font"
        CANCEL="Cancel"
        SELECT="Select"
        ;;
    esac
      
	FONT=$(yad --title="${SELECTFONT}" --font --fontname="${CURRENT_FONT}" --separate-output --button="$CANCEL:1" --button="$SELECT:0")
	FONTNAME=$(echo $FONT | cut -d'|' -f1)
	FONTSTYLE=$(echo $FONT | cut -d'|' -f2)
	FONTSIZE=$(echo $FONT | cut -d'|' -f3)
}

font_obtitle () {
    if [[ "$FONT" ]]; then
    [[ "$FONTSTYLE" =~ .*"Bold".* ]] && WEIGHT="Bold" || WEIGHT="Normal"
    [[ "$FONTSTYLE" =~ .*"Italic".* ]] && SLANT="Italic" || SLANT="Normal"
    
    nspace="http://openbox.org/3.4/rc"
    cfg="$HOME/.config/openbox/rc.xml"

    xmlstarlet ed -L -N a="$nspace" -u '/a:openbox_config/a:theme/a:font[@place="ActiveWindow"]/a:name' -v "$FONTNAME" "$cfg"
    xmlstarlet ed -L -N a="$nspace" -u '/a:openbox_config/a:theme/a:font[@place="ActiveWindow"]/a:size' -v "$FONTSIZE" "$cfg"
    xmlstarlet ed -L -N a="$nspace" -u '/a:openbox_config/a:theme/a:font[@place="ActiveWindow"]/a:weight' -v "$WEIGHT" "$cfg"
    xmlstarlet ed -L -N a="$nspace" -u '/a:openbox_config/a:theme/a:font[@place="ActiveWindow"]/a:slant' -v "$SLANT" "$cfg"

    xmlstarlet ed -L -N a="$nspace" -u '/a:openbox_config/a:theme/a:font[@place="InactiveWindow"]/a:name' -v "$FONTNAME" "$cfg"
    xmlstarlet ed -L -N a="$nspace" -u '/a:openbox_config/a:theme/a:font[@place="InactiveWindow"]/a:size' -v "$FONTSIZE" "$cfg"
    xmlstarlet ed -L -N a="$nspace" -u '/a:openbox_config/a:theme/a:font[@place="InactiveWindow"]/a:weight' -v "$WEIGHT" "$cfg"
    xmlstarlet ed -L -N a="$nspace" -u '/a:openbox_config/a:theme/a:font[@place="InactiveWindow"]/a:slant' -v "$SLANT" "$cfg"
    openbox --reconfigure
    else
    exit 0
    fi
}
font_gtk () {
	if [[ "$FONT" ]]; then
	sd "^gtk-font-name=.*" "gtk-font-name=\"${FONTNAME} ${FONTSTYLE} ${FONTSIZE}\"" ${GTK2RC}
	sd "^gtk-font-name=.*" "gtk-font-name=${FONTNAME} ${FONTSTYLE} ${FONTSIZE}" ${GTK3RC}
	reload-gtk
	else
	exit 0
	fi
}
font_menu_item () {
    if [[ "$FONT" ]]; then
    mb-setvar menu_font_family="'${FONTNAME} ${FONTSTYLE}'"
    mb-setvar menu_font_size=${FONTSIZE}
    else
    exit 0
    fi
}
font_menu_sep () {
    if [[ "$FONT" ]]; then
    mb-setvar menu_sep_font_family="'${FONTNAME} ${FONTSTYLE}'"
    mb-setvar menu_sep_font_size=${FONTSIZE}
    else
    exit 0
    fi
}
font_conky_single () {
    # if -f "$1"
    if [[ "$FONT" ]]; then
    sd "font .*=.*,$" "font = '${FONTNAME}:size=${FONTSIZE}'," ${1}
    else
    exit 0
    fi
    #fi
}
font_conky_all () {
    if [[ "$FONT" ]]; then
    CONKYDIR="$HOME/.config/conky"
    sd "font .*=.*,$" "font = '${FONTNAME}:size=${FONTSIZE}'," ${CONKYDIR}/*mbcolor.conkyrc
    else
    exit 0
    fi
}
t2_time1_font(){
	sd "^time1_font.*$" "time1_font = ${FONTNAME} ${FONTSTYLE} ${FONTSIZE}" ${1}
	killall -SIGUSR1 tint2
}
t2_time2_font(){
	sd "^time2_font.*$" "time2_font = ${FONTNAME} ${FONTSTYLE} ${FONTSIZE}" ${1}
	killall -SIGUSR1 tint2
}

case "$1" in
    
    obtitle)
    get_obfont
    selectfont
    font_obtitle
    ;;
    gtk)
    get_gtkfont
    selectfont
    font_gtk
    ;;
    menu_item)
    get_menu_item_font
    selectfont
    font_menu_item
    ;;
    menu_sep)
    get_menu_sep_font
    selectfont
    font_menu_sep
    ;;
    conky_single)
    selectfont
    font_conky_single "${2}"
    ;;
    conky_all)
    selectfont
    font_conky_all
    ;;
    t2_time1_font)
    selectfont
    t2_time1_font "$2"
    ;;
    t2_time2_font)
    selectfont
    t2_time2_font "$2"
    ;;
    *)
    :
    ;;
esac
