#!/bin/bash

Info () {
title="Info"
author="Peter Starfinger"
mail="E-Mail: info@die-starfingers.de"
year="2023"
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 --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
}

list="$1"
[ "$1" = "info" ] && Info && exit 0
[[ "$list" =~ ^notes$|^files$|^links$ ]] && list="$HOME/.config/gtk-3.0/$list.lst"
[[ "$list" =~ ^apps$ ]] && list="$HOME/.config/menus/Apps.lst"
title="Search List Item"

if [ "${list: -4}" != ".lst" ] || [ ! -f "$list" ]; then
    text=$"&#10;Which list to search?&#10;"
    field=$"List: "
    button1=$"Cancel"
    cd
    list=`yad --center --title="$title" --width=600 --height=200 --window-icon="$HOME/.icons/system-search.png" --borders=10 --text="<b>$text</b>" --text-align=center --form --field="$field":SFL "$HOME/.config/gtk-3.0" --file-filter $"List Files "" | *.lst" --button="$button1":1 --button="Info":"$0 info" --button="OK":0`
    button=$?
    if [ $button -eq 1 ] || [ -z "$list" ]; then exit 1; fi
    list="${list::-1}"
    [[ "$list" =~ ^notes$|^files$|^links$ ]] && list="$HOME/.config/gtk-3.0/$list.lst"
    [[ "$list" =~ ^apps$ ]] && list="$HOME/.config/menus/Apps.lst"
fi

keyword="$@"
keyword="${keyword/$1/}"
keyword="${keyword:1}"
IFS=$'\n'

title1=$"List Item Keyword"
text1="$title1:"
text2=$"&#10;    Item with this keyword not found!&#10;"
button1=$"Cancel"

if [ -z "$keyword" ]; then
    keyword="`yad --center --fixed --title="$title1" --image="system-search" --image-on-top --window-icon="system-search" --borders=10 --text="<b>$text1</b>" --entry --button=$button1:1 --button="Info":"$0 info" --button="OK":0`"
  err=$?
  if [ $err -ne 0 ] || [ -z "$keyword" ]; then exit 1; fi
fi

if [ "$(basename "$list")" = "Apps.lst" ]; then
    cd $HOME/.config/menus
    menus=()
    found="$(grep -E -i "$keyword[^‖]*$" *.lst)"
    if [ -n "$found" ]; then
	found="${found%%.lst*}"
	menus+=($(cat "$found.lst" | sed -E "s/‖([^‖]*$)/‖{$found}\1/g; s/‖(.*)‖▶ /‖{\1}‖▶ /g"))
	until [ "$found" = "Apps" ]; do
	    found="$(grep "‖$found‖" *.lst)"
	    found="${found%%.lst*}"
	    menus+=($(cat "$found.lst"))
	    menus=($(echo "${menus[*]}" | sed -E "s/‖(.*)‖(.*)▶ /‖{\1}‖\2▶ /g; s/\{\{/{/g; s/\}\}/}/g"))
	    [ "$found" != "Apps" ] && menus=($(echo "${menus[*]}" | sed -E "s/‖([^‖]*$)/‖{$found}\1/g"))
	done
	echo "${menus[*]}" > /tmp/hsperfdata_$USER/menus.lst
	list="/tmp/hsperfdata_$USER/menus.lst"
    else
	l=0
    fi
fi

if [ "$(basename "$list")" != "Apps.lst" ]; then
    found=($(grep -E -i "$keyword" "$list"))
    l="${#found[@]}"
fi
if [ $l -eq 0 ]; then
    yad --center --fixed --title="$title1" --borders=20 --image="dialog-warning" --window-icon="system-search" --text="$text2" --button="Info":"$0 info" --button="OK"
    exit 0
fi
found0="${found[0]}"
ids="${found0##*‖}"
if [ "${ids::1}" = "{" ]; then
    ids="${ids%\}*}}"
    id="${ids%%\}*}}"
    id_old="‖"
    title_new="$(grep "${id}‖" "$list")"
    title_new="${title_new#*▶ }"
fi

multimenu "$list" &

case "$list" in
    *notes.lst ) title="Memo Menu" ;;
    *files.lst ) title="Files Menu" ;;
    *links.lst ) title="Links Menu" ;;
    *          ) title="List Menu" ;;
esac

while [ "${ids::1}" = "{" ]; do
    wunwa "$title" > /dev/null
    n="$(grep "${id_old}[^{^‖]*$" "$list" | grep -n "▶ $title_new$")"
    n=${n%%:*}
    ([ $id_old = "‖" ] || [ -e "/tmp/hsperfdata_$USER/menus.lst" ]) && ((n--))
    for ((k=0; k<n; k++)); do xdotool key Down; done
    sleep 1
    xdotool key Return
    ids="${ids/$id/}"
    title="$(grep "${id}‖" "$list")"
    title="${title#*▶ } Menu"
    id_old="$id"
    id="${ids%%\}*}}"; [ "$id" = "}" ] && id=""
    if [ -n "$id" ]; then
	title_new="$(grep "${id}‖" "$list")"
	title_new="${title_new#*▶ }"
    fi
done
wunwa "$title" > /dev/null
line="$(grep "${id_old}[^{^‖]*$" "$list" | grep -n -i "$keyword")"
n=${line%%:*}
([ $id_old = "‖" ] || [ -e "/tmp/hsperfdata_$USER/menus.lst" ]) && ((n--))
for ((k=0; k<n; k++)); do xdotool key Down; done
[ -e "/tmp/hsperfdata_$USER/menus.lst" ] && rm /tmp/hsperfdata_$USER/menus.lst
