#!/bin/sh
# Start Wave Terminal the kalango way.

# kalango's defaults: Wave merges every <part>/*.json in its config folder, and the
# user's own <part>.json wins over them, so kalango and the user never share a file.
# Links, so a package update applies the next time Wave starts.
config="${XDG_CONFIG_HOME:-$HOME/.config}/waveterm"
for part in settings termthemes backgrounds; do
    mkdir -p "$config/$part"
    ln -sfn "/usr/share/kalango/waveterm/$part.json" "$config/$part/kalango.json"
done

# Blur behind its transparent window. KWin only blurs windows that ask for it. Wave
# never asks, but an X11 window can be marked from outside, so Wave runs under XWayland
# and this script marks its windows.
/opt/Wave/waveterm --ozone-platform=x11 --no-sandbox "$@" &
wave=$!
# Stays while Wave runs, for new windows. A second launch hands over to the first and exits.
while kill -0 "$wave" 2>/dev/null; do
    for id in $(xprop -root _NET_CLIENT_LIST | grep -oE '0x[0-9a-f]+'); do
        xprop -id "$id" WM_CLASS 2>/dev/null | grep -q '"Wave"' || continue
        xprop -id "$id" -f _KDE_NET_WM_BLUR_BEHIND_REGION 32c -set _KDE_NET_WM_BLUR_BEHIND_REGION 0
    done
    sleep 2
done
