#!/bin/sh

# Check to see if qb is running
if [ $(pgrep -c qutebrowser) -gt 0 ]; then

    echo "qb running"

    if [ $XDG_SESSION_TYPE = "x11" ]; then
        #X11
        echo "X11"
        qbrg=$(wmctrl -lx | rg qutebrowser | awk '{print $1}')
        # echo $emacsrg

        if [ -z $qbrg ]; then
            exec env QT_QPA_PLATFORMTHEME="" qutebrowser
            exit
        else
            exec wmctrl -ia $qbrg
            exit
        fi
    else
        if [ $KDE_FULL_SESSION = "true" ]; then
            exec /home/chris/bin/ww -f org.qutebrowser.qutebrowser -c qutebrowser
            exit
        else
            echo "other wayland"
            # WAYLAND
            qbrg=$(wlrctl window list | rg qutebrowser)

            qbwin=$(echo $qbrg | sed 's/\(.*r\)\:.*/\1/')
            # echo $ffwin

            exec wlrctl toplevel focus "$qbwin"
            exit
        fi
    fi
else
    exec env QT_QPA_PLATFORMTHEME="" qutebrowser
fi
