#!/bin/sh
# Wi-Fi sign-in pages (captive portals): when NetworkManager's check lands on a login page, tell
# every logged-in Moodtop user and open the page in Mood Browser when they click the notification.
[ "$2" = connectivity-change ] && [ "$CONNECTIVITY_STATE" = PORTAL ] || exit 0
for r in /run/user/*; do
  u=$(stat -c %U "$r" 2>/dev/null) || continue
  [ "$u" = root ] && continue
  w=$(cd "$r" && ls wayland-[0-9] 2>/dev/null | head -n1) || continue
  [ -n "$w" ] || continue
  runuser -u "$u" -- env XDG_RUNTIME_DIR="$r" WAYLAND_DISPLAY="$w" DBUS_SESSION_BUS_ADDRESS="unix:path=$r/bus" \
    HOME="$(getent passwd "$u" | cut -d: -f6)" setsid /usr/lib/mood/runtime/mood-portal-open </dev/null >/dev/null 2>&1 &
done
exit 0
