#!/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

[ -z "$param" ] && exit 1
IFS=$'\n'
if [ -e "$param" ]; then
    type="$(xdg-mime query filetype "$param")"
    [ "${param::5}" = "/dev/" ] && type="inode/blockdevice"
else
    param="*.${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|')"
fi
title="applist-add"
win_icon="$HOME/.icons/Apps-Run.png"
image="dialog-question"
app="$(yad --fixed --title="$title" --borders=20 --window-icon="$win_icon" --image="$image" --text=$"For type $type, which app should be added to app list?" --entry --button=$"Cancel":1 --button=$"Help":"yelp /usr/share/help/"$"C""/Starbuntu/starbuntu-apps-details.page#applist-add" --button="Info":"$0 info" --button="OK":0)"
[ -z "$app" ] && exit 1

if [ "${app: -8}" != ".desktop" ]; then
    found=($(grep -li "^Exec=.*\b$app\b" $HOME/.local/share/applications/*.desktop /usr/share/applications/*.desktop))
    found_=()
    for ((k=0; k<${#found[@]}; k++)); do found_=(${found_[@]} 1); done
    for ((k=0; k<${#found[@]}; k++)); do for ((l=$((k+1)); l<${#found[@]}; l++)); do [ "$(basename ${found[$l]})" = "$(basename ${found[$k]})" ] && found_[$l]=0; done; done
    for ((k=0; k<${#found_[@]}; k++)); do [ "${found_[$k]}" = 0 ] && unset found[$k]; done
else
    found=($(which $app))
fi
for ((k=0; k<${#found[@]}; k++)); do [ -z "$(which ${found[$k]})" ] && unset found[$k]; done
found=(${found[@]})
for ((k=0; k<${#found[@]}; k++)); do [ -z "$(grep "Categories=" $(which ${found[$k]}))" ] && unset found[$k]; done
found=(${found[@]})
for ((k=0; k<${#found[@]}; k++)); do [ -n "$(grep "NoDisplay=true" $(which ${found[$k]}))" ] && unset found[$k]; done
found=(${found[@]})
for ((k=0; k<${#found[@]}; k++)); do found[$k]="$((k+1)): ${found[$k]}"; done
[ -z "$found" ] && yad --fixed --title="$title" --borders=20 --window-icon="$win_icon" --image="dialog-warning" --text="&#10;<b>"$"For $app there is no desktop file!""</b>&#10;&#10;" --button=$"Help":"yelp /usr/share/help/"$"C""/Starbuntu/starbuntu-apps-details.page#applist-add" --button="Info":"$0 info" --button="OK":0 && exit 1 || dsk="$(yad --fixed --title="$title" --borders=20 --window-icon="$win_icon" --image="info" --text="<b>"$"For $app the following desktop files were found:""</b>&#10;&#10;${found[*]}&#10;&#10;"$"Type the number of the desktop file to add:""&#10;" --entry --button=$"Cancel":1 --button=$"Help":"yelp /usr/share/help/"$"C""/Starbuntu/starbuntu-apps-details.page#applist-add" --button="Info":"$0 info" --button="OK":0)"
[ $? -gt 0 ] && exit 1
[[ ! $dsk =~ ^[0-9]*$ ]] && exit 1 

((dsk--))

if [ -n "$(grep -E "$type.*[=;]$(basename ${found[$dsk]})" $HOME/.config/mimeapps.list $HOME/.local/share/applications/mimeapps.list $HOME/.local/share/applications/mimeinfo.cache /usr/share/applications/defaults.list /usr/share/applications/mimeinfo.cache)" ]; then
    yad --fixed --title="$title" --borders=20 --window-icon="$win_icon" --image="dialog-warning" --text="&#10;<b>"$"For $app there is already an entry in the app list!""</b>&#10;&#10;" --button=$"Help":"yelp /usr/share/help/"$"C""/Starbuntu/starbuntu-apps-details.page#applist-add" --button="Info":"$0 info" --button="OK":0; exit 1
elif [ -n "$(grep "$type=" $HOME/.local/share/applications/mimeapps.list | head -1)" ]; then
    line="$(grep "$type=" $HOME/.local/share/applications/mimeapps.list | head -1)"
    [ "${line: -1}" != ';' ] && sign=';' || sign=""
    sed -i "s|\($line\)|\1$sign$(basename ${found[$dsk]});|" $HOME/.local/share/applications/mimeapps.list
else   
    echo "$type=$(basename ${found[$dsk]});" >> $HOME/.local/share/applications/mimeapps.list
fi
yad --center --title="$title" --fixed --window-icon="$win_icon" --image="info" --borders=20 --text="&#10;&#9;"$(gettext "Done!")"&#9;&#9;&#9;&#10;&#10;" --button="OK"
