#!/bin/bash

Info () {
title="Info"
author="Peter Starfinger"
mail="E-Mail: info@die-starfingers.de"
year="2022"
app="$(basename $0)"
msgid="The Starbuntu application
<i>\$app</i>
was written by"
info="$(gettext "$msgid" | (export PATH `envsubst --variables "$msgid"`; envsubst "$msgid"))"

yad --center --fixed --fixed --title="$title" --window-icon="info" --image="info" --borders=20 --text="$info"$'\n'$'\n'"$author"$'\n'"$mail"$'\n'$'\n'"$year"$'\n' --text-align=center --selectable-labels --button="OK" &
wunwa "$title" > /dev/null; xdotool key Home key Down
}

Tip () {
    adv=$"instead of"
    alarm=$"Attention while inserting special characters:"
    title=$"Special Characters"
    conj=$"or"

#    help0=$'\t'"    $adv \' &amp; \'&#9;\' &amp;#38; \' $conj \' &amp;amp; \'"
    help1=$'\t'"    $adv nbsp&#9;\' \' $conj \'&amp;#160;\'"
    help2=$'\t'"    $adv Tab&#9;\'&#92;t\' $conj \'&amp;#9;\'"
#    help="    $alarm    &#10;&#10;$help0&#10;$help1&#10;$help2&#10;"
    help="    $alarm    &#10;&#10;$help1&#10;$help2&#10;"
    yad --center --fixed --title="$title" --window-icon="/usr/share/icons/gnome/48x48/status/dialog-warning.png" --image="/usr/share/icons/gnome/48x48/status/dialog-warning.png" --image-on-top --borders=20 --text="$help" --selectable-labels --button="OK" &
    wunwa "$title" > /dev/null; xdotool key Home key Down
}

