-- ─────────────────────────────────────────────────────────────────────────────
-- Autostart
--
-- `exec-once` no longer exists in 0.56; startup is an event handler.
--
-- Session: Fedora installs two sessions, "Hyprland" and "Hyprland
-- (uwsm-managed)". Panama targets the uwsm one, because
-- xdg-desktop-portal-hyprland's systemd unit requires graphical-session.target
-- and Fedora ships no hyprland-session.target -- without uwsm, screen sharing
-- in OBS/Sunshine/Zoom silently fails. The target is started explicitly below
-- so the plain session works too.
--
-- Anything that ships a systemd user unit is started as a unit rather than as
-- a compositor child. That is not cosmetic: under uwsm, units get the correct
-- activation environment (see uwsm/env), they restart on failure, and they
-- shut down in order. Panama's `change-settings` script enables them once;
-- the explicit starts here make a fresh clone work before that has run.
-- ─────────────────────────────────────────────────────────────────────────────

hl.on("hyprland.start", function()
    -- Publish the session environment to systemd and D-Bus so user units and
    -- the portals can see WAYLAND_DISPLAY. Cheap, and essential without uwsm.
    hl.exec_cmd("dbus-update-activation-environment --systemd WAYLAND_DISPLAY XDG_CURRENT_DESKTOP=Hyprland")
    hl.exec_cmd("systemctl --user start hyprland-session.target")

    -- Units: polkit prompts, wallpaper, launcher daemon, idle/lock.
    hl.exec_cmd("systemctl --user start hyprpolkitagent.service hyprpaper.service vicinae.service hypridle.service")

    -- The shell: bar, dock, overview, quick settings, notifications, capture.
    -- No systemd unit ships with quickshell, so it runs as a compositor child.
    hl.exec_cmd("quickshell --daemonize")

    -- Removable-media automounting. GNOME did this invisibly via gvfs+udisks;
    -- outside GNOME something has to ask udisks to mount. No tray icon: the
    -- Quickshell bar already has a tray, and udiskie's own icon would be
    -- redundant. -f opens Nautilus when you click the mount notification.
    hl.exec_cmd("udiskie --automount --notify --no-tray --file-manager nautilus")

    -- Keyring unlock, for Nextcloud and Bitwarden credential storage.
    hl.exec_cmd("/usr/bin/gnome-keyring-daemon --start --components=secrets,ssh,pkcs11")

    -- Tray applications carried over from ~/.config/autostart. These need the
    -- Quickshell tray (an SNI host) to be up, hence starting after it.
    --
    -- Commands are copied verbatim from the GNOME .desktop files rather than
    -- guessed: Bitwarden is a flatpak with no `bitwarden` binary on PATH, and
    -- its autostart entry launches a specific script with --autostart.
    --
    -- RustDesk is deliberately absent: it ships an enabled *system* service
    -- (`rustdesk --service`) that spawns --server and --tray for the session on
    -- its own. Starting it here as well would give you two trays.
    hl.exec_cmd("nextcloud --background")
    hl.exec_cmd("flatpak run --command=bitwarden.sh com.bitwarden.desktop --autostart")
end)

hl.on("hyprland.shutdown", function()
    hl.exec_cmd("systemctl --user stop hyprland-session.target")
end)

return true
