#!/bin/bash

Info () {
title="Info"
author="Peter Starfinger"
mail="E-Mail: info@die-starfingers.de"
year="2021"
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="/usr/share/icons/gnome/48x48/status/info.png" --image="/usr/share/icons/gnome/48x48/status/info.png" --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
}

[ "$1" = "info" ] && Info && exit 0

theme="$1"; shift; app="$@"

if [ -z "$theme" ] || [ -z "$app" ]; then
text="$(echo $"&#9;Call:&#10;&#10;&#9;gtk-theme &lt;GTK theme&gt; &lt;application&gt;")"
    yad --center --title="gtk-theme" --borders=20 --window-icon="dialog-warning" --image="dialog-warning" --text="$text" --button="Info":"$0 info" --button="OK":0
    exit 1
fi

dsk=""
[[ "$app" =~ ^(x|exec-desktop)\  ]] && app="${app/.desktop}" && app="$(echo "$app" | sed -E 's@^[^ ]* ([^ ]*)@\1.desktop@' )"
[[ "$app" =~ ^[^\ ]*\.desktop ]] && dsk="$(echo "$app" | sed -E 's@^([^ ]*) ?.*$@\1@')"

[ -n "$dsk" ] && cont="$(cat "$(which "$dsk")" | grep "^Exec=" | cut -d= -f2-)" && cont="${cont//\&/\\&}" && app="${app/$dsk/$cont}" # "

anw="$(echo "$app" | sed "s@'@@g; s@\"@@g; s@\[@@g; s@\]@@g; s@\;@@g")" #'
anw=($anw)
for ((k=${#anw[@]}; k>=0; k--)); do eco="echo ${anw[$k]}"; [[ ! "${anw[$k]}" =~ ^(ba)?sh$|orient-act-win|wunwa|xdotool|^sed$|grep|^w$|xseticon|^cut$|set-resol ]] && [ -n "$(which "${anw[$k]}")" ] && prog="$(which "${anw[$k]}")" && break; done
prog="$(basename "$prog")"

case $app in
    *Annual Calendar*)		prog="evince" ;;
    *arandr*)			prog="arandr" ;;
    *Control_Center*|*Rox*)	prog="ROX-Filer" ;;
esac

gtk="$(dpkg -s "$prog" 2>&1)"

if [ $? -gt 0 ]; then
    gtk="2_3"
elif [ "${gtk::10}" = "dpkg-query" ]; then
    gtk="3"
else
    gtk="$(echo "$gtk" | grep "Depends")"
    gtk=${gtk#*gtk}
    [ "${gtk::1}" = "-" ] && gtk="${gtk: 1}"
    gtk=${gtk::1}
    [ "$gtk" = "D" ] && gtk=2
fi

[ "$gtk" = "2_3" ] && gtk=3

if [[ $gtk =~ 2 ]]; then
    GTK2_RC_FILES=/usr/share/themes/${theme// /\\ }/gtk-2.0/gtkrc eval "$app"
else
    GTK_THEME="${theme// /\\ }" eval "$app"
fi

exitstatus=$?

exit $exitstatus

