#!/bin/bash

if [ "$1" ]; then
  case "$1" in
  edit) edit;;
  reset) reset;;
  *) xdotool type --delay 0 "$1"
  exit 1
  ;;
  esac
fi


# IF exist ~/.config/mabox/jgtype.csv CSV="$HOME/.config/mabox/jgtype.csv
# ELSE CSV=/usr/share/mb-jgtools/jgtype.csv


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

jgmenu_theme=${jgmenu_theme:-obtheme}
jgmenu_use_icons=${jgmenu_use_icons:-true}
case $jgmenu_theme in
    default) 
    export color_norm_fg="#FFFFFF "
    ;;
    obtheme) 
        export color_title_fg="#FFFFFF "
        ob_file=$HOME/.config/openbox/rc.xml
        [[ -f $ob_file ]] && wm_theme=$(awk '/<theme>/ {while (getline n) {if (match(n, /<name>/)){l=n; exit}}} END {split(l, a, "[<>]"); print a[3]}' "$ob_file")
        while read -r a b c 
		do
			[[ -n "$a" && "$a" != [[:blank:]#]* ]] && export "$a$b$c"
		done <  $HOME/.config/mabox/jgobthemes/$wm_theme.colorrc
    ;;
    *) 
    export color_norm_fg="#FFFFFF "
    ;;
esac
. $HOME/.config/mabox/jgmenusize.conf





config_file=$(mktemp)
menu_file=$(mktemp)
trap "rm -f ${config_file} ${menu_file}" EXIT

cat <<EOF >${config_file}
stay_alive			= 0
menu_width        = 340
#menu_height_min      = 480
menu_padding_top 	= ${MENU_PADDING_TOP:-4}
menu_padding_right 	= ${MENU_PADDING_RIGHT:-4}
menu_padding_bottom = ${MENU_PADDING_BOTTOM:-4}
menu_padding_left	= ${MENU_PADDING_LEFT:-4}
menu_radius          = ${MENU_RADIUS:-0}
menu_halign          = center
menu_valign          = center
tabs 				= ${TABS:-80}
item_margin_y        = 4
item_height          = 24
item_padding_x       = 10
item_radius          = 0
item_border          = 1


icon_size           = 0

color_menu_bg		= ${color_menu_bg:-#222222 80}
color_menu_border 	= ${color_menu_border:-#2f9b85 100}
color_norm_bg		= ${color_norm_bg:-#000000 0}
color_norm_fg 		= ${color_norm_fg:-#CCCCCC 100}
color_sel_bg 		= ${color_sel_bg:-#169f6f 60}
color_sel_fg 		= ${color_sel_fg:-#f8f8f8 100}
color_sel_border 	= ${color_sel_border:-#504e65 100}
color_sep_fg 		= ${color_sep_fg:-#4D4D4D 100}
color_sep_bg 		= ${color_sep_bg:-#262626 100}
color_title_fg		= ${color_title_fg:-#4D4D4D 100}
color_title_border	= ${color_title_border:-#169f6f 100}
color_title_bg		= ${color_title_bg:-#262626 100}
color_scroll_ind 	= ${COLOR_SCROLL_IND:-#504e65 100}


sep_markup 			= weight="bold" foreground="${color_title_fg% *}"
sep_height			= ${sep_height:-5}
font 				= ${jgmenu_font:-Noto Sans Medium 9}
EOF

cat <<EOF >${menu_file}
. ~/.config/mabox/jgtype.csv

EOF

jgmenu --config-file=${config_file} --csv-file=${menu_file}
