#!/bin/bash

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

param="$@" # existing regular file or *.extension
[ "$1" = "info" ] && Info && exit 0

IFS=$'\n'
xdotool sleep 0.1 key Return & applist=($(mimeopen -a "$param" | grep "[0-9]) "))
applist=($(echo "${applist[*]}" | cut -d' ' -f2- | tr -s ' ' | sed -E 's/\& /\&amp; /g'))
l=0; for ((k=0; k<${#applist[@]}; k++)); do [ ${#applist[$k]} -gt $l ] && l=${#applist[$k]}; done
[ -e "$param" ] && type="$(xdg-mime query filetype "$param")" || type="$(grep -r "$param[^\.]" /usr/share/mime/* | grep -v 'freedesktop.org' | head -1 | cut -d: -f1 | sed -E 's|/usr/share/mime/(.*)\.xml|\1|')"
title=$"App list to $type"
app=$(yad --center --title="$title" --height=$((${#applist[@]}*22+110)) --width=$((l*8+70)) --borders=20 --list --no-headers --window-icon=$HOME/.icons/list.png --column=Applist ${applist[@]} --button=$"Cancel":1 --button="Info":"$0 info" --button="OK":0)
[ $? -gt 0 ] && exit 1
app="$(echo "${app::-1}" | sed -E 's/.*(\([^\)]*\))$/\1/g' | cut -d'(' -f2 | cut -d')' -f1)".desktop
echo "$app"
 