#!/usr/bin/env bash

#Translation
export TEXTDOMAINDIR="/usr/share/locale"
export TEXTDOMAIN=biglinux-webapps

function msg(){
    yad --image=emblem-warning --image-on-top --form --width=500 --height=100 --fixed \
    --align=center \
    --text $"O navegador definido para abrir os WebApps não está instalado! \nTente alterar o navegador no Gerenciador de WebApps! \n" \
    --button=$" Fechar" \
    --center --on-top --borders=20 --title=$"BigLinux WebApps" \
    --window-icon=/usr/share/bigbashview/bcc/apps/biglinux-webapps/icons/webapp.svg
    exit
}

if [ "$1" = "firelibre" ];then
    msg
fi

if [ ! -e ~/.bigwebapps/BROWSER ];then
    /usr/share/bigbashview/bcc/apps/biglinux-webapps/check_browser.sh
fi

_GET_BROWSER=$(<~/.bigwebapps/BROWSER)

case $_GET_BROWSER in
    brave)
        if [ -e /usr/lib/brave-browser/brave ] || [ -e /usr/lib/brave-bin/brave ] || [ -e /opt/brave-bin/brave ];then
            BROWSER="brave"
        else
            msg
        fi
    ;;

    com.brave.Browser)
        if [ -e /var/lib/flatpak/exports/bin/com.brave.Browser ];then
            BROWSER="/var/lib/flatpak/exports/bin/com.brave.Browser"
        else
            msg
        fi
    ;;

    chromium)
        if [ -e /usr/lib/chromium/chromium ];then
            BROWSER="chromium"
        else
            msg
        fi
    ;;

    org.chromium.Chromium)
        if [ -e /var/lib/flatpak/exports/bin/org.chromium.Chromium ];then
            BROWSER="/var/lib/flatpak/exports/bin/org.chromium.Chromium"
        else
            msg
        fi
    ;;
    
    com.github.Eloston.UngoogledChromium)
        if [ -e /var/lib/flatpak/exports/bin/com.github.Eloston.UngoogledChromium ];then
            BROWSER="/var/lib/flatpak/exports/bin/com.github.Eloston.UngoogledChromium"
        else
            msg
        fi
    ;;

    google-chrome-stable)
        if [ -e /opt/google/chrome/google-chrome ];then
            BROWSER="google-chrome-stable"
        else
            msg
        fi
    ;;

    com.google.Chrome)
        if [ -e /var/lib/flatpak/exports/bin/com.google.Chrome ];then
            BROWSER="/var/lib/flatpak/exports/bin/com.google.Chrome"
        else
            msg
        fi
    ;;

    microsoft-edge-stable)
        if [ -e /opt/microsoft/msedge/microsoft-edge ];then
            BROWSER="microsoft-edge-stable"
        else
            msg
        fi
    ;;

    com.microsoft.Edge)
        if [ -e /var/lib/flatpak/exports/bin/com.microsoft.Edge ];then
            BROWSER="/var/lib/flatpak/exports/bin/com.microsoft.Edge"
        else
            msg
        fi
    ;;

    vivaldi-stable)
        if [ -e /opt/vivaldi/vivaldi ];then
            BROWSER="vivaldi-stable"
        else
            msg
        fi
    ;;

    falkon)
        if [ -e /usr/bin/falkon ];then
            BROWSER="falkon"
        else
            msg
        fi
    ;;

    *) exit ;;
esac

if [ "$BROWSER" = "falkon" ];then
    link=$(echo "$3" | cut -d "=" -f2)
    exec $BROWSER -ro $link
else
    exec $BROWSER "$@"
fi
