#!/bin/bash
# relaunch: reopens the apps that were open when the desktop's session was
# last saved and that XFCE's session management does not start again itself.
# Run once as the session starts, from /etc/xdg/autostart.
#
# devgrail-desktop saves the session every minute and before it stops it, and
# notes then which of these apps were running (relaunchList in sessionbus.go).
# Each one opens its own windows again when started: Firefox its tabs (the
# add-on's Firefox policy sets browser.startup.page to 3).
set -uo pipefail

list="$HOME/.local/state/devgrail-desktop/relaunch"
[ -s "$list" ] || exit 0

# The ones xfce4-session reopens come first, so the windows stack as before.
sleep 2
while read -r app; do
    case "$app" in
        firefox) ;;
        *) continue ;;
    esac
    # Left alone if it is running already: Firefox would only open a window.
    if command -v "$app" >/dev/null && ! pgrep -u "$(id -u)" -x "$app" >/dev/null; then
        "$app" </dev/null >/dev/null 2>&1 &
        disown
    fi
done <"$list"
exit 0
