#!/bin/sh
# gnome-session starts the XDG autostart entries of the Wayland session and
# filters them with OnlyShowIn/NotShowIn against its XDG_CURRENT_DESKTOP,
# which the display manager sets from the session entry's DesktopNames.
# Without it, gnome-session 49 and later skipped entries meant for GNOME and
# ran entries that exclude GNOME, such as nm-applet and polkit agents that
# sway already starts; older gnome-session fell back to plain GNOME.
set -eu

tmp=${AUTOPKGTEST_TMP:-$(mktemp -d)}
session=/usr/share/wayland-sessions/regolith-wayland.desktop
launcher=/usr/bin/regolith-session-wayland-launch
fail=0

# 1. The desktop names the display manager exports, and the ones sway gets.
names=$(sed -n 's/^DesktopNames=//p' "$session" | sed 's/;$//; s/;/:/g')
sway_names=$(sed -n 's/^export XDG_CURRENT_DESKTOP="\(.*\)"$/\1/p' /usr/bin/regolith-session-wayland)
if [ -z "$names" ]; then
    echo "FAIL: $session has no DesktopNames"
    fail=1
elif [ "$names" != "$sway_names" ]; then
    echo "FAIL: $session DesktopNames ($names) differ from sway's XDG_CURRENT_DESKTOP ($sway_names)"
    fail=1
else
    echo "ok: DesktopNames -> XDG_CURRENT_DESKTOP=$names"
fi

# 2. The launcher keeps what the display manager set and fills it in for
#    display managers that ignore DesktopNames.
mkdir -p "$tmp/stub"
cat > "$tmp/stub/gnome-session" <<'STUB'
#!/bin/sh
echo "${XDG_CURRENT_DESKTOP-(unset)}"
STUB
chmod +x "$tmp/stub/gnome-session"
got=$(env -u XDG_CURRENT_DESKTOP PATH="$tmp/stub:$PATH" "$launcher")
if [ -n "$names" ] && [ "$got" = "$names" ]; then
    echo "ok: launcher without XDG_CURRENT_DESKTOP -> $got"
else
    echo "FAIL: launcher without XDG_CURRENT_DESKTOP -> $got, want ${names:-the DesktopNames}"
    fail=1
fi
got=$(env XDG_CURRENT_DESKTOP=Custom PATH="$tmp/stub:$PATH" "$launcher")
if [ "$got" = Custom ]; then
    echo "ok: launcher keeps XDG_CURRENT_DESKTOP=Custom"
else
    echo "FAIL: launcher replaced XDG_CURRENT_DESKTOP=Custom with $got"
    fail=1
fi

# 3. Which autostart entries gnome-session starts in this session: it asks
#    GLib, g_desktop_app_info_get_show_in() with a NULL desktop, which checks
#    the session's desktop names. Call it from libgio directly: before GLib
#    2.86 the introspection data does not let Python pass NULL there.
mkdir -p "$tmp/autostart"
entry() { # entry <name> <extra line>
    printf '[Desktop Entry]\nType=Application\nName=%s\nExec=true\n%s\n' "$1" "$2" \
        > "$tmp/autostart/$1.desktop"
}
entry for-gnome 'OnlyShowIn=GNOME;'
entry for-regolith-wayland 'OnlyShowIn=Regolith-Wayland;'
entry for-x11-session 'OnlyShowIn=Regolith;GNOME-Flashback;'
entry not-in-gnome 'NotShowIn=GNOME;'
entry everywhere ''
expected="for-gnome:yes for-regolith-wayland:yes for-x11-session:no not-in-gnome:no everywhere:yes"
for real in nm-applet polkit-mate-authentication-agent-1 gnome-keyring-secrets; do
    if [ -f "/etc/xdg/autostart/$real.desktop" ]; then
        cp "/etc/xdg/autostart/$real.desktop" "$tmp/autostart/"
        case $real in
            gnome-keyring-*) expected="$expected $real:yes" ;;
            *) expected="$expected $real:no" ;;
        esac
    fi
done
for case in $expected; do
    name=${case%%:*}
    want=${case#*:}
    got=$(XDG_CURRENT_DESKTOP="$names" python3 -c '
import ctypes
import sys
gio = ctypes.CDLL("libgio-2.0.so.0")
gio.g_desktop_app_info_new_from_filename.restype = ctypes.c_void_p
gio.g_desktop_app_info_new_from_filename.argtypes = [ctypes.c_char_p]
gio.g_desktop_app_info_get_show_in.restype = ctypes.c_int
gio.g_desktop_app_info_get_show_in.argtypes = [ctypes.c_void_p, ctypes.c_char_p]
info = gio.g_desktop_app_info_new_from_filename(sys.argv[1].encode())
if not info:
    sys.exit("cannot load " + sys.argv[1])
print("yes" if gio.g_desktop_app_info_get_show_in(info, None) else "no")
' "$tmp/autostart/$name.desktop")
    if [ "$got" = "$want" ]; then
        echo "ok: $name.desktop started: $got"
    else
        echo "FAIL: $name.desktop started: $got, want $want"
        fail=1
    fi
done

exit $fail
