#!/bin/bash

# fontctl - cli to handle fonts in Mabox: openbox, jgmenu, tint2, conky, gtk
CONKYDIR="$HOME"/.config/conky
BASECONKY="$CONKYDIR/sysinfo_mbcolor.conkyrc"
. $HOME/.config/mabox/mabox.conf

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]}
}

gtk_fontfamily() {
	get_gtkfont
	sd "^gtk-font-name=.*" "gtk-font-name=\"${1} ${GTK_SIZE}\"" ${GTK2RC}
	sd "^gtk-font-name=.*" "gtk-font-name=${1} ${GTK_SIZE}" ${GTK3RC}
	reload-gtk
}
gtk_fontsize() {
	get_gtkfont
	case "$1" in
		inc) SIZE=$((GTK_SIZE+1));;
		dec) SIZE=$((GTK_SIZE-1));;
		*) SIZE=${1};;
	esac
	sd "^gtk-font-name=.*" "gtk-font-name=\"${GTK_FAMILY} ${SIZE}\"" ${GTK2RC}
	sd "^gtk-font-name=.*" "gtk-font-name=${GTK_FAMILY} ${SIZE}" ${GTK3RC}
	reload-gtk
}
resetgtk() {
	get_gtkfont
	sd "^gtk-font-name=.*" "gtk-font-name=\"Ubuntu 10\"" ${GTK2RC}
	sd "^gtk-font-name=.*" "gtk-font-name=Ubuntu 10" ${GTK3RC}
	reload-gtk
}
resetob () {
	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 "Ubuntu" "$cfg"
    xmlstarlet ed -L -N a="$nspace" -u '/a:openbox_config/a:theme/a:font[@place="InactiveWindow"]/a:name' -v "Ubuntu" "$cfg"
    xmlstarlet ed -L -N a="$nspace" -u '/a:openbox_config/a:theme/a:font[@place="ActiveWindow"]/a:size' -v 10 "$cfg"
    xmlstarlet ed -L -N a="$nspace" -u '/a:openbox_config/a:theme/a:font[@place="InactiveWindow"]/a:size' -v 10 "$cfg"
    
    #add Weight and slant
    
    openbox --reconfigure
}
resetmenus () {
	mb-setvar menu_font_size=10
	mb-setvar menu_sep_font_size=11
	mb-setvar "menu_font_family=\'Ubuntu\'"
	mb-setvar "menu_sep_font_family=\'Ubuntu Bold\'"
}
resetconky () {
	sd "font .*=.*,$" "font = 'Ubuntu:size=8'," ${CONKYDIR}/*mbcolor.conkyrc
}
resetall () {
	resetconky
	resetob
	resetmenus
	resetgtk
	
	}
	
inc_all() {
	gtk_fontsize inc
	inc_conky
	inc_menus
  ob_fontsize inc
}
dec_all() {
	gtk_fontsize dec
	dec_conky
	dec_menus
  ob_fontsize dec
}
inc_conky () {
    read FONTDEF <<< "$(grep "font .*=.*,$" ${BASECONKY} | cut -d'=' -f2,3 |cut -d"'" -f2)"
    FONT=${FONTDEF%:*}
    FONTSIZE=${FONTDEF#*=}
    ((FONTSIZE++))
    sd "font .*=.*,$" "font = '${FONT}:size=${FONTSIZE}'," ${CONKYDIR}/*mbcolor.conkyrc
}
dec_conky () {
    read FONTDEF <<< "$(grep "font .*=.*,$" ${BASECONKY} | cut -d'=' -f2,3 |cut -d"'" -f2)"
    FONT=${FONTDEF%:*}
    FONTSIZE=${FONTDEF#*=}
    ((FONTSIZE--))
    sd "font .*=.*,$" "font = '${FONT}:size=${FONTSIZE}'," ${CONKYDIR}/*mbcolor.conkyrc
}	
inc_menus () {
	mb-setvar menu_font_size=$((menu_font_size+1))
	mb-setvar menu_sep_font_size=$((menu_sep_font_size+1))
}
dec_menus () {
	mb-setvar menu_font_size=$((menu_font_size-1))
	mb-setvar menu_sep_font_size=$((menu_sep_font_size-1))
}
ob_fontsize () {
    nspace="http://openbox.org/3.4/rc"
    cfg="$HOME/.config/openbox/rc.xml"
    case "$1" in
        inc|increase)
        SIZE=$(xmlstarlet sel -N a="$nspace" -t -v '/a:openbox_config/a:theme/a:font[@place="ActiveWindow"]/a:size'  "$cfg")
        if [ "$SIZE" -lt "21" ];then
        ((SIZE=SIZE+1))
        xmlstarlet ed -L -N a="$nspace" -u '/a:openbox_config/a:theme/a:font[@place="ActiveWindow"]/a:size' -v "$SIZE" "$cfg"
        xmlstarlet ed -L -N a="$nspace" -u '/a:openbox_config/a:theme/a:font[@place="InactiveWindow"]/a:size' -v "$SIZE" "$cfg"
        fi
        ;;
        dec|decrease)
        SIZE=$(xmlstarlet sel -N a="$nspace" -t -v '/a:openbox_config/a:theme/a:font[@place="ActiveWindow"]/a:size'  "$cfg")
        if [ "$SIZE" -gt "7" ];then
            ((SIZE=SIZE-1))
            xmlstarlet ed -L -N a="$nspace" -u '/a:openbox_config/a:theme/a:font[@place="ActiveWindow"]/a:size' -v "$SIZE" "$cfg"
            xmlstarlet ed -L -N a="$nspace" -u '/a:openbox_config/a:theme/a:font[@place="InactiveWindow"]/a:size' -v "$SIZE" "$cfg"
        fi
        ;;
        *)
        xmlstarlet ed -L -N a="$nspace" -u '/a:openbox_config/a:theme/a:font[@place="ActiveWindow"]/a:size' -v "$1" "$cfg"
        xmlstarlet ed -L -N a="$nspace" -u '/a:openbox_config/a:theme/a:font[@place="InactiveWindow"]/a:size' -v "$1" "$cfg"
        ;;
    esac
    openbox --reconfigure
}

case "$1" in
	gtk_fontfamily) gtk_fontfamily "$2";;
	gtk_fontsize) gtk_fontsize "$2";;
	inc_all) inc_all;;
	dec_all) dec_all;;
	inc_conky) inc_conky;;
	dec_conky) dec_conky;;
	inc_menus) inc_menus;;
	dec_menus) dec_menus;;
	ob_fontsize) ob_fontsize "$2";; # arg: size, inc or dec
	resetall) resetall;;
	resetob) resetob;;
	resetmenus) resetmenus;;
	resetconky) resetconky;;
	resetgtk) resetgtk;;
	*):;;
esac
