#!/bin/sh

# Check to see if an kdenlive is running
if [ $(pgrep -c kdenlive) -gt 0 ]; then
    
    if [ $XDG_SESSION_TYPE = "x11" ]; then
        #X11
        kdenliverg=$(wmctrl -lx | rg kdenlive.kdenlive | rg -v org-agenda | awk '{print $1}')
        # echo $kdenliverg

        if [ -z $kdenliverg ]; then
            exec kdenlive &
            exit
        else
            exec wmctrl -ia $kdenliverg
            exit
        fi
    else
        if [ $KDE_FULL_SESSION = "true" ]; then
            exec /home/chris/bin/ww -fa Kdenlive -c kdenlive
            exit
        else
            # WAYLAND
            kdenliverg=$(wlrctl window list | rg -v 'kdenlive:\sorg-agenda' | rg 'kdenlive:')

            kdenlivewin=$(echo $kdenliverg | sed 's/.*\: //')
            # echo $kdenlivewin

            exec wlrctl toplevel focus title:"$kdenlivewin"
            exit
        fi
    fi

else
    exec kdenlive &
fi
