#!/bin/bash

## Post install script to replace the installed gambas3.desktop
## with a new gambas3.desktop only showing installed components for 'Run with'
## Author: Bruce Steers
## GNU Public Licence - version 3
## Modified to be used in debian/ubuntu recipes by gbWilly (W.J.L. Raets)

set -e

LIBS=$(gbx3 -e 'Component.Path')

for gui in GTK3 GTK4 QT4 QT5 QT6; do
LCASE=$(echo -n $gui | tr '[:upper:]' '[:lower:]')
[ -e "$LIBS/gb.$LCASE.webview.component" ] && AGUIS="$AGUIS$gui;"
done

AGUIS=${AGUIS%;*}

ENTRY="[Desktop Entry]
Name=Gambas 3
Exec=gambas3
GenericName=Gambas 3 IDE
GenericName[fr]=EDI Gambas 3
GenericName[it]=Gambas 3 IDE
GenericName[nl]=Gambas 3 IDE
GenericName[ru]=Gambas 3 IDE(ИСР)
Comment=Gambas3 Integrated Development Environment
Comment[fr]=Environnement de développement intégré Gambas3
Comment[it]=Ambiente di sviluppo integrato Gambas3
Comment[nl]=Gambas3 Geïntegreerde Ontwikkel Omgeving
Comment[ru]=Gambas3 IDE(ИСР) - альтернатива для Visual Basic
Icon=gambas3
Terminal=false
Type=Application
Categories=Development;IDE;
StartupNotify=true
Actions=$AGUIS
"
OIFS=$IFS
IFS=';'
for gui in $AGUIS; do
LCASE=$(echo -n $gui | tr '[:upper:]' '[:lower:]')
ACTIONS="$ACTIONS
[Desktop Action $gui]
Name=Run with $gui
Name[fr]=Exécuter avec $gui
Name[it]=Esegui con $gui
Name[nl]=Uitvoeren met $gui
Name[ru]=Выполнение с помощью $gui
Exec=env GB_GUI=gb.$LCASE gambas3
"
done
IFS=$OIFS

[ -e "/tmp/gambas3.desktop" ] && rm /tmp/gambas3.desktop
echo "$ENTRY$ACTIONS" >/tmp/gambas3.desktop
xdg-desktop-menu install --novendor /tmp/gambas3.desktop
rm /tmp/gambas3.desktop