Edit () {
field1=$"Icon: "
field2=$"Tooltip:         "
field2="${field2/:/\&#58;}"
loop=true; new=false
field3=$"Left click command&#58; "
field4=$"Right click command&#58; "
field5=$"Middle click command&#58; "
text1=$"OK!!Adoption of the changes"
text2=$"Del!!Delete button"
text3=$"New!!The new button will be positioned before the current one."
text4=$"Input of special charakters"
button4=$"Hints"
del="--button="$text2":1"
neu="--button="$text3":2"
icon="${button_icons[$k]}"
tooltip="${button_tooltips[$k]}"
lclick="${button_lclick_commands[$k]/button_lclick_command = /}"
rclick="${button_rclick_commands[$k]/button_rclick_command = /}"
mclick="${button_mclick_commands[$k]/button_mclick_command = /}"
while $loop; do
    edited=`yad --center --title="Edit Button "$((k+1)) --width=700 --height=250 --borders=10 --window-icon="tint2" --no-escape --always-print-result --form --separator="‖" --field="$field1":SFL "$icon" --file-filter $"Graphic Files "" | *.jpg *.jpeg *.png *.JPG *.JPEG *.PNG" --add-preview --field="$field2" "$tooltip" --field="$field3" "$lclick" --field="$field4" "$rclick" --field="$field5" "$mclick" --button=$"Quit":3 $del $neu --button="$button4!!""$text4":"$0 tip" --button="Info":"$0 info" --button="$text1":0`
    knopf=$?
    if [ $knopf -eq 2 ]; then
	cd $HOME/.icons; icon=""; tooltip=""; lclick=""; rclick=""; mclick=""; del=""; neu=""; new=true
    else
	loop=false
	[ $knopf -eq 3 ] && new=false
    fi
done
evaluated=false
$new && knopf=2

for c in ${conffile[@]}; do
    file_cont="$(cat "$c")"
    n_buttons=$(echo "$file_cont" | grep '# Button' | wc -l)
    panel_items=$(echo "$file_cont" | grep 'panel_items')
    if [ $knopf -lt 3 ]; then
	if [ $knopf -eq 0 ] || [ $knopf -eq 2 ]; then
	    if ! $evaluated; then
		edited="${edited::-1}"
		icon="${edited%%‖*}"
		edited="${edited#*‖}"
		tooltip="${edited%%‖*}"
		edited="${edited#*‖}"
		lclick="${edited%%‖*}"
		edited="${edited#*‖}"
		rclick="${edited%%‖*}"
		mclick="${edited#*‖}"
		context=($(echo "$file_cont" | grep -A 7 "# Button $((k+1))$"))
		context_mod=(${context[*]})
		context_mod[2]="button_icon = $icon"
		context_mod[4]="button_tooltip = $tooltip"
		context_mod[5]="button_lclick_command = $lclick"
		context_mod[6]="button_rclick_command = $rclick"
		context_mod[7]="button_mclick_command = $mclick"
		evaluated=true
	    fi
	    if [ $knopf -eq 2 ]; then
		panel_items_mod=$(sed -e "s/P/PP/$((k+1))" <<< $panel_items)
		file_cont="${file_cont/$panel_items/$panel_items_mod}"
		line_no=$(echo "$file_cont" | grep -n "# Button $((k+1))")
		line_no=${line_no%%:*}
		file_cont="$(sed "$line_no,$((line_no+16))H;$((line_no+17)){;x;s/^\n//;p;x;}" <<< $file_cont)"
		for ((i=$n_buttons; i>=k+1; i--)); do
		    file_cont="$(sed "s/\# Button $i/\# Button $((i+1))/g" <<< $file_cont)"
		done
		file_cont="${file_cont/\# Button $((k+2))/\# Button $((k+1))}"
	    fi
	    file_cont="${file_cont/"${context[*]}"/"${context_mod[*]}"}"
	fi
	if [ $knopf -eq 1 ]; then
	    panel_items_mod=$(sed -e "s/P//$((k+1))" <<< $panel_items)
	    file_cont="${file_cont/$panel_items/$panel_items_mod}"
	    file_cont="$(sed "/\# Button $((k+1))/,+16d" <<< $file_cont)"
	    for ((i=k+2; i<=$n_buttons; i++)); do
		file_cont="$(sed "s/\# Button $i/\# Button $((i-1))/g" <<< $file_cont)"
	    done
	fi
	echo "$file_cont" > $c
	echo >> $c
    fi
done
}


param="$1"
[ "$param" = "tip" ] && Tip && exit 0
[ "$param" = "info" ] && Info && exit 0

IFS=$'\n'
title="Edit Tint2 Buttons"
cancel=$"Cancel"

conffile=($HOME/.config/tint2/tint2rc $HOME/.config/tint2/tint2rc-horizontal $HOME/.config/tint2/tint2rc-vertical)
[ -d "/tmp/hsperfdata_$USER/equalized-tint2-icons" ] && rm /tmp/hsperfdata_$USER/equalized-tint2-icons/* || mkdir /tmp/hsperfdata_$USER/equalized-tint2-icons
button_icons=($(grep 'button_icon = ' ${conffile[0]} | sed 's/button_icon = //g'))
button_tooltips=($(grep 'button_tooltip = ' ${conffile[0]} | sed 's/button_tooltip = //g'))
button_lclick_commands=($(grep 'button_lclick_command = ' ${conffile[0]}))
button_rclick_commands=($(grep 'button_rclick_command = ' ${conffile[0]}))
button_mclick_commands=($(grep 'button_mclick_command = ' ${conffile[0]}))
trimmed_icons=(); k=0 
for b in ${button_icons[@]}; do
    f=""
    [[ "$b" =~ '/' ]] && f="$b"
    [ -z "$f" ] && f="$(find $HOME/.icons -name "$b\."* | head -n 1)"
    [ -z "$f" ] &&
    gtk_icon_theme="$(cat $HOME/.config/gtk-3.0/settings.ini | grep gtk-icon-theme-name | sed 's/gtk-icon-theme-name=//g')" &&
    theme_path="$(find /usr/share/icons/$gtk_icon_theme -type d -name 48*)" &&
    f="$(find $theme_path -name "$b\."* | head -n 1)"
    [ -z "$f" ] && f="$(find /usr/share/icons/gnome/48x48 -name "$b\."* | head -n 1)"
    [ -n "$f" ] &&
    convert -resize 22x22 "$f" "/tmp/hsperfdata_$USER/equalized-tint2-icons/$(basename "$f")" &&
    trimmed_icons=(${trimmed_icons[@]} $((k+1)): "/tmp/hsperfdata_$USER/equalized-tint2-icons/$(basename "$f")" ${button_tooltips[$k]}) && ((k++))
done

select=`yad --center --title="$title" --window-icon="tint2" --borders=20 --width=520 --height=400 --list --separator="‖" --no-headers --column='Number' --column='Icons':IMG --column='Tooltips' --tooltip-column=3 ${trimmed_icons[@]} --button="$cancel":1 --button="Info":"$0 info" --button="OK":0`; button=$?

if [ $button -eq 0 ]; then
    k=${select%%:*}; ((k--))
    Edit
    if [ $knopf -lt 3 ]; then
	killall tint2; tint2 &
    fi
fi
