#!/bin/sh

##This app uses information in the Fluxbox Wiki: http://fluxbox-wiki.org/Make_windows_only_show_a_border.html 
##and presents the operations in graphical form
##It was developed by MX Devs July 2021 and released as a beta under GPLv3

TEXTDOMAINDIR=/usr/share/locale
export TEXTDOMAIN="mxfb-windowoverlay"

TITLE="OPTIONAL WINDOW STYLE CONFIG"
SUBTITLE="Existing config will be overwritten -- be sure to fill in all fields you want enabled"

helpfile=/usr/share/mxflux/HELP_Windowoverlay
titlebar=$(echo """,show,hide,tab")
size=$(echo """,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20")
color=$(echo """,Red,Orange,Yellow,Green,Blue,Cyan,Purple,White,Black,Brown,Magenta,Tan,Olive,Maroon,Navy,Aquamarine,Turquoise,Silver,Lime,Teal,Indigo,Violet,Pink,Gray,Black")

#remove existing config from overlay
sed -i '/border/d' ~/.fluxbox/overlay

yad --text="

<b>$TITLE</b>

<b><i>$SUBTITLE</i></b> 

" \
--text-align=center \
--form --width=20 --separator="," --item-separator="," \
--button="Help":"featherpad $helpfile" --button=gtk-cancel:1 --button=gtk-ok:0 --buttons-layout=center \
--field="Titlebar":CB \
--field="Border" \
--field="     Size":CB \
--field="     Color Focused":CB \
--field="     Color Unfocused":CB \
"$titlebar" "" "$size" "$color" "$color"| while read line; do
echo "Titlebar='`echo $line | awk -F',' '{print $1}'`'" > /tmp/init
echo "window.borderWidth: `echo $line | awk -F',' '{print $3}'`" >> /tmp/border
echo "window.focus.borderColor: `echo $line | awk -F',' '{print $4}'`" >> /tmp/border
echo "window.unfocus.borderColor: `echo $line | awk -F',' '{print $5}'`" >> /tmp/border
done

if [ retval -eq 1 ]; then
exit 0
fi

## titlebar config ##
if grep -Fq "hide" /tmp/init
then
	sed -i 's/NORMAL/BORDER/' ~/.fluxbox/init 
	sed -i 's/TAB/BORDER/' ~/.fluxbox/init
fi

if grep -Fq "show" /tmp/init
then
	sed -i 's/TAB/NORMAL/' ~/.fluxbox/init 
	sed -i 's/BORDER/NORMAL/' ~/.fluxbox/init 
fi

if grep -Fq "tab" /tmp/init
then
	sed -i 's/NORMAL/TAB/' ~/.fluxbox/init 
	sed -i 's/BORDER/TAB/' ~/.fluxbox/init 
fi

## border config ##

cat /tmp/border >> ~/.fluxbox/overlay
rm /tmp/border

fluxbox-remote restart 

exit=0
